-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
119 lines (108 loc) · 2.63 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
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"time"
"github.com/gin-gonic/gin"
"github.com/orange-jacky/albums/data"
"github.com/orange-jacky/albums/router"
"github.com/orange-jacky/albums/util"
)
var stoper []data.Stoper
func usage(programName string) {
fmt.Println(`
usage:
albums [configure file]
eg: albums conf/conf.xml
`)
}
func main() {
if len(os.Args) != 2 {
usage(os.Args[0])
os.Exit(-1)
}
//加载配置文件
configure := util.Configure(os.Args[1])
Init()
defer Release()
//设置gin模式
gin.SetMode(gin.ReleaseMode)
//配置路由
r := gin.Default()
r.Use(util.CORS()) //支持跨域访问,但放弃了安全性
r.POST("/signup", router.SignUp)
authMiddleware := router.GetAuthMiddleware()
r.POST("/login", authMiddleware.LoginHandler)
auth := r.Group("/auth")
auth.Use(authMiddleware.MiddlewareFunc())
auth.Use(util.CORS()) //支持跨域访问,但放弃了安全性
{
auth.GET("/test", router.SignIn)
auth.POST("/upload", router.UpLoad)
auth.POST("/download", router.DownLoad)
auth.POST("/search", router.Search)
auth.POST("/managealbum/:action", router.AlbumManage)
auth.POST("/delete", router.Delete)
auth.POST("/deeplearning", router.DeepLearning)
auth.POST("/objectdetection_dl", router.ObjectDetectionDL)
}
server := fmt.Sprintf("%s:%s", configure.Gin.Host, configure.Gin.Port)
//起一个http服务器
s := &http.Server{
Addr: server,
Handler: r,
ReadTimeout: 120 * time.Second,
WriteTimeout: 120 * time.Second,
}
go func(s *http.Server) {
log.Printf("[Main] http server start\n")
err := s.ListenAndServe()
log.Printf("[Main] http server stop (%+v)\n", err)
}(s)
// Trap SIGINT to trigger a shutdown.
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, os.Kill)
for {
select {
case sig := <-signals:
log.Println("[Main] Catch signal", sig)
//平滑关闭server
err := s.Shutdown(context.Background())
log.Printf("[Main] start gracefully shuts down http serve %+v", err)
return
}
}
}
func Init() {
//启动日志单实例
mylog := util.Mylog()
stoper = append(stoper, mylog)
//创建jobqueue
jobqueue := util.JobQueue()
jobqueue.Start()
stoper = append(stoper, jobqueue)
//user
user := util.MongoUser()
stoper = append(stoper, user)
//album
album := util.MongoAlbum()
stoper = append(stoper, album)
//image
image := util.MongoImage()
stoper = append(stoper, image)
//imageinfo
imageInfo := util.MongoImageInfo()
stoper = append(stoper, imageInfo)
//feature
f := util.Service_feature()
stoper = append(stoper, f)
}
func Release() {
for _, v := range stoper {
v.Stop()
}
}