init game when database is empty
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
bind server to interface given by CLI option
This commit is contained in:
parent
ad71364b13
commit
a243deb79e
20
server/src/application/getServerInfo.go
Normal file
20
server/src/application/getServerInfo.go
Normal file
@ -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))
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
@ -11,9 +12,16 @@ func (app *Application) loadGames() error {
|
||||
dirName := path.Join(app.config.DataDir, "games")
|
||||
files, err := os.ReadDir(dirName)
|
||||
if err != nil {
|
||||
if !errors.Is(err, os.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = os.Mkdir(dirName, 0777); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
id := file.Name()
|
||||
gameDirName := path.Join(dirName, id)
|
||||
|
@ -1,6 +1,7 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
@ -11,9 +12,16 @@ func (app *Application) loadUsers() error {
|
||||
dirName := path.Join(app.config.DataDir, "users")
|
||||
files, err := os.ReadDir(dirName)
|
||||
if err != nil {
|
||||
if !errors.Is(err, os.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = os.Mkdir(dirName, 0777); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
app.mu.Lock()
|
||||
defer app.mu.Unlock()
|
||||
|
||||
|
@ -17,3 +17,7 @@ type Application struct {
|
||||
playerATime map[string]time.Time
|
||||
debounceMap map[string]bool
|
||||
}
|
||||
|
||||
type ServerInfo struct {
|
||||
IsInitialized bool `json:"isInitialized"`
|
||||
}
|
||||
|
@ -8,24 +8,26 @@ import (
|
||||
|
||||
type ApplicationConfig struct {
|
||||
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/",
|
||||
BindString: *bindStringPtr,
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,8 @@ package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"sirlab.de/go/knowyt/application"
|
||||
"sirlab.de/go/knowyt/user"
|
||||
)
|
||||
@ -11,21 +13,32 @@ type PrivateHandlerFunc func(*user.User, http.ResponseWriter, *http.Request)
|
||||
|
||||
type AuthMux struct {
|
||||
mux *http.ServeMux
|
||||
Port int
|
||||
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,
|
||||
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)
|
||||
}
|
||||
|
@ -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()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user