Skip to content

Commit

Permalink
fix: export configuration for port
Browse files Browse the repository at this point in the history
  • Loading branch information
SomethingSexy committed Nov 19, 2024
1 parent f735402 commit ce5aaa1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
6 changes: 3 additions & 3 deletions internal/chronicle/adapter/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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 {
Expand Down
19 changes: 18 additions & 1 deletion internal/chronicle/service/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -37,6 +42,9 @@ func NewService() error {

service := ChronicleService{
ChronicleApplication: app,
Config: ChronicleServiceConfig{
Port: httpPort,
},
}

httpServer := http.NewHttpServer(service)
Expand All @@ -46,6 +54,7 @@ func NewService() error {

type ChronicleService struct {
ChronicleApplication port.ChronicleApplication
Config ChronicleServiceConfig
}

func (c ChronicleService) Routes() map[string][]chi.Router {
Expand All @@ -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
}
5 changes: 3 additions & 2 deletions internal/common/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit ce5aaa1

Please sign in to comment.