diff --git a/internal/chronicle/adapter/http/http.go b/internal/chronicle/adapter/http/http.go index a1ce857..018b161 100644 --- a/internal/chronicle/adapter/http/http.go +++ b/internal/chronicle/adapter/http/http.go @@ -17,12 +17,12 @@ type Parser interface { } type HttpServer struct { - app common.Service + app common.RestService } // TODO: Does it make more sense that we pass in the core interfaces here // and keep the route logic internal to this package, instead of passing it in? -func NewHttpServer(application common.Service) HttpServer { +func NewHttpServer(application common.RestService) HttpServer { return HttpServer{ app: application, } @@ -40,7 +40,7 @@ func (h HttpServer) Start() error { r.Mount("/characters", h.app.Routes()["Characters"][0]) r.Mount("/worlds", h.app.Routes()["Worlds"][0]) - return http.ListenAndServe(":3000", r) + return http.ListenAndServe(":"+h.app.Port(), r) } func DefaultDecoder(r *http.Request, v interface{}) error { diff --git a/internal/chronicle/service/application.go b/internal/chronicle/service/application.go index 4fe579f..b4ecf98 100644 --- a/internal/chronicle/service/application.go +++ b/internal/chronicle/service/application.go @@ -19,7 +19,12 @@ import ( func NewService() error { // TODO: This shold probably come from the adapter instead - psqlConnStr := os.Getenv("DATABASE_URL") + psqlConnStr := os.Getenv("CHRONICLE_DATABASE_URL") + httpPort, httpPortFound := os.LookupEnv("CHRONICLE_HTTP_PORT") + if !httpPortFound { + httpPort = "3000" + } + db, err := pgxpool.New(context.Background(), psqlConnStr) if err != nil { return fmt.Errorf("Unable to connect to database: %v\n", err) @@ -37,6 +42,9 @@ func NewService() error { service := ChronicleService{ ChronicleApplication: app, + Config: ChronicleServiceConfig{ + Port: httpPort, + }, } httpServer := http.NewHttpServer(service) @@ -46,6 +54,7 @@ func NewService() error { type ChronicleService struct { ChronicleApplication port.ChronicleApplication + Config ChronicleServiceConfig } func (c ChronicleService) Routes() map[string][]chi.Router { @@ -65,3 +74,11 @@ func (c ChronicleService) Routes() map[string][]chi.Router { "Worlds": {worldRoutes}, } } + +func (c ChronicleService) Port() string { + return c.Config.Port +} + +type ChronicleServiceConfig struct { + Port string +} diff --git a/internal/common/service.go b/internal/common/service.go index c3fd208..10c8f34 100644 --- a/internal/common/service.go +++ b/internal/common/service.go @@ -2,7 +2,8 @@ package common import "github.com/go-chi/chi/v5" -// This should represent an overall service application -type Service interface { +// Represents an overall rest http based service +type RestService interface { Routes() map[string][]chi.Router + Port() string }