Cleanup
This commit is contained in:
parent
1101dafc90
commit
504993861b
15
assets.go
15
assets.go
@ -9,8 +9,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// Scripts
|
// Script bundle
|
||||||
scripts := js.Bundle()
|
scriptBundle := js.Bundle()
|
||||||
|
|
||||||
|
// Service worker
|
||||||
serviceWorkerBytes, err := ioutil.ReadFile("sw/service-worker.js")
|
serviceWorkerBytes, err := ioutil.ReadFile("sw/service-worker.js")
|
||||||
serviceWorker := string(serviceWorkerBytes)
|
serviceWorker := string(serviceWorkerBytes)
|
||||||
|
|
||||||
@ -19,18 +21,15 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
app.Get("/scripts", func(ctx *aero.Context) string {
|
app.Get("/scripts", func(ctx *aero.Context) string {
|
||||||
ctx.SetResponseHeader("Content-Type", "application/javascript")
|
return ctx.JavaScript(scriptBundle)
|
||||||
return scripts
|
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Get("/scripts.js", func(ctx *aero.Context) string {
|
app.Get("/scripts.js", func(ctx *aero.Context) string {
|
||||||
ctx.SetResponseHeader("Content-Type", "application/javascript")
|
return ctx.JavaScript(scriptBundle)
|
||||||
return scripts
|
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Get("/service-worker", func(ctx *aero.Context) string {
|
app.Get("/service-worker", func(ctx *aero.Context) string {
|
||||||
ctx.SetResponseHeader("Content-Type", "application/javascript")
|
return ctx.JavaScript(serviceWorker)
|
||||||
return serviceWorker
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// Web manifest
|
// Web manifest
|
||||||
|
8
main.go
8
main.go
@ -153,6 +153,9 @@ func configure(app *aero.Application) *aero.Application {
|
|||||||
app.Ajax("/paypal/cancel", paypal.Cancel)
|
app.Ajax("/paypal/cancel", paypal.Cancel)
|
||||||
app.Get("/api/paypal/payment/create", paypal.CreatePayment)
|
app.Get("/api/paypal/payment/create", paypal.CreatePayment)
|
||||||
|
|
||||||
|
// Rewrite
|
||||||
|
app.Rewrite(Rewrite)
|
||||||
|
|
||||||
// Middleware
|
// Middleware
|
||||||
app.Use(middleware.Firewall())
|
app.Use(middleware.Firewall())
|
||||||
app.Use(middleware.Log())
|
app.Use(middleware.Log())
|
||||||
@ -173,5 +176,10 @@ func configure(app *aero.Application) *aero.Application {
|
|||||||
// Authentication
|
// Authentication
|
||||||
auth.Install(app)
|
auth.Install(app)
|
||||||
|
|
||||||
|
// Specify test routes
|
||||||
|
for route, examples := range routeTests {
|
||||||
|
app.Test(route, examples)
|
||||||
|
}
|
||||||
|
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
57
main_test.go
57
main_test.go
@ -1,11 +1,16 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/aerogo/aero"
|
"github.com/aerogo/aero"
|
||||||
|
"github.com/aerogo/api"
|
||||||
|
"github.com/animenotifier/arn"
|
||||||
|
"github.com/fatih/color"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRoutes(t *testing.T) {
|
func TestRoutes(t *testing.T) {
|
||||||
@ -23,9 +28,55 @@ func TestRoutes(t *testing.T) {
|
|||||||
app.Handler().ServeHTTP(responseRecorder, request)
|
app.Handler().ServeHTTP(responseRecorder, request)
|
||||||
|
|
||||||
if status := responseRecorder.Code; status != http.StatusOK {
|
if status := responseRecorder.Code; status != http.StatusOK {
|
||||||
t.Errorf("%s | Wrong status code | %v instead of %v", example, status, http.StatusOK)
|
color.Red("%s | Wrong status code | %v instead of %v", example, status, http.StatusOK)
|
||||||
} else {
|
}
|
||||||
t.Logf("%s | Correct status code | %v == %v", example, status, http.StatusOK)
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInterfaceImplementations(t *testing.T) {
|
||||||
|
// API interfaces
|
||||||
|
var creatable = reflect.TypeOf((*api.Creatable)(nil)).Elem()
|
||||||
|
var updatable = reflect.TypeOf((*api.Updatable)(nil)).Elem()
|
||||||
|
var actionable = reflect.TypeOf((*api.Actionable)(nil)).Elem()
|
||||||
|
var collection = reflect.TypeOf((*api.Collection)(nil)).Elem()
|
||||||
|
|
||||||
|
// Required interface implementations
|
||||||
|
var interfaceImplementations = map[string][]reflect.Type{
|
||||||
|
"User": []reflect.Type{
|
||||||
|
updatable,
|
||||||
|
},
|
||||||
|
"Thread": []reflect.Type{
|
||||||
|
creatable,
|
||||||
|
updatable,
|
||||||
|
actionable,
|
||||||
|
},
|
||||||
|
"Post": []reflect.Type{
|
||||||
|
creatable,
|
||||||
|
updatable,
|
||||||
|
actionable,
|
||||||
|
},
|
||||||
|
"SoundTrack": []reflect.Type{
|
||||||
|
creatable,
|
||||||
|
},
|
||||||
|
"Analytics": []reflect.Type{
|
||||||
|
creatable,
|
||||||
|
},
|
||||||
|
"AnimeList": []reflect.Type{
|
||||||
|
collection,
|
||||||
|
},
|
||||||
|
"PushSubscriptions": []reflect.Type{
|
||||||
|
collection,
|
||||||
|
},
|
||||||
|
"UserFollows": []reflect.Type{
|
||||||
|
collection,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for typeName, interfaces := range interfaceImplementations {
|
||||||
|
for _, requiredInterface := range interfaces {
|
||||||
|
if !reflect.PtrTo(arn.DB.Type(typeName)).Implements(requiredInterface) {
|
||||||
|
panic(errors.New(typeName + " does not implement interface " + requiredInterface.Name()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
73
rewrite.go
73
rewrite.go
@ -6,48 +6,43 @@ import (
|
|||||||
"github.com/aerogo/aero"
|
"github.com/aerogo/aero"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
// Rewrite will rewrite certain routes
|
||||||
plusRoute := "/+"
|
func Rewrite(ctx *aero.RewriteContext) {
|
||||||
plusRouteAjax := "/_/+"
|
requestURI := ctx.URI()
|
||||||
|
|
||||||
// This will rewrite /+UserName requests to /user/UserName
|
// User profiles
|
||||||
app.Rewrite(func(ctx *aero.RewriteContext) {
|
if strings.HasPrefix(requestURI, "/+") {
|
||||||
requestURI := ctx.URI()
|
newURI := "/user/"
|
||||||
|
userName := requestURI[2:]
|
||||||
|
ctx.SetURI(newURI + userName)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// User profiles
|
if strings.HasPrefix(requestURI, "/_/+") {
|
||||||
if strings.HasPrefix(requestURI, plusRoute) {
|
newURI := "/_/user/"
|
||||||
newURI := "/user/"
|
userName := requestURI[4:]
|
||||||
userName := requestURI[2:]
|
ctx.SetURI(newURI + userName)
|
||||||
ctx.SetURI(newURI + userName)
|
return
|
||||||
return
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if strings.HasPrefix(requestURI, plusRouteAjax) {
|
// Search
|
||||||
newURI := "/_/user/"
|
if strings.HasPrefix(requestURI, "/search/") {
|
||||||
userName := requestURI[4:]
|
searchTerm := requestURI[len("/search/"):]
|
||||||
ctx.SetURI(newURI + userName)
|
ctx.Request.URL.RawQuery = "q=" + searchTerm
|
||||||
return
|
ctx.SetURI("/search")
|
||||||
}
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Search
|
if strings.HasPrefix(requestURI, "/_/search/") {
|
||||||
if strings.HasPrefix(requestURI, "/search/") {
|
searchTerm := requestURI[len("/_/search/"):]
|
||||||
searchTerm := requestURI[len("/search/"):]
|
ctx.Request.URL.RawQuery = "q=" + searchTerm
|
||||||
ctx.Request.URL.RawQuery = "q=" + searchTerm
|
ctx.SetURI("/_/search")
|
||||||
ctx.SetURI("/search")
|
return
|
||||||
return
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if strings.HasPrefix(requestURI, "/_/search/") {
|
// Analytics
|
||||||
searchTerm := requestURI[len("/_/search/"):]
|
if requestURI == "/dark-flame-master" {
|
||||||
ctx.Request.URL.RawQuery = "q=" + searchTerm
|
ctx.SetURI("/api/new/analytics")
|
||||||
ctx.SetURI("/_/search")
|
return
|
||||||
return
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Analytics
|
|
||||||
if requestURI == "/dark-flame-master" {
|
|
||||||
ctx.SetURI("/api/new/analytics")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
62
tests.go
62
tests.go
@ -1,13 +1,5 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"github.com/aerogo/api"
|
|
||||||
"github.com/animenotifier/arn"
|
|
||||||
)
|
|
||||||
|
|
||||||
var routeTests = map[string][]string{
|
var routeTests = map[string][]string{
|
||||||
// User
|
// User
|
||||||
"/user/:nick": []string{
|
"/user/:nick": []string{
|
||||||
@ -241,57 +233,3 @@ var routeTests = map[string][]string{
|
|||||||
"/settings": nil,
|
"/settings": nil,
|
||||||
"/extension/embed": nil,
|
"/extension/embed": nil,
|
||||||
}
|
}
|
||||||
|
|
||||||
// API interfaces
|
|
||||||
var creatable = reflect.TypeOf((*api.Creatable)(nil)).Elem()
|
|
||||||
var updatable = reflect.TypeOf((*api.Updatable)(nil)).Elem()
|
|
||||||
var actionable = reflect.TypeOf((*api.Actionable)(nil)).Elem()
|
|
||||||
var collection = reflect.TypeOf((*api.Collection)(nil)).Elem()
|
|
||||||
|
|
||||||
// Required interface implementations
|
|
||||||
var interfaceImplementations = map[string][]reflect.Type{
|
|
||||||
"User": []reflect.Type{
|
|
||||||
updatable,
|
|
||||||
},
|
|
||||||
"Thread": []reflect.Type{
|
|
||||||
creatable,
|
|
||||||
updatable,
|
|
||||||
actionable,
|
|
||||||
},
|
|
||||||
"Post": []reflect.Type{
|
|
||||||
creatable,
|
|
||||||
updatable,
|
|
||||||
actionable,
|
|
||||||
},
|
|
||||||
"SoundTrack": []reflect.Type{
|
|
||||||
creatable,
|
|
||||||
},
|
|
||||||
"Analytics": []reflect.Type{
|
|
||||||
creatable,
|
|
||||||
},
|
|
||||||
"AnimeList": []reflect.Type{
|
|
||||||
collection,
|
|
||||||
},
|
|
||||||
"PushSubscriptions": []reflect.Type{
|
|
||||||
collection,
|
|
||||||
},
|
|
||||||
"UserFollows": []reflect.Type{
|
|
||||||
collection,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
// Specify test routes
|
|
||||||
for route, examples := range routeTests {
|
|
||||||
app.Test(route, examples)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check interface implementations
|
|
||||||
for typeName, interfaces := range interfaceImplementations {
|
|
||||||
for _, requiredInterface := range interfaces {
|
|
||||||
if !reflect.PtrTo(arn.DB.Type(typeName)).Implements(requiredInterface) {
|
|
||||||
panic(errors.New(typeName + " does not implement interface " + requiredInterface.Name()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user