app.go
1.68 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
package main
import (
"fmt"
"net/http"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
_ "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
router *mux.Router
}
func NewApp(config Config) *App {
app := new(App)
app.Config = config
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s", config.Database.User, config.Database.Password, config.Database.Host, config.Database.Name)
app.db = sqlx.MustOpen("mysql", dsn)
app.router = mux.NewRouter()
app.router.HandleFunc("/users", app.PostUsers).Methods("POST")
app.router.HandleFunc("/users/tokens", app.PostTokens).Methods("POST")
app.router.Handle("/extractions", app.WithAuth(app.PostExtractions)).Methods("Post")
app.router.HandleFunc("/extractions/{file}/calls", app.GetCalls).Methods("GET")
app.router.HandleFunc("/extractions/{file}/messages", app.GetMessages).Methods("GET")
app.router.HandleFunc("/extractions/{file}/calls/analyses", app.GetCallsAnalyses).Methods("GET")
app.router.HandleFunc("/extractions/{file}/apps/analyses", app.GetAppsAnalyses).Methods("GET")
app.router.HandleFunc("/extractions/{file}/messages/analyses", app.GetMessagesAnalyses).Methods("GET")
app.router.HandleFunc("/extractions/{file}/processes", app.GetProcesses).Methods("GET")
app.router.HandleFunc("/extractions/{file}/alarms", app.GetAlarms).Methods("GET")
return app
}
func (app *App) Serve() {
http.ListenAndServe(fmt.Sprintf(":%d", app.Config.Port), handlers.CORS(
handlers.AllowedMethods([]string{"GET", "POST", "UPDATE", "DELETE"}),
handlers.AllowedOrigins([]string{"*"}),
)(app.router))
}