Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion middles/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type CacheHTTP struct {
func (c *CacheHTTP) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var ttl time.Duration

switch c.Env.String() {
switch c.Env.Get() {
case runtime.Local:
ttl = 5 * time.Second

Expand Down
10 changes: 5 additions & 5 deletions middles/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestCache_ServeHTTP_local(t *testing.T) {
run := new(atomic.Bool)
c := &CacheHTTP{
Env: runtime.Setup(&runtime.Config{
Environment: runtime.Local,
Environment: "local",
Domain: "example.com",
}),
H: http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
Expand All @@ -39,7 +39,7 @@ func TestCache_ServeHTTP_staging_css(t *testing.T) {
run := new(atomic.Bool)
c := &CacheHTTP{
Env: runtime.Setup(&runtime.Config{
Environment: runtime.Staging,
Environment: "staging",
Domain: "example.com",
}),
H: http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
Expand All @@ -62,7 +62,7 @@ func TestCache_ServeHTTP_staging_txt(t *testing.T) {
run := new(atomic.Bool)
c := &CacheHTTP{
Env: runtime.Setup(&runtime.Config{
Environment: runtime.Staging,
Environment: "staging",
Domain: "example.com",
}),
H: http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
Expand All @@ -85,7 +85,7 @@ func TestCache_ServeHTTP_production_css(t *testing.T) {
run := new(atomic.Bool)
c := &CacheHTTP{
Env: runtime.Setup(&runtime.Config{
Environment: runtime.Production,
Environment: "production",
Domain: "example.com",
}),
H: http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
Expand All @@ -108,7 +108,7 @@ func TestCache_ServeHTTP_production_txt(t *testing.T) {
run := new(atomic.Bool)
c := &CacheHTTP{
Env: runtime.Setup(&runtime.Config{
Environment: runtime.Production,
Environment: "production",
Domain: "example.com",
}),
H: http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
Expand Down
17 changes: 12 additions & 5 deletions runtime/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,32 @@ import (
// LocalPort is the normal port used for local development.
var LocalPort = 3000

// Platform is one of "local", "staging", or "production".
type Platform string

func Setup(c *Config) Environment {
return Environment{
domain: c.Domain,
environment: c.Environment,
environment: Platform(c.Environment),
}
}

type Environment struct {
domain string
environment string
environment Platform
}

const (
Local = "local"
Staging = "staging"
Production = "production"
Local Platform = "local"
Staging Platform = "staging"
Production Platform = "production"
)

func (e Environment) String() string {
return string(e.Get())
}

func (e Environment) Get() Platform {
return e.environment
}

Expand Down
18 changes: 9 additions & 9 deletions runtime/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ func TestEnvironment_Validate(t *testing.T) {
t.Parallel()

t.Run("local", func(t *testing.T) {
e := Setup(&Config{Environment: Local, Domain: "test.local"})
e := Setup(&Config{Environment: "local", Domain: "test.local"})
err := e.Validate()
must.NoError(t, err)
})

t.Run("staging", func(t *testing.T) {
e := Setup(&Config{Environment: Staging, Domain: "example.com"})
e := Setup(&Config{Environment: "staging", Domain: "example.com"})
err := e.Validate()
must.NoError(t, err)
})

t.Run("production", func(t *testing.T) {
e := Setup(&Config{Environment: Production, Domain: "example.com"})
e := Setup(&Config{Environment: "production", Domain: "example.com"})
err := e.Validate()
must.NoError(t, err)
})
Expand All @@ -34,7 +34,7 @@ func TestEnvironment_Validate(t *testing.T) {
})

t.Run("missing domain", func(t *testing.T) {
e := Setup(&Config{Environment: Production, Domain: ""})
e := Setup(&Config{Environment: "production", Domain: ""})
err := e.Validate()
must.ErrorContains(t, err, "domain must be set")
})
Expand All @@ -44,31 +44,31 @@ func TestEnvironment_Canonical(t *testing.T) {
t.Parallel()

t.Run("production", func(t *testing.T) {
e := Setup(&Config{Environment: Production, Domain: "example.com"})
e := Setup(&Config{Environment: "production", Domain: "example.com"})
result := e.Canonical("/about")
must.Eq(t, result, "https://example.com/about")
})

t.Run("staging", func(t *testing.T) {
e := Setup(&Config{Environment: Staging, Domain: "example.com"})
e := Setup(&Config{Environment: "staging", Domain: "example.com"})
result := e.Canonical("/about")
must.Eq(t, result, "https://stage.example.com/about")
})

t.Run("local", func(t *testing.T) {
e := Setup(&Config{Environment: Local, Domain: "example.com"})
e := Setup(&Config{Environment: "local", Domain: "example.com"})
result := e.Canonical("/about")
must.Eq(t, result, "http://localhost:3000/about")
})

t.Run("missing slash", func(t *testing.T) {
e := Setup(&Config{Environment: Local, Domain: "example.com"})
e := Setup(&Config{Environment: "local", Domain: "example.com"})
result := e.Canonical("login")
must.Eq(t, result, "http://localhost:3000/login")
})

t.Run("empty", func(t *testing.T) {
e := Setup(&Config{Environment: Production, Domain: "example.com"})
e := Setup(&Config{Environment: "production", Domain: "example.com"})
result := e.Canonical("")
must.Eq(t, result, "https://example.com")
})
Expand Down