-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.go
81 lines (67 loc) · 1.78 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
package main
import (
"github.com/go-redis/cache"
"github.com/go-redis/redis"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
// 定义全区变量 为了保证执行顺序 初始化均在main中执行
var (
// gorm mysql db connection
db *gorm.DB
// redis client
rdb *redis.Client
// global cache
cc *cache.Codec
)
// @title RESTful API DEMO by Golang & Echo
// @version 1.0
// @description This is a demo server.
// @contact.name Muninn
// @contact.email [email protected]
// @license.name MIT
// @license.url https://github.com/hyacinthus/restdemo/blob/master/LICENSE
// @host demo.crandom.com
// @BasePath /
func main() {
// init echo
e := echo.New()
e.HTTPErrorHandler = httpErrorHandler
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(middleware.CORS())
e.Use(middleware.KeyAuthWithConfig(middleware.KeyAuthConfig{
Skipper: skipper, // 跳过验证条件 在 auth.go 定义
Validator: validator, // 处理验证结果 在 auth.go 定义
}))
e.Use(ParsePagination) // 分页参数解析,在 pagination.go 定义
// Echo debug setting
if config.APP.Debug {
e.Debug = true
}
// init mysql and redis
initDB()
defer db.Close()
initRedis()
defer rdb.Close()
// init global cache
initCache()
// async create tables
go createTables()
// status
e.GET("/status", getStatus)
// auth
e.POST("/login", login)
// note Routes
e.GET("/notes", getNotes)
e.POST("/notes", createNote)
e.GET("/notes/:id", getNote)
e.PUT("/notes/:id", updateNote)
e.DELETE("/notes/:id", deleteNote)
e.GET("/public/notes", getPublicNotes)
e.GET("/public/notes/:id", getPublicNote)
// Start echo server
e.Logger.Fatal(e.Start(config.APP.Host + ":" + config.APP.Port))
}