feat: allow port to be set in config

This commit is contained in:
2024-12-02 17:15:39 +00:00
parent 3efed700c1
commit da5c01ccce
2 changed files with 8 additions and 1 deletions

View File

@@ -7,12 +7,15 @@ import (
) )
type Config struct { type Config struct {
Port int `json:"port"`
DatabasePath string `json:"databasePath"` DatabasePath string `json:"databasePath"`
TemplateDirectoryPath string `json:"templateDirectoryPath"` TemplateDirectoryPath string `json:"templateDirectoryPath"`
HostKeyDirectoryPath string `json:"hostKeyDirectoryPath"` HostKeyDirectoryPath string `json:"hostKeyDirectoryPath"`
HostName string `json:"hostName"` HostName string `json:"hostName"`
} }
const defaultPort = 8080
func ReadConfigFrom(reader io.Reader) (Config, error) { func ReadConfigFrom(reader io.Reader) (Config, error) {
var config Config var config Config
err := json.NewDecoder(reader).Decode(&config) err := json.NewDecoder(reader).Decode(&config)
@@ -35,5 +38,9 @@ func ReadConfigFrom(reader io.Reader) (Config, error) {
return Config{}, err return Config{}, err
} }
if config.Port == 0 {
config.Port = defaultPort
}
return config, nil return config, nil
} }

View File

@@ -103,5 +103,5 @@ func main() {
_ = c.NoContent(http.StatusInternalServerError) _ = c.NoContent(http.StatusInternalServerError)
} }
apiServer.Logger.Fatal(apiServer.Start(":8080")) apiServer.Logger.Fatal(apiServer.Start(fmt.Sprintf(":%d", config.Port)))
} }