/
main.go
121 lines (100 loc) · 3.28 KB
/
main.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"os"
"log"
"time"
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/gorilla/handlers"
"github.com/chinakyc/AgBlog/app"
"github.com/chinakyc/AgBlog/cache"
"github.com/chinakyc/AgBlog/models"
"github.com/chinakyc/AgBlog/logging"
"github.com/chinakyc/AgBlog/controllers"
)
func main() {
models.OpenDB("sqlite3", "./goblog.db")
// My Customized httprouter
webapp := app.NewApp()
// `auth` group login and authentication methods
auth := controllers.AuthMiddware()
// simple cache
redisPool := cache.NewCachePool("127.0.0.1:6379")
cacheBackend := cache.NewCache("blog_", redisPool, time.Hour * 12)
// static file
webapp.ServeFiles("/static/*filepath", http.Dir("static/"))
// template use for angularjs
webapp.ServeFiles("/templates/*filepath", http.Dir("static/templates/"))
// call origin `httprouter.GET`
webapp.Router.GET("/", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, "static/templates/base.html")
})
// use email and password login return JWT token
webapp.POST("/login", auth.LoginController)
webapp.GET("/categorys", controllers.CategorysController)
// return articles by category max limit 10
webapp.Handler("GET", "/category/:category_id", controllers.ArticlesController(10))
// return and cache owner data
webapp.Handler(
"GET",
"/owner",
app.CacheHanle(
cacheBackend,
app.MiddlewareFunc(controllers.OwnerController),
),
)
// return and cache article
webapp.Handler(
"GET",
"/article/:article_id",
app.CacheHanle(
cacheBackend,
app.MiddlewareFunc(controllers.GetArticle)))
// require user token and at least editor permissions
// create article
webapp.Handler(
"POST",
"/article",
app.RequireUserHandle(
auth.ValidateUser,
app.AccessHandle(
auth.ValidateAuthority,
models.EDITOR,
app.MiddlewareFunc(controllers.PostArticle)),
),
)
// require user token and at least admin permissions
// delete article and cache
webapp.Handler(
"DELETE",
"/article/:article_id",
app.RequireUserHandle(
auth.ValidateUser,
app.AccessHandle(
auth.ValidateAuthority,
models.ADMIN,
app.DeleteCacheHanle(
cacheBackend,
app.MiddlewareFunc(controllers.DeleteArticle)),
),
),
)
// require user token and at least admin permissions
// update article and delete cache
webapp.Handler(
"PUT",
"/article/:article_id",
app.RequireUserHandle(
auth.ValidateUser,
app.AccessHandle(
auth.ValidateAuthority,
models.ADMIN,
app.DeleteCacheHanle(
cacheBackend,
app.MiddlewareFunc(controllers.ModifyArticle)),
),
),
)
logging.Logger.Info("Start listen...")
log.Fatal(http.ListenAndServe(":8080", handlers.LoggingHandler(os.Stdout, handlers.CompressHandler(webapp))))
}