refactoring

This commit is contained in:
Settel 2021-08-01 20:05:00 +02:00
parent a8093dc0ad
commit 07deb59b5c
4 changed files with 21 additions and 13 deletions

View File

@ -1,26 +1,26 @@
package application package application
import ( import (
"sirlab.de/go/knyt/applicationConfig"
"sirlab.de/go/knyt/users" "sirlab.de/go/knyt/users"
) )
type Application struct { type Application struct {
config ApplicationConfig config applicationConfig.ApplicationConfig
users users.Users users users.Users
} }
func NewApplication(config ApplicationConfig) *Application { func NewApplication(config applicationConfig.ApplicationConfig) *Application {
app := Application{ app := Application{
config: config, config: config,
users: users.NewUsers(config),
} }
app.users = users.NewUsers()
return &app return &app
} }
func (app *Application) GetConfig() *ApplicationConfig { func (app *Application) GetConfig() applicationConfig.ApplicationConfig {
return &app.config return app.config
} }
func (app *Application) GetUsers() *users.Users { func (app *Application) GetUsers() *users.Users {

View File

@ -1,4 +1,4 @@
package application package applicationConfig
type ApplicationConfig struct { type ApplicationConfig struct {
DataDir string DataDir string

View File

@ -4,11 +4,12 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"sirlab.de/go/knyt/application" "sirlab.de/go/knyt/application"
"sirlab.de/go/knyt/applicationConfig"
"sirlab.de/go/knyt/handler" "sirlab.de/go/knyt/handler"
) )
func main() { func main() {
appConfig := application.NewApplicationConfig() appConfig := applicationConfig.NewApplicationConfig()
app := application.NewApplication(appConfig) app := application.NewApplication(appConfig)
mux := handler.NewAuthMux(app) mux := handler.NewAuthMux(app)

View File

@ -2,21 +2,28 @@ package users
import ( import (
"fmt" "fmt"
"sirlab.de/go/knyt/applicationConfig"
"sirlab.de/go/knyt/user" "sirlab.de/go/knyt/user"
) )
type Users map[string]*user.User type Users struct {
appConfig applicationConfig.ApplicationConfig
users map[string]*user.User
}
func NewUsers() Users { func NewUsers(appConfig applicationConfig.ApplicationConfig) Users {
return make(Users) return Users{
users: make(map[string]*user.User),
appConfig: appConfig,
}
} }
func (users Users) AddUser(usr *user.User) { func (users Users) AddUser(usr *user.User) {
users[usr.Id] = usr users.users[usr.Id] = usr
} }
func (users Users) GetUserById(id string) (*user.User, error) { func (users Users) GetUserById(id string) (*user.User, error) {
usr := users[id] usr := users.users[id]
if usr == nil { if usr == nil {
return nil, fmt.Errorf("unknown id") return nil, fmt.Errorf("unknown id")
} }