add login/register forms. Restructure pixelapi

This commit is contained in:
2018-06-20 23:47:47 +02:00
parent 75197310bf
commit ffa9fb3395
18 changed files with 292 additions and 228 deletions

View File

@@ -24,8 +24,8 @@ func (wc *WebController) serveFilePreview(w http.ResponseWriter, r *http.Request
return
}
inf := wc.api.GetFileInfo(p.ByName("id")) // TODO: Error handling
if inf == nil {
inf, err := wc.api.GetFileInfo(p.ByName("id")) // TODO: Error handling
if err != nil {
wc.serveNotFound(w, r)
return
}

View File

@@ -28,8 +28,8 @@ func (wc *WebController) serveFileViewer(w http.ResponseWriter, r *http.Request,
var finfo []*pixelapi.FileInfo
for _, id := range ids {
inf := wc.api.GetFileInfo(id)
if inf == nil {
inf, err := wc.api.GetFileInfo(id)
if err != nil {
continue
}
finfo = append(finfo, inf)

View File

@@ -10,10 +10,10 @@ import (
// ServeListViewer controller for GET /l/:id
func (wc *WebController) serveListViewer(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
var list = wc.api.GetList(p.ByName("id"))
if list.Error != nil {
if list.Error.ReqError {
log.Error("API request error occurred: %s", list.Error.Value)
var list, aerr = wc.api.GetList(p.ByName("id"))
if aerr != nil {
if aerr.ReqError {
log.Error("API request error occurred: %s", aerr.Value)
}
wc.serveNotFound(w, r)
return

View File

@@ -0,0 +1,25 @@
package webcontroller
import (
"net/http"
)
// TemplateData is a struct that every template expects when being rendered. In
// the field Other you can pass your own template-specific variables.
type TemplateData struct {
Authenticated bool
Username string
Recaptcha struct {
Enabled bool
PubKey string
}
Other interface{}
}
func (wc *WebController) newTemplateData(r http.Request) *TemplateData {
var t = &TemplateData{}
return t
}

View File

@@ -26,8 +26,9 @@ func New(templateDir, externalAPIEndpoint string, debugMode bool) *TemplateManag
}
// ParseTemplates parses the templates in the template directory which is
// defined in the config file
func (tm *TemplateManager) ParseTemplates() {
// defined in the config file.
// If silent is false it will print an info log message for every template found
func (tm *TemplateManager) ParseTemplates(silent bool) {
var templatePaths []string
filepath.Walk(tm.templateDir, func(path string, f os.FileInfo, err error) error {
@@ -35,7 +36,9 @@ func (tm *TemplateManager) ParseTemplates() {
return nil
}
templatePaths = append(templatePaths, path)
log.Info("Template found: %s", path)
if !silent {
log.Info("Template found: %s", path)
}
return nil
})
@@ -57,5 +60,8 @@ func (tm *TemplateManager) ParseTemplates() {
// Get returns the templates, so they can be used to render views
func (tm *TemplateManager) Get() *template.Template {
if tm.debugModeEnabled {
tm.ParseTemplates(true)
}
return tm.templates
}

View File

@@ -0,0 +1,15 @@
package webcontroller
import (
"net/http"
"github.com/julienschmidt/httprouter"
)
func (wc *WebController) handleLogin(
w http.ResponseWriter,
r *http.Request,
p httprouter.Params,
) {
}

View File

@@ -28,7 +28,7 @@ func New(r *httprouter.Router, prefix string, conf *conf.PixelWebConfig) *WebCon
conf.APIURLExternal,
conf.DebugMode,
)
wc.templates.ParseTemplates()
wc.templates.ParseTemplates(false)
// Serve static files
r.ServeFiles(prefix+"/res/*filepath", http.Dir(wc.staticResourceDir+"/res"))
@@ -43,13 +43,16 @@ func New(r *httprouter.Router, prefix string, conf *conf.PixelWebConfig) *WebCon
r.GET(prefix+"/l/:id" /* */, wc.serveListViewer)
r.GET(prefix+"/t" /* */, wc.serveTemplate("paste"))
r.GET(prefix+"/register" /* */, wc.serveTemplate("register"))
r.GET(prefix+"/login" /* */, wc.serveTemplate("login"))
r.NotFound = http.HandlerFunc(wc.serveNotFound)
return wc
}
func (wc *WebController) ReloadTemplates() {
wc.templates.ParseTemplates()
wc.templates.ParseTemplates(false)
}
func (wc *WebController) serveTemplate(tpl string) httprouter.Handle {