api/server/main.go

69 lines
1.1 KiB
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)
e.GET(`/:attr/:date`, pmax)
panic(e.Start(`localhost:33333`))
}
func count(c echo.Context) error {
return c.JSON(200, app.Count())
}
func pmax(c echo.Context) error {
attr := c.Param(`attr`)
if len(attr) < 2 {
return c.NoContent(400)
}
attr = convertAttribute(attr)
date, err := time.Parse(time.RFC3339, c.Param(`date`))
if err != nil {
return c.NoContent(400)
}
data := app.GetAttribute(attr, date)
if data == nil {
return c.NoContent(404)
}
return c.JSON(200, data)
}
func convertAttribute(attr string) string {
attr = strings.ToUpper(attr[:1]) + strings.ToLower(attr[1:])
attr = strings.Replace(attr, `gem`, `Gem`, -1)
attr = strings.Replace(attr, `max`, `Max`, -1)
return attr
}