Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

major refactor

goenning85127579a351
diff --git a/.gitignore b/.gitignore
index afc505ce5..b845f29a1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,4 +3,5 @@ data/
tmp/
wchy
-gin-bin
\ No newline at end of file
+gin-bin
+gin-bin.exe
\ No newline at end of file
diff --git a/.vscode/settings.json b/.vscode/settings.json
index d6fec8b5e..9e5d39d0c 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -9,6 +9,7 @@
"vendor/g*": true,
"vendor/vendor.json": false,
"gin-bin": true,
+ "gin-bin.exe": true,
"wchy": true,
"tmp": true
}
diff --git a/env/env.go b/env/env.go
new file mode 100644
index 000000000..17b910570
--- /dev/null
+++ b/env/env.go
@@ -0,0 +1,62 @@
+package env
+
+import "os"
+
+// GetEnvOrDefault retrieves the value of the environment variable named by the key.
+// It returns the value if available, otherwise returns defaultValue
+func GetEnvOrDefault(name string, defaultValue string) string {
+ value := os.Getenv(name)
+ if value == "" {
+ return defaultValue
+ }
+ return value
+}
+
+// GetCurrentDomain returns WCHY domain based on current environment variables
+func GetCurrentDomain() string {
+ env := os.Getenv("GO_ENV")
+ switch env {
+ case "test":
+ return "test.canhearyou.com"
+ case "development":
+ return "dev.canhearyou.com"
+ case "staging":
+ return "staging.canhearyou.com"
+ }
+ return "canhearyou.com"
+
+}
+
+// Current returns current WCHY environment
+func Current() string {
+ env := os.Getenv("GO_ENV")
+ switch env {
+ case "test":
+ return "test"
+ case "staging":
+ return "staging"
+ case "production":
+ return "production"
+ }
+ return "development"
+}
+
+// IsProduction returns true on WCHY production environment
+func IsProduction() bool {
+ return Current() == "production"
+}
+
+// IsStaging returns true on WCHY production environment
+func IsStaging() bool {
+ return Current() == "staging"
+}
+
+// IsTest returns true on WCHY production environment
+func IsTest() bool {
+ return Current() == "test"
+}
+
+// IsDevelopment returns true on WCHY production environment
+func IsDevelopment() bool {
+ return Current() == "development"
+}
diff --git a/env/env_test.go b/env/env_test.go
new file mode 100644
index 000000000..42361a48f
--- /dev/null
+++ b/env/env_test.go
@@ -0,0 +1,48 @@
+package env_test
+
+import (
+ "testing"
+
+ "os"
+
+ "github.com/WeCanHearYou/wchy/env"
+ "github.com/stretchr/testify/assert"
+)
+
+var envs = []struct {
+ go_env string
+ domain string
+ env string
+}{
+ {"test", "test.canhearyou.com", "test"},
+ {"staging", "staging.canhearyou.com", "staging"},
+ {"development", "dev.canhearyou.com", "development"},
+ {"production", "canhearyou.com", "production"},
+ {"anything", "canhearyou.com", "development"},
+}
+
+func TestGetEnvOrDefault_ShouldReturnNullWhenKeyIsUnknown(t *testing.T) {
+ value := env.GetEnvOrDefault("UNKNOWN_KEY", "some value")
+ assert.Equal(t, "some value", value)
+}
+
+func TestGetEnvOrDefault_ShouldReturnEnvironmentValue(t *testing.T) {
+ value := env.GetEnvOrDefault("PATH", "default path")
+ assert.NotEqual(t, "default path", value)
+}
+
+func TestGetCurrentDomain_ShouldReturnCorrectDomain(t *testing.T) {
+ for _, testCase := range envs {
+ os.Setenv("GO_ENV", testCase.go_env)
+ actual := env.GetCurrentDomain()
+ assert.Equal(t, testCase.domain, actual)
+ }
+}
+
+func TestEnvironment_ShouldReturnCorrectEnvironment(t *testing.T) {
+ for _, testCase := range envs {
+ os.Setenv("GO_ENV", testCase.go_env)
+ actual := env.Current()
+ assert.Equal(t, testCase.env, actual)
+ }
+}
diff --git a/handlers/router_test.go b/handlers/helper_test.go
similarity index 92%
rename from handlers/router_test.go
rename to handlers/helper_test.go
index a7462cd3a..d377f5de8 100644
--- a/handlers/router_test.go
+++ b/handlers/helper_test.go
@@ -6,8 +6,8 @@ import (
"net/http/httptest"
"github.com/WeCanHearYou/wchy/context"
- "github.com/WeCanHearYou/wchy/handlers"
"github.com/WeCanHearYou/wchy/models"
+ "github.com/WeCanHearYou/wchy/router"
"github.com/WeCanHearYou/wchy/services"
"github.com/jmoiron/jsonq"
)
@@ -23,7 +23,7 @@ func makeRequest(method, url string) (int, *jsonq.JsonQuery) {
BuildTime: "today",
},
}
- router := handlers.GetMainEngine(ctx)
+ router := router.GetMainEngine(ctx)
request, _ := http.NewRequest(method, url, nil)
response := httptest.NewRecorder()
diff --git a/main.go b/main.go
index a71ca765d..617650975 100644
--- a/main.go
+++ b/main.go
@@ -8,9 +8,9 @@ import (
_ "github.com/lib/pq"
"github.com/WeCanHearYou/wchy/context"
- "github.com/WeCanHearYou/wchy/handlers"
+ "github.com/WeCanHearYou/wchy/env"
+ "github.com/WeCanHearYou/wchy/router"
"github.com/WeCanHearYou/wchy/services"
- "github.com/WeCanHearYou/wchy/util"
logging "github.com/op/go-logging"
)
@@ -21,11 +21,10 @@ var db *sql.DB
var buildtime string
func init() {
- goEnv := os.Getenv("GO_ENV")
log.Info("Application is starting...")
- log.Infof("GO_ENV: %s", goEnv)
+ log.Infof("GO_ENV: %s", env.Current())
- if goEnv == "production" || goEnv == "staging" {
+ if env.IsProduction() || env.IsStaging() {
gin.SetMode(gin.ReleaseMode)
}
@@ -41,5 +40,5 @@ func init() {
}
func main() {
- handlers.GetMainEngine(ctx).Run(":" + util.GetEnvOrDefault("PORT", "3000"))
+ router.GetMainEngine(ctx).Run(":" + env.GetEnvOrDefault("PORT", "3000"))
}
diff --git a/handlers/router.go b/router/router.go
similarity index 84%
rename from handlers/router.go
rename to router/router.go
index e4a31050a..b3ac645d2 100644
--- a/handlers/router.go
+++ b/router/router.go
@@ -1,4 +1,4 @@
-package handlers
+package router
import (
"net/http"
@@ -7,7 +7,9 @@ import (
"strings"
"github.com/WeCanHearYou/wchy/context"
+ "github.com/WeCanHearYou/wchy/handlers"
+ "github.com/WeCanHearYou/wchy/env"
"github.com/gin-gonic/gin"
)
@@ -41,8 +43,7 @@ func GetMainEngine(ctx context.WchyContext) *gin.Engine {
router.Use(gin.Logger())
router.Use(multiTenant(ctx))
- goEnv := os.Getenv("GO_ENV")
- if goEnv == "test" {
+ if env.IsTest() {
router.LoadHTMLGlob(filepath.Join(os.Getenv("GOPATH"), "src/github.com/WeCanHearYou/wchy/views/*"))
} else {
router.LoadHTMLGlob("views/*")
@@ -56,8 +57,8 @@ func GetMainEngine(ctx context.WchyContext) *gin.Engine {
api := router.Group("/api")
{
- api.GET("/status", Status(ctx))
- api.GET("/tenants/:domain", TenantByDomain(ctx))
+ api.GET("/status", handlers.Status(ctx))
+ api.GET("/tenants/:domain", handlers.TenantByDomain(ctx))
}
return router
}
diff --git a/services/tenant.go b/services/tenant.go
index 9b9e17e25..2f87d7df2 100644
--- a/services/tenant.go
+++ b/services/tenant.go
@@ -4,8 +4,8 @@ import (
"database/sql"
"strings"
+ "github.com/WeCanHearYou/wchy/env"
"github.com/WeCanHearYou/wchy/models"
- "github.com/WeCanHearYou/wchy/util"
)
// TenantService contains read and write operations for tenants
@@ -43,7 +43,7 @@ func (svc PostgresTenantService) GetByDomain(domain string) (*models.Tenant, err
return nil, ErrNotFound
}
- tenant.Domain = tenant.Domain + "." + util.GetCurrentDomain()
+ tenant.Domain = tenant.Domain + "." + env.GetCurrentDomain()
return tenant, nil
}
diff --git a/util/env.go b/util/env.go
deleted file mode 100644
index 549c8128c..000000000
--- a/util/env.go
+++ /dev/null
@@ -1,28 +0,0 @@
-package util
-
-import "os"
-
-// GetEnvOrDefault retrieves the value of the environment variable named by the key.
-// It returns the value if available, otherwise returns defaultValue
-func GetEnvOrDefault(name string, defaultValue string) string {
- value := os.Getenv(name)
- if value == "" {
- return defaultValue
- }
- return value
-}
-
-// GetCurrentDomain returns WCHY domain based on current environment variables
-func GetCurrentDomain() string {
- env := os.Getenv("GO_ENV")
- switch env {
- case "test":
- return "test.canhearyou.com"
- case "development":
- return "dev.canhearyou.com"
- case "staging":
- return "staging.canhearyou.com"
- }
- return "canhearyou.com"
-
-}
diff --git a/util/env_test.go b/util/env_test.go
deleted file mode 100644
index 5ddcc1ada..000000000
--- a/util/env_test.go
+++ /dev/null
@@ -1,39 +0,0 @@
-package util_test
-
-import (
- "testing"
-
- "os"
-
- "github.com/WeCanHearYou/wchy/util"
- "github.com/stretchr/testify/assert"
-)
-
-func TestGetEnvOrDefault_ShouldReturnNullWhenKeyIsUnknown(t *testing.T) {
- value := util.GetEnvOrDefault("UNKNOWN_KEY", "some value")
- assert.Equal(t, "some value", value)
-}
-
-func TestGetEnvOrDefault_ShouldReturnEnvironmentValue(t *testing.T) {
- value := util.GetEnvOrDefault("PATH", "default path")
- assert.NotEqual(t, "default path", value)
-}
-
-func TestGetCurrentDomain_ShouldReturnCorrectDomain(t *testing.T) {
- var tests = []struct {
- env string
- expected string
- }{
- {"test", "test.canhearyou.com"},
- {"staging", "staging.canhearyou.com"},
- {"development", "dev.canhearyou.com"},
- {"production", "canhearyou.com"},
- {"anything", "canhearyou.com"},
- }
-
- for _, testCase := range tests {
- os.Setenv("GO_ENV", testCase.env)
- actual := util.GetCurrentDomain()
- assert.Equal(t, testCase.expected, actual)
- }
-}