diff --git a/server/src/application/getServerInfo.go b/server/src/application/getServerInfo.go new file mode 100644 index 0000000..d106829 --- /dev/null +++ b/server/src/application/getServerInfo.go @@ -0,0 +1,20 @@ +package application + +import ( + "encoding/json" + "fmt" + "net/http" +) + +func (app *Application) GetServerInfo(w http.ResponseWriter, r *http.Request) { + app.mu.Lock() + defer app.mu.Unlock() + + serverInfo := ServerInfo{ + IsInitialized: len(app.users) > 1, + } + + w.Header().Add("Content-Type", "application/json") + jsonString, _ := json.Marshal(serverInfo) + fmt.Fprintf(w, "%s", string(jsonString)) +} diff --git a/server/src/application/loadGames.go b/server/src/application/loadGames.go index 9931028..1e04e58 100644 --- a/server/src/application/loadGames.go +++ b/server/src/application/loadGames.go @@ -1,6 +1,7 @@ package application import ( + "errors" "os" "path" @@ -11,7 +12,14 @@ func (app *Application) loadGames() error { dirName := path.Join(app.config.DataDir, "games") files, err := os.ReadDir(dirName) if err != nil { - return err + if !errors.Is(err, os.ErrNotExist) { + return err + } + + if err = os.Mkdir(dirName, 0777); err != nil { + return err + } + return nil } for _, file := range files { diff --git a/server/src/application/loadUsers.go b/server/src/application/loadUsers.go index ee6269d..a2d13ac 100644 --- a/server/src/application/loadUsers.go +++ b/server/src/application/loadUsers.go @@ -1,6 +1,7 @@ package application import ( + "errors" "os" "path" @@ -11,7 +12,14 @@ func (app *Application) loadUsers() error { dirName := path.Join(app.config.DataDir, "users") files, err := os.ReadDir(dirName) if err != nil { - return err + if !errors.Is(err, os.ErrNotExist) { + return err + } + + if err = os.Mkdir(dirName, 0777); err != nil { + return err + } + return nil } app.mu.Lock() diff --git a/server/src/application/struct.go b/server/src/application/struct.go index 9fd1e79..6e2dc26 100644 --- a/server/src/application/struct.go +++ b/server/src/application/struct.go @@ -17,3 +17,7 @@ type Application struct { playerATime map[string]time.Time debounceMap map[string]bool } + +type ServerInfo struct { + IsInitialized bool `json:"isInitialized"` +} diff --git a/server/src/applicationConfig/appconfig.go b/server/src/applicationConfig/appconfig.go index 25e7d3c..d18d6e4 100644 --- a/server/src/applicationConfig/appconfig.go +++ b/server/src/applicationConfig/appconfig.go @@ -7,25 +7,27 @@ import ( ) type ApplicationConfig struct { - DataDir string + DataDir string + BindString string } func NewApplicationConfig() ApplicationConfig { - - flagVerbose := flag.Bool("v", false, "log debug messages, too") - flagQuiet := flag.Bool("q", false, "be quiet; warning and error messages only") + flagVerbosePtr := flag.Bool("v", false, "log debug messages, too") + flagQuietPtr := flag.Bool("q", false, "be quiet; warning and error messages only") + bindStringPtr := flag.String("b", "localhost:32039", "interface and port to bind to") flag.Parse() log.SetLoglevel(log.LOG_INFO) - if *flagVerbose { + if *flagVerbosePtr { log.SetLoglevel(log.LOG_DEBUG) } - if *flagQuiet { + if *flagQuietPtr { log.SetLoglevel(log.LOG_WARN) } return ApplicationConfig{ - DataDir: "data/", + DataDir: "data/", + BindString: *bindStringPtr, } } diff --git a/server/src/handler/authmux.go b/server/src/handler/authmux.go index cee075f..4e5d279 100644 --- a/server/src/handler/authmux.go +++ b/server/src/handler/authmux.go @@ -2,6 +2,8 @@ package handler import ( "net/http" + "strings" + "sirlab.de/go/knowyt/application" "sirlab.de/go/knowyt/user" ) @@ -10,22 +12,33 @@ type HandlerFunc func(http.ResponseWriter, *http.Request) type PrivateHandlerFunc func(*user.User, http.ResponseWriter, *http.Request) type AuthMux struct { - mux *http.ServeMux - Port int - app *application.Application + mux *http.ServeMux + bindInterfaceAndPort string + app *application.Application } func NewAuthMux(app *application.Application) *AuthMux { + bindString := app.GetConfig().BindString + if !strings.Contains(bindString, ":") { + bindString = bindString + ":32039" + } mux := AuthMux{ - app: app, - mux: http.NewServeMux(), - Port: 32039, + app: app, + mux: http.NewServeMux(), + bindInterfaceAndPort: bindString, } http.Handle("/", mux.mux) return &mux } +func (authMux *AuthMux) GetBindString() string { + if authMux.bindInterfaceAndPort[0] == ':' { + return "*" + authMux.bindInterfaceAndPort + } + return authMux.bindInterfaceAndPort +} + func (authMux *AuthMux) ServeHTTP(w http.ResponseWriter, r *http.Request) { authMux.mux.ServeHTTP(w, r) } @@ -39,5 +52,5 @@ func (authMux *AuthMux) PublicHandle(pattern string, handler http.Handler) { } func (authMux *AuthMux) ListenAndServe() { - http.ListenAndServe(":32039", nil) + http.ListenAndServe(authMux.bindInterfaceAndPort, nil) } diff --git a/server/src/knowyt.go b/server/src/knowyt.go index 89293a6..05b5ab8 100644 --- a/server/src/knowyt.go +++ b/server/src/knowyt.go @@ -22,6 +22,7 @@ func main() { mux.PublicHandleFunc("/api/login", mux.Login) mux.PublicHandleFunc("/api/logout", mux.Logout) mux.PublicHandleFunc("/api/createGame", app.CreateGame) + mux.PublicHandleFunc("/api/serverinfo", app.GetServerInfo) mux.PrivateHandleFunc("/api/cameo", mux.Cameo) mux.PrivateHandleFunc("/api/userinfo", mux.GetUserInfo) mux.PrivateHandleFunc("/api/gameinfo", app.GetGameInfo) @@ -47,6 +48,6 @@ func main() { mux.PublicHandleFunc("/", handler.FileHandler) // start listening - log.Info("Listening at http://localhost:%d/\n", mux.Port) + log.Info("Listening at http://%s/\n", mux.GetBindString()) mux.ListenAndServe() }