diff --git a/middles/cache.go b/middles/cache.go index b719d87..b35dc88 100644 --- a/middles/cache.go +++ b/middles/cache.go @@ -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 diff --git a/middles/cache_test.go b/middles/cache_test.go index 7ec3d53..64e7c51 100644 --- a/middles/cache_test.go +++ b/middles/cache_test.go @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { diff --git a/runtime/environment.go b/runtime/environment.go index 209bc43..9430d74 100644 --- a/runtime/environment.go +++ b/runtime/environment.go @@ -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 } diff --git a/runtime/environment_test.go b/runtime/environment_test.go index 7998714..10fe3ea 100644 --- a/runtime/environment_test.go +++ b/runtime/environment_test.go @@ -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) }) @@ -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") }) @@ -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") })