0
0
mirror of https://github.com/rls-moe/nyx synced 2025-08-19 06:18:38 +00:00

MVP, no mod tools or anything but it works

This commit is contained in:
Tim Schuster
2017-03-12 20:37:53 +01:00
parent 70b12c516a
commit 69b0d20825
186 changed files with 44200 additions and 0 deletions

21
http/middle/base.go Normal file
View File

@@ -0,0 +1,21 @@
package middle
import (
"github.com/justinas/nosurf"
"github.com/pressly/chi/middleware"
"net/http"
)
func GetBaseCtx(r *http.Request) map[string]interface{} {
val := map[string]interface{}{
"Config": GetConfig(r),
"ReqID": middleware.GetReqID(r.Context()),
"CSRFToken": nosurf.Token(r),
}
return val
}
func CSRFProtect(next http.Handler) http.Handler {
return nosurf.New(next)
}

24
http/middle/config.go Normal file
View File

@@ -0,0 +1,24 @@
package middle
import (
"context"
"go.rls.moe/nyx/config"
"net/http"
)
func ConfigCtx(config *config.Config) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r = r.WithContext(context.WithValue(r.Context(), configKey, config))
next.ServeHTTP(w, r)
})
}
}
func GetConfig(r *http.Request) *config.Config {
val := r.Context().Value(configKey)
if val == nil {
panic("Config Middleware not configured")
}
return val.(*config.Config)
}

9
http/middle/ctx.go Normal file
View File

@@ -0,0 +1,9 @@
package middle
type ctxKey int64
const (
configKey ctxKey = iota
dbCtxKey
sessionKey
)

34
http/middle/db.go Normal file
View File

@@ -0,0 +1,34 @@
package middle
import (
"context"
"github.com/tidwall/buntdb"
"go.rls.moe/nyx/config"
"go.rls.moe/nyx/resources"
"net/http"
)
func Database(c *config.Config) (func(http.Handler) http.Handler, error) {
db, err := buntdb.Open(c.DB.File)
if err != nil {
return nil, err
}
if err = resources.InitialSetup(db); err != nil {
return nil, err
}
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r = r.WithContext(context.WithValue(r.Context(),
dbCtxKey, db))
next.ServeHTTP(w, r)
})
}, nil
}
func GetDB(r *http.Request) *buntdb.DB {
val := r.Context().Value(dbCtxKey)
if val == nil {
panic("DB Middleware not configured")
}
return val.(*buntdb.DB)
}

15
http/middle/session.go Normal file
View File

@@ -0,0 +1,15 @@
package middle
import (
"github.com/icza/session"
"net/http"
)
func init() {
session.Global.Close()
session.Global = session.NewCookieManager(session.NewInMemStore())
}
func GetSession(r *http.Request) session.Session {
return session.Get(r)
}