refactoring

This commit is contained in:
Settel 2021-08-01 18:36:02 +02:00
parent 8231bda18b
commit 59a81fd73e
3 changed files with 16 additions and 23 deletions

View File

@ -1,14 +1,12 @@
package app package app
import ( import (
"sirlab.de/go/knyt/engine"
"sirlab.de/go/knyt/handler" "sirlab.de/go/knyt/handler"
) )
type App struct { type App struct {
Config AppConfig Config AppConfig
Mux *handler.AuthMux Mux *handler.AuthMux
Eng *engine.Engine
} }
func NewApp(config AppConfig) *App { func NewApp(config AppConfig) *App {

View File

@ -7,13 +7,18 @@ import (
type HandlerFunc func(http.ResponseWriter, *http.Request) type HandlerFunc func(http.ResponseWriter, *http.Request)
type AuthMux struct { type AuthMux struct {
mux *http.ServeMux mux *http.ServeMux
Port int
} }
func NewAuthMux() *AuthMux { func NewAuthMux() *AuthMux {
return &(AuthMux{ mux := AuthMux{
mux: http.NewServeMux(), mux: http.NewServeMux(),
}) Port: 32039,
}
http.Handle("/", mux.mux)
return &mux
} }
func (authMux *AuthMux) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (authMux *AuthMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -27,3 +32,7 @@ func (authMux *AuthMux) PublicHandleFunc(pattern string, handlerFunc HandlerFunc
func (authMux *AuthMux) PublicHandle(pattern string, handler http.Handler) { func (authMux *AuthMux) PublicHandle(pattern string, handler http.Handler) {
authMux.mux.Handle(pattern, handler) authMux.mux.Handle(pattern, handler)
} }
func (authMux *AuthMux) ListenAndServe() {
http.ListenAndServe(":32039", nil)
}

View File

@ -4,35 +4,21 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"sirlab.de/go/knyt/app" "sirlab.de/go/knyt/app"
"sirlab.de/go/knyt/engine"
"sirlab.de/go/knyt/handler" "sirlab.de/go/knyt/handler"
"sirlab.de/go/knyt/user"
"sirlab.de/go/knyt/users"
) )
func main() { func main() {
appConfig := app.NewAppConfig() appConfig := app.NewAppConfig()
App := app.NewApp(appConfig) App := app.NewApp(appConfig)
App.Mux = handler.NewAuthMux() App.Mux = handler.NewAuthMux()
App.Eng = engine.NewEngine()
http.Handle("/", App.Mux)
usrs := users.NewUsers()
u1 := user.NewUser("123", "Volkmar", user.ROLE_PLAYER)
u2 := user.NewUser("123", "Annabell", user.ROLE_GAMEMASTER)
usrs.AddUser(u1)
usrs.AddUser(u2)
App.Mux.PublicHandleFunc("/__intern__/exit", handler.Exit) App.Mux.PublicHandleFunc("/__intern__/exit", handler.Exit)
// default handler // default handler
fsHandler := http.FileServer(http.Dir("../client/dist/")) fsHandler := http.FileServer(http.Dir("../../client/dist/"))
App.Mux.PublicHandle("/", fsHandler) App.Mux.PublicHandle("/", fsHandler)
App.Mux.PublicHandleFunc("/api/sync", App.Eng.GetHttpHandler())
go App.Eng.Run()
// start listening // start listening
fmt.Println("http://localhost:32039") fmt.Printf("http://localhost:%d/\n", App.Mux.Port)
http.ListenAndServe(":32039", nil) App.Mux.ListenAndServe()
} }