user management
This commit is contained in:
parent
59a81fd73e
commit
685bf8d69c
5
client/dist/index.html
vendored
5
client/dist/index.html
vendored
@ -5,6 +5,9 @@
|
||||
<title>KnYC</title>
|
||||
</head>
|
||||
<body>
|
||||
KnYC
|
||||
<h1>KnYC</h1>
|
||||
<p>
|
||||
<a href="/private/">/private/</a>
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
|
@ -1,16 +0,0 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"sirlab.de/go/knyt/handler"
|
||||
)
|
||||
|
||||
type App struct {
|
||||
Config AppConfig
|
||||
Mux *handler.AuthMux
|
||||
}
|
||||
|
||||
func NewApp(config AppConfig) *App {
|
||||
return &App{
|
||||
Config: config,
|
||||
}
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
package app
|
||||
|
||||
type AppConfig struct {
|
||||
DataDir string
|
||||
}
|
||||
|
||||
func NewAppConfig() AppConfig {
|
||||
return AppConfig{
|
||||
DataDir: "data/",
|
||||
}
|
||||
}
|
11
server/src/application/appconfig.go
Normal file
11
server/src/application/appconfig.go
Normal file
@ -0,0 +1,11 @@
|
||||
package application
|
||||
|
||||
type ApplicationConfig struct {
|
||||
DataDir string
|
||||
}
|
||||
|
||||
func NewApplicationConfig() ApplicationConfig {
|
||||
return ApplicationConfig{
|
||||
DataDir: "data/",
|
||||
}
|
||||
}
|
26
server/src/application/application.go
Normal file
26
server/src/application/application.go
Normal file
@ -0,0 +1,26 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"sirlab.de/go/knyt/user"
|
||||
"sirlab.de/go/knyt/users"
|
||||
)
|
||||
|
||||
type Application struct {
|
||||
Config ApplicationConfig
|
||||
users users.Users
|
||||
}
|
||||
|
||||
func NewApplication(config ApplicationConfig) *Application {
|
||||
app := Application{
|
||||
Config: config,
|
||||
users: users.NewUsers(),
|
||||
}
|
||||
|
||||
app.users.AddUser(user.NewUser("120", "Settel", user.ROLE_ADMIN))
|
||||
|
||||
return &app
|
||||
}
|
||||
|
||||
func (app *Application) GetUsers() *users.Users {
|
||||
return &app.users
|
||||
}
|
@ -2,6 +2,7 @@ package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"sirlab.de/go/knyt/application"
|
||||
)
|
||||
|
||||
type HandlerFunc func(http.ResponseWriter, *http.Request)
|
||||
@ -9,10 +10,12 @@ type HandlerFunc func(http.ResponseWriter, *http.Request)
|
||||
type AuthMux struct {
|
||||
mux *http.ServeMux
|
||||
Port int
|
||||
app *application.Application
|
||||
}
|
||||
|
||||
func NewAuthMux() *AuthMux {
|
||||
func NewAuthMux(app *application.Application) *AuthMux {
|
||||
mux := AuthMux{
|
||||
app: app,
|
||||
mux: http.NewServeMux(),
|
||||
Port: 32039,
|
||||
}
|
||||
|
43
server/src/handler/private.go
Normal file
43
server/src/handler/private.go
Normal file
@ -0,0 +1,43 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (authMux *AuthMux) PrivateHandleFunc(pattern string, handlerFunc HandlerFunc) {
|
||||
authMux.mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
|
||||
if authMux.isAuthenticated(r) {
|
||||
handlerFunc(w, r)
|
||||
return
|
||||
}
|
||||
authMux.accessDenied(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func (authMux *AuthMux) PrivateHandle(pattern string, handler http.Handler) {
|
||||
authMux.PrivateHandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
|
||||
handler.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func (authMux *AuthMux) accessDenied(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
fmt.Fprintf(w, "Forbidden")
|
||||
}
|
||||
|
||||
func (authMux *AuthMux) isAuthenticated(r *http.Request) bool {
|
||||
authCookie, err := r.Cookie("knyt-auth")
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
fmt.Printf("authCookie: %s\n", authCookie.Value)
|
||||
|
||||
usr, usrErr := authMux.app.GetUsers().GetUserById(authCookie.Value)
|
||||
if usrErr != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
fmt.Printf("%v\n", usr)
|
||||
return true
|
||||
}
|
@ -3,22 +3,23 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sirlab.de/go/knyt/app"
|
||||
"sirlab.de/go/knyt/application"
|
||||
"sirlab.de/go/knyt/handler"
|
||||
)
|
||||
|
||||
func main() {
|
||||
appConfig := app.NewAppConfig()
|
||||
App := app.NewApp(appConfig)
|
||||
App.Mux = handler.NewAuthMux()
|
||||
appConfig := application.NewApplicationConfig()
|
||||
app := application.NewApplication(appConfig)
|
||||
mux := handler.NewAuthMux(app)
|
||||
|
||||
App.Mux.PublicHandleFunc("/__intern__/exit", handler.Exit)
|
||||
mux.PublicHandleFunc("/__intern__/exit", handler.Exit)
|
||||
|
||||
// default handler
|
||||
fsHandler := http.FileServer(http.Dir("../../client/dist/"))
|
||||
App.Mux.PublicHandle("/", fsHandler)
|
||||
mux.PrivateHandle("/private/", fsHandler)
|
||||
mux.PublicHandle("/", fsHandler)
|
||||
|
||||
// start listening
|
||||
fmt.Printf("http://localhost:%d/\n", App.Mux.Port)
|
||||
App.Mux.ListenAndServe()
|
||||
fmt.Printf("http://localhost:%d/\n", mux.Port)
|
||||
mux.ListenAndServe()
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package users
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sirlab.de/go/knyt/user"
|
||||
)
|
||||
|
||||
@ -14,6 +15,10 @@ func (users Users) AddUser(usr *user.User) {
|
||||
users[usr.Id] = usr
|
||||
}
|
||||
|
||||
func (users Users) GetUserById(id string) *user.User {
|
||||
return users[id]
|
||||
func (users Users) GetUserById(id string) (*user.User, error) {
|
||||
usr := users[id]
|
||||
if usr == nil {
|
||||
return nil, fmt.Errorf("unknown id")
|
||||
}
|
||||
return usr, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user