-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo_gin.go
48 lines (37 loc) · 1.21 KB
/
demo_gin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
// gin.SetMode(gin.ReleaseMode)
router := gin.Default()
// http://localhost:8080/welcome?firstname=Jane&lastname=Doe
router.GET("/welcome", func(c *gin.Context) {
firstname := c.DefaultQuery("firstname", "Guest")
// shortcut for c.Request.URL.Query().Get("lastname")
lastname := c.Query("lastname")
c.String(http.StatusOK, "Hello %s %s", firstname, lastname)
})
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.StaticFile("/favicon.ico", "./favicon.ico")
router.StaticFS("/static", http.Dir("static/"))
router.LoadHTMLGlob("templates/*")
//router.LoadHTMLFiles("templates/template1.html", "templates/template2.html")
router.GET("/index", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"title": "Main website",
})
})
router.GET("/", func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, "/index")
})
router.Run(":8080")
}