Open platform to collect and prioritize feedback
move web to its own package
goenningb30e21377f57
diff --git a/app/cache.go b/app/cache.godeleted file mode 100644index 5d28e3d75..000000000--- a/app/cache.go+++ /dev/null@@ -1,11 +0,0 @@-package app--// OneYearCache adds Cache-Control header for one year-func OneYearCache() MiddlewareFunc {- return func(next HandlerFunc) HandlerFunc {- return func(c Context) error {- c.Response().Header().Add("Cache-Control", "public, max-age=30672000")- return next(c)- }- }-}diff --git a/app/handlers/feedback.go b/app/handlers/feedback.goindex f34a346fe..90685445c 100644--- a/app/handlers/feedback.go+++ b/app/handlers/feedback.go@@ -4,6 +4,7 @@ import ("strings""github.com/WeCanHearYou/wechy/app"+ "github.com/WeCanHearYou/wechy/app/pkg/web""github.com/WeCanHearYou/wechy/app/storage""github.com/labstack/echo")@@ -19,8 +20,8 @@ func Handlers(ideas storage.Idea) AllHandlers {}// List all tenant ideas-func (h AllHandlers) List() app.HandlerFunc {- return func(c app.Context) error {+func (h AllHandlers) List() web.HandlerFunc {+ return func(c web.Context) error {ideas, err := h.ideas.GetAll(c.Tenant().ID)if err != nil {return c.Failure(err)@@ -38,8 +39,8 @@ type newIdeaInput struct {}// PostIdea creates a new idea on current tenant-func (h AllHandlers) PostIdea() app.HandlerFunc {- return func(c app.Context) error {+func (h AllHandlers) PostIdea() web.HandlerFunc {+ return func(c web.Context) error {input := new(newIdeaInput)if err := c.Bind(input); err != nil {return c.Failure(err)@@ -63,8 +64,8 @@ func (h AllHandlers) PostIdea() app.HandlerFunc {}// Details shows details of given Idea by id-func (h AllHandlers) Details() app.HandlerFunc {- return func(c app.Context) error {+func (h AllHandlers) Details() web.HandlerFunc {+ return func(c web.Context) error {tenant := c.Tenant()number, err := c.ParamAsInt("number")@@ -97,8 +98,8 @@ type newCommentInput struct {}// PostComment creates a new comment on given idea-func (h AllHandlers) PostComment() app.HandlerFunc {- return func(c app.Context) error {+func (h AllHandlers) PostComment() web.HandlerFunc {+ return func(c web.Context) error {input := new(newCommentInput)if err := c.Bind(input); err != nil {return c.Failure(err)diff --git a/app/handlers/identity.go b/app/handlers/identity.goindex c03bd14c9..97cdd233b 100644--- a/app/handlers/identity.go+++ b/app/handlers/identity.go@@ -10,6 +10,7 @@ import ("github.com/WeCanHearYou/wechy/app/models""github.com/WeCanHearYou/wechy/app/pkg/jwt""github.com/WeCanHearYou/wechy/app/pkg/oauth"+ "github.com/WeCanHearYou/wechy/app/pkg/web""github.com/WeCanHearYou/wechy/app/storage")@@ -32,8 +33,8 @@ func OAuth(tenantStorage storage.Tenant, oauthService oauth.Service, userStorage}// Callback handles OAuth callbacks-func (h OAuthHandlers) Callback(provider string) app.HandlerFunc {- return func(c app.Context) error {+func (h OAuthHandlers) Callback(provider string) web.HandlerFunc {+ return func(c web.Context) error {redirect := c.QueryParam("state")redirectURL, err := url.ParseRequestURI(redirect)@@ -109,16 +110,16 @@ func (h OAuthHandlers) Callback(provider string) app.HandlerFunc {}// Login handles OAuth logins-func (h OAuthHandlers) Login(provider string) app.HandlerFunc {- return func(c app.Context) error {+func (h OAuthHandlers) Login(provider string) web.HandlerFunc {+ return func(c web.Context) error {authURL := h.oauthService.GetAuthURL(provider, c.QueryParam("redirect"))return c.Redirect(http.StatusTemporaryRedirect, authURL)}}// Logout remove auth cookies-func (h OAuthHandlers) Logout() app.HandlerFunc {- return func(c app.Context) error {+func (h OAuthHandlers) Logout() web.HandlerFunc {+ return func(c web.Context) error {c.SetCookie(&http.Cookie{Name: "auth",MaxAge: -1,diff --git a/app/handlers/status.go b/app/handlers/status.goindex 7e425e5ce..6ecac97d8 100644--- a/app/handlers/status.go+++ b/app/handlers/status.go@@ -5,9 +5,9 @@ import ("runtime""time"- "github.com/WeCanHearYou/wechy/app""github.com/WeCanHearYou/wechy/app/models""github.com/WeCanHearYou/wechy/app/pkg/env"+ "github.com/WeCanHearYou/wechy/app/pkg/web""github.com/labstack/echo")@@ -16,12 +16,12 @@ type statusHandler struct {}// Status creates a new Status HTTP handler-func Status(settings *models.WechySettings) app.HandlerFunc {+func Status(settings *models.WechySettings) web.HandlerFunc {return statusHandler{settings}.get()}-func (h statusHandler) get() app.HandlerFunc {- return func(c app.Context) error {+func (h statusHandler) get() web.HandlerFunc {+ return func(c web.Context) error {return c.JSON(http.StatusOK, echo.Map{"build": h.settings.BuildTime,"version": h.settings.Version,diff --git a/app/middlewares/auth.go b/app/middlewares/auth.goindex a10dbca70..b96e8bef1 100644--- a/app/middlewares/auth.go+++ b/app/middlewares/auth.go@@ -4,14 +4,14 @@ import ("net/http""net/url"- "github.com/WeCanHearYou/wechy/app""github.com/WeCanHearYou/wechy/app/models"+ "github.com/WeCanHearYou/wechy/app/pkg/web")// IsAuthenticated blocks non-authenticated requests-func IsAuthenticated() app.MiddlewareFunc {- return func(next app.HandlerFunc) app.HandlerFunc {- return func(c app.Context) error {+func IsAuthenticated() web.MiddlewareFunc {+ return func(next web.HandlerFunc) web.HandlerFunc {+ return func(c web.Context) error {if c.User() == nil {return c.NoContent(http.StatusForbidden)}@@ -21,9 +21,9 @@ func IsAuthenticated() app.MiddlewareFunc {}// IsAuthorized blocks non-authorized requests-func IsAuthorized(roles ...models.Role) app.MiddlewareFunc {- return func(next app.HandlerFunc) app.HandlerFunc {- return func(c app.Context) error {+func IsAuthorized(roles ...models.Role) web.MiddlewareFunc {+ return func(next web.HandlerFunc) web.HandlerFunc {+ return func(c web.Context) error {user := c.User()for _, role := range roles {if user.Role == role {@@ -36,9 +36,9 @@ func IsAuthorized(roles ...models.Role) app.MiddlewareFunc {}// HostChecker checks for a specific host-func HostChecker(baseURL string) app.MiddlewareFunc {- return func(next app.HandlerFunc) app.HandlerFunc {- return func(c app.Context) error {+func HostChecker(baseURL string) web.MiddlewareFunc {+ return func(next web.HandlerFunc) web.HandlerFunc {+ return func(c web.Context) error {u, err := url.Parse(baseURL)if err != nil {return c.Failure(err)diff --git a/app/middlewares/auth_test.go b/app/middlewares/auth_test.goindex 961ca67ea..c29e97af3 100644--- a/app/middlewares/auth_test.go+++ b/app/middlewares/auth_test.go@@ -5,10 +5,10 @@ import ("net/http/httptest""testing"- "github.com/WeCanHearYou/wechy/app""github.com/WeCanHearYou/wechy/app/middlewares""github.com/WeCanHearYou/wechy/app/mock""github.com/WeCanHearYou/wechy/app/models"+ "github.com/WeCanHearYou/wechy/app/pkg/web""github.com/labstack/echo". "github.com/onsi/gomega")@@ -26,7 +26,7 @@ func TestIsAuthorized_WithAllowedRole(t *testing.T) {})mw := middlewares.IsAuthorized(models.RoleAdministrator, models.RoleMember)- mw(func(c app.Context) error {+ mw(func(c web.Context) error {return c.NoContent(http.StatusOK)})(c)@@ -46,7 +46,7 @@ func TestIsAuthorized_WithForbiddenRole(t *testing.T) {})mw := middlewares.IsAuthorized(models.RoleAdministrator, models.RoleMember)- mw(func(c app.Context) error {+ mw(func(c web.Context) error {return c.NoContent(http.StatusOK)})(c)@@ -65,7 +65,7 @@ func TestIsAuthenticated_WithUser(t *testing.T) {})mw := middlewares.IsAuthenticated()- mw(func(c app.Context) error {+ mw(func(c web.Context) error {return c.NoContent(http.StatusOK)})(c)@@ -81,7 +81,7 @@ func TestIsAuthenticated_WithoutUser(t *testing.T) {c := server.NewContext(req, rec)mw := middlewares.IsAuthenticated()- mw(func(c app.Context) error {+ mw(func(c web.Context) error {return c.NoContent(http.StatusOK)})(c)@@ -98,7 +98,7 @@ func TestHostChecker(t *testing.T) {c.Request().Host = "login.test.canhearyou.com"mw := middlewares.HostChecker("http://login.test.canhearyou.com")- mw(func(c app.Context) error {+ mw(func(c web.Context) error {return c.NoContent(http.StatusOK)})(c)@@ -115,7 +115,7 @@ func TestHostChecker_DifferentHost(t *testing.T) {c.Request().Host = "orange.test.canhearyou.com"mw := middlewares.HostChecker("login.test.canhearyou.com")- mw(app.HandlerFunc(func(c app.Context) error {+ mw(web.HandlerFunc(func(c web.Context) error {return c.NoContent(http.StatusOK)}))(c)diff --git a/app/middlewares/cache.go b/app/middlewares/cache.gonew file mode 100644index 000000000..48e35901b--- /dev/null+++ b/app/middlewares/cache.go@@ -0,0 +1,13 @@+package middlewares++import "github.com/WeCanHearYou/wechy/app/pkg/web"++// OneYearCache adds Cache-Control header for one year+func OneYearCache() web.MiddlewareFunc {+ return func(next web.HandlerFunc) web.HandlerFunc {+ return func(c web.Context) error {+ c.Response().Header().Add("Cache-Control", "public, max-age=30672000")+ return next(c)+ }+ }+}diff --git a/app/middlewares/jwt.go b/app/middlewares/jwt.goindex 532912613..352616276 100644--- a/app/middlewares/jwt.go+++ b/app/middlewares/jwt.go@@ -4,15 +4,15 @@ import ("net/http""strings"- "github.com/WeCanHearYou/wechy/app""github.com/WeCanHearYou/wechy/app/pkg/jwt"+ "github.com/WeCanHearYou/wechy/app/pkg/web""github.com/WeCanHearYou/wechy/app/storage")// MultiTenant extract tenant information from hostname and inject it into current context-func MultiTenant(tenants storage.Tenant) app.MiddlewareFunc {- return func(next app.HandlerFunc) app.HandlerFunc {- return func(c app.Context) error {+func MultiTenant(tenants storage.Tenant) web.MiddlewareFunc {+ return func(next web.HandlerFunc) web.HandlerFunc {+ return func(c web.Context) error {hostname := stripPort(c.Request().Host)tenant, err := tenants.GetByDomain(hostname)if err == nil {@@ -27,9 +27,9 @@ func MultiTenant(tenants storage.Tenant) app.MiddlewareFunc {}// JwtGetter gets JWT token from cookie and add into context-func JwtGetter(users storage.User) app.MiddlewareFunc {- return func(next app.HandlerFunc) app.HandlerFunc {- return func(c app.Context) error {+func JwtGetter(users storage.User) web.MiddlewareFunc {+ return func(next web.HandlerFunc) web.HandlerFunc {+ return func(c web.Context) error {if cookie, err := c.Cookie("auth"); err == nil {if claims, err := jwt.Decode(cookie.Value); err == nil {@@ -49,9 +49,9 @@ func JwtGetter(users storage.User) app.MiddlewareFunc {}// JwtSetter sets JWT token into cookie-func JwtSetter() app.MiddlewareFunc {- return func(next app.HandlerFunc) app.HandlerFunc {- return func(c app.Context) error {+func JwtSetter() web.MiddlewareFunc {+ return func(next web.HandlerFunc) web.HandlerFunc {+ return func(c web.Context) error {query := c.Request().URL.Query()diff --git a/app/middlewares/jwt_test.go b/app/middlewares/jwt_test.goindex 28b39fd51..e63341f4b 100644--- a/app/middlewares/jwt_test.go+++ b/app/middlewares/jwt_test.go@@ -11,6 +11,7 @@ import ("github.com/WeCanHearYou/wechy/app/models""github.com/WeCanHearYou/wechy/app/pkg/dbx""github.com/WeCanHearYou/wechy/app/pkg/jwt"+ "github.com/WeCanHearYou/wechy/app/pkg/web""github.com/WeCanHearYou/wechy/app/storage/postgres""github.com/labstack/echo". "github.com/onsi/gomega"@@ -77,7 +78,7 @@ func TestMultiTenant(t *testing.T) {c.Request().Host = hostmw := middlewares.MultiTenant(&mockTenantStorage{})- mw(func(c app.Context) error {+ mw(func(c web.Context) error {return c.String(http.StatusOK, c.Tenant().Name)})(c)@@ -97,7 +98,7 @@ func TestMultiTenant_UnknownDomain(t *testing.T) {c.Request().Host = "somedomain.com"mw := middlewares.MultiTenant(&mockTenantStorage{})- mw(func(c app.Context) error {+ mw(func(c web.Context) error {return c.String(http.StatusOK, c.Tenant().Name)})(c)@@ -114,7 +115,7 @@ func TestJwtGetter_NoCookie(t *testing.T) {c := server.NewContext(req, rec)mw := middlewares.JwtGetter(UserStorage)- mw(func(c app.Context) error {+ mw(func(c web.Context) error {if c.IsAuthenticated() {return c.NoContent(http.StatusOK)} else {@@ -145,7 +146,7 @@ func TestJwtGetter_WithCookie(t *testing.T) {})mw := middlewares.JwtGetter(UserStorage)- mw(func(c app.Context) error {+ mw(func(c web.Context) error {return c.String(http.StatusOK, c.User().Name)})(c)@@ -173,7 +174,7 @@ func TestJwtGetter_WithCookie_DifferentTenant(t *testing.T) {})mw := middlewares.JwtGetter(UserStorage)- mw(func(c app.Context) error {+ mw(func(c web.Context) error {if c.User() == nil {return c.NoContent(http.StatusNoContent)}@@ -193,7 +194,7 @@ func TestJwtSetter_WithoutJwt(t *testing.T) {c.Request().Host = "orange.test.canhearyou.com"mw := middlewares.JwtSetter()- mw(func(c app.Context) error {+ mw(func(c web.Context) error {return c.NoContent(http.StatusOK)})(c)@@ -214,7 +215,7 @@ func TestJwtSetter_WithJwt_WithoutParameter(t *testing.T) {c.Request().Host = "orange.test.canhearyou.com"mw := middlewares.JwtSetter()- mw(func(c app.Context) error {+ mw(func(c web.Context) error {return c.NoContent(http.StatusOK)})(c)@@ -236,7 +237,7 @@ func TestJwtSetter_WithJwt_WithParameter(t *testing.T) {c.Request().Host = "orange.test.canhearyou.com"mw := middlewares.JwtSetter()- mw(func(c app.Context) error {+ mw(func(c web.Context) error {return c.NoContent(http.StatusOK)})(c)diff --git a/app/mock/server.go b/app/mock/server.goindex d23da856f..426887e3c 100644--- a/app/mock/server.go+++ b/app/mock/server.go@@ -9,7 +9,7 @@ import ("fmt"- "github.com/WeCanHearYou/wechy/app"+ "github.com/WeCanHearYou/wechy/app/pkg/web""github.com/jmoiron/jsonq""github.com/labstack/echo")@@ -25,7 +25,7 @@ type Server struct {// NewServer creates a new test serverfunc NewServer() *Server {engine := echo.New()- engine.Renderer = app.NewHTMLRenderer(engine.Logger)+ engine.Renderer = web.NewHTMLRenderer(engine.Logger)engine.HTTPErrorHandler = func(e error, c echo.Context) {fmt.Println(e)c.NoContent(e.(*echo.HTTPError).Code)@@ -43,14 +43,14 @@ func NewServer() *Server {}// NewContext creates a new HTTP context-func (s *Server) NewContext(req *http.Request, w http.ResponseWriter) app.Context {+func (s *Server) NewContext(req *http.Request, w http.ResponseWriter) web.Context {c := s.engine.NewContext(req, w)- return app.Context{Context: c}+ return web.Context{Context: c}}// Execute given handler and return response as JSON-func (s *Server) Execute(handler app.HandlerFunc) (int, *jsonq.JsonQuery) {- ctx := app.Context{Context: s.Context}+func (s *Server) Execute(handler web.HandlerFunc) (int, *jsonq.JsonQuery) {+ ctx := web.Context{Context: s.Context}if err := handler(ctx); err != nil {s.engine.HTTPErrorHandler(err, ctx)}@@ -59,8 +59,8 @@ func (s *Server) Execute(handler app.HandlerFunc) (int, *jsonq.JsonQuery) {}// ExecutePost executes given handler with posted JSON-func (s *Server) ExecutePost(handler app.HandlerFunc, body string) (int, *jsonq.JsonQuery) {- ctx := app.Context{Context: s.Context}+func (s *Server) ExecutePost(handler web.HandlerFunc, body string) (int, *jsonq.JsonQuery) {+ ctx := web.Context{Context: s.Context}req, _ := http.NewRequest("POST", "/", strings.NewReader(body))req.Header.Add(echo.HeaderContentType, echo.MIMEApplicationJSON)@@ -86,8 +86,8 @@ func parseJSONBody(s *Server) (int, *jsonq.JsonQuery) {}// ExecuteRaw executes given handler and return raw response-func (s *Server) ExecuteRaw(handler app.HandlerFunc) (int, *http.Response) {- ctx := app.Context{Context: s.Context}+func (s *Server) ExecuteRaw(handler web.HandlerFunc) (int, *http.Response) {+ ctx := web.Context{Context: s.Context}if err := handler(ctx); err != nil {s.engine.HTTPErrorHandler(err, ctx)}diff --git a/app/context.go b/app/pkg/web/context.gosimilarity index 99%rename from app/context.gorename to app/pkg/web/context.goindex b0956b231..de08b0098 100644--- a/app/context.go+++ b/app/pkg/web/context.go@@ -1,4 +1,4 @@-package app+package webimport ("net/http"diff --git a/app/renderer.go b/app/pkg/web/renderer.gosimilarity index 99%rename from app/renderer.gorename to app/pkg/web/renderer.goindex 9cd1fa4e3..caede4642 100644--- a/app/renderer.go+++ b/app/pkg/web/renderer.go@@ -1,4 +1,4 @@-package app+package webimport ("fmt"diff --git a/router.go b/router.goindex c4f74e60a..b6ce23ea6 100644--- a/router.go+++ b/router.go@@ -5,12 +5,12 @@ import ("strings"- "github.com/WeCanHearYou/wechy/app""github.com/WeCanHearYou/wechy/app/handlers""github.com/WeCanHearYou/wechy/app/middlewares""github.com/WeCanHearYou/wechy/app/models""github.com/WeCanHearYou/wechy/app/pkg/env""github.com/WeCanHearYou/wechy/app/pkg/oauth"+ "github.com/WeCanHearYou/wechy/app/pkg/web""github.com/WeCanHearYou/wechy/app/storage""github.com/labstack/echo""github.com/labstack/echo/middleware"@@ -49,30 +49,30 @@ func createLogger() echo.Logger {return logger}-func wrapFunc(handler app.HandlerFunc) echo.HandlerFunc {+func wrapFunc(handler web.HandlerFunc) echo.HandlerFunc {return func(c echo.Context) error {- ctx := app.Context{Context: c}+ ctx := web.Context{Context: c}return handler(ctx)}}-func wrapMiddleware(mw app.MiddlewareFunc) echo.MiddlewareFunc {+func wrapMiddleware(mw web.MiddlewareFunc) echo.MiddlewareFunc {return func(h echo.HandlerFunc) echo.HandlerFunc {- return wrapFunc(mw(func(c app.Context) error {+ return wrapFunc(mw(func(c web.Context) error {return h(c)}))}}-func get(group *echo.Group, path string, handler app.HandlerFunc) {+func get(group *echo.Group, path string, handler web.HandlerFunc) {group.GET(path, wrapFunc(handler))}-func post(group *echo.Group, path string, handler app.HandlerFunc) {+func post(group *echo.Group, path string, handler web.HandlerFunc) {group.POST(path, wrapFunc(handler))}-func use(group *echo.Group, mw app.MiddlewareFunc) {+func use(group *echo.Group, mw web.MiddlewareFunc) {group.Use(wrapMiddleware(mw))}@@ -85,14 +85,14 @@ func GetMainEngine(ctx *WechyServices) *echo.Echo {router := echo.New()router.Logger = createLogger()- router.Renderer = app.NewHTMLRenderer(router.Logger)+ router.Renderer = web.NewHTMLRenderer(router.Logger)router.HTTPErrorHandler = errorHandlerrouter.Use(middleware.Gzip())router.Static("/favicon.ico", "favicon.ico")assetsGroup := group(router, "/assets"){- use(assetsGroup, app.OneYearCache())+ use(assetsGroup, middlewares.OneYearCache())assetsGroup.Static("/", "dist")}@@ -138,7 +138,7 @@ func GetMainEngine(ctx *WechyServices) *echo.Echo {use(adminGroup, middlewares.IsAuthenticated())use(adminGroup, middlewares.IsAuthorized(models.RoleMember, models.RoleAdministrator))- get(adminGroup, "", func(ctx app.Context) error {+ get(adminGroup, "", func(ctx web.Context) error {return ctx.HTML(http.StatusOK, "Welcome to Admin Page :)")})}