151 lines
3.7 KiB
Go
Raw Normal View History

2016-11-09 11:32:19 +00:00
package main
import (
2017-06-12 19:11:20 +00:00
"errors"
"net/http"
2016-11-09 13:11:49 +00:00
"github.com/aerogo/aero"
2017-06-08 21:26:58 +00:00
"github.com/animenotifier/arn"
2016-11-12 16:40:16 +00:00
"github.com/animenotifier/notify.moe/components"
2016-11-23 03:44:28 +00:00
"github.com/animenotifier/notify.moe/pages/airing"
2016-11-19 14:54:31 +00:00
"github.com/animenotifier/notify.moe/pages/anime"
2017-06-08 09:51:34 +00:00
"github.com/animenotifier/notify.moe/pages/awards"
2017-06-02 14:17:58 +00:00
"github.com/animenotifier/notify.moe/pages/dashboard"
2016-11-19 14:54:31 +00:00
"github.com/animenotifier/notify.moe/pages/forum"
"github.com/animenotifier/notify.moe/pages/forums"
"github.com/animenotifier/notify.moe/pages/posts"
2016-11-20 10:26:11 +00:00
"github.com/animenotifier/notify.moe/pages/profile"
"github.com/animenotifier/notify.moe/pages/search"
2016-11-19 14:54:31 +00:00
"github.com/animenotifier/notify.moe/pages/threads"
2016-12-06 03:36:31 +00:00
"github.com/animenotifier/notify.moe/pages/users"
2016-11-09 11:32:19 +00:00
)
var app = aero.New()
func main() {
2016-12-02 15:23:05 +00:00
// HTTPS
2016-12-06 03:36:31 +00:00
app.Security.Load("security/fullchain.pem", "security/privkey.pem")
2016-12-02 15:23:05 +00:00
2017-06-11 09:13:59 +00:00
// CSS
app.SetStyle(components.CSS())
2017-06-08 21:26:58 +00:00
// Session store
app.Sessions.Store = arn.NewAerospikeStore("Session")
// Layout
2016-11-09 11:32:19 +00:00
app.Layout = func(ctx *aero.Context, content string) string {
2017-06-14 15:16:03 +00:00
return components.Layout(app, content)
2016-11-09 11:32:19 +00:00
}
// Ajax routes
2017-06-02 14:17:58 +00:00
app.Ajax("/", dashboard.Get)
app.Ajax("/anime", search.Get)
2016-11-19 14:54:31 +00:00
app.Ajax("/anime/:id", anime.Get)
app.Ajax("/forum", forums.Get)
2016-11-19 14:54:31 +00:00
app.Ajax("/forum/:tag", forum.Get)
app.Ajax("/threads/:id", threads.Get)
app.Ajax("/posts/:id", posts.Get)
2016-11-20 10:26:11 +00:00
app.Ajax("/user/:nick", profile.Get)
2017-06-08 12:46:38 +00:00
app.Ajax("/user/:nick/threads", threads.GetByUser)
2016-12-06 03:36:31 +00:00
app.Ajax("/users", users.Get)
2017-06-10 00:19:31 +00:00
app.Ajax("/airing", airing.Get)
2017-06-08 09:51:34 +00:00
app.Ajax("/awards", awards.Get)
2017-06-11 09:13:59 +00:00
// app.Ajax("/genres", genres.Get)
// app.Ajax("/genres/:name", genre.Get)
2017-06-06 14:59:04 +00:00
2017-06-06 11:07:42 +00:00
// Favicon
app.Get("/favicon.ico", func(ctx *aero.Context) string {
2017-06-13 17:39:59 +00:00
if ctx.CanUseWebP() {
return ctx.File("images/icons/favicon.webp")
}
return ctx.File("images/icons/favicon.png")
2017-06-01 13:38:08 +00:00
})
2017-06-11 09:13:59 +00:00
// Scripts
app.Get("/scripts.js", func(ctx *aero.Context) string {
return ctx.File("temp/scripts.js")
})
2017-06-06 11:07:42 +00:00
// Web manifest
app.Get("/manifest.json", func(ctx *aero.Context) string {
return ctx.JSON(app.Config.Manifest)
2017-06-05 18:21:06 +00:00
})
2017-06-11 09:13:59 +00:00
// Cover image
app.Get("/images/cover/:file", func(ctx *aero.Context) string {
format := ".jpg"
if ctx.CanUseWebP() {
format = ".webp"
}
return ctx.File("images/cover/" + ctx.Get("file") + format)
2017-06-06 13:51:22 +00:00
})
2017-06-12 19:11:20 +00:00
// Avatars
app.Get("/user/:nick/avatar", func(ctx *aero.Context) string {
nick := ctx.Get("nick")
user, err := arn.GetUserByNick(nick)
if err != nil {
return ctx.Error(http.StatusNotFound, "User not found", err)
}
if ctx.CanUseWebP() {
2017-06-13 11:23:54 +00:00
return ctx.File("images/avatars/large/webp/" + user.ID + ".webp")
2017-06-12 19:11:20 +00:00
}
2017-06-13 13:08:10 +00:00
original := arn.FindFileWithExtension(
user.ID,
"images/avatars/large/original/",
arn.OriginalImageExtensions,
)
if original == "" {
return ctx.Error(http.StatusNotFound, "Avatar not found", errors.New("Image not found for user: "+user.ID))
}
return ctx.File(original)
2017-06-12 19:11:20 +00:00
})
2017-06-12 22:06:35 +00:00
// Avatars
app.Get("/user/:nick/avatar/small", func(ctx *aero.Context) string {
nick := ctx.Get("nick")
user, err := arn.GetUserByNick(nick)
if err != nil {
return ctx.Error(http.StatusNotFound, "User not found", err)
}
if ctx.CanUseWebP() {
2017-06-13 11:23:54 +00:00
return ctx.File("images/avatars/small/webp/" + user.ID + ".webp")
2017-06-12 22:06:35 +00:00
}
2017-06-13 13:08:10 +00:00
original := arn.FindFileWithExtension(
user.ID,
"images/avatars/small/original/",
arn.OriginalImageExtensions,
)
if original == "" {
return ctx.Error(http.StatusNotFound, "Avatar not found", errors.New("Image not found for user: "+user.ID))
}
return ctx.File(original)
2017-06-12 22:06:35 +00:00
})
2017-06-12 19:11:20 +00:00
// Elements
2017-06-12 18:06:31 +00:00
app.Get("/images/elements/:file", func(ctx *aero.Context) string {
return ctx.File("images/elements/" + ctx.Get("file"))
})
2017-06-05 18:21:06 +00:00
// For benchmarks
app.Get("/hello", func(ctx *aero.Context) string {
return ctx.Text("Hello World")
2017-06-03 23:17:00 +00:00
})
// Let's go
2016-11-09 11:32:19 +00:00
app.Run()
}