Golang rapid development framework - add static address directory, add template directory and 404 pages (V)
background
The Golang chapter of knowledge sharing is a record of all kinds of knowledge I learned when I use Golang everyday. I will sort it out and share it with you in the form of articles for common learning. We welcome your continued attention.
The knowledge sharing series currently includes Java, Golang, Linux, Docker and so on.
development environment
- System: windows10
- Language: Golang
- golang version: 1.17
- Code warehouse: FastDevelopGo
content
We often need to use some basic components when we use golang to develop projects. Each new project is cumbersome, and the existing market feeling is not suitable for us. Therefore, we decided to build a set ourselves and open source it for everyone to use. We welcome you to put forward various needs. Now let's continue to improve the framework. The requirements to be completed in this section are:
- Add static address directory
- Add template directory
- Write a 404 unified page
1. Create the following folder under the root directory of the project
image.png
2. Add static resource loading and template directory loading codes in gateway loading
func InitRouter(r *gin.Engine) { //TODO here we initialize various gateway configurations // Initialize default static resources r.StaticFS("/static", http.Dir("./static")) //TODO initializes the default exception handling gateway // Initialize default template directory r.LoadHTMLGlob("templates/*/**") // Load 404 error page r.NoRoute(func(c *gin.Context) { // Implement internal redirection c.HTML(http.StatusOK, "error/404", gin.H{ "title": "404", }) }) // Initial template access gateway temRouter(r) } func temRouter(r *gin.Engine) { // Unified 404 error r.GET("/404", func(c *gin.Context) { c.HTML(http.StatusOK, "error/404", gin.H{ "title": "404", }) }) r.GET("/index", func(c *gin.Context) { c.HTML(200, "home/index", gin.H{ "title": "Gin Test template", }) }) }
3. Add a 404 template page in the templates/error / directory. The code is as follows
{{define "error/404"}} <html lang="en"> <head> <meta charset="utf-8"> <title>{{.title}}</title> <link rel="stylesheet" href="/static/css/style.css"> </head> <body> <div class="header"> </div> <p class="error">404</p> </body> </html> {{end}}
style.css, background map and static resources increased
@-webkit-keyframes appear{ from{ opacity: 0; } to { opacity: 1; } } @-webkit-keyframes headline_appear_animation{ from{ opacity: 0; } 25% { opacity: 0; } to { opacity: 1; } } @-webkit-keyframes contentappear{ from { -webkit-transform: scale(0); opacity: 0; } 50% { -webkit-transform: scale(0.5); opacity: 0; } to { -webkit-transform: scale(1); opacity: 1; } } @-moz-keyframes appear{ from{ opacity: 0; } to { opacity: 1; } } @-moz-keyframes headline_appear_animation{ from{ opacity: 0; } 25% { opacity: 0; } to { opacity: 1; } } @-moz-keyframes contentappear{ from { -moz-transform: scale(0); opacity: 0; } 50% { -moz-transform: scale(0.5); opacity: 0; } to { -moz-transform: scale(1); opacity: 1; } } * { margin: 0; padding: 0; } /*################ Sliding and appearing animation for the 404 page. Works in webkit browsers and mozilla firefox.*/ a:active{ position: relative; top: 1px; } html{ background: url(/static/img/background-3.png) no-repeat center center fixed; /*Image for the full size image background. If you want to have a color as a background, just remove the complete html{} style and uncomment the last line in the body{}*/ -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; /*CSS3 for the fullscreen image background*/ } body{ font-family: 'Helvetica Neue'; width: auto; margin: 0 auto 100px auto; /* background: #C9D0F5 /*373A4D*!/;*/ } .header { position: fixed; top: 0; width: 100%; height: 55px; padding: 0 0 0 10px; color: #fff; background-image: -moz-linear-gradient(top, rgba(85, 85, 85, 0.7), rgba(0,0,0, 1)); background-image: -o-linear-gradient(top, rgba(85, 85, 85, 0.7), rgba(0,0,0, 1)); background-image: -webkit-linear-gradient(top, rgba(85, 85, 85, 0.7), rgba(0,0,0,1)); background-image: linear-gradient(top, rgba(85, 85, 85, 0.7), rgba(0,0,0, 1)); border-top: 1px solid #000; box-shadow: inset 0px 1px rgba(255, 255, 255, 0.4), 0px 0px 13px #000000; z-index: 99; -webkit-animation: 1s appear; -moz-animation: 1s appear; } p.error { color: #000; text-shadow: #fff 0 1px 0; text-align:center; font:900 25em helvetica neue; -webkit-animation: 2s headline_appear_animation; -moz-animation: 2s headline_appear_animation; }
Note: This template is a temporary template, which will be replaced with a unified template later
4. Add gateway loading logic in gin startup
// Core startup gin framework function, main function func startGin() { // Initialize basic configuration r := gin.Default() // Initialize gateway router.InitRouter(r) r.Run(webConfig.Host + ":" + webConfig.Port) }
5. Start up test
image.png
OK, the test is normal, and the 404 page configuration in this section is completed.
Note: In this framework, I will add visualization page, code quick generation module, project framework quick generation module, etc. for my initial ideas. If you have other requirements and ideas, you are welcome to leave a message in the comment area or directly put forward valuable issue s in the code warehouse
Welcome to start actively. Your attention is my biggest motivation.
- Code warehouse: FastDevelopGo