-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
85 lines (65 loc) · 1.6 KB
/
server.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"flag"
"net/http"
"strconv"
"github.com/gin-gonic/contrib/static"
"github.com/gin-gonic/gin"
)
const apiPrefix = "/api/v1"
var (
dbName = flag.String("db", "/tmp/blog.sqlite", "sqlite database filename")
env = flag.String("env", "prod", "environment ('prod' or 'dev')")
)
type Post struct {
ID int64 `json:"id" binding:"-"`
Title string `json:"title" binding:"required"`
Text string `json:"text" binding:"required"`
}
func main() {
flag.Parse()
r := gin.Default()
r.LoadHTMLGlob("templates/*")
r.Use(static.Serve("/static", static.LocalFile("static", false)))
r.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{
"env": *env,
"apiPrefix": apiPrefix,
})
})
api := r.Group(apiPrefix)
posts := []*Post{
&Post{1, "my first post", "hello"},
&Post{2, "my next post", "hello again"},
}
api.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"posts": posts})
})
api.POST("/", func(c *gin.Context) {
post := &Post{}
if err := c.BindJSON(post); err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
post.ID = int64(len(posts) + 1)
posts = append(posts, post)
c.JSON(http.StatusOK, post)
})
api.GET("/:id", func(c *gin.Context) {
id, err := strconv.ParseInt(c.Params.ByName("id"), 10, 64)
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
for _, post := range posts {
if post.ID == id {
c.JSON(http.StatusOK, post)
return
}
}
c.String(http.StatusNotFound, "No post found")
})
if err := r.Run(":8080"); err != nil {
panic(err)
}
}