Skip to content
Myr

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 file

Take 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 back
1package main
23import (

chore: use http.Status* instead of hard code (#1482)田欧before adoptionf45c928

lines 4–5why, one step back
4 "net/http"
5

update template example (#1038)田欧before adoptiond39ed41

lines 6–8why, one step back
6 "github.com/gin-gonic/gin"
7)8

chore: add some annotations (#1544)田欧before adoptiond510595

line 9why, one step back
9var db = make(map[string]string)

update template example (#1038)田欧before adoptiond39ed41

line 10why, one step back
10

Added simple testing documentation and examples (#1156)Andrii Bubisbefore adoption80f6911

line 11why, one step back
11func setupRouter() *gin.Engine {

update template example (#1038)田欧before adoptiond39ed41

lines 12–17why, one step back
12 // Disable Console Color
13 // 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 back
18 c.String(http.StatusOK, "pong")

update template example (#1038)田欧before adoptiond39ed41

lines 19–23why, one step back
19 })
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 back
24 value, ok := db[user]

update template example (#1038)田欧before adoptiond39ed41

line 25why, one step back
25 if ok {

chore: use http.Status* instead of hard code (#1482)田欧before adoptionf45c928

line 26why, one step back
26 c.JSON(http.StatusOK, gin.H{"user": user, "value": value})

update template example (#1038)田欧before adoptiond39ed41

line 27why, one step back
27 } else {

chore: use http.Status* instead of hard code (#1482)田欧before adoptionf45c928

line 28why, one step back
28 c.JSON(http.StatusOK, gin.H{"user": user, "status": "no value"})

update template example (#1038)田欧before adoptiond39ed41

lines 29–52why, one step back
29 }
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 back
53 db[user] = json.Value

chore: use http.Status* instead of hard code (#1482)田欧before adoptionf45c928

line 54why, one step back
54 c.JSON(http.StatusOK, gin.H{"status": "ok"})

update template example (#1038)田欧before adoptiond39ed41

lines 55–57why, one step back
55 }
56 })57

Added simple testing documentation and examples (#1156)Andrii Bubisbefore adoption80f6911

lines 58–62why, one step back
58 return r
59}6061func main() {62 r := setupRouter()

update template example (#1038)田欧before adoptiond39ed41

lines 63–65why, one step back
63 // Listen and Server in 0.0.0.0:8080
64 r.Run(":8080")65}