131 lines
2.8 KiB
Go
Raw Normal View History

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)
2016-11-03 07:54:30 +00:00
app.SetStyle(css)
2016-11-02 13:47:22 +00:00
scripts, _ := ioutil.ReadFile("scripts.js")
js := string(scripts)
2016-10-22 17:03:16 +00:00
2016-11-03 04:01:06 +00:00
// Define layout
app.Layout = func(ctx *aero.Context, content string) string {
2016-11-03 07:54:30 +00:00
return Render.Layout(content)
2016-11-03 04:01:06 +00:00
}
app.Register("/", func(ctx *aero.Context) string {
return ctx.HTML(Render.Dashboard())
2016-11-02 13:47:22 +00:00
})
2016-11-03 04:01:06 +00:00
app.Register("/genres", func(ctx *aero.Context) string {
return ctx.HTML(Render.GenreOverview())
2016-10-22 17:03:16 +00:00
})
2016-11-03 04:01:06 +00:00
type GenreInfo struct {
Genre string `json:"genre"`
AnimeList []*arn.Anime `json:"animeList"`
}
2016-11-02 15:00:17 +00:00
2016-11-03 04:01:06 +00:00
app.Register("/genres/:name", func(ctx *aero.Context) string {
2016-11-04 14:03:13 +00:00
genreName := ctx.Get("name")
2016-11-03 04:01:06 +00:00
genreInfo := new(GenreInfo)
2016-11-02 15:00:17 +00:00
2016-11-03 04:01:06 +00:00
err := arn.GetObject("Genres", genreName, genreInfo)
if err != nil {
return err.Error()
2016-11-02 15:00:17 +00:00
}
2016-11-03 04:01:06 +00:00
return ctx.HTML(Render.AnimeInGenre(genreInfo.Genre, genreInfo.AnimeList))
// var animeList []*arn.Anime
// results := make(chan *arn.Anime)
// arn.Scan("Anime", results)
// for anime := range results {
// genres := Map(anime.Genres, arn.FixGenre)
// if Contains(genres, genreName) {
// animeList = append(animeList, anime)
// }
// }
// return ctx.HTML(Render.AnimeInGenre(genreName, animeList))
2016-11-02 15:00:17 +00:00
})
2016-11-03 04:01:06 +00:00
app.Register("/anime/:id", func(ctx *aero.Context) string {
2016-11-04 14:03:13 +00:00
id, _ := strconv.Atoi(ctx.Get("id"))
2016-10-21 16:25:35 +00:00
anime, err := arn.GetAnime(id)
if err != nil {
2016-11-03 04:01:06 +00:00
return ctx.Text("Anime not found")
2016-11-02 13:47:22 +00:00
}
2016-11-03 04:01:06 +00:00
return ctx.HTML(Render.Anime(anime))
2016-11-02 13:47:22 +00:00
})
2016-11-03 04:01:06 +00:00
// ---------------------------------------------------------------
// API
// ---------------------------------------------------------------
2016-11-02 13:47:22 +00:00
2016-11-03 04:01:06 +00:00
app.Get("/scripts.js", func(ctx *aero.Context) string {
ctx.SetHeader("Content-Type", "application/javascript")
return js
})
app.Get("/all/anime", func(ctx *aero.Context) string {
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)
2016-10-21 16:25:35 +00:00
}
2016-11-03 04:01:06 +00:00
sort.Strings(titles)
2016-10-21 16:25:35 +00:00
2016-11-03 04:01:06 +00:00
return ctx.Text(s(len(titles)) + " anime fetched in " + s(time.Since(start)) + "\n\n" + strings.Join(titles, "\n"))
2016-11-02 13:47:22 +00:00
})
2016-11-03 04:01:06 +00:00
app.Get("/api/anime/:id", func(ctx *aero.Context) string {
2016-11-04 14:03:13 +00:00
id, _ := strconv.Atoi(ctx.Get("id"))
2016-11-02 13:47:22 +00:00
anime, err := arn.GetAnime(id)
if err != nil {
2016-11-03 04:01:06 +00:00
return ctx.Text("Anime not found")
2016-11-02 13:47:22 +00:00
}
2016-11-03 04:01:06 +00:00
return ctx.JSON(anime)
2016-11-02 13:47:22 +00:00
})
2016-11-03 04:01:06 +00:00
app.Get("/api/users/:nick", func(ctx *aero.Context) string {
2016-11-04 14:03:13 +00:00
nick := ctx.Get("nick")
2016-11-02 13:47:22 +00:00
user, err := arn.GetUserByNick(nick)
if err != nil {
2016-11-03 04:01:06 +00:00
return ctx.Text("User not found")
2016-11-02 13:47:22 +00:00
}
2016-11-03 04:01:06 +00:00
return ctx.JSON(user)
2016-11-03 00:01:23 +00:00
})
2016-10-28 16:11:25 +00:00
2016-10-21 16:25:35 +00:00
app.Run()
}