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) }