main.go
This file
Path
examples/basic/main.go
65 lines · 1.3 KiB · Go
Last changed by
chore: add some annotations (#1544)田欧before adoptiond510595
History of this fileTake it
The walk stopped after 200 commits: lines older than d39ed41 point at it.
update template example (#1038)田欧before adoptiond39ed41
lines 1–3why, one step back1package main23import (chore: use http.Status* instead of hard code (#1482)田欧before adoptionf45c928
lines 4–5why, one step back4 "net/http"5update template example (#1038)田欧before adoptiond39ed41
lines 6–8why, one step back6 "github.com/gin-gonic/gin"7)8chore: add some annotations (#1544)田欧before adoptiond510595
line 9why, one step back9var db = make(map[string]string)update template example (#1038)田欧before adoptiond39ed41
line 10why, one step back10Added simple testing documentation and examples (#1156)Andrii Bubisbefore adoption80f6911
line 11why, one step back11func setupRouter() *gin.Engine {update template example (#1038)田欧before adoptiond39ed41
lines 12–17why, one step back12 // Disable Console Color13 // gin.DisableConsoleColor()14 r := gin.Default()1516 // Ping test17 r.GET("/ping", func(c *gin.Context) {chore: use http.Status* instead of hard code (#1482)田欧before adoptionf45c928
line 18why, one step back18 c.String(http.StatusOK, "pong")update template example (#1038)田欧before adoptiond39ed41
lines 19–23why, one step back19 })2021 // Get user value22 r.GET("/user/:name", func(c *gin.Context) {23 user := c.Params.ByName("name")chore: add some annotations (#1544)田欧before adoptiond510595
line 24why, one step back24 value, ok := db[user]update template example (#1038)田欧before adoptiond39ed41
line 25why, one step back25 if ok {chore: use http.Status* instead of hard code (#1482)田欧before adoptionf45c928
line 26why, one step back26 c.JSON(http.StatusOK, gin.H{"user": user, "value": value})update template example (#1038)田欧before adoptiond39ed41
line 27why, one step back27 } else {chore: use http.Status* instead of hard code (#1482)田欧before adoptionf45c928
line 28why, one step back28 c.JSON(http.StatusOK, gin.H{"user": user, "status": "no value"})update template example (#1038)田欧before adoptiond39ed41
lines 29–52why, one step back29 }30 })3132 // Authorized group (uses gin.BasicAuth() middleware)33 // Same than:34 // authorized := r.Group("/")35 // authorized.Use(gin.BasicAuth(gin.Credentials{36 // "foo": "bar",37 // "manu": "123",38 //}))39 authorized := r.Group("/", gin.BasicAuth(gin.Accounts{40 "foo": "bar", // user:foo password:bar41 "manu": "123", // user:manu password:12342 }))4344 authorized.POST("admin", func(c *gin.Context) {45 user := c.MustGet(gin.AuthUserKey).(string)4647 // Parse JSON48 var json struct {49 Value string `json:"value" binding:"required"`50 }5152 if c.Bind(&json) == nil {chore: add some annotations (#1544)田欧before adoptiond510595
line 53why, one step back53 db[user] = json.Valuechore: use http.Status* instead of hard code (#1482)田欧before adoptionf45c928
line 54why, one step back54 c.JSON(http.StatusOK, gin.H{"status": "ok"})update template example (#1038)田欧before adoptiond39ed41
lines 55–57why, one step back55 }56 })57Added simple testing documentation and examples (#1156)Andrii Bubisbefore adoption80f6911
lines 58–62why, one step back58 return r59}6061func main() {62 r := setupRouter()update template example (#1038)田欧before adoptiond39ed41
lines 63–65why, one step back63 // Listen and Server in 0.0.0.0:808064 r.Run(":8080")65}