2016-10-21 16:25:35 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-10-22 17:03:16 +00:00
|
|
|
"io/ioutil"
|
2016-11-02 15:00:17 +00:00
|
|
|
"sort"
|
2016-10-21 16:25:35 +00:00
|
|
|
"strconv"
|
2016-11-02 15:00:17 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2016-10-21 16:25:35 +00:00
|
|
|
|
|
|
|
"github.com/aerojs/aero"
|
2016-11-02 13:47:22 +00:00
|
|
|
"github.com/animenotifier/arn"
|
2016-10-21 16:25:35 +00:00
|
|
|
)
|
|
|
|
|
2016-10-22 17:03:16 +00:00
|
|
|
func main() {
|
2016-10-21 16:25:35 +00:00
|
|
|
app := aero.New()
|
2016-10-22 17:03:16 +00:00
|
|
|
|
2016-10-28 16:11:25 +00:00
|
|
|
cssBytes, _ := ioutil.ReadFile("layout.css")
|
|
|
|
css := string(cssBytes)
|
2016-11-02 13:47:22 +00:00
|
|
|
|
|
|
|
animeCSSBytes, _ := ioutil.ReadFile("anime.css")
|
|
|
|
css += string(animeCSSBytes)
|
|
|
|
|
|
|
|
scripts, _ := ioutil.ReadFile("scripts.js")
|
|
|
|
js := string(scripts)
|
2016-10-22 17:03:16 +00:00
|
|
|
|
2016-10-23 15:45:37 +00:00
|
|
|
app.Get("/", func(ctx *aero.Context) {
|
2016-11-02 13:47:22 +00:00
|
|
|
ctx.HTML(Render.Layout(Render.Dashboard(), css))
|
|
|
|
})
|
|
|
|
|
|
|
|
app.Get("/_/", func(ctx *aero.Context) {
|
|
|
|
ctx.HTML(Render.Dashboard())
|
2016-10-22 17:03:16 +00:00
|
|
|
})
|
|
|
|
|
2016-11-02 15:00:17 +00:00
|
|
|
app.Get("/all/anime", func(ctx *aero.Context) {
|
|
|
|
start := time.Now()
|
|
|
|
var titles []string
|
|
|
|
|
|
|
|
results := make(chan *arn.Anime)
|
|
|
|
arn.Scan("Anime", results)
|
|
|
|
|
|
|
|
for anime := range results {
|
|
|
|
titles = append(titles, anime.Title.Romaji)
|
|
|
|
}
|
|
|
|
sort.Strings(titles)
|
|
|
|
|
|
|
|
ctx.Text(s(len(titles)) + " anime fetched in " + s(time.Since(start)) + "\n\n" + strings.Join(titles, "\n"))
|
|
|
|
})
|
|
|
|
|
2016-10-23 15:45:37 +00:00
|
|
|
app.Get("/anime/:id", func(ctx *aero.Context) {
|
|
|
|
id, _ := strconv.Atoi(ctx.Params.ByName("id"))
|
2016-10-21 16:25:35 +00:00
|
|
|
anime, err := arn.GetAnime(id)
|
|
|
|
|
|
|
|
if err != nil {
|
2016-11-02 13:47:22 +00:00
|
|
|
ctx.Text("Anime not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.HTML(Render.Layout(Render.Anime(anime), css))
|
|
|
|
})
|
|
|
|
|
|
|
|
app.Get("/_/anime/:id", func(ctx *aero.Context) {
|
|
|
|
id, _ := strconv.Atoi(ctx.Params.ByName("id"))
|
|
|
|
anime, err := arn.GetAnime(id)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ctx.Text("Anime not found")
|
2016-10-21 16:25:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-11-02 13:47:22 +00:00
|
|
|
ctx.HTML(Render.Anime(anime))
|
|
|
|
})
|
|
|
|
|
|
|
|
app.Get("/api/anime/:id", func(ctx *aero.Context) {
|
|
|
|
id, _ := strconv.Atoi(ctx.Params.ByName("id"))
|
|
|
|
anime, err := arn.GetAnime(id)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ctx.Text("Anime not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.JSON(anime)
|
|
|
|
})
|
|
|
|
|
|
|
|
app.Get("/api/users/:nick", func(ctx *aero.Context) {
|
|
|
|
nick := ctx.Params.ByName("nick")
|
|
|
|
user, err := arn.GetUserByNick(nick)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ctx.Text("User not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.JSON(user)
|
|
|
|
})
|
|
|
|
|
2016-11-02 15:00:17 +00:00
|
|
|
app.Get("/genres", func(ctx *aero.Context) {
|
|
|
|
ctx.HTML(Render.Layout(Render.GenreOverview(), css))
|
|
|
|
})
|
2016-11-02 13:47:22 +00:00
|
|
|
|
2016-11-02 15:00:17 +00:00
|
|
|
app.Get("/_/genres", func(ctx *aero.Context) {
|
|
|
|
ctx.HTML(Render.GenreOverview())
|
2016-11-02 13:47:22 +00:00
|
|
|
})
|
|
|
|
|
2016-11-03 00:01:23 +00:00
|
|
|
app.Get("/genres/:name", func(ctx *aero.Context) {
|
|
|
|
genreName := ctx.Params.ByName("name")
|
2016-10-28 16:11:25 +00:00
|
|
|
|
2016-11-03 00:01:23 +00:00
|
|
|
var animeList []*arn.Anime
|
|
|
|
results := make(chan *arn.Anime)
|
|
|
|
arn.Scan("Anime", results)
|
2016-10-28 16:11:25 +00:00
|
|
|
|
2016-11-03 00:01:23 +00:00
|
|
|
for anime := range results {
|
|
|
|
genres := Map(anime.Genres, arn.FixGenre)
|
|
|
|
if Contains(genres, genreName) {
|
|
|
|
animeList = append(animeList, anime)
|
|
|
|
}
|
|
|
|
}
|
2016-10-28 16:11:25 +00:00
|
|
|
|
2016-11-03 00:01:23 +00:00
|
|
|
ctx.HTML(Render.Layout(Render.AnimeInGenre(genreName, animeList), css))
|
|
|
|
})
|
2016-10-28 16:11:25 +00:00
|
|
|
|
2016-11-03 00:01:23 +00:00
|
|
|
app.Get("/scripts.js", func(ctx *aero.Context) {
|
|
|
|
ctx.SetHeader("Content-Type", "application/javascript")
|
|
|
|
ctx.Respond(js)
|
|
|
|
})
|
2016-10-28 16:11:25 +00:00
|
|
|
|
2016-10-21 16:25:35 +00:00
|
|
|
app.Run()
|
|
|
|
}
|