106 lines
2.5 KiB
Go
Raw Normal View History

2017-06-16 23:12:28 +00:00
package main
import (
"errors"
"net/http"
2017-06-18 11:50:53 +00:00
"strings"
2017-06-16 23:12:28 +00:00
"github.com/aerogo/aero"
"github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components/js"
2017-06-16 23:12:28 +00:00
)
func init() {
2017-06-21 00:02:30 +00:00
// Scripts
scripts := js.Bundle()
2017-06-21 00:02:30 +00:00
app.Get("/scripts", func(ctx *aero.Context) string {
ctx.SetResponseHeader("Content-Type", "application/javascript")
return scripts
2017-06-21 00:02:30 +00:00
})
2017-06-23 11:28:54 +00:00
app.Get("/scripts.js", func(ctx *aero.Context) string {
ctx.SetResponseHeader("Content-Type", "application/javascript")
return scripts
2017-06-23 11:28:54 +00:00
})
2017-06-18 11:50:53 +00:00
// Web manifest
app.Get("/manifest.json", func(ctx *aero.Context) string {
return ctx.JSON(app.Config.Manifest)
2017-06-16 23:12:28 +00:00
})
2017-06-18 11:50:53 +00:00
// Favicon
app.Get("/favicon.ico", func(ctx *aero.Context) string {
return ctx.TryWebP("images/brand/64", ".png")
})
// Brand icons
app.Get("/images/brand/:file", func(ctx *aero.Context) string {
file := strings.TrimSuffix(ctx.Get("file"), ".webp")
return ctx.TryWebP("images/brand/"+file, ".png")
2017-06-16 23:12:28 +00:00
})
// Cover image
app.Get("/images/cover/:file", func(ctx *aero.Context) string {
2017-06-18 11:50:53 +00:00
file := strings.TrimSuffix(ctx.Get("file"), ".webp")
return ctx.TryWebP("images/cover/"+file, ".jpg")
2017-06-16 23:12:28 +00:00
})
// Login buttons
app.Get("/images/login/:file", func(ctx *aero.Context) string {
return ctx.File("images/login/" + ctx.Get("file") + ".png")
})
// Avatars
2017-06-18 11:50:53 +00:00
app.Get("/images/avatars/large/:file", func(ctx *aero.Context) string {
file := strings.TrimSuffix(ctx.Get("file"), ".webp")
2017-06-16 23:12:28 +00:00
if ctx.CanUseWebP() {
2017-06-18 11:50:53 +00:00
return ctx.File("images/avatars/large/webp/" + file + ".webp")
2017-06-16 23:12:28 +00:00
}
original := arn.FindFileWithExtension(
2017-06-18 11:50:53 +00:00
file,
2017-06-16 23:12:28 +00:00
"images/avatars/large/original/",
arn.OriginalImageExtensions,
)
if original == "" {
2017-06-18 11:50:53 +00:00
return ctx.Error(http.StatusNotFound, "Avatar not found", errors.New("Image not found: "+file))
2017-06-16 23:12:28 +00:00
}
return ctx.File(original)
})
// Avatars
2017-06-18 11:50:53 +00:00
app.Get("/images/avatars/small/:file", func(ctx *aero.Context) string {
file := strings.TrimSuffix(ctx.Get("file"), ".webp")
2017-06-16 23:12:28 +00:00
if ctx.CanUseWebP() {
2017-06-18 11:50:53 +00:00
return ctx.File("images/avatars/small/webp/" + file + ".webp")
2017-06-16 23:12:28 +00:00
}
original := arn.FindFileWithExtension(
2017-06-18 11:50:53 +00:00
file,
2017-06-16 23:12:28 +00:00
"images/avatars/small/original/",
arn.OriginalImageExtensions,
)
if original == "" {
2017-06-18 11:50:53 +00:00
return ctx.Error(http.StatusNotFound, "Avatar not found", errors.New("Image not found: "+file))
2017-06-16 23:12:28 +00:00
}
return ctx.File(original)
})
// Elements
app.Get("/images/elements/:file", func(ctx *aero.Context) string {
return ctx.File("images/elements/" + ctx.Get("file"))
})
// For benchmarks
app.Get("/hello", func(ctx *aero.Context) string {
return ctx.Text("Hello World")
})
}