From a9f88db566a26f458b476cf5074019b8a02db6c6 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sat, 17 Jun 2017 01:12:28 +0200 Subject: [PATCH] Cleanup --- auth.go => api-keys.go | 0 api.go | 11 --- assets.go | 103 ++++++++++++++++++++++ helper.go | 27 ------ login.go | 11 --- main.go | 114 +++---------------------- logger.go => middleware/request-log.go | 9 +- middleware/save-session.go | 16 ++++ utils/production.go | 17 ++++ 9 files changed, 154 insertions(+), 154 deletions(-) rename auth.go => api-keys.go (100%) delete mode 100644 api.go create mode 100644 assets.go delete mode 100644 helper.go rename logger.go => middleware/request-log.go (87%) create mode 100644 middleware/save-session.go create mode 100644 utils/production.go diff --git a/auth.go b/api-keys.go similarity index 100% rename from auth.go rename to api-keys.go diff --git a/api.go b/api.go deleted file mode 100644 index a5861872..00000000 --- a/api.go +++ /dev/null @@ -1,11 +0,0 @@ -package main - -import ( - "github.com/aerogo/api" - "github.com/animenotifier/arn" -) - -func init() { - api := api.New("/api/", arn.DB) - api.Install(app) -} diff --git a/assets.go b/assets.go new file mode 100644 index 00000000..d2f7e263 --- /dev/null +++ b/assets.go @@ -0,0 +1,103 @@ +package main + +import ( + "errors" + "net/http" + + "github.com/aerogo/aero" + "github.com/animenotifier/arn" +) + +func init() { + // Favicon + app.Get("/favicon.ico", func(ctx *aero.Context) string { + return ctx.Image("images/icons/favicon", ".png") + }) + + // Scripts + app.Get("/scripts.js", func(ctx *aero.Context) string { + return ctx.File("temp/scripts.js") + }) + + // Web manifest + app.Get("/manifest.json", func(ctx *aero.Context) string { + return ctx.JSON(app.Config.Manifest) + }) + + // SVG icons + app.Get("/icons/:file", func(ctx *aero.Context) string { + return ctx.File("images/icons/svg/" + ctx.Get("file") + ".svg") + }) + + // Cover image + app.Get("/images/cover/:file", func(ctx *aero.Context) string { + return ctx.Image("images/cover/"+ctx.Get("file"), ".jpg") + }) + + // Login buttons + app.Get("/images/login/:file", func(ctx *aero.Context) string { + return ctx.File("images/login/" + ctx.Get("file") + ".png") + }) + + // 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() { + return ctx.File("images/avatars/large/webp/" + user.ID + ".webp") + } + + 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) + }) + + // 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() { + return ctx.File("images/avatars/small/webp/" + user.ID + ".webp") + } + + 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) + }) + + // 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") + }) +} diff --git a/helper.go b/helper.go deleted file mode 100644 index 4a0b4fc6..00000000 --- a/helper.go +++ /dev/null @@ -1,27 +0,0 @@ -package main - -import "fmt" - -// Converts anything into a string -func toString(v interface{}) string { - return fmt.Sprint(v) -} - -// Contains ... -func Contains(list []string, a string) bool { - for _, b := range list { - if b == a { - return true - } - } - return false -} - -// Map ... -func Map(original []string, f func(string) string) []string { - mapped := make([]string, len(original)) - for index, value := range original { - mapped[index] = f(value) - } - return mapped -} diff --git a/login.go b/login.go index 328d50ca..fdbadb18 100644 --- a/login.go +++ b/login.go @@ -12,15 +12,4 @@ func EnableLogin(app *aero.Application) { ctx.Session().Set("userId", nil) return ctx.Redirect("/") }) - - // Session middleware - app.Use(func(ctx *aero.Context, next func()) { - // Handle the request first - next() - - // Update session if it has been modified - if ctx.HasSession() && ctx.Session().Modified() { - app.Sessions.Store.Set(ctx.Session().ID(), ctx.Session()) - } - }) } diff --git a/main.go b/main.go index 7af100f9..61be8ff7 100644 --- a/main.go +++ b/main.go @@ -1,14 +1,12 @@ package main import ( - "errors" - "net/http" - "os" - "github.com/aerogo/aero" + "github.com/aerogo/api" "github.com/animenotifier/arn" "github.com/animenotifier/notify.moe/components" "github.com/animenotifier/notify.moe/layout" + "github.com/animenotifier/notify.moe/middleware" "github.com/animenotifier/notify.moe/pages/airing" "github.com/animenotifier/notify.moe/pages/anime" "github.com/animenotifier/notify.moe/pages/awards" @@ -20,6 +18,7 @@ import ( "github.com/animenotifier/notify.moe/pages/search" "github.com/animenotifier/notify.moe/pages/threads" "github.com/animenotifier/notify.moe/pages/users" + "github.com/animenotifier/notify.moe/utils" ) var app = aero.New() @@ -37,14 +36,6 @@ func main() { // Layout app.Layout = layout.Render - // Production or Development mode - host, _ := os.Hostname() - isProduction := host != "home" - - if !isProduction { - app.Config.Domain = "beta.notify.moe" - } - // Ajax routes app.Ajax("/", dashboard.Get) app.Ajax("/anime", search.Get) @@ -61,97 +52,18 @@ func main() { // app.Ajax("/genres", genres.Get) // app.Ajax("/genres/:name", genre.Get) - // Favicon - app.Get("/favicon.ico", func(ctx *aero.Context) string { - return ctx.Image("images/icons/favicon", ".png") - }) + // Middleware + app.Use(middleware.RequestLog()) + app.Use(middleware.SaveSession()) - // Scripts - app.Get("/scripts.js", func(ctx *aero.Context) string { - return ctx.File("temp/scripts.js") - }) + // API + api := api.New("/api/", arn.DB) + api.Install(app) - // Web manifest - app.Get("/manifest.json", func(ctx *aero.Context) string { - return ctx.JSON(app.Config.Manifest) - }) - - // SVG icons - app.Get("/icons/:file", func(ctx *aero.Context) string { - return ctx.File("images/icons/svg/" + ctx.Get("file") + ".svg") - }) - - // Cover image - app.Get("/images/cover/:file", func(ctx *aero.Context) string { - return ctx.Image("images/cover/"+ctx.Get("file"), ".jpg") - }) - - // Login buttons - app.Get("/images/login/:file", func(ctx *aero.Context) string { - return ctx.File("images/login/" + ctx.Get("file") + ".png") - }) - - // 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() { - return ctx.File("images/avatars/large/webp/" + user.ID + ".webp") - } - - 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) - }) - - // 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() { - return ctx.File("images/avatars/small/webp/" + user.ID + ".webp") - } - - 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) - }) - - // 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") - }) + // Domain + if utils.IsDevelopment() { + app.Config.Domain = "beta.notify.moe" + } // Authentication EnableLogin(app) diff --git a/logger.go b/middleware/request-log.go similarity index 87% rename from logger.go rename to middleware/request-log.go index c8bd9911..c0072593 100644 --- a/logger.go +++ b/middleware/request-log.go @@ -1,4 +1,4 @@ -package main +package middleware import ( "net/http" @@ -11,7 +11,8 @@ import ( "github.com/aerogo/log" ) -func init() { +// RequestLog logs every request into logs/request.log. +func RequestLog() aero.Middleware { err := log.NewLog() err.AddOutput(log.File("logs/error.log")) err.AddOutput(os.Stderr) @@ -19,7 +20,7 @@ func init() { request := log.NewLog() request.AddOutput(log.File("logs/request.log")) - app.Use(func(ctx *aero.Context, next func()) { + return func(ctx *aero.Context, next func()) { start := time.Now() next() responseTime := time.Since(start) @@ -42,5 +43,5 @@ func init() { if responseTime >= 200*time.Millisecond { err.Error("Long response time", ctx.RealIP(), ctx.StatusCode, responseTimeString, ctx.URI()) } - }) + } } diff --git a/middleware/save-session.go b/middleware/save-session.go new file mode 100644 index 00000000..57f51705 --- /dev/null +++ b/middleware/save-session.go @@ -0,0 +1,16 @@ +package middleware + +import "github.com/aerogo/aero" + +// SaveSession saves an existing session if it has been modified. +func SaveSession() aero.Middleware { + return func(ctx *aero.Context, next func()) { + // Handle the request first + next() + + // Update session if it has been modified + if ctx.HasSession() && ctx.Session().Modified() { + ctx.App.Sessions.Store.Set(ctx.Session().ID(), ctx.Session()) + } + } +} diff --git a/utils/production.go b/utils/production.go new file mode 100644 index 00000000..334a0991 --- /dev/null +++ b/utils/production.go @@ -0,0 +1,17 @@ +package utils + +import ( + "os" + "strings" +) + +// IsProduction returns true if the hostname contains "arn". +func IsProduction() bool { + host, _ := os.Hostname() + return strings.Contains(host, "arn") +} + +// IsDevelopment returns true if the hostname does not contain "arn". +func IsDevelopment() bool { + return !IsProduction() +}