瀏覽代碼

refactor(mgmt): add configurable port (#26)

siohaza 2 年之前
父節點
當前提交
b88e1ab78f
共有 3 個文件被更改,包括 5 次插入4 次删除
  1. 1 1
      cmd/server/main.go
  2. 1 0
      config/config.go
  3. 3 3
      server/http/mgmt_api.go

+ 1 - 1
cmd/server/main.go

@@ -40,7 +40,7 @@ func main() {
 	wg.Add(6)
 
 	go func() {
-		http.StartManagementAPI(feedbagStore, sessionManager, logger)
+		http.StartManagementAPI(cfg, feedbagStore, sessionManager, logger)
 		wg.Done()
 	}()
 	go func(logger *slog.Logger) {

+ 1 - 0
config/config.go

@@ -3,6 +3,7 @@ package config
 //go:generate go run github.com/mk6i/retro-aim-server/cmd/config_generator windows settings.bat
 //go:generate go run github.com/mk6i/retro-aim-server/cmd/config_generator unix settings.env
 type Config struct {
+	ApiPort     string `envconfig:"API_PORT" required:"true" val:"8080" description:"The port that the management API service binds to."`
 	AlertPort   string `envconfig:"ALERT_PORT" required:"true" val:"5194" description:"The port that the Alert service binds to."`
 	AuthPort    string `envconfig:"AUTH_PORT" required:"true" val:"5190" description:"The port that the auth service binds to."`
 	BOSPort     string `envconfig:"BOS_PORT" required:"true" val:"5191" description:"The port that the BOS service binds to."`

+ 3 - 3
server/http/mgmt_api.go

@@ -11,6 +11,7 @@ import (
 
 	"github.com/google/uuid"
 
+	"github.com/mk6i/retro-aim-server/config"
 	"github.com/mk6i/retro-aim-server/state"
 )
 
@@ -38,7 +39,7 @@ type SessionRetriever interface {
 	AllSessions() []*state.Session
 }
 
-func StartManagementAPI(userManager UserManager, sessionRetriever SessionRetriever, logger *slog.Logger) {
+func StartManagementAPI(cfg config.Config, userManager UserManager, sessionRetriever SessionRetriever, logger *slog.Logger) {
 	mux := http.NewServeMux()
 	newUser := func() state.User {
 		return state.User{AuthKey: uuid.New().String()}
@@ -53,8 +54,7 @@ func StartManagementAPI(userManager UserManager, sessionRetriever SessionRetriev
 		sessionHandler(w, r, sessionRetriever)
 	})
 
-	//todo make port configurable
-	addr := net.JoinHostPort("", "8080")
+	addr := net.JoinHostPort("", cfg.ApiPort)
 	logger.Info("starting management API server", "addr", addr)
 	if err := http.ListenAndServe(addr, mux); err != nil {
 		logger.Error("unable to bind management API address address", "err", err.Error())