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

View File

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