/
server.go
executable file
·70 lines (58 loc) · 1.69 KB
/
server.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"github.com/prometheus/client_golang/prometheus"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/y-tac/gosom/dataporter"
"github.com/y-tac/gosom/handler"
"github.com/y-tac/gosom/som"
)
// Config 構造体。サブパッケージのconfigも設定する
type Config struct {
Server ServerConfig `json:"server"`
Som som.Config `json:"som"`
DataPorter dataporter.Config `json:"dataporter"`
}
// ServerConfig サーバconfigを設定
type ServerConfig struct {
Host string `json:"host"`
Port string `json:"port"`
}
func main() {
file, err := ioutil.ReadFile("config.json")
if err != nil {
panic(err)
}
var config Config
json.Unmarshal(file, &config)
fmt.Println(config)
dataporterQuit := make(chan bool)
go dataporter.Dataporter(config.DataPorter, dataporterQuit)
defer closeGoroutine(dataporterQuit)
gosomQuit := make(chan bool)
chset := som.MakeChannelRoutine()
collector := som.MakeCollector()
go som.Routine(chset, config.Som, collector, gosomQuit)
defer closeGoroutine(gosomQuit)
// Echoのインスタンス作る
e := echo.New()
// 全てのリクエストで差し込みたいミドルウェア(ログとか)はここ
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Static("/front", "front/dist")
// ルーティング
e.GET("/hello", handler.MainPage())
e.POST("/trait", handler.TraitAPI(chset.TraitCh))
e.GET("/map", handler.MapAPI(chset.MapCh))
e.GET("/metrics", echo.WrapHandler(prometheus.Handler()))
// サーバー起動
e.Start(":" + config.Server.Port)
}
// goroutine終了関数
func closeGoroutine(quit chan bool) {
quit <- true
return
}