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
import (
"sirlab.de/go/knyt/applicationConfig"
"sirlab.de/go/knyt/users"
)
type Application struct {
config ApplicationConfig
config applicationConfig.ApplicationConfig
users users.Users
}
func NewApplication(config ApplicationConfig) *Application {
func NewApplication(config applicationConfig.ApplicationConfig) *Application {
app := Application{
config: config,
users: users.NewUsers(config),
}
app.users = users.NewUsers()
return &app
}
func (app *Application) GetConfig() *ApplicationConfig {
return &app.config
func (app *Application) GetConfig() applicationConfig.ApplicationConfig {
return app.config
}
func (app *Application) GetUsers() *users.Users {

View File

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

View File

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

View File

@ -2,21 +2,28 @@ package users
import (
"fmt"
"sirlab.de/go/knyt/applicationConfig"
"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 {
return make(Users)
func NewUsers(appConfig applicationConfig.ApplicationConfig) Users {
return Users{
users: make(map[string]*user.User),
appConfig: appConfig,
}
}
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) {
usr := users[id]
usr := users.users[id]
if usr == nil {
return nil, fmt.Errorf("unknown id")
}