From 2d2a9456c62bb1229805fc18165dbc7adcdec723 Mon Sep 17 00:00:00 2001 From: Settel Date: Sun, 1 Aug 2021 20:32:40 +0200 Subject: [PATCH] read users from filesystem --- server/data/users/764353.json | 6 +++--- server/data/users/837293.json | 6 +++--- server/src/application/application.go | 8 ++++++-- server/src/applicationConfig/appconfig.go | 2 +- server/src/handler/private.go | 1 - server/src/knyt.go | 7 ++++++- server/src/user/user.go | 20 ++++++++++++++++++++ server/src/users/users.go | 21 +++++++++++++++++++++ 8 files changed, 60 insertions(+), 11 deletions(-) diff --git a/server/data/users/764353.json b/server/data/users/764353.json index fe8437f..1c26bda 100644 --- a/server/data/users/764353.json +++ b/server/data/users/764353.json @@ -1,5 +1,5 @@ { - id: "764353", - name: "Settel", - role: "admin", + "id": "764353", + "name": "Settel", + "role": "admin" } diff --git a/server/data/users/837293.json b/server/data/users/837293.json index c74faed..594f529 100644 --- a/server/data/users/837293.json +++ b/server/data/users/837293.json @@ -1,5 +1,5 @@ { - id: "837293", - name: "Player #1", - role: "player", + "id": "837293", + "name": "Player #1", + "role": "player" } diff --git a/server/src/application/application.go b/server/src/application/application.go index 47333c6..17a5f46 100644 --- a/server/src/application/application.go +++ b/server/src/application/application.go @@ -10,13 +10,17 @@ type Application struct { users users.Users } -func NewApplication(config applicationConfig.ApplicationConfig) *Application { +func NewApplication(config applicationConfig.ApplicationConfig) (*Application, error) { app := Application{ config: config, users: users.NewUsers(config), } + err := app.users.LoadUsers() + if err != nil { + return nil, err + } - return &app + return &app, nil } func (app *Application) GetConfig() applicationConfig.ApplicationConfig { diff --git a/server/src/applicationConfig/appconfig.go b/server/src/applicationConfig/appconfig.go index 3de92b5..ad7c0d3 100644 --- a/server/src/applicationConfig/appconfig.go +++ b/server/src/applicationConfig/appconfig.go @@ -6,6 +6,6 @@ type ApplicationConfig struct { func NewApplicationConfig() ApplicationConfig { return ApplicationConfig{ - DataDir: "data/", + DataDir: "../data/", } } diff --git a/server/src/handler/private.go b/server/src/handler/private.go index ed38a1e..a62ea59 100644 --- a/server/src/handler/private.go +++ b/server/src/handler/private.go @@ -31,7 +31,6 @@ func (authMux *AuthMux) isAuthenticated(r *http.Request) bool { if err != nil { return false } - fmt.Printf("authCookie: %s\n", authCookie.Value) usr, usrErr := authMux.app.GetUsers().GetUserById(authCookie.Value) if usrErr != nil { diff --git a/server/src/knyt.go b/server/src/knyt.go index 9c14db0..0c534d5 100644 --- a/server/src/knyt.go +++ b/server/src/knyt.go @@ -3,6 +3,7 @@ package main import ( "fmt" "net/http" + "os" "sirlab.de/go/knyt/application" "sirlab.de/go/knyt/applicationConfig" "sirlab.de/go/knyt/handler" @@ -10,7 +11,11 @@ import ( func main() { appConfig := applicationConfig.NewApplicationConfig() - app := application.NewApplication(appConfig) + app, err := application.NewApplication(appConfig) + if err != nil { + fmt.Printf("%v\n", err) + os.Exit(1) + } mux := handler.NewAuthMux(app) mux.PublicHandleFunc("/__intern__/exit", handler.Exit) diff --git a/server/src/user/user.go b/server/src/user/user.go index 83b281b..c8708de 100644 --- a/server/src/user/user.go +++ b/server/src/user/user.go @@ -1,5 +1,11 @@ package user +import ( + "encoding/json" + "fmt" + "os" +) + func NewUser(id, name, role string) *User { return &User{ Id: id, @@ -8,6 +14,20 @@ func NewUser(id, name, role string) *User { } } +func NewUserFromFile(fileName string) (*User, error) { + jsonBytes, err := os.ReadFile(fileName) + if err != nil { + return nil, err + } + + var usr User + if err := json.Unmarshal(jsonBytes, &usr); err != nil { + return nil, fmt.Errorf("%s: %v\n", fileName, err) + } else { + return &usr, nil + } +} + func (user *User) IsPlayer() bool { return true } diff --git a/server/src/users/users.go b/server/src/users/users.go index 3f0fde8..a8b0d49 100644 --- a/server/src/users/users.go +++ b/server/src/users/users.go @@ -2,6 +2,8 @@ package users import ( "fmt" + "os" + "path" "sirlab.de/go/knyt/applicationConfig" "sirlab.de/go/knyt/user" ) @@ -18,6 +20,25 @@ func NewUsers(appConfig applicationConfig.ApplicationConfig) Users { } } +func (users Users) LoadUsers() error { + dirName := path.Join(users.appConfig.DataDir, "users") + files, err := os.ReadDir(dirName) + if err != nil { + return err + } + for _, file := range files { + fileName := path.Join(dirName, file.Name()) + + if usr, err := user.NewUserFromFile(fileName); err != nil { + return err + } else { + users.AddUser(usr) + } + } + + return nil +} + func (users Users) AddUser(usr *user.User) { users.users[usr.Id] = usr }