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
|
package application
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
@ -11,7 +12,14 @@ func (app *Application) loadGames() error {
|
|||||||
dirName := path.Join(app.config.DataDir, "games")
|
dirName := path.Join(app.config.DataDir, "games")
|
||||||
files, err := os.ReadDir(dirName)
|
files, err := os.ReadDir(dirName)
|
||||||
if err != nil {
|
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 {
|
for _, file := range files {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package application
|
package application
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
@ -11,7 +12,14 @@ func (app *Application) loadUsers() error {
|
|||||||
dirName := path.Join(app.config.DataDir, "users")
|
dirName := path.Join(app.config.DataDir, "users")
|
||||||
files, err := os.ReadDir(dirName)
|
files, err := os.ReadDir(dirName)
|
||||||
if err != nil {
|
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()
|
app.mu.Lock()
|
||||||
|
@ -17,3 +17,7 @@ type Application struct {
|
|||||||
playerATime map[string]time.Time
|
playerATime map[string]time.Time
|
||||||
debounceMap map[string]bool
|
debounceMap map[string]bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ServerInfo struct {
|
||||||
|
IsInitialized bool `json:"isInitialized"`
|
||||||
|
}
|
||||||
|
@ -7,25 +7,27 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ApplicationConfig struct {
|
type ApplicationConfig struct {
|
||||||
DataDir string
|
DataDir string
|
||||||
|
BindString string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewApplicationConfig() ApplicationConfig {
|
func NewApplicationConfig() ApplicationConfig {
|
||||||
|
flagVerbosePtr := flag.Bool("v", false, "log debug messages, too")
|
||||||
flagVerbose := flag.Bool("v", false, "log debug messages, too")
|
flagQuietPtr := flag.Bool("q", false, "be quiet; warning and error messages only")
|
||||||
flagQuiet := 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()
|
flag.Parse()
|
||||||
|
|
||||||
log.SetLoglevel(log.LOG_INFO)
|
log.SetLoglevel(log.LOG_INFO)
|
||||||
|
|
||||||
if *flagVerbose {
|
if *flagVerbosePtr {
|
||||||
log.SetLoglevel(log.LOG_DEBUG)
|
log.SetLoglevel(log.LOG_DEBUG)
|
||||||
}
|
}
|
||||||
if *flagQuiet {
|
if *flagQuietPtr {
|
||||||
log.SetLoglevel(log.LOG_WARN)
|
log.SetLoglevel(log.LOG_WARN)
|
||||||
}
|
}
|
||||||
|
|
||||||
return ApplicationConfig{
|
return ApplicationConfig{
|
||||||
DataDir: "data/",
|
DataDir: "data/",
|
||||||
|
BindString: *bindStringPtr,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,8 @@ package handler
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"sirlab.de/go/knowyt/application"
|
"sirlab.de/go/knowyt/application"
|
||||||
"sirlab.de/go/knowyt/user"
|
"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 PrivateHandlerFunc func(*user.User, http.ResponseWriter, *http.Request)
|
||||||
|
|
||||||
type AuthMux struct {
|
type AuthMux struct {
|
||||||
mux *http.ServeMux
|
mux *http.ServeMux
|
||||||
Port int
|
bindInterfaceAndPort string
|
||||||
app *application.Application
|
app *application.Application
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAuthMux(app *application.Application) *AuthMux {
|
func NewAuthMux(app *application.Application) *AuthMux {
|
||||||
|
bindString := app.GetConfig().BindString
|
||||||
|
if !strings.Contains(bindString, ":") {
|
||||||
|
bindString = bindString + ":32039"
|
||||||
|
}
|
||||||
mux := AuthMux{
|
mux := AuthMux{
|
||||||
app: app,
|
app: app,
|
||||||
mux: http.NewServeMux(),
|
mux: http.NewServeMux(),
|
||||||
Port: 32039,
|
bindInterfaceAndPort: bindString,
|
||||||
}
|
}
|
||||||
http.Handle("/", mux.mux)
|
http.Handle("/", mux.mux)
|
||||||
|
|
||||||
return &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) {
|
func (authMux *AuthMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
authMux.mux.ServeHTTP(w, r)
|
authMux.mux.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
@ -39,5 +52,5 @@ func (authMux *AuthMux) PublicHandle(pattern string, handler http.Handler) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (authMux *AuthMux) ListenAndServe() {
|
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/login", mux.Login)
|
||||||
mux.PublicHandleFunc("/api/logout", mux.Logout)
|
mux.PublicHandleFunc("/api/logout", mux.Logout)
|
||||||
mux.PublicHandleFunc("/api/createGame", app.CreateGame)
|
mux.PublicHandleFunc("/api/createGame", app.CreateGame)
|
||||||
|
mux.PublicHandleFunc("/api/serverinfo", app.GetServerInfo)
|
||||||
mux.PrivateHandleFunc("/api/cameo", mux.Cameo)
|
mux.PrivateHandleFunc("/api/cameo", mux.Cameo)
|
||||||
mux.PrivateHandleFunc("/api/userinfo", mux.GetUserInfo)
|
mux.PrivateHandleFunc("/api/userinfo", mux.GetUserInfo)
|
||||||
mux.PrivateHandleFunc("/api/gameinfo", app.GetGameInfo)
|
mux.PrivateHandleFunc("/api/gameinfo", app.GetGameInfo)
|
||||||
@ -47,6 +48,6 @@ func main() {
|
|||||||
mux.PublicHandleFunc("/", handler.FileHandler)
|
mux.PublicHandleFunc("/", handler.FileHandler)
|
||||||
|
|
||||||
// start listening
|
// start listening
|
||||||
log.Info("Listening at http://localhost:%d/\n", mux.Port)
|
log.Info("Listening at http://%s/\n", mux.GetBindString())
|
||||||
mux.ListenAndServe()
|
mux.ListenAndServe()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user