This commit is contained in:
daru
2022-04-13 19:11:14 +02:00
parent 14a146fc53
commit 1bf126afa4
11 changed files with 639 additions and 0 deletions

76
ober.go Normal file
View File

@@ -0,0 +1,76 @@
package main
import (
"encoding/json"
"log"
"strconv"
"github.com/fasthttp/router"
"github.com/valyala/fasthttp"
)
func RunWebserv() {
r := router.New()
r.GET("/", Start)
r.GET("/api/season", Season)
r.POST("api/register", Register)
log.Fatal(fasthttp.ListenAndServe(":"+strconv.Itoa(*webServerPort), r.Handler))
}
func Start(ctx *fasthttp.RequestCtx) {
data, err := cache.Get(seasonApiJikan)
if err != nil {
addErrorToCtx(ctx, err)
return
}
var seasonData SeasonJikan
err = json.Unmarshal(data, &seasonData)
if err != nil {
addErrorToCtx(ctx, err)
return
}
WriteIndex(ctx, &seasonData)
ctx.SetContentType("text/html; charset=utf-8")
ctx.SetStatusCode(fasthttp.StatusOK)
}
func Season(ctx *fasthttp.RequestCtx) {
data, err := cache.Get(seasonApiJikan)
if err != nil {
addErrorToCtx(ctx, err)
return
}
var seasonData SeasonJikan
err = json.Unmarshal(data, &seasonData)
if err != nil {
addErrorToCtx(ctx, err)
return
}
_, err = ctx.Write(data)
if err != nil {
addErrorToCtx(ctx, err)
return
}
ctx.SetContentType("application/json; charset=utf-8")
ctx.SetStatusCode(fasthttp.StatusOK)
}
func Register(ctx *fasthttp.RequestCtx) {
_, err := ctx.WriteString("AAA")
if err != nil {
addErrorToCtx(ctx, err)
return
}
ctx.SetContentType("application/json; charset=utf-8")
ctx.SetStatusCode(fasthttp.StatusOK)
}
func addErrorToCtx(ctx *fasthttp.RequestCtx, err error) {
ctx.WriteString(err.Error())
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
}