app.go
1.81 KB
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
package main
import (
"fmt"
"net/http"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/olivere/elastic/v7"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
type Prop int
const (
PropUserNo Prop = iota
)
type App struct {
Config Config
db *sqlx.DB
es *elastic.Client
echo *echo.Echo
}
func NewApp(config Config) *App {
app := new(App)
app.Config = config
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?parseTime=true", config.Database.User, config.Database.Password, config.Database.Host, config.Database.Name)
app.db = sqlx.MustOpen("mysql", dsn)
es, err := elastic.NewClient()
if err != nil {
panic(err)
}
app.es = es
auth := middleware.JWTWithConfig(middleware.JWTConfig{
SigningKey: []byte(config.TokenSecret),
Claims: &AuthClaims{},
})
app.echo = echo.New()
app.echo.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowMethods: []string{
http.MethodGet,
http.MethodPost,
http.MethodPut,
http.MethodDelete,
http.MethodPatch,
http.MethodOptions,
http.MethodHead,
},
AllowHeaders: []string{"*"},
}))
app.echo.POST("/users", app.PostUsers)
app.echo.POST("/users/tokens", app.PostTokens)
app.echo.POST("/extractions", app.PostExtractions, auth)
extraction := app.echo.Group("/extractions/:no")
extraction.GET("/calls", app.GetCalls)
extraction.GET("/messages", app.GetMessages)
extraction.GET("/calls/analyses", app.GetCallsAnalyses)
extraction.GET("/apps/analyses", app.GetAppsAnalyses)
extraction.GET("/messages/analyses", app.GetMessagesAnalyses)
extraction.GET("/processes", app.GetProcesses)
extraction.GET("/alarms", app.GetAlarms)
app.echo.GET("/graph/:extractions", app.GetGraph)
return app
}
func (app *App) Serve() {
app.echo.Logger.Fatal(app.echo.Start(fmt.Sprintf(":%d", app.Config.Port)))
}