Skip to content

Commit

Permalink
Implement env.URL config type
Browse files Browse the repository at this point in the history
This type was added for better error handling of the
Webserver.HostName config property.
  • Loading branch information
ARolek committed Aug 20, 2024
1 parent df48b14 commit 287545e
Show file tree
Hide file tree
Showing 13 changed files with 488 additions and 362 deletions.
10 changes: 3 additions & 7 deletions cmd/tegola/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,9 @@ var serverCmd = &cobra.Command{
serverPort = string(conf.Webserver.Port)
}

if conf.Webserver.HostName != "" {
hostname, err := url.Parse(string(conf.Webserver.HostName))
if err != nil {
log.Fatalf("unable to parse webserver.hostname: %s", err)
}

server.HostName = hostname
if conf.Webserver.HostName.Host != "" {
u := url.URL(conf.Webserver.HostName)
server.HostName = &u
}

// set our server version
Expand Down
10 changes: 3 additions & 7 deletions cmd/tegola_lambda/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,9 @@ func init() {

// set our server version
server.Version = build.Version
if conf.Webserver.HostName != "" {
hostname, err := url.Parse(string(conf.Webserver.HostName))
if err != nil {
log.Fatalf("unable to parse webserver.hostname: %s", err)
}

server.HostName = hostname
if conf.Webserver.HostName.Host != "" {
u := url.URL(conf.Webserver.HostName)
server.HostName = &u
}

// set user defined response headers
Expand Down
14 changes: 1 addition & 13 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"time"
Expand Down Expand Up @@ -72,7 +71,7 @@ type Config struct {

// Webserver represents the config options for the webserver part of Tegola
type Webserver struct {
HostName env.String `toml:"hostname"`
HostName env.URL `toml:"hostname"`
Port env.String `toml:"port"`
URIPrefix env.String `toml:"uri_prefix"`
Headers env.Dict `toml:"headers"`
Expand Down Expand Up @@ -333,17 +332,6 @@ func (c *Config) Validate() error {
}
}

// if HostName is set, validate it
if string(c.Webserver.HostName) != "" {
_, err := url.Parse(string(c.Webserver.HostName))
if err != nil {
return ErrInvalidHostName{
HostName: string(c.Webserver.HostName),
Err: err,
}
}
}

return nil
}

Expand Down
Loading

0 comments on commit 287545e

Please sign in to comment.