Skip to content

Commit

Permalink
Refactor to make host a field of router
Browse files Browse the repository at this point in the history
Signed-off-by: jayme-github <[email protected]>
  • Loading branch information
jayme-github committed Jul 14, 2020
1 parent 91088c5 commit f18ed1f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
3 changes: 2 additions & 1 deletion cmd/chartmuseum/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,15 @@ func cliHandler(c *cli.Context) {
WriteTimeout: conf.GetInt("writetimeout"),
ReadTimeout: conf.GetInt("readtimeout"),
EnforceSemver2: conf.GetBool("enforce-semver2"),
Host: conf.GetString("listen.host"),
}

server, err := newServer(options)
if err != nil {
crash(err)
}

server.Listen(conf.GetString("listen.host"), conf.GetInt("port"))
server.Listen(conf.GetInt("port"))
}

func backendFromConfig(conf *config.Config) storage.Backend {
Expand Down
9 changes: 6 additions & 3 deletions pkg/chartmuseum/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type (
CORSAllowOrigin string
ReadTimeout time.Duration
WriteTimeout time.Duration
Host string
}

// RouterOptions are options for constructing a Router
Expand All @@ -75,6 +76,7 @@ type (
ReadTimeout int
WriteTimeout int
CORSAllowOrigin string
Host string
}

// Route represents an application route
Expand Down Expand Up @@ -113,6 +115,7 @@ func NewRouter(options RouterOptions) *Router {
CORSAllowOrigin: options.CORSAllowOrigin,
ReadTimeout: time.Duration(options.ReadTimeout) * time.Second,
WriteTimeout: time.Duration(options.WriteTimeout) * time.Second,
Host: options.Host,
}

var err error
Expand Down Expand Up @@ -163,13 +166,13 @@ func NewRouter(options RouterOptions) *Router {
return router
}

func (router *Router) Start(host string, port int) {
func (router *Router) Start(port int) {
router.Logger.Infow("Starting ChartMuseum",
"host", host, "port", port,
"host", router.Host, "port", port,
)

server := http.Server{
Addr: fmt.Sprintf("%s:%d", host, port),
Addr: fmt.Sprintf("%s:%d", router.Host, port),
Handler: router,
ReadTimeout: router.ReadTimeout,
WriteTimeout: router.WriteTimeout,
Expand Down
3 changes: 2 additions & 1 deletion pkg/chartmuseum/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ type (
// EnforceSemver2 represents if the museum server always accept the Chart with [valid semantic version 2](https://semver.org/)
// More refers to : https://github.com/helm/chartmuseum/issues/320
EnforceSemver2 bool
Host string
}

// Server is a generic interface for web servers
Server interface {
Listen(host string, port int)
Listen(port int)
}
)

Expand Down
4 changes: 2 additions & 2 deletions pkg/chartmuseum/server/multitenant/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ func NewMultiTenantServer(options MultiTenantServerOptions) (*MultiTenantServer,
}

// Listen starts the router on a given port
func (server *MultiTenantServer) Listen(host string, port int) {
server.Router.Start(host, port)
func (server *MultiTenantServer) Listen(port int) {
server.Router.Start(port)
}

func (server *MultiTenantServer) genIndex() {
Expand Down

0 comments on commit f18ed1f

Please sign in to comment.