Initial commit

This commit is contained in:
Nise Void 2018-05-14 14:19:29 +02:00
commit 6bc66975db
Signed by: NiseVoid
GPG key ID: FBA14AC83EA602F3
10 changed files with 407 additions and 0 deletions

19
server/logger.go Normal file
View file

@ -0,0 +1,19 @@
package main
import (
"os"
"os/signal"
"syscall"
"git.fuyu.moe/Fuyu/flog"
)
func catchSignals() {
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
go func() {
s := <-sc
flog.Info(`Stopping, signal: `, s)
os.Exit(0)
}()
}

12
server/logger_debug.go Normal file
View file

@ -0,0 +1,12 @@
// +build cgo
package main
import (
"git.fuyu.moe/Fuyu/flog"
)
func setLogger() {
flog.MinLevel = flog.LevelDebug
flog.MinStackLevel = flog.LevelWarning
}

20
server/logger_release.go Normal file
View file

@ -0,0 +1,20 @@
// +build !cgo
package main
import (
"os"
"git.fuyu.moe/Fuyu/flog"
)
func setLogger() {
f, err := os.OpenFile(`log`, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
flog.Output = f
flog.MinLevel = flog.LevelInfo
flog.MinStackLevel = flog.LevelWarning
}

63
server/main.go Normal file
View file

@ -0,0 +1,63 @@
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)
}

20
server/panic.go Normal file
View file

@ -0,0 +1,20 @@
package main
import (
"git.fuyu.moe/Fuyu/flog"
"github.com/labstack/echo"
)
func recoverMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
defer func() {
v := recover()
if v != nil {
flog.Critical(`panic: `, v)
_ = c.JSON(500, `Fatal error occurred`)
}
}()
return next(c)
}
}