This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
router.go
61 lines (52 loc) · 1.45 KB
/
router.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
/**
* REST API router
* Rosbit Xu
*/
package main
import (
"github.com/rosbit/mgin"
"go-search/indexer"
"go-search/conf"
"go-search/rest"
"net/http"
"fmt"
"os"
"log"
"os/signal"
"syscall"
)
// 设置路由,进入服务状态
func StartService() error {
initIndexers()
api := mgin.NewMgin(mgin.WithLogger("go-search"))
api.GET("/schema/:index", rest.ShowSchema)
api.POST("/schema/:index", rest.CreateSchema)
api.DELETE("/schema/:index", rest.DeleteSchema)
api.PUT("/schema/:index/:newIndex", rest.RenameSchema)
api.PUT("/doc/:index", rest.IndexDoc)
api.PUT("/docs/:index", rest.IndexDocs)
api.PUT("/update/:index", rest.UpdateDoc)
api.DELETE("/doc/:index", rest.DeleteDoc)
api.DELETE("/docs/:index", rest.DeleteDocs)
api.GET("/search/:index", rest.Search)
// health check
api.GET("/health", func(c *mgin.Context) {
c.String(http.StatusOK, "OK\n")
})
serviceConf := conf.ServiceConf
listenParam := fmt.Sprintf("%s:%d", serviceConf.ListenHost, serviceConf.ListenPort)
log.Printf("I am listening at %s...\n", listenParam)
return http.ListenAndServe(listenParam, api)
}
func initIndexers() {
indexer.StartIndexers(conf.ServiceConf.WorkerNum)
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM, syscall.SIGSTOP, syscall.SIGQUIT)
go func() {
for range c {
log.Println("I will exit in a while")
indexer.StopIndexers(conf.ServiceConf.WorkerNum)
os.Exit(0)
}
}()
}