2019-05-18 18:06:10 -04:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-07-31 18:34:27 -08:00
|
|
|
"fmt"
|
2019-05-18 18:06:10 -04:00
|
|
|
"net/http"
|
|
|
|
|
2019-07-31 18:34:27 -08:00
|
|
|
"geeks-accelerator/oss/saas-starter-kit/internal/platform/auth"
|
2019-07-13 12:16:28 -08:00
|
|
|
"geeks-accelerator/oss/saas-starter-kit/internal/platform/web"
|
2019-07-31 18:34:27 -08:00
|
|
|
"geeks-accelerator/oss/saas-starter-kit/internal/platform/web/webcontext"
|
|
|
|
project_routes "geeks-accelerator/oss/saas-starter-kit/internal/project-routes"
|
2019-05-23 14:32:24 -05:00
|
|
|
"github.com/jmoiron/sqlx"
|
2019-05-18 18:06:10 -04:00
|
|
|
)
|
|
|
|
|
2019-07-31 18:34:27 -08:00
|
|
|
// Root represents the Root API method handler set.
|
2019-05-18 18:06:10 -04:00
|
|
|
type Root struct {
|
2019-07-31 18:34:27 -08:00
|
|
|
MasterDB *sqlx.DB
|
|
|
|
Renderer web.Renderer
|
|
|
|
ProjectRoutes project_routes.ProjectRoutes
|
2019-05-18 18:06:10 -04:00
|
|
|
}
|
|
|
|
|
2019-07-31 18:34:27 -08:00
|
|
|
// Index determines if the user has authentication and loads the associated page.
|
|
|
|
func (h *Root) Index(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
|
2019-07-31 13:47:30 -08:00
|
|
|
|
2019-07-31 18:34:27 -08:00
|
|
|
if claims, err := auth.ClaimsFromContext(ctx); err == nil && claims.HasAuth() {
|
|
|
|
return h.indexDashboard(ctx, w, r, params)
|
2019-07-31 13:47:30 -08:00
|
|
|
}
|
|
|
|
|
2019-07-31 18:34:27 -08:00
|
|
|
return h.indexDefault(ctx, w, r, params)
|
|
|
|
}
|
|
|
|
|
|
|
|
// indexDashboard loads the dashboard for a user when they are authenticated.
|
|
|
|
func (h *Root) indexDashboard(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
|
2019-05-20 22:16:58 -05:00
|
|
|
data := map[string]interface{}{
|
|
|
|
"imgSizes": []int{100, 200, 300, 400, 500},
|
|
|
|
}
|
|
|
|
|
2019-08-04 14:48:43 -08:00
|
|
|
return h.Renderer.Render(ctx, w, r, TmplLayoutBase, "root-dashboard.gohtml", web.MIMETextHTMLCharsetUTF8, http.StatusOK, data)
|
2019-07-31 18:34:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// indexDefault loads the root index page when a user has no authentication.
|
|
|
|
func (u *Root) indexDefault(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
|
2019-08-03 15:02:00 -08:00
|
|
|
|
|
|
|
return u.Renderer.Render(ctx, w, r, tmplLayoutSite, "site-index.gohtml", web.MIMETextHTMLCharsetUTF8, http.StatusOK, nil)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// indexDefault loads the root index page when a user has no authentication.
|
|
|
|
func (u *Root) SitePage(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
|
|
|
|
|
|
|
|
var tmpName string
|
|
|
|
switch r.RequestURI {
|
2019-08-04 14:48:43 -08:00
|
|
|
case "/":
|
|
|
|
tmpName = "site-index.gohtml"
|
|
|
|
case "/api":
|
|
|
|
tmpName = "site-api.gohtml"
|
2019-08-04 21:30:26 -08:00
|
|
|
case "/pricing":
|
|
|
|
tmpName = "site-pricing.gohtml"
|
2019-08-04 14:48:43 -08:00
|
|
|
case "/support":
|
|
|
|
tmpName = "site-support.gohtml"
|
|
|
|
case "/legal/privacy":
|
|
|
|
tmpName = "legal-privacy.gohtml"
|
|
|
|
case "/legal/terms":
|
|
|
|
tmpName = "legal-terms.gohtml"
|
|
|
|
default:
|
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
|
|
|
return nil
|
2019-08-03 15:02:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return u.Renderer.Render(ctx, w, r, tmplLayoutSite, tmpName, web.MIMETextHTMLCharsetUTF8, http.StatusOK, nil)
|
|
|
|
|
2019-07-31 18:34:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// IndexHtml redirects /index.html to the website root page.
|
|
|
|
func (u *Root) IndexHtml(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
|
|
|
|
http.Redirect(w, r, "/", http.StatusMovedPermanently)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RobotHandler returns a robots.txt response.
|
|
|
|
func (h *Root) RobotTxt(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
|
|
|
|
if webcontext.ContextEnv(ctx) != webcontext.Env_Prod {
|
|
|
|
txt := "User-agent: *\nDisallow: /"
|
|
|
|
return web.RespondText(ctx, w, txt, http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
sitemapUrl := h.ProjectRoutes.WebAppUrl("/sitemap.xml")
|
|
|
|
|
|
|
|
txt := fmt.Sprintf("User-agent: *\nDisallow: /ping\nDisallow: /status\nDisallow: /debug/\nSitemap: %s", sitemapUrl)
|
|
|
|
return web.RespondText(ctx, w, txt, http.StatusOK)
|
2019-05-18 18:06:10 -04:00
|
|
|
}
|