-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
55 lines (49 loc) · 1.53 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
package main
import (
"cn-universitysrv/config"
"cn-universitysrv/handles"
"cn-universitysrv/middlewares"
"flag"
"runtime"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/go-xweb/log"
)
var (
tomlFile = flag.String("config", "docs/test.toml", "config file")
)
// init 初始化配置
func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
gin.SetMode(gin.ReleaseMode)
}
func main() {
flag.Parse()
// 解析配置文件
tomlConfig, err := config.UnmarshalConfig(*tomlFile)
if err != nil {
log.Errorf("UnmarshalConfig: err:%v\n", err)
return
}
router := gin.New()
router.Use(gin.Recovery())
//设置跨域
router.Use(cors.New(cors.Config{
AllowAllOrigins: true,
AllowMethods: []string{"GET", "PUT", "POST", "DELETE", "OPTIONS"},
AllowHeaders: []string{"Content-Type", "Origin", "Content-Length"},
AllowCredentials: true,
MaxAge: 12 * time.Hour,
}))
router.Use(middlewares.Config(tomlConfig))
middlewares.PgConnect("universitydb", tomlConfig)
router.GET("/", handles.Index) // 首页
router.POST("/store/schools", handles.StoreUniversity) // 存储大学信息
router.GET("/schools", handles.GetUniversityList) // 获取大学列表
router.GET("/provinces", handles.GetProvinceList) // 省份列表
router.GET("/citys", handles.GetCityList) // 获取城市列表 路由命名错误,不做修改
// 启动服务
log.Debugf("run cn-universitysrv at %v\n", tomlConfig.GetListenAddr())
router.Run(tomlConfig.GetListenAddr())
}