added error handling

This commit is contained in:
David Alasow 2018-12-03 15:26:52 +01:00
parent dcddc3da66
commit 453cb96ab8
3 changed files with 28 additions and 5 deletions

24
main.go
View File

@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"git.fuyu.moe/Fuyu/flog"
"git.fuyu.moe/Fuyu/router"
"net/http"
"strconv"
@ -36,6 +37,11 @@ type meikanListItem struct {
Recommended bool `json:"recommend"`
}
type meikanList struct {
User string
Anime []meikanListItem
}
type meikanAnime struct {
ID int `json:"id"`
Title string `json:"title"`
@ -56,9 +62,10 @@ func main() {
r.Renderer = NewRenderer()
r.GET(`/static/*asset`, getStatic)
r.GET(`/`, home)
r.GET(`/compare/:user`, compare)
r.GET(`/compare`, compare)
err := r.Start(`:8070`)
check(err)
r.ErrorHandler = errorHandler
}
func check(e error) {
@ -72,11 +79,14 @@ func home(c *router.Context) error {
}
func compare(c *router.Context) error {
user := c.Param(`user`)
user := c.QueryParam(`name`)
fmt.Println(user)
//https://api.jikan.moe/v3/user/nekomata1037/animelist/all
//https://api.meikan.moe/v1/ids/anime
jikanList := jikanAPIGetUserAnimelist(user)
var anime []meikanListItem
var list meikanList
list.User = user
anime := list.Anime
fmt.Println("Getting IDs")
IDs := meikanAPIGetIDs()
for i := 0; i < len(jikanList.Anime); i++ {
@ -106,7 +116,7 @@ func compare(c *router.Context) error {
anime = append(anime, temp)
}
return c.JSON(200, anime)
return c.Render(200, `list`, list)
}
func jikanAPIGetUserAnimelist(user string) jikanAPIUserList {
@ -135,3 +145,9 @@ func meikanAPIGetIDs() map[int]int {
}
return IDsMap
}
func errorHandler(c *router.Context, v interface{}) {
flog.Critical(c.Request.Method, ` `, c.Request.URL.Path, `: `, v)
_ = c.StatusText(http.StatusBadRequest)
}

View File

@ -1,3 +1,7 @@
{{ define "body" }}
<h1>Hello, 世界!</h1>
<h1>Import MAL list</h1>
<form action="/compare" method="get">
<input name="name" placeholder="name" type="text">
<button type="submit">Import</button>
</form>
{{ end }}

View File

@ -0,0 +1,3 @@
{{ define "body" }}
<h1>List for {{.List.User}}</h1>
{{ end }}