-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (58 loc) · 1.37 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
package main
import (
"log"
"net/http"
"path"
"strconv"
"freeapi/config"
"freeapi/controller"
"freeapi/db"
"freeapi/middleware"
"freeapi/model"
"freeapi/router"
"freeapi/utils"
"github.com/jinzhu/gorm"
"github.com/labstack/echo/v4"
)
func initMiddlewares(e *echo.Echo, conf *config.Config) {
middleware.Init(e, conf)
}
func initRoutes(e *echo.Echo, conf *config.Config, db *gorm.DB) {
router.Init(e, conf, db)
}
func initDB(db *gorm.DB) {
db.AutoMigrate(&model.Attachment{}, &model.Area{})
}
func main() {
rootDir := utils.GetRootDir()
// 初始化配置
conf, err := config.Initialize()
if err != nil {
log.Fatal(err)
}
dbConfig, ok := conf.Databases["sqlite3"]
if !ok {
log.Fatal("Can not load configuration of SQLite3")
}
orm := db.ORM{Name: "sqlite3"}
orm.Open(path.Join(rootDir, dbConfig.DBPath))
defer orm.Close()
initDB(orm.DB)
e := echo.New()
// 全局错误处理
e.HTTPErrorHandler = func(err error, c echo.Context) {
c.JSON(http.StatusBadRequest, controller.BaseResponseJSON{
Success: false,
Code: controller.STATUS_ERROR,
Message: "url=" + c.Request().RequestURI + ", error=" + err.Error(),
})
}
// 初始化中间件
initMiddlewares(e, &conf)
// 初始化路由
initRoutes(e, &conf, orm.DB)
// 运行服务
e.Logger.Fatal(e.Start(conf.Server.Host + ":" + strconv.Itoa(conf.Server.Port)))
}
func init() {
}