Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

refactoring

goenningc51a0fbe01b9
diff --git a/context/wchy.go b/context/wchy.go
index 15ef2352a..a9b4e2800 100644
--- a/context/wchy.go
+++ b/context/wchy.go
@@ -1,9 +1,6 @@
package context
-import (
- "github.com/WeCanHearYou/wchy/auth"
- "github.com/WeCanHearYou/wchy/service"
-)
+import "github.com/WeCanHearYou/wchy/service"
// WchySettings is an application-wide settings
type WchySettings struct {
@@ -14,7 +11,7 @@ type WchySettings struct {
// WchyContext is an application-wide context
type WchyContext struct {
Health service.HealthCheckService
- Auth auth.Service
+ User service.UserService
Idea service.IdeaService
Tenant service.TenantService
Settings WchySettings
diff --git a/handler/oauth.go b/handler/oauth.go
index ad9e7006d..c37ad89d6 100644
--- a/handler/oauth.go
+++ b/handler/oauth.go
@@ -14,6 +14,8 @@ import (
"github.com/WeCanHearYou/wchy/auth"
"github.com/WeCanHearYou/wchy/context"
+ "github.com/WeCanHearYou/wchy/model"
+ "github.com/WeCanHearYou/wchy/service"
"github.com/labstack/echo"
)
@@ -31,7 +33,7 @@ type oauthProviderSettings struct {
var (
authEndpoint = os.Getenv("AUTH_ENDPOINT")
oauthSettings = map[string]*oauthProviderSettings{
- auth.OAuthFacebookProvider: &oauthProviderSettings{
+ service.OAuthFacebookProvider: &oauthProviderSettings{
profileURL: func(token *oauth2.Token) string {
return "https://graph.facebook.com/me?fields=name,email&access_token=" + url.QueryEscape(token.AccessToken)
},
@@ -43,7 +45,7 @@ var (
Endpoint: facebook.Endpoint,
},
},
- auth.OAuthGoogleProvider: &oauthProviderSettings{
+ service.OAuthGoogleProvider: &oauthProviderSettings{
profileURL: func(token *oauth2.Token) string {
return "https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + url.QueryEscape(token.AccessToken)
},
@@ -108,20 +110,20 @@ func (h OAuthHandlers) Callback(provider string) echo.HandlerFunc {
return c.Redirect(http.StatusTemporaryRedirect, redirect) //TODO: redirect to some error page
}
- user, err := h.ctx.Auth.GetByEmail(oauthUser.Email)
+ user, err := h.ctx.User.GetByEmail(oauthUser.Email)
if err != nil {
- if err == auth.ErrUserNotFound {
- user = &auth.User{
+ if err == service.ErrNotFound {
+ user = &model.User{
Name: oauthUser.Name,
Email: oauthUser.Email,
- Providers: []*auth.UserProvider{
- &auth.UserProvider{
+ Providers: []*model.UserProvider{
+ &model.UserProvider{
UID: oauthUser.ID,
Name: provider,
},
},
}
- err = h.ctx.Auth.Register(user)
+ err = h.ctx.User.Register(user)
if err != nil {
c.Logger().Error(err)
}
diff --git a/main.go b/main.go
index ef4db6dd4..fba0f6d80 100644
--- a/main.go
+++ b/main.go
@@ -7,7 +7,6 @@ import (
"fmt"
- "github.com/WeCanHearYou/wchy/auth"
"github.com/WeCanHearYou/wchy/context"
"github.com/WeCanHearYou/wchy/env"
"github.com/WeCanHearYou/wchy/router"
@@ -28,7 +27,7 @@ func main() {
ctx := &context.WchyContext{
Health: &service.PostgresHealthCheckService{DB: db},
Idea: &service.PostgresIdeaService{DB: db},
- Auth: &auth.PostgresService{DB: db},
+ User: &service.PostgresUserService{DB: db},
Tenant: &service.PostgresTenantService{DB: db},
Settings: context.WchySettings{
BuildTime: buildtime,
diff --git a/model/idea.go b/model/idea.go
deleted file mode 100644
index 40968b351..000000000
--- a/model/idea.go
+++ /dev/null
@@ -1,8 +0,0 @@
-package model
-
-//Idea represents an idea on a tenant board
-type Idea struct {
- ID int64 `json:"id"`
- Title string `json:"title"`
- Description string `json:"description"`
-}
diff --git a/model/model.go b/model/model.go
new file mode 100644
index 000000000..8094d27be
--- /dev/null
+++ b/model/model.go
@@ -0,0 +1,29 @@
+package model
+
+//Tenant represents a tenant
+type Tenant struct {
+ ID int64 `json:"id"`
+ Name string `json:"name"`
+ Domain string `json:"domain"`
+}
+
+//Idea represents an idea on a tenant board
+type Idea struct {
+ ID int64 `json:"id"`
+ Title string `json:"title"`
+ Description string `json:"description"`
+}
+
+//User represents an user inside our application
+type User struct {
+ ID int64
+ Name string
+ Email string
+ Providers []*UserProvider
+}
+
+//UserProvider represents the relashionship between an User and an Authentication provide
+type UserProvider struct {
+ Name string
+ UID string
+}
diff --git a/model/tenant.go b/model/tenant.go
deleted file mode 100644
index 7f5636198..000000000
--- a/model/tenant.go
+++ /dev/null
@@ -1,8 +0,0 @@
-package model
-
-//Tenant represents a tenant
-type Tenant struct {
- ID int64 `json:"id"`
- Name string `json:"name"`
- Domain string `json:"domain"`
-}
diff --git a/router/middleware/hostchecker.go b/router/middleware/hostchecker.go
new file mode 100644
index 000000000..13abfc274
--- /dev/null
+++ b/router/middleware/hostchecker.go
@@ -0,0 +1,28 @@
+package middleware
+
+import (
+ "net/http"
+ "strings"
+
+ "github.com/labstack/echo"
+)
+
+// HostChecker checks for a specific host
+func HostChecker(baseURL string) echo.MiddlewareFunc {
+ return func(next echo.HandlerFunc) echo.HandlerFunc {
+ return func(c echo.Context) error {
+
+ scheme := "http"
+ if c.Request().TLS != nil {
+ scheme = "https"
+ }
+ url := scheme + "://" + c.Request().Host
+
+ if strings.Index(url, baseURL) < 0 {
+ c.Logger().Errorf("%s is not valid for this operation", url)
+ return c.NoContent(http.StatusBadRequest)
+ }
+ return next(c)
+ }
+ }
+}
diff --git a/router/middlewares.go b/router/middleware/jwt.go
similarity index 50%
rename from router/middlewares.go
rename to router/middleware/jwt.go
index 196e0aa36..3b6351309 100644
--- a/router/middlewares.go
+++ b/router/middleware/jwt.go
@@ -1,54 +1,12 @@
-package router
+package middleware
import (
"net/http"
- "strings"
-
- "fmt"
"github.com/WeCanHearYou/wchy/auth"
- "github.com/WeCanHearYou/wchy/context"
- "github.com/WeCanHearYou/wchy/util"
"github.com/labstack/echo"
)
-// MultiTenant extract tenant information from hostname and inject it into current context
-func MultiTenant(ctx *context.WchyContext) echo.MiddlewareFunc {
- return func(next echo.HandlerFunc) echo.HandlerFunc {
- return func(c echo.Context) error {
- hostname := util.StripPort(c.Request().Host)
- tenant, err := ctx.Tenant.GetByDomain(hostname)
- if err == nil {
- c.Set("Tenant", tenant)
- return next(c)
- }
-
- fmt.Printf("Tenant not found for %s.\n", hostname)
- return c.NoContent(http.StatusNotFound)
- }
- }
-}
-
-// HostChecker checks for a specific host
-func HostChecker(baseURL string) echo.MiddlewareFunc {
- return func(next echo.HandlerFunc) echo.HandlerFunc {
- return func(c echo.Context) error {
-
- scheme := "http"
- if c.Request().TLS != nil {
- scheme = "https"
- }
- url := scheme + "://" + c.Request().Host
-
- if strings.Index(url, baseURL) < 0 {
- c.Logger().Errorf("%s is not valid for this operation", url)
- return c.NoContent(http.StatusBadRequest)
- }
- return next(c)
- }
- }
-}
-
// JwtGetter gets JWT token from cookie and add into context
func JwtGetter() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
diff --git a/router/middleware/jwt_test.go b/router/middleware/jwt_test.go
new file mode 100644
index 000000000..0ac2f18d2
--- /dev/null
+++ b/router/middleware/jwt_test.go
@@ -0,0 +1 @@
+package middleware_test
diff --git a/router/middleware/multitenant.go b/router/middleware/multitenant.go
new file mode 100644
index 000000000..50e912fd8
--- /dev/null
+++ b/router/middleware/multitenant.go
@@ -0,0 +1,27 @@
+package middleware
+
+import (
+ "fmt"
+ "net/http"
+
+ "github.com/WeCanHearYou/wchy/context"
+ "github.com/WeCanHearYou/wchy/util"
+ "github.com/labstack/echo"
+)
+
+// MultiTenant extract tenant information from hostname and inject it into current context
+func MultiTenant(ctx *context.WchyContext) echo.MiddlewareFunc {
+ return func(next echo.HandlerFunc) echo.HandlerFunc {
+ return func(c echo.Context) error {
+ hostname := util.StripPort(c.Request().Host)
+ tenant, err := ctx.Tenant.GetByDomain(hostname)
+ if err == nil {
+ c.Set("Tenant", tenant)
+ return next(c)
+ }
+
+ fmt.Printf("Tenant not found for %s.\n", hostname)
+ return c.NoContent(http.StatusNotFound)
+ }
+ }
+}
diff --git a/router/middlewares_test.go b/router/middleware/multitenant_test.go
similarity index 92%
rename from router/middlewares_test.go
rename to router/middleware/multitenant_test.go
index ae4698445..886e95397 100644
--- a/router/middlewares_test.go
+++ b/router/middleware/multitenant_test.go
@@ -1,4 +1,4 @@
-package router_test
+package middleware_test
import (
"net/http"
@@ -7,7 +7,7 @@ import (
"github.com/WeCanHearYou/wchy/context"
"github.com/WeCanHearYou/wchy/model"
- "github.com/WeCanHearYou/wchy/router"
+ "github.com/WeCanHearYou/wchy/router/middleware"
"github.com/WeCanHearYou/wchy/service"
"github.com/labstack/echo"
. "github.com/onsi/gomega"
@@ -64,7 +64,7 @@ func TestMultiTenant(t *testing.T) {
c := e.NewContext(req, rec)
c.Request().Host = host
- cors := router.MultiTenant(ctx)
+ cors := middleware.MultiTenant(ctx)
cors(echo.HandlerFunc(func(c echo.Context) error {
return c.String(http.StatusOK, c.Get("Tenant").(*model.Tenant).Name)
}))(c)
@@ -86,7 +86,7 @@ func TestMultiTenant_UnknownDomain(t *testing.T) {
c := e.NewContext(req, rec)
c.Request().Host = "somedomain.com"
- cors := router.MultiTenant(ctx)
+ cors := middleware.MultiTenant(ctx)
cors(echo.HandlerFunc(func(c echo.Context) error {
return c.String(http.StatusOK, c.Get("Tenant").(*model.Tenant).Name)
}))(c)
diff --git a/router/router.go b/router/router.go
index 2335d57f8..b1b0ac366 100644
--- a/router/router.go
+++ b/router/router.go
@@ -5,10 +5,11 @@ import (
"strings"
- "github.com/WeCanHearYou/wchy/auth"
"github.com/WeCanHearYou/wchy/context"
"github.com/WeCanHearYou/wchy/env"
"github.com/WeCanHearYou/wchy/handler"
+ "github.com/WeCanHearYou/wchy/router/middleware"
+ "github.com/WeCanHearYou/wchy/service"
"github.com/labstack/echo"
"github.com/labstack/gommon/log"
)
@@ -48,19 +49,19 @@ func GetMainEngine(ctx *context.WchyContext) *echo.Echo {
router.Static("/public", "public")
router.Static("/assets", "node_modules") //TODO: Don't expose node_modules
- authGroup := router.Group("", HostChecker(env.MustGet("AUTH_ENDPOINT")))
+ authGroup := router.Group("", middleware.HostChecker(env.MustGet("AUTH_ENDPOINT")))
{
- authGroup.GET("/oauth/facebook", handler.OAuth(ctx).Login(auth.OAuthFacebookProvider))
- authGroup.GET("/oauth/facebook/callback", handler.OAuth(ctx).Callback(auth.OAuthFacebookProvider))
- authGroup.GET("/oauth/google", handler.OAuth(ctx).Login(auth.OAuthGoogleProvider))
- authGroup.GET("/oauth/google/callback", handler.OAuth(ctx).Callback(auth.OAuthGoogleProvider))
+ authGroup.GET("/oauth/facebook", handler.OAuth(ctx).Login(service.OAuthFacebookProvider))
+ authGroup.GET("/oauth/facebook/callback", handler.OAuth(ctx).Callback(service.OAuthFacebookProvider))
+ authGroup.GET("/oauth/google", handler.OAuth(ctx).Login(service.OAuthGoogleProvider))
+ authGroup.GET("/oauth/google/callback", handler.OAuth(ctx).Callback(service.OAuthGoogleProvider))
}
app := router.Group("")
{
- app.Use(JwtGetter())
- app.Use(JwtSetter())
- app.Use(MultiTenant(ctx))
+ app.Use(middleware.JwtGetter())
+ app.Use(middleware.JwtSetter())
+ app.Use(middleware.MultiTenant(ctx))
app.GET("/", handler.Index(ctx))
diff --git a/run.sh b/run.sh
deleted file mode 100644
index f6b1b7f5d..000000000
--- a/run.sh
+++ /dev/null
@@ -1 +0,0 @@
- gin --buildArgs "-ldflags='-X main.buildtime=${BUILD_TIME}'"
\ No newline at end of file
diff --git a/auth/service.go b/service/user.go
similarity index 53%
rename from auth/service.go
rename to service/user.go
index 871af92d7..18f3247ca 100644
--- a/auth/service.go
+++ b/service/user.go
@@ -1,8 +1,9 @@
-package auth
+package service
import (
"database/sql"
- "errors"
+
+ "github.com/WeCanHearYou/wchy/model"
)
const (
@@ -12,48 +13,31 @@ const (
OAuthGoogleProvider = "google"
)
-//User represents an user inside our application
-type User struct {
- ID int64
- Name string
- Email string
- Providers []*UserProvider
-}
-
-//UserProvider represents the relashionship between an User and an Authentication provide
-type UserProvider struct {
- Name string
- UID string
-}
-
-//ErrUserNotFound is "User not found"
-var ErrUserNotFound = errors.New("User not found")
-
-// Service is used for auth operations
-type Service interface {
- GetByEmail(email string) (*User, error)
- Register(user *User) error
+// UserService is used for user operations
+type UserService interface {
+ GetByEmail(email string) (*model.User, error)
+ Register(user *model.User) error
}
-// PostgresService is used for auth operations using a Postgres database
-type PostgresService struct {
+// PostgresUserService is used for user operations using a Postgres database
+type PostgresUserService struct {
DB *sql.DB
}
// GetByEmail returns a user based on given email
-func (svc PostgresService) GetByEmail(email string) (*User, error) {
- user := &User{}
+func (svc PostgresUserService) GetByEmail(email string) (*model.User, error) {
+ user := &model.User{}
row := svc.DB.QueryRow("SELECT id, name, email FROM users WHERE email = $1", email)
err := row.Scan(&user.ID, &user.Name, &user.Email)
if err != nil {
- return nil, ErrUserNotFound
+ return nil, ErrNotFound
}
return user, nil
}
// Register creates a new user based on given information
-func (svc PostgresService) Register(user *User) error {
+func (svc PostgresUserService) Register(user *model.User) error {
tx, err := svc.DB.Begin()
if err != nil {
return err
diff --git a/test.sh b/test.sh
deleted file mode 100644
index 4fc5783d5..000000000
--- a/test.sh
+++ /dev/null
@@ -1,3 +0,0 @@
-AUTH_ENDPOINT=http://login.test.canhearyou.com:3000 \
-GO_ENV=test \
-go test $(go list ./... | grep -v /vendor/) -cover
\ No newline at end of file