api/server/main.go

64 lines
998 B
Go
Raw Normal View History

2018-05-14 14:19:29 +02:00
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)
}
if len(attr) == 2 {
attr = strings.ToUpper(attr[:2])
} else {
attr = strings.ToUpper(attr[:2]) + strings.ToLower(attr[2:])
}
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)
}