read users from filesystem

This commit is contained in:
Settel 2021-08-01 20:32:40 +02:00
parent 07deb59b5c
commit 2d2a9456c6
8 changed files with 60 additions and 11 deletions

View File

@ -1,5 +1,5 @@
{
id: "764353",
name: "Settel",
role: "admin",
"id": "764353",
"name": "Settel",
"role": "admin"
}

View File

@ -1,5 +1,5 @@
{
id: "837293",
name: "Player #1",
role: "player",
"id": "837293",
"name": "Player #1",
"role": "player"
}

View File

@ -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 {

View File

@ -6,6 +6,6 @@ type ApplicationConfig struct {
func NewApplicationConfig() ApplicationConfig {
return ApplicationConfig{
DataDir: "data/",
DataDir: "../data/",
}
}

View File

@ -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 {

View File

@ -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)

View File

@ -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
}

View File

@ -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
}