98 lines
2.1 KiB
Go
Raw Normal View History

2016-11-09 20:32:19 +09:00
package main
import (
2017-11-25 14:11:55 +01:00
"strings"
2016-11-09 22:11:49 +09:00
"github.com/aerogo/aero"
2018-12-07 16:20:10 +09:00
nanostore "github.com/aerogo/session-store-nano"
2019-06-01 13:55:49 +09:00
"github.com/akyoto/color"
2017-06-08 23:26:58 +02:00
"github.com/animenotifier/arn"
2019-05-23 15:02:13 +09:00
"github.com/animenotifier/notify.moe/assets"
2017-06-17 01:32:47 +02:00
"github.com/animenotifier/notify.moe/auth"
2019-05-11 16:03:37 +09:00
"github.com/animenotifier/notify.moe/graphql"
2017-06-17 01:12:28 +02:00
"github.com/animenotifier/notify.moe/middleware"
2017-11-07 16:49:56 +01:00
"github.com/animenotifier/notify.moe/pages"
2018-03-28 01:32:49 +02:00
"github.com/animenotifier/notify.moe/utils/routetests"
2016-11-09 20:32:19 +09:00
)
func main() {
2017-06-22 16:08:34 +02:00
// Configure and start
2019-05-10 17:36:39 +09:00
app := aero.New()
2017-06-22 16:08:34 +02:00
configure(app).Run()
}
func configure(app *aero.Application) *aero.Application {
2017-06-17 22:19:26 +02:00
// Sessions
2017-07-14 04:58:21 +02:00
app.Sessions.Duration = 3600 * 24 * 30 * 6
2017-10-31 12:12:21 +01:00
app.Sessions.Store = nanostore.New(arn.DB.Collection("Session"))
2017-06-08 23:26:58 +02:00
2018-03-02 17:18:29 +01:00
// Content security policy
app.ContentSecurityPolicy.Set("img-src", "https: data:")
2019-03-26 15:06:15 +09:00
app.ContentSecurityPolicy.Set("connect-src", "https: wss: data:")
2019-05-26 12:00:10 +09:00
app.ContentSecurityPolicy.Set("font-src", "https: data:")
2018-03-02 17:18:29 +01:00
2017-11-08 10:53:27 +01:00
// Security
configureHTTPS(app)
2017-07-14 23:50:34 +02:00
2017-10-02 02:09:05 +02:00
// Assets
2019-05-23 15:02:13 +09:00
assets.Configure(app)
2017-10-02 02:09:05 +02:00
2017-11-08 10:53:27 +01:00
// Pages
pages.Configure(app)
2017-10-02 00:31:44 +02:00
// Rewrite
2017-10-02 02:09:05 +02:00
app.Rewrite(rewrite)
2017-10-02 00:31:44 +02:00
2017-06-17 01:12:28 +02:00
// Middleware
2017-10-14 17:18:32 +02:00
app.Use(
2019-06-01 13:55:49 +09:00
middleware.OpenGraph,
2019-06-03 12:20:17 +09:00
middleware.Layout,
2019-06-01 13:55:49 +09:00
middleware.Log,
middleware.Session,
middleware.UserInfo,
2017-10-14 17:18:32 +02:00
)
2017-06-12 21:11:20 +02:00
2017-06-17 01:12:28 +02:00
// API
2017-10-05 07:22:14 +02:00
arn.API.Install(app)
2017-06-12 21:11:20 +02:00
2018-07-08 17:02:53 +09:00
// Development server configuration
2017-06-23 19:22:39 +02:00
if arn.IsDevelopment() {
2019-06-01 13:55:49 +09:00
assets.Domain = "beta.notify.moe"
assets.Manifest.Name += " - Beta"
2017-06-17 01:12:28 +02:00
}
2017-06-04 01:17:00 +02:00
2017-06-15 19:56:09 +02:00
// Authentication
2017-06-17 01:32:47 +02:00
auth.Install(app)
2017-06-15 19:56:09 +02:00
2019-05-10 17:36:39 +09:00
// GraphQL
2019-05-11 16:03:37 +09:00
graphql.Install(app)
2019-05-10 17:36:39 +09:00
2017-11-02 04:08:45 +01:00
// Close the database node on shutdown
2018-05-29 15:35:18 +09:00
app.OnEnd(arn.Node.Close)
2017-11-02 04:08:45 +01:00
2019-06-01 13:55:49 +09:00
// Show errors in the console
app.OnError(func(ctx aero.Context, err error) {
color.Red(err.Error())
})
2017-11-22 14:26:12 +01:00
// Check that this is the server
2018-03-21 20:22:57 +01:00
if !arn.Node.IsServer() && !arn.IsTest() {
2017-11-22 14:26:12 +01:00
panic("Another program is currently running as the database server")
}
2017-11-07 17:00:09 +01:00
// Prefetch all collections
2017-11-06 19:12:03 +01:00
arn.DB.Prefetch()
2017-11-02 04:08:45 +01:00
// Do not use HTTP/2 push on service worker requests
2019-06-01 13:55:49 +09:00
app.AddPushCondition(func(ctx aero.Context) bool {
return !strings.Contains(ctx.Request().Header("Referer"), "/service-worker")
})
2017-10-02 00:31:44 +02:00
// Specify test routes
2018-03-28 01:32:49 +02:00
for route, examples := range routetests.All() {
2019-06-01 13:55:49 +09:00
app.Test(route, examples...)
2017-10-02 00:31:44 +02:00
}
2017-06-22 16:08:34 +02:00
return app
2016-11-09 20:32:19 +09:00
}