controller-legacy/main.go

100 lines
2.4 KiB
Go

package main
/*
#cgo CFLAGS: -g -Wall
#cgo LDFLAGS: -lwiringPi -lwiringPiDev
#include <wiringPi.h>
*/
import "C"
import (
"net/http"
"strings"
"github.com/go-playground/validator/v10"
"github.com/spf13/viper"
"emgauwa.app/emgauwa-core/models"
"emgauwa.app/emgauwa-core/utils"
)
type (
EmgauwaValidator struct {
validator *validator.Validate
}
)
func (cv *EmgauwaValidator) Validate(i interface{}) error {
return cv.validator.Struct(i)
}
func SkipperSwagger(c echo.Context) bool {
if strings.Contains(c.Request().URL.Path, "swagger") {
return true
}
return false
}
func init() {
utils.ConfigInit()
utils.LoggerInit()
models.DatabaseInit()
}
// @title Emgauwa API
// @version 1.0
// @description This is a sample server server.
// @termsOfService http://swagger.io/terms/
// @contact.name Tobias Reisinger
// @contact.url https://serguzim.me
// @contact.email tobias@msrg.cc
func main() {
C.wiringPiSetup()
e := echo.New()
v := validator.New()
v.RegisterValidation("hhmmformat", utils.ValidatorsHHMMFormat)
e.Validator = &EmgauwaValidator{validator: v}
e.Pre(middleware.RemoveTrailingSlash())
e.Use(middleware.CORS())
e.Use(middleware.GzipWithConfig(middleware.GzipConfig{
Skipper: SkipperSwagger,
}))
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: "${time_rfc3339} [${method}] ${uri} > ${status} (${latency_human})\n",
}))
e.Use(middleware.Recover())
e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
Skipper: SkipperSwagger,
Root: viper.GetString("content-dir"),
HTML5: true,
}))
e.GET("api/swagger/*", echoSwagger.WrapHandler)
e.GET("api/swagger", func(c echo.Context) error {
return c.Redirect(http.StatusMovedPermanently, "/api/swagger/index.html")
})
r_api_v1 := e.Group("api/v1")
{
r_api_v1.GET("/schedules", api_v1.Schedules_GET)
r_api_v1.POST("/schedules", api_v1.Schedules_POST)
r_api_v1.GET("/schedules/:schedule_uid", api_v1.Schedules_UID_GET)
r_api_v1.PUT("/schedules/:schedule_uid", api_v1.Schedules_UID_PUT)
r_api_v1.DELETE("/schedules/:schedule_uid", api_v1.Schedules_UID_DELETE)
r_api_v1.GET("/schedules/tag/:tag", api_v1.Schedules_Tag_TAG_GET)
r_api_v1.POST("/schedules/list", api_v1.Schedules_List_POST)
r_api_v1.GET("/controllers", api_v1.Controllers_GET)
r_api_v1.GET("/tags", api_v1.Tags_GET)
r_api_v1.GET("/ws/relays", api_v1.WS_Relays_GET)
}
e.Start(viper.GetString("bind.http"))
}