187 lines
4.1 KiB
Go
Raw Normal View History

2019-05-23 06:02:13 +00:00
package assets
2017-06-16 23:12:28 +00:00
import (
2017-07-13 15:56:14 +00:00
"io/ioutil"
2019-05-23 06:02:13 +00:00
"strings"
2017-06-16 23:12:28 +00:00
"github.com/aerogo/aero"
2019-05-22 10:09:25 +00:00
"github.com/aerogo/manifest"
2017-11-18 15:01:27 +00:00
"github.com/aerogo/sitemap"
2019-05-23 06:02:13 +00:00
"github.com/akyoto/stringutils/unsafe"
2019-06-03 09:32:43 +00:00
"github.com/animenotifier/notify.moe/arn"
2017-11-28 20:48:12 +00:00
"github.com/animenotifier/notify.moe/components/css"
"github.com/animenotifier/notify.moe/components/js"
2017-06-16 23:12:28 +00:00
)
2019-05-23 06:02:13 +00:00
var (
Manifest *manifest.Manifest
JS string
CSS string
ServiceWorker string
Organization string
2019-06-01 04:55:49 +00:00
Domain = "notify.moe"
2019-05-23 06:02:13 +00:00
)
// load loads all the necessary assets into memory.
func load() {
var err error
// Manifest
2019-08-27 09:51:58 +00:00
Manifest, err = manifest.FromFile("assets/manifest.json")
2019-05-23 06:02:13 +00:00
if err != nil {
panic("Couldn't load manifest.json")
}
2017-10-01 22:31:44 +00:00
// Service worker
2019-05-23 06:02:13 +00:00
data, err := ioutil.ReadFile("scripts/ServiceWorker/ServiceWorker.js")
2019-05-22 10:09:25 +00:00
if err != nil {
panic("Couldn't load service worker")
}
2019-05-23 06:02:13 +00:00
ServiceWorker = unsafe.BytesToString(data)
2017-11-28 20:48:12 +00:00
2019-05-23 06:02:13 +00:00
// Organization
2019-08-27 09:51:58 +00:00
data, err = ioutil.ReadFile("assets/organization.json")
2019-05-22 10:09:25 +00:00
2017-07-13 15:56:14 +00:00
if err != nil {
2019-05-23 06:02:13 +00:00
panic("Couldn't load organization.json")
2017-07-13 15:56:14 +00:00
}
2017-06-21 00:02:30 +00:00
2019-05-23 06:02:13 +00:00
Organization = unsafe.BytesToString(data)
Organization = strings.ReplaceAll(Organization, "\n", "")
Organization = strings.ReplaceAll(Organization, "\t", "")
// Bundles
JS = js.Bundle()
CSS = css.Bundle()
}
2019-06-01 04:55:49 +00:00
// Configure adds all the routes used for media
2019-05-23 06:02:13 +00:00
func Configure(app *aero.Application) {
load()
2019-06-01 04:55:49 +00:00
app.Get("/scripts", func(ctx aero.Context) error {
2019-05-23 06:02:13 +00:00
return ctx.JavaScript(JS)
2017-06-21 00:02:30 +00:00
})
2019-06-01 04:55:49 +00:00
app.Get("/styles", func(ctx aero.Context) error {
2019-05-23 06:02:13 +00:00
return ctx.CSS(CSS)
2017-06-23 11:28:54 +00:00
})
2019-06-01 04:55:49 +00:00
app.Get("/service-worker", func(ctx aero.Context) error {
2019-05-23 06:02:13 +00:00
return ctx.JavaScript(ServiceWorker)
2017-07-13 15:56:14 +00:00
})
2017-06-18 11:50:53 +00:00
// Web manifest
2019-06-01 04:55:49 +00:00
app.Get("/manifest.json", func(ctx aero.Context) error {
2019-05-23 06:02:13 +00:00
return ctx.JSON(Manifest)
2017-06-16 23:12:28 +00:00
})
2017-06-18 11:50:53 +00:00
// Favicon
2019-06-01 04:55:49 +00:00
app.Get("/favicon.ico", func(ctx aero.Context) error {
ctx.Response().SetHeader("Access-Control-Allow-Origin", "*")
2017-11-15 13:41:51 +00:00
return ctx.File("images/brand/64.png")
2017-06-18 11:50:53 +00:00
})
2017-11-11 17:08:56 +00:00
// Images
2019-06-01 04:55:49 +00:00
app.Get("/images/*file", func(ctx aero.Context) error {
ctx.Response().SetHeader("Access-Control-Allow-Origin", "*")
2018-05-31 14:20:32 +00:00
return ctx.File("images/" + ctx.Get("file"))
2017-06-16 23:12:28 +00:00
})
2017-11-13 17:07:25 +00:00
// Videos
2019-06-01 04:55:49 +00:00
app.Get("/videos/*file", func(ctx aero.Context) error {
ctx.Response().SetHeader("Access-Control-Allow-Origin", "*")
2018-05-31 14:20:32 +00:00
return ctx.File("videos/" + ctx.Get("file"))
2017-11-13 17:07:25 +00:00
})
2018-03-11 01:20:30 +00:00
// Audio
2019-06-01 04:55:49 +00:00
app.Get("/audio/*file", func(ctx aero.Context) error {
ctx.Response().SetHeader("Access-Control-Allow-Origin", "*")
2018-05-31 14:20:32 +00:00
return ctx.File("audio/" + ctx.Get("file"))
2018-03-11 01:20:30 +00:00
})
2017-11-18 15:01:27 +00:00
// Anime sitemap
2019-06-01 04:55:49 +00:00
app.Get("/sitemap/anime.txt", func(ctx aero.Context) error {
2017-11-18 15:01:27 +00:00
sitemap := sitemap.New()
2019-06-01 04:55:49 +00:00
prefix := "https://" + Domain
2017-11-18 15:01:27 +00:00
for anime := range arn.StreamAnime() {
sitemap.Add(prefix + anime.Link())
}
return ctx.Text(sitemap.Text())
})
// Character sitemap
2019-06-01 04:55:49 +00:00
app.Get("/sitemap/character.txt", func(ctx aero.Context) error {
2017-11-18 15:01:27 +00:00
sitemap := sitemap.New()
2019-06-01 04:55:49 +00:00
prefix := "https://" + Domain
2017-11-18 15:01:27 +00:00
for character := range arn.StreamCharacters() {
sitemap.Add(prefix + character.Link())
}
return ctx.Text(sitemap.Text())
})
// User sitemap
2019-06-01 04:55:49 +00:00
app.Get("/sitemap/user.txt", func(ctx aero.Context) error {
2017-11-18 15:01:27 +00:00
sitemap := sitemap.New()
2019-06-01 04:55:49 +00:00
prefix := "https://" + Domain
2017-11-18 15:01:27 +00:00
for user := range arn.StreamUsers() {
if !user.HasNick() {
continue
}
2017-11-18 15:01:27 +00:00
sitemap.Add(prefix + user.Link())
}
return ctx.Text(sitemap.Text())
})
2017-11-18 15:11:54 +00:00
// SoundTrack sitemap
2019-06-01 04:55:49 +00:00
app.Get("/sitemap/soundtrack.txt", func(ctx aero.Context) error {
2017-11-18 15:11:54 +00:00
sitemap := sitemap.New()
2019-06-01 04:55:49 +00:00
prefix := "https://" + Domain
2017-11-18 15:11:54 +00:00
for soundTrack := range arn.StreamSoundTracks() {
sitemap.Add(prefix + soundTrack.Link())
}
return ctx.Text(sitemap.Text())
})
// Thread sitemap
2019-06-01 04:55:49 +00:00
app.Get("/sitemap/thread.txt", func(ctx aero.Context) error {
2017-11-18 15:11:54 +00:00
sitemap := sitemap.New()
2019-06-01 04:55:49 +00:00
prefix := "https://" + Domain
2017-11-18 15:11:54 +00:00
for thread := range arn.StreamThreads() {
sitemap.Add(prefix + thread.Link())
}
return ctx.Text(sitemap.Text())
})
// Post sitemap
2019-06-01 04:55:49 +00:00
app.Get("/sitemap/post.txt", func(ctx aero.Context) error {
2017-11-18 15:11:54 +00:00
sitemap := sitemap.New()
2019-06-01 04:55:49 +00:00
prefix := "https://" + Domain
2017-11-18 15:11:54 +00:00
for post := range arn.StreamPosts() {
sitemap.Add(prefix + post.Link())
}
return ctx.Text(sitemap.Text())
})
2017-06-16 23:12:28 +00:00
// For benchmarks
2019-06-01 04:55:49 +00:00
app.Get("/hello", func(ctx aero.Context) error {
2017-06-16 23:12:28 +00:00
return ctx.Text("Hello World")
})
}