mirror of
https://github.com/ultrasn0w/huso.git
synced 2025-12-13 17:29:54 +01:00
77 lines
1.5 KiB
Go
77 lines
1.5 KiB
Go
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)
|
|
}
|