app.go 1.34 KB
package main

import (
	"fmt"

	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"

	_ "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
	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)

	auth := middleware.JWTWithConfig(middleware.JWTConfig{
		SigningKey: []byte(config.TokenSecret),
		Claims:     &AuthClaims{},
	})

	app.echo = echo.New()
	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)

	return app
}

func (app *App) Serve() {
	app.echo.Logger.Fatal(app.echo.Start(fmt.Sprintf(":%d", app.Config.Port)))
}