Created
April 25, 2019 17:40
-
-
Save ganlvtech/43ab85a8c8acf0f1fb2792ae12930e5b to your computer and use it in GitHub Desktop.
go gin-gonic with go-assets demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "time" | |
| "github.com/jessevdk/go-assets" | |
| ) | |
| var _Assets819d045243ed7dd9963dbf5ddc547313b24498cb = "{{ define \"users/index.tmpl\" }}\r\n <html><h1>\r\n {{ .title }}\r\n </h1>\r\n <p>Using users/index.tmpl</p>\r\n </html>\r\n{{ end }}" | |
| var _Assets65a861e7def6a9565721daf270a18d977ffc3f39 = "{{ define \"posts/index.tmpl\" }}\r\n <html><h1>\r\n {{ .title }}\r\n </h1>\r\n <p>Using posts/index.tmpl</p>\r\n </html>\r\n{{ end }}" | |
| // Assets returns go-assets FileSystem | |
| var Assets = assets.NewFileSystem(map[string][]string{"/templates/users": []string{"index.html"}, "/": []string{"templates"}, "/templates": []string{}, "/templates/posts": []string{"index.html"}}, map[string]*assets.File{ | |
| "/": &assets.File{ | |
| Path: "/", | |
| FileMode: 0x800001ff, | |
| Mtime: time.Unix(1544628879, 1544628879938366200), | |
| Data: nil, | |
| }, "/templates": &assets.File{ | |
| Path: "/templates", | |
| FileMode: 0x800001ff, | |
| Mtime: time.Unix(1544628002, 1544628002622986300), | |
| Data: nil, | |
| }, "/templates/posts": &assets.File{ | |
| Path: "/templates/posts", | |
| FileMode: 0x800001ff, | |
| Mtime: time.Unix(1544627989, 1544627989549587400), | |
| Data: nil, | |
| }, "/templates/posts/index.html": &assets.File{ | |
| Path: "/templates/posts/index.html", | |
| FileMode: 0x1b6, | |
| Mtime: time.Unix(1544627962, 1544627962306487200), | |
| Data: []byte(_Assets65a861e7def6a9565721daf270a18d977ffc3f39), | |
| }, "/templates/users": &assets.File{ | |
| Path: "/templates/users", | |
| FileMode: 0x800001ff, | |
| Mtime: time.Unix(1544628036, 1544628036104870100), | |
| Data: nil, | |
| }, "/templates/users/index.html": &assets.File{ | |
| Path: "/templates/users/index.html", | |
| FileMode: 0x1b6, | |
| Mtime: time.Unix(1544628036, 1544628036095346900), | |
| Data: []byte(_Assets819d045243ed7dd9963dbf5ddc547313b24498cb), | |
| }}, "") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "github.com/gin-gonic/gin" | |
| "html/template" | |
| "io/ioutil" | |
| "net/http" | |
| "os" | |
| "path/filepath" | |
| "strings" | |
| ) | |
| type Login struct { | |
| User string `form:"user" json:"user" xml:"user" binding:"required"` | |
| Password string `form:"password" json:"password" xml:"password" binding:"required"` | |
| } | |
| func loadTemplate() (*template.Template, error) { | |
| t := template.New("") | |
| for name, file := range Assets.Files { | |
| if file.IsDir() || !strings.HasPrefix(name, "/templates/") { | |
| continue | |
| } | |
| h, err := ioutil.ReadAll(file) | |
| if err != nil { | |
| return nil, err | |
| } | |
| t, err = t.New(name).Parse(string(h)) | |
| if err != nil { | |
| return nil, err | |
| } | |
| } | |
| return t, nil | |
| } | |
| func main() { | |
| gin.DisableConsoleColor() | |
| router := gin.Default() | |
| t, err := loadTemplate() | |
| if err != nil { | |
| panic(err) | |
| } | |
| router.SetHTMLTemplate(t) | |
| router.GET("/ping", func(c *gin.Context) { | |
| c.JSON(http.StatusOK, gin.H{ | |
| "message": "pong", | |
| }) | |
| }) | |
| router.GET("/user/:name", func(c *gin.Context) { | |
| name := c.Param("name") | |
| c.String(http.StatusOK, "Hello, %s!", name) | |
| }) | |
| router.GET("/user/:name/*action", func(c *gin.Context) { | |
| name := c.Param("name") | |
| action := c.Param("action") | |
| message := name + " is " + action | |
| c.String(http.StatusOK, message) | |
| }) | |
| router.GET("/welcome", func(c *gin.Context) { | |
| firstname := c.DefaultQuery("firstname", "Guest") | |
| lastname := c.Query("lastname") // shortcut for c.Request.URL.Query().Get("lastname") | |
| c.String(http.StatusOK, "Hello %s %s", firstname, lastname) | |
| }) | |
| router.POST("/form_post", func(c *gin.Context) { | |
| message := c.PostForm("message") | |
| nick := c.DefaultPostForm("nick", "anonymous") | |
| c.JSON(200, gin.H{ | |
| "status": "posted", | |
| "message": message, | |
| "nick": nick, | |
| }) | |
| }) | |
| router.POST("/post", func(c *gin.Context) { | |
| id := c.Query("id") | |
| page := c.DefaultQuery("page", "0") | |
| name := c.PostForm("name") | |
| message := c.PostForm("message") | |
| fmt.Printf("id: %s; page: %s; name: %s; message: %s", id, page, name, message) | |
| }) | |
| router.POST("/post2", func(c *gin.Context) { | |
| id := c.Query("ids") | |
| fmt.Println(id) | |
| ids := c.QueryMap("ids") | |
| names := c.PostFormMap("names") | |
| fmt.Printf("ids: %v; names: %v", ids, names) | |
| }) | |
| router.POST("/upload", func(c *gin.Context) { | |
| file, err := c.FormFile("file") | |
| if err != nil { | |
| c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error())) | |
| return | |
| } | |
| filename := filepath.Base(file.Filename) | |
| if err := c.SaveUploadedFile(file, filename); err != nil { | |
| c.String(http.StatusBadRequest, fmt.Sprintf("upload file err: %s", err.Error())) | |
| return | |
| } | |
| c.String(http.StatusOK, fmt.Sprintf("File %s uploaded successfully.", filename)) | |
| }) | |
| v1 := router.Group("/v1") | |
| { | |
| v1.POST("/login", func(c *gin.Context) { | |
| c.String(200, "/login") | |
| }) | |
| v1.POST("/submit", func(c *gin.Context) { | |
| c.String(200, "/submit") | |
| }) | |
| v1.POST("/read", func(c *gin.Context) { | |
| c.String(200, "/read") | |
| }) | |
| } | |
| router.POST("/loginJSON", func(c *gin.Context) { | |
| var json Login | |
| if err := c.ShouldBindJSON(&json); err != nil { | |
| c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | |
| return | |
| } | |
| if json.User != "manu" || json.Password != "123" { | |
| c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"}) | |
| return | |
| } | |
| c.JSON(http.StatusOK, gin.H{"status": "you are logged in"}) | |
| }) | |
| router.GET("/posts/index", func(c *gin.Context) { | |
| c.HTML(http.StatusOK, "posts/index.tmpl", gin.H{ | |
| "title": "Posts", | |
| }) | |
| }) | |
| router.GET("/users/index", func(c *gin.Context) { | |
| c.HTML(http.StatusOK, "users/index.tmpl", gin.H{ | |
| "title": "Users", | |
| }) | |
| }) | |
| port := "8000" | |
| if len(os.Args) > 1 { | |
| port = os.Args[1] | |
| } | |
| router.Run(":" + port) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment