add admin router to gametable
This commit is contained in:
parent
464a159354
commit
882e7b14c3
4 changed files with 58 additions and 31 deletions
|
@ -1,37 +1,23 @@
|
|||
package admin
|
||||
|
||||
import (
|
||||
"json"
|
||||
"context"
|
||||
"net/http"
|
||||
"nilfm.cc/git/felt/models"
|
||||
"nilfm.cc/git/felt/mongodb"
|
||||
"nilfm.cc/git/felt/admin/util"
|
||||
"nilfm.cc/git/quartzgun/auth"
|
||||
"nilfm.cc/git/quartzgun/cookie"
|
||||
"nilfm.cc/git/quartzgun/indentalUserDB"
|
||||
. "nilfm.cc/git/quartzgun/middleware"
|
||||
"nilfm.cc/git/quartzgun/renderer"
|
||||
"nilfm.cc/git/quartzgun/router"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func getUserFromToken(req *http.Request) string {
|
||||
authHeader := req.Header.Get("Authorization")
|
||||
if strings.HasPrefix(authHeader, "Bearer ") {
|
||||
authToken := strings.Split(authHeader, "Bearer ")[1]
|
||||
data, err := base64.StdEncoding.DecodeString(token)
|
||||
if err == nil {
|
||||
parts := strings.Split(string(data), "\n")
|
||||
if len(parts) == 2 {
|
||||
return parts[0]
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func apiGetTableData(next http.Handler, udb auth.UserStore) http.Handler {
|
||||
func apiGetTableList(next http.Handler, udb auth.UserStore) http.Handler {
|
||||
handlerFunc := func(w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
// get username from
|
||||
user := util.GetUserFromToken(req)
|
||||
|
||||
rawTableData, err := udb.GetData(user, "tables")
|
||||
if err != nil {
|
||||
|
@ -39,8 +25,8 @@ func apiGetTableData(next http.Handler, udb auth.UserStore) http.Handler {
|
|||
}
|
||||
|
||||
// split rawTableData - tableName,passCode;tableName,passCode;
|
||||
tables := strings.Split(rawTableData, ";")
|
||||
self := make([]models.TableKey)
|
||||
tables := strings.Split(rawTableData.(string), ";")
|
||||
self := []models.TableKey{}
|
||||
for _, t := range tables {
|
||||
parts := strings.Split(t, ",")
|
||||
if len(parts) == 2 {
|
||||
|
@ -51,21 +37,33 @@ func apiGetTableData(next http.Handler, udb auth.UserStore) http.Handler {
|
|||
}
|
||||
}
|
||||
|
||||
*req = *req.WithContext(context.WithValue(req.Context(), "tableData", self))
|
||||
next.serveHTTP(w, req)
|
||||
*req = *req.WithContext(context.WithValue(req.Context(), "tableList", self))
|
||||
next.ServeHTTP(w, req)
|
||||
}
|
||||
|
||||
return handlerFunc
|
||||
return http.HandlerFunc(handlerFunc)
|
||||
}
|
||||
|
||||
func CreateAdminInterface(udb auth.UserStore) http.Handler {
|
||||
func apiGetTableData(next http.Handler, udb auth.UserStore, dbAdapter mongodb.DbAdapter) http.Handler {
|
||||
handlerFunc := func(w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
// tableName := req.Context().Value("Slug")
|
||||
// get table entities from mongodb and store the data object in the context
|
||||
next.ServeHTTP(w, req)
|
||||
}
|
||||
|
||||
return http.HandlerFunc(handlerFunc)
|
||||
}
|
||||
|
||||
func CreateAdminInterface(udb auth.UserStore, dbAdapter mongodb.DbAdapter) http.Handler {
|
||||
// create quartzgun router
|
||||
rtr := &router.Router{}
|
||||
|
||||
scopes := map[string]string{}
|
||||
|
||||
rtr.Post("api/auth", Provision(udb, 84))
|
||||
rtr.Get(`api/table/`, Validate(apiGetTableList(renderer.JSON("tableList"), udb), udb, scopes))
|
||||
rtr.Get(`api/table/(?P<Slug>\S+)`, Validate(apiGetTableData(renderer.JSON("tableData"), udb, dbAdapter), udb, scopes))
|
||||
|
||||
// initialize routes with admin interface
|
||||
rtr.Get(`api/table/?P<Slug>\S+)`, Validate(apiGetTableData(renderer.JSON("tableData"), udb)))
|
||||
|
||||
return router.ServeHTTP
|
||||
return http.HandlerFunc(rtr.ServeHTTP)
|
||||
}
|
||||
|
|
22
admin/util/util.go
Normal file
22
admin/util/util.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetUserFromToken(req *http.Request) string {
|
||||
authHeader := req.Header.Get("Authorization")
|
||||
if strings.HasPrefix(authHeader, "Bearer ") {
|
||||
authToken := strings.Split(authHeader, "Bearer ")[1]
|
||||
data, err := base64.StdEncoding.DecodeString(authToken)
|
||||
if err == nil {
|
||||
parts := strings.Split(string(data), "\n")
|
||||
if len(parts) == 2 {
|
||||
return parts[0]
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
|
@ -8,9 +8,11 @@ import (
|
|||
"log"
|
||||
"net/http"
|
||||
"nhooyr.io/websocket"
|
||||
"nilfm.cc/git/felt/admin"
|
||||
"nilfm.cc/git/felt/models"
|
||||
"nilfm.cc/git/felt/mongodb"
|
||||
"nilfm.cc/git/quartzgun/cookie"
|
||||
"nilfm.cc/git/quartzgun/auth"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
@ -30,7 +32,7 @@ type GameTableServer struct {
|
|||
dbAdapter mongodb.DbAdapter
|
||||
}
|
||||
|
||||
func New(adapter mongodb.DbAdapter) *GameTableServer {
|
||||
func New(adapter mongodb.DbAdapter, udb auth.UserStore) *GameTableServer {
|
||||
srvr := &GameTableServer{
|
||||
subscribeMessageBuffer: 16,
|
||||
logf: log.Printf,
|
||||
|
@ -39,6 +41,7 @@ func New(adapter mongodb.DbAdapter) *GameTableServer {
|
|||
dbAdapter: adapter,
|
||||
}
|
||||
srvr.serveMux.Handle("/table/", http.FileServer(http.Dir("./static")))
|
||||
srvr.serveMux.Handle("/admin/", admin.CreateAdminInterface(udb, adapter))
|
||||
srvr.serveMux.HandleFunc("/subscribe", srvr.subscribeHandler)
|
||||
srvr.serveMux.HandleFunc("/publish", srvr.publishHandler)
|
||||
|
||||
|
|
6
main.go
6
main.go
|
@ -7,6 +7,7 @@ import (
|
|||
"net/http"
|
||||
"nilfm.cc/git/felt/gametable"
|
||||
"nilfm.cc/git/felt/mongodb"
|
||||
"nilfm.cc/git/quartzgun/indentalUserDB"
|
||||
"os"
|
||||
"os/signal"
|
||||
"time"
|
||||
|
@ -31,7 +32,10 @@ func run() error {
|
|||
return err
|
||||
}
|
||||
|
||||
gt := gametable.New(dbEngine)
|
||||
udb := indentalUserDB.CreateIndentalUserDB(
|
||||
"./user.db")
|
||||
|
||||
gt := gametable.New(dbEngine, udb)
|
||||
s := &http.Server{
|
||||
Handler: gt,
|
||||
ReadTimeout: time.Second * 10,
|
||||
|
|
Loading…
Reference in a new issue