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
|
package admin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"json"
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
"nilfm.cc/git/felt/models"
|
"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/auth"
|
||||||
"nilfm.cc/git/quartzgun/cookie"
|
|
||||||
"nilfm.cc/git/quartzgun/indentalUserDB"
|
|
||||||
. "nilfm.cc/git/quartzgun/middleware"
|
. "nilfm.cc/git/quartzgun/middleware"
|
||||||
"nilfm.cc/git/quartzgun/renderer"
|
"nilfm.cc/git/quartzgun/renderer"
|
||||||
"nilfm.cc/git/quartzgun/router"
|
"nilfm.cc/git/quartzgun/router"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getUserFromToken(req *http.Request) string {
|
func apiGetTableList(next http.Handler, udb auth.UserStore) http.Handler {
|
||||||
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 {
|
|
||||||
handlerFunc := func(w http.ResponseWriter, req *http.Request) {
|
handlerFunc := func(w http.ResponseWriter, req *http.Request) {
|
||||||
|
|
||||||
// get username from
|
// get username from
|
||||||
|
user := util.GetUserFromToken(req)
|
||||||
|
|
||||||
rawTableData, err := udb.GetData(user, "tables")
|
rawTableData, err := udb.GetData(user, "tables")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -39,8 +25,8 @@ func apiGetTableData(next http.Handler, udb auth.UserStore) http.Handler {
|
||||||
}
|
}
|
||||||
|
|
||||||
// split rawTableData - tableName,passCode;tableName,passCode;
|
// split rawTableData - tableName,passCode;tableName,passCode;
|
||||||
tables := strings.Split(rawTableData, ";")
|
tables := strings.Split(rawTableData.(string), ";")
|
||||||
self := make([]models.TableKey)
|
self := []models.TableKey{}
|
||||||
for _, t := range tables {
|
for _, t := range tables {
|
||||||
parts := strings.Split(t, ",")
|
parts := strings.Split(t, ",")
|
||||||
if len(parts) == 2 {
|
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))
|
*req = *req.WithContext(context.WithValue(req.Context(), "tableList", self))
|
||||||
next.serveHTTP(w, req)
|
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
|
// create quartzgun router
|
||||||
rtr := &router.Router{}
|
rtr := &router.Router{}
|
||||||
|
|
||||||
|
scopes := map[string]string{}
|
||||||
|
|
||||||
rtr.Post("api/auth", Provision(udb, 84))
|
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
|
return http.HandlerFunc(rtr.ServeHTTP)
|
||||||
rtr.Get(`api/table/?P<Slug>\S+)`, Validate(apiGetTableData(renderer.JSON("tableData"), udb)))
|
|
||||||
|
|
||||||
return router.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"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"nhooyr.io/websocket"
|
"nhooyr.io/websocket"
|
||||||
|
"nilfm.cc/git/felt/admin"
|
||||||
"nilfm.cc/git/felt/models"
|
"nilfm.cc/git/felt/models"
|
||||||
"nilfm.cc/git/felt/mongodb"
|
"nilfm.cc/git/felt/mongodb"
|
||||||
"nilfm.cc/git/quartzgun/cookie"
|
"nilfm.cc/git/quartzgun/cookie"
|
||||||
|
"nilfm.cc/git/quartzgun/auth"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -30,7 +32,7 @@ type GameTableServer struct {
|
||||||
dbAdapter mongodb.DbAdapter
|
dbAdapter mongodb.DbAdapter
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(adapter mongodb.DbAdapter) *GameTableServer {
|
func New(adapter mongodb.DbAdapter, udb auth.UserStore) *GameTableServer {
|
||||||
srvr := &GameTableServer{
|
srvr := &GameTableServer{
|
||||||
subscribeMessageBuffer: 16,
|
subscribeMessageBuffer: 16,
|
||||||
logf: log.Printf,
|
logf: log.Printf,
|
||||||
|
@ -39,6 +41,7 @@ func New(adapter mongodb.DbAdapter) *GameTableServer {
|
||||||
dbAdapter: adapter,
|
dbAdapter: adapter,
|
||||||
}
|
}
|
||||||
srvr.serveMux.Handle("/table/", http.FileServer(http.Dir("./static")))
|
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("/subscribe", srvr.subscribeHandler)
|
||||||
srvr.serveMux.HandleFunc("/publish", srvr.publishHandler)
|
srvr.serveMux.HandleFunc("/publish", srvr.publishHandler)
|
||||||
|
|
||||||
|
|
6
main.go
6
main.go
|
@ -7,6 +7,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"nilfm.cc/git/felt/gametable"
|
"nilfm.cc/git/felt/gametable"
|
||||||
"nilfm.cc/git/felt/mongodb"
|
"nilfm.cc/git/felt/mongodb"
|
||||||
|
"nilfm.cc/git/quartzgun/indentalUserDB"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"time"
|
"time"
|
||||||
|
@ -31,7 +32,10 @@ func run() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
gt := gametable.New(dbEngine)
|
udb := indentalUserDB.CreateIndentalUserDB(
|
||||||
|
"./user.db")
|
||||||
|
|
||||||
|
gt := gametable.New(dbEngine, udb)
|
||||||
s := &http.Server{
|
s := &http.Server{
|
||||||
Handler: gt,
|
Handler: gt,
|
||||||
ReadTimeout: time.Second * 10,
|
ReadTimeout: time.Second * 10,
|
||||||
|
|
Loading…
Reference in a new issue