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)
|
|
|
|
}
|
2018-05-22 12:07:18 +02:00
|
|
|
attr = convertAttribute(attr)
|
2018-05-14 14:19:29 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2018-05-22 12:07:18 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|