api/server/main.go

54 lines
864 B
Go

package main
import (
"strings"
"time"
"git.fuyu.moe/5GPowerQuality/api/app"
"git.fuyu.moe/Fuyu/flog"
"github.com/labstack/echo"
)
func main() {
defer func() {
v := recover()
if v != nil {
flog.Critical(`panic: `, v)
}
}()
setLogger()
flog.Info(`Starting`)
catchSignals()
app.InitDB()
e := echo.New()
e.HideBanner = true
e.Use(recoverMiddleware)
e.GET(`/count`, count)
for _, v := range app.GetAttributes() {
e.GET(`/`+strings.ToLower(v.Name)+`/:date`, getAttr(v.Index))
}
panic(e.Start(`localhost:33333`))
}
func count(c echo.Context) error {
return c.JSON(200, app.Count())
}
func getAttr(index int) echo.HandlerFunc {
return func(c echo.Context) error {
date, err := time.Parse(time.RFC3339, c.Param(`date`))
if err != nil {
return c.NoContent(400)
}
return c.JSON(200, app.GetAttribute(index, date))
}
}