chore(settings): default root url to / (cannot be empty)

This commit is contained in:
Quentin McGaw
2023-06-12 12:17:19 +00:00
parent ac67611b06
commit 3e4d359140
4 changed files with 7 additions and 5 deletions

View File

@@ -262,7 +262,7 @@ func _main(ctx context.Context, settingsSource SettingsSource, args []string, lo
address := ":" + fmt.Sprint(*config.Server.Port)
serverLogger := logger.New(log.SetComponent("http server"))
server := server.New(ctx, address, *config.Server.RootURL, db, serverLogger, runner)
server := server.New(ctx, address, config.Server.RootURL, db, serverLogger, runner)
serverHandler, serverCtx, serverDone := goshutdown.NewGoRoutineHandler("server")
go server.Run(serverCtx, serverDone)
notify("Launched with " + strconv.Itoa(len(records)) + " records to watch")

View File

@@ -10,18 +10,18 @@ import (
type Server struct {
Port *uint16
RootURL *string
RootURL string
}
func (s *Server) setDefaults() {
const defaultPort = 8000
s.Port = gosettings.DefaultPointer(s.Port, defaultPort)
s.RootURL = gosettings.DefaultPointer(s.RootURL, "")
s.RootURL = gosettings.DefaultString(s.RootURL, "/")
}
func (s Server) mergeWith(other Server) (merged Server) {
merged.Port = gosettings.MergeWithPointer(s.Port, other.Port)
merged.RootURL = gosettings.MergeWithPointer(s.RootURL, other.RootURL)
merged.RootURL = gosettings.MergeWithString(s.RootURL, other.RootURL)
return merged
}

View File

@@ -3,7 +3,7 @@ package env
import "github.com/qdm12/ddns-updater/internal/config/settings"
func (s *Source) readServer() (settings settings.Server, err error) {
settings.RootURL = s.env.Get("ROOT_URL")
settings.RootURL = s.env.String("ROOT_URL")
settings.Port, err = s.env.Uint16Ptr("LISTENING_PORT") // TODO change to address
return settings, err
}

View File

@@ -4,6 +4,7 @@ import (
"context"
"embed"
"net/http"
"strings"
"text/template"
"time"
@@ -40,6 +41,7 @@ func newHandler(ctx context.Context, rootURL string,
router := chi.NewRouter()
router.Use(middleware.Logger)
rootURL = strings.TrimSuffix(rootURL, "/")
router.Get(rootURL+"/", handlers.index)