This commit is contained in:
Eduard Urbach 2017-06-17 01:12:28 +02:00
parent 4e02a8f4bc
commit a9f88db566
9 changed files with 154 additions and 154 deletions

11
api.go
View File

@ -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)
}

103
assets.go Normal file
View File

@ -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")
})
}

View File

@ -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
}

View File

@ -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())
}
})
}

112
main.go
View File

@ -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,98 +52,19 @@ 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)
// Domain
if utils.IsDevelopment() {
app.Config.Domain = "beta.notify.moe"
}
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")
})
// Authentication
EnableLogin(app)

View File

@ -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())
}
})
}
}

View File

@ -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())
}
}
}

17
utils/production.go Normal file
View File

@ -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()
}