get recaptcha site key from api

This commit is contained in:
2018-09-19 22:26:52 +02:00
parent 616fe14ba3
commit 5207abd15f
9 changed files with 84 additions and 42 deletions

View File

@@ -1,9 +1,10 @@
package templates
package webcontroller
import (
"html/template"
"os"
"path/filepath"
"time"
"github.com/Fornaxian/log"
)
@@ -17,7 +18,7 @@ type TemplateManager struct {
debugModeEnabled bool
}
func New(templateDir, externalAPIEndpoint string, debugMode bool) *TemplateManager {
func NewTemplateManager(templateDir, externalAPIEndpoint string, debugMode bool) *TemplateManager {
return &TemplateManager{
templateDir: templateDir,
externalAPIEndpoint: externalAPIEndpoint,
@@ -65,3 +66,23 @@ func (tm *TemplateManager) Get() *template.Template {
}
return tm.templates
}
func (tm *TemplateManager) funcMap() template.FuncMap {
return template.FuncMap{
"bgPatternCount": tm.bgPatternCount,
"debugMode": tm.debugMode,
"apiUrl": tm.apiURL,
}
}
func (tm *TemplateManager) bgPatternCount() uint8 {
return uint8(time.Now().UnixNano() % 17)
}
func (tm *TemplateManager) debugMode() bool {
return tm.debugModeEnabled
}
func (tm *TemplateManager) apiURL() string {
return tm.externalAPIEndpoint
}

View File

@@ -1,26 +0,0 @@
package templates
import (
"html/template"
"time"
)
func (tm *TemplateManager) funcMap() template.FuncMap {
return template.FuncMap{
"bgPatternCount": tm.bgPatternCount,
"debugMode": tm.debugMode,
"apiUrl": tm.apiURL,
}
}
func (tm *TemplateManager) bgPatternCount() uint8 {
return uint8(time.Now().UnixNano() % 17)
}
func (tm *TemplateManager) debugMode() bool {
return tm.debugModeEnabled
}
func (tm *TemplateManager) apiURL() string {
return tm.externalAPIEndpoint
}

View File

@@ -8,6 +8,37 @@ import (
"github.com/julienschmidt/httprouter"
)
func (wc *WebController) serveRegister(
w http.ResponseWriter,
r *http.Request,
p httprouter.Params,
) {
var tpld = wc.newTemplateData(w, r)
// This only runs on the first request
if wc.captchaSiteKey == "" {
var api = pixelapi.New(wc.conf.APIURLInternal, "")
capt, err := api.GetRecaptcha()
if err != nil {
log.Error("Error getting recaptcha key: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if capt.SiteKey == "" {
wc.captchaSiteKey = "none"
} else {
wc.captchaSiteKey = capt.SiteKey
}
}
tpld.Other = wc.captchaSiteKey
err := wc.templates.Get().ExecuteTemplate(w, "register", tpld)
if err != nil {
log.Error("Error executing template '%s': %s", "register", err)
}
}
func (wc *WebController) serveLogout(
w http.ResponseWriter,
r *http.Request,

View File

@@ -7,7 +7,6 @@ import (
"github.com/google/uuid"
"fornaxian.com/pixeldrain-web/init/conf"
"fornaxian.com/pixeldrain-web/webcontroller/templates"
"github.com/Fornaxian/log"
"github.com/julienschmidt/httprouter"
)
@@ -16,8 +15,11 @@ import (
// proper context when running
type WebController struct {
conf *conf.PixelWebConfig
templates *templates.TemplateManager
templates *TemplateManager
staticResourceDir string
// page-specific variables
captchaSiteKey string
}
// New initializes a new WebController by registering all the request handlers
@@ -27,7 +29,7 @@ func New(r *httprouter.Router, prefix string, conf *conf.PixelWebConfig) *WebCon
conf: conf,
staticResourceDir: conf.StaticResourceDir,
}
wc.templates = templates.New(
wc.templates = NewTemplateManager(
conf.TemplateDir,
conf.APIURLExternal,
conf.DebugMode,
@@ -49,7 +51,7 @@ func New(r *httprouter.Router, prefix string, conf *conf.PixelWebConfig) *WebCon
r.GET(prefix+"/t" /* */, wc.serveTemplate("paste", false))
// User account pages
r.GET(prefix+"/register" /* */, wc.serveTemplate("register", false))
r.GET(prefix+"/register" /* */, wc.serveRegister)
r.GET(prefix+"/login" /* */, wc.serveTemplate("login", false))
r.GET(prefix+"/logout" /* */, wc.serveTemplate("logout", true))
r.POST(prefix+"/logout" /* */, wc.serveLogout)