refactoring. remove global state, use new logging, config functions
This commit is contained in:
@@ -1,18 +0,0 @@
|
||||
package webcontroller
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"fornaxian.com/pixeldrain-web/log"
|
||||
"fornaxian.com/pixeldrain-web/webcontroller/templates"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
// ServeAPIDoc serves the API docuementation
|
||||
func ServeAPIDoc(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
err := templates.Get().ExecuteTemplate(w, "apidoc", nil)
|
||||
if err != nil {
|
||||
log.Error("Error executing template apidoc: %s", err)
|
||||
}
|
||||
}
|
@@ -1,14 +0,0 @@
|
||||
package webcontroller
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
|
||||
"fornaxian.com/pixeldrain-web/conf"
|
||||
)
|
||||
|
||||
// ServeFavicon yes we need a controller for this
|
||||
func ServeFavicon(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
http.ServeFile(w, r, conf.StaticResourceDir()+"favicon.ico")
|
||||
}
|
@@ -10,7 +10,6 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"fornaxian.com/pixeldrain-web/conf"
|
||||
"fornaxian.com/pixeldrain-web/pixelapi"
|
||||
"github.com/Fornaxian/log"
|
||||
|
||||
@@ -19,19 +18,22 @@ import (
|
||||
)
|
||||
|
||||
// ServeFilePreview controller for GET /u/:id/preview
|
||||
func ServeFilePreview(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
func (wc *WebController) serveFilePreview(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
if p.ByName("id") == "demo" {
|
||||
ServeFilePreviewDemo(w) // Required for a-ads.com quality check
|
||||
serveFilePreviewDemo(w) // Required for a-ads.com quality check
|
||||
return
|
||||
}
|
||||
|
||||
inf := pixelapi.GetFileInfo(p.ByName("id")) // TODO: Error handling
|
||||
inf := wc.api.GetFileInfo(p.ByName("id")) // TODO: Error handling
|
||||
if inf == nil {
|
||||
ServeNotFound(w, r)
|
||||
wc.serveNotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
var fp FilePreview
|
||||
var fp = FilePreview{
|
||||
APIURL: wc.conf.APIURLExternal,
|
||||
PixelAPI: wc.api,
|
||||
}
|
||||
io.WriteString(w, fp.Run(inf))
|
||||
}
|
||||
|
||||
@@ -39,12 +41,15 @@ type FilePreview struct {
|
||||
FileInfo *pixelapi.FileInfo
|
||||
FileURL string
|
||||
DownloadURL string
|
||||
|
||||
APIURL string
|
||||
PixelAPI *pixelapi.PixelAPI
|
||||
}
|
||||
|
||||
func (f FilePreview) Run(inf *pixelapi.FileInfo) string {
|
||||
f.FileInfo = inf
|
||||
f.FileURL = conf.ApiUrlExternal() + "/file/" + f.FileInfo.ID
|
||||
f.DownloadURL = conf.ApiUrlExternal() + "/file/" + f.FileInfo.ID + "/download"
|
||||
f.FileURL = f.APIURL + "/file/" + f.FileInfo.ID
|
||||
f.DownloadURL = f.APIURL + "/file/" + f.FileInfo.ID + "/download"
|
||||
|
||||
if strings.HasPrefix(f.FileInfo.MimeType, "image") {
|
||||
return f.image()
|
||||
@@ -139,7 +144,7 @@ func (f FilePreview) text() string {
|
||||
)
|
||||
}
|
||||
|
||||
body, err := pixelapi.GetFile(f.FileInfo.ID)
|
||||
body, err := f.PixelAPI.GetFile(f.FileInfo.ID)
|
||||
if err != nil {
|
||||
log.Error("Can't download text file for preview: %s", err)
|
||||
return fmt.Sprintf(htmlOut, "",
|
||||
@@ -189,6 +194,29 @@ func (f FilePreview) def() string {
|
||||
f.FileInfo.FileName,
|
||||
f.FileInfo.MimeType,
|
||||
f.DownloadURL,
|
||||
conf.ApiUrlExternal()+f.FileInfo.ThumbnailHREF,
|
||||
f.APIURL+f.FileInfo.ThumbnailHREF,
|
||||
)
|
||||
}
|
||||
|
||||
// ServeFilePreviewDemo serves the content of the demo file. It contains a nice
|
||||
// message to the human reviewers of the a-ads ad network so they can properly
|
||||
// categorize the website.
|
||||
func serveFilePreviewDemo(w http.ResponseWriter) {
|
||||
io.WriteString(w,
|
||||
`<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js?skin=desert"></script>
|
||||
<div class="text-container"><pre class="pre-container linenums" style="width: 100%">
|
||||
, __ _
|
||||
/|/ \o | | | o
|
||||
|___/ _ | | __| ,_ __, _ _
|
||||
| | /\/ |/ |/ / | / | / | | / |/ |
|
||||
| |_/ /\_/|__/|__/\_/|_/ |_/\_/|_/|_/ | |_/
|
||||
|
||||
This is a demonstration of Pixeldrain's file viewer.
|
||||
|
||||
The website automatically detects what kind of file you requested and prepares a page for viewing it properly. This is what a text file would look like on Pixeldrain. You can upload your own text file at pixeldrain.com/t.
|
||||
|
||||
Pixeldrain is a free service for sharing files with large or small groups of people. For more information visit the home page by pressing the home button on the toolbar at the left side of the screen.
|
||||
|
||||
Press the Details button or "i" for more info about Pixeldrain's file viewer.
|
||||
</pre></div>`)
|
||||
}
|
||||
|
@@ -5,16 +5,15 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"fornaxian.com/pixeldrain-web/log"
|
||||
"fornaxian.com/pixeldrain-web/pixelapi"
|
||||
"fornaxian.com/pixeldrain-web/webcontroller/templates"
|
||||
"github.com/Fornaxian/log"
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
// ServeFileViewer controller for GET /u/:id
|
||||
func ServeFileViewer(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
func (wc *WebController) serveFileViewer(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
if p.ByName("id") == "demo" {
|
||||
ServeFileViewerDemo(w) // Required for a-ads.com quality check
|
||||
wc.serveFileViewerDemo(w) // Required for a-ads.com quality check
|
||||
return
|
||||
}
|
||||
|
||||
@@ -29,7 +28,7 @@ func ServeFileViewer(w http.ResponseWriter, r *http.Request, p httprouter.Params
|
||||
|
||||
var finfo []*pixelapi.FileInfo
|
||||
for _, id := range ids {
|
||||
inf := pixelapi.GetFileInfo(id)
|
||||
inf := wc.api.GetFileInfo(id)
|
||||
if inf == nil {
|
||||
continue
|
||||
}
|
||||
@@ -37,7 +36,7 @@ func ServeFileViewer(w http.ResponseWriter, r *http.Request, p httprouter.Params
|
||||
}
|
||||
|
||||
if len(finfo) == 0 {
|
||||
ServeNotFound(w, r)
|
||||
wc.serveNotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -51,14 +50,14 @@ func ServeFileViewer(w http.ResponseWriter, r *http.Request, p httprouter.Params
|
||||
"date_lastview": "now",
|
||||
"views": 0,
|
||||
}
|
||||
err = templates.Get().ExecuteTemplate(w, "file_viewer", map[string]interface{}{
|
||||
err = wc.templates.Get().ExecuteTemplate(w, "file_viewer", map[string]interface{}{
|
||||
"Title": fmt.Sprintf("%d files in Pixeldrain", len(finfo)),
|
||||
"APIResponse": listdata,
|
||||
"Type": "list",
|
||||
"OGData": ogData.FromFile(*finfo[0]),
|
||||
})
|
||||
} else {
|
||||
err = templates.Get().ExecuteTemplate(w, "file_viewer", map[string]interface{}{
|
||||
err = wc.templates.Get().ExecuteTemplate(w, "file_viewer", map[string]interface{}{
|
||||
"Title": fmt.Sprintf("%s ~ Pixeldrain file", finfo[0].FileName),
|
||||
"APIResponse": finfo[0],
|
||||
"Type": "file",
|
||||
@@ -69,3 +68,24 @@ func ServeFileViewer(w http.ResponseWriter, r *http.Request, p httprouter.Params
|
||||
log.Error("Error executing template file_viewer: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// ServeFileViewerDemo is a dummy API response that responds with info about a
|
||||
// non-existent demo file. This is required by the a-ads ad network to allow for
|
||||
// automatic checking of the presence of the ad unit on this page.
|
||||
func (wc *WebController) serveFileViewerDemo(w http.ResponseWriter) {
|
||||
wc.templates.Get().ExecuteTemplate(w, "file_viewer", map[string]interface{}{
|
||||
"APIResponse": map[string]interface{}{
|
||||
"id": "demo",
|
||||
"file_name": "Demo file",
|
||||
"date_upload": "2017-01-01 12:34:56",
|
||||
"date_lastview": "2017-01-01 12:34:56",
|
||||
"file_size": 123456789,
|
||||
"views": 1,
|
||||
"mime_type": "text/demo",
|
||||
"description": "A file to demonstrate the viewer page",
|
||||
"mime_image": "/res/img/mime/text.png",
|
||||
"thumbnail": "/res/img/mime/text.png",
|
||||
},
|
||||
"Type": "file",
|
||||
})
|
||||
}
|
||||
|
@@ -1,52 +0,0 @@
|
||||
package webcontroller
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"fornaxian.com/pixeldrain-web/webcontroller/templates"
|
||||
)
|
||||
|
||||
// ServeFileViewerDemo is a dummy API response that responds with info about a
|
||||
// non-existent demo file. This is required by the a-ads ad network to allow for
|
||||
// automatic checking of the presence of the ad unit on this page.
|
||||
func ServeFileViewerDemo(w http.ResponseWriter) {
|
||||
templates.Get().ExecuteTemplate(w, "file_viewer", map[string]interface{}{
|
||||
"APIResponse": map[string]interface{}{
|
||||
"id": "demo",
|
||||
"file_name": "Demo file",
|
||||
"date_upload": "2017-01-01 12:34:56",
|
||||
"date_lastview": "2017-01-01 12:34:56",
|
||||
"file_size": 123456789,
|
||||
"views": 1,
|
||||
"mime_type": "text/demo",
|
||||
"description": "A file to demonstrate the viewer page",
|
||||
"mime_image": "/res/img/mime/text.png",
|
||||
"thumbnail": "/res/img/mime/text.png",
|
||||
},
|
||||
"Type": "file",
|
||||
})
|
||||
}
|
||||
|
||||
// ServeFilePreviewDemo serves the content of the demo file. It contains a nice
|
||||
// message to the human reviewers of the a-ads ad network so they can properly
|
||||
// categorize the website.
|
||||
func ServeFilePreviewDemo(w http.ResponseWriter) {
|
||||
io.WriteString(w,
|
||||
`<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js?skin=desert"></script>
|
||||
<div class="text-container"><pre class="pre-container linenums" style="width: 100%">
|
||||
, __ _
|
||||
/|/ \o | | | o
|
||||
|___/ _ | | __| ,_ __, _ _
|
||||
| | /\/ |/ |/ / | / | / | | / |/ |
|
||||
| |_/ /\_/|__/|__/\_/|_/ |_/\_/|_/|_/ | |_/
|
||||
|
||||
This is a demonstration of Pixeldrain's file viewer.
|
||||
|
||||
The website automatically detects what kind of file you requested and prepares a page for viewing it properly. This is what a text file would look like on Pixeldrain. You can upload your own text file at pixeldrain.com/t.
|
||||
|
||||
Pixeldrain is a free service for sharing files with large or small groups of people. For more information visit the home page by pressing the home button on the toolbar at the left side of the screen.
|
||||
|
||||
Press the Details button or "i" for more info about Pixeldrain's file viewer.
|
||||
</pre></div>`)
|
||||
}
|
@@ -1,17 +0,0 @@
|
||||
package webcontroller
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"fornaxian.com/pixeldrain-web/log"
|
||||
"fornaxian.com/pixeldrain-web/webcontroller/templates"
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
// ServeHistory is the controller for the upload history viewer
|
||||
func ServeHistory(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
err := templates.Get().ExecuteTemplate(w, "history-cookies", nil)
|
||||
if err != nil {
|
||||
log.Error("Error executing template history: %s", err)
|
||||
}
|
||||
}
|
@@ -1,14 +0,0 @@
|
||||
package webcontroller
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"fornaxian.com/pixeldrain-web/webcontroller/templates"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
// ServeHome serves the home page
|
||||
func ServeHome(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
templates.Get().ExecuteTemplate(w, "home", nil)
|
||||
}
|
@@ -4,20 +4,18 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"fornaxian.com/pixeldrain-web/log"
|
||||
"fornaxian.com/pixeldrain-web/pixelapi"
|
||||
"fornaxian.com/pixeldrain-web/webcontroller/templates"
|
||||
"github.com/Fornaxian/log"
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
// ServeListViewer controller for GET /l/:id
|
||||
func ServeListViewer(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
var list = pixelapi.GetList(p.ByName("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)
|
||||
}
|
||||
ServeNotFound(w, r)
|
||||
wc.serveNotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -30,7 +28,7 @@ func ServeListViewer(w http.ResponseWriter, r *http.Request, p httprouter.Params
|
||||
"title": list.Title,
|
||||
"views": 0,
|
||||
}
|
||||
err = templates.Get().ExecuteTemplate(w, "file_viewer", map[string]interface{}{
|
||||
err = wc.templates.Get().ExecuteTemplate(w, "file_viewer", map[string]interface{}{
|
||||
"Title": fmt.Sprintf("%s ~ Pixeldrain list", list.Title),
|
||||
"APIResponse": listdata,
|
||||
"Type": "list",
|
||||
|
@@ -1,13 +0,0 @@
|
||||
package webcontroller
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"fornaxian.com/pixeldrain-web/log"
|
||||
"fornaxian.com/pixeldrain-web/webcontroller/templates"
|
||||
)
|
||||
|
||||
func ServeNotFound(w http.ResponseWriter, r *http.Request) {
|
||||
log.Debug("Not Found: %s", r.URL)
|
||||
templates.Get().ExecuteTemplate(w, "error", nil)
|
||||
}
|
@@ -1,14 +0,0 @@
|
||||
package webcontroller
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"fornaxian.com/pixeldrain-web/webcontroller/templates"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
// ServePaste serves the page that is used to upload plain text files
|
||||
func ServePaste(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
templates.Get().ExecuteTemplate(w, "paste", nil)
|
||||
}
|
@@ -8,7 +8,7 @@ import (
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
func GlobalCSSHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||
func (wc *WebController) globalCSSHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||
w.Header().Add("Content-Type", "text/css; charset=utf-8")
|
||||
|
||||
var textColor = "hsl(0, 0%, 75%)"
|
||||
|
@@ -3,24 +3,24 @@ package templates
|
||||
import (
|
||||
"html/template"
|
||||
"time"
|
||||
|
||||
"fornaxian.com/pixeldrain-web/conf"
|
||||
)
|
||||
|
||||
var funcMap = template.FuncMap{
|
||||
"bgPatternCount": bgPatternCount,
|
||||
"debugMode": debugMode,
|
||||
"apiUrl": apiURL,
|
||||
func (tm *TemplateManager) funcMap() template.FuncMap {
|
||||
return template.FuncMap{
|
||||
"bgPatternCount": tm.bgPatternCount,
|
||||
"debugMode": tm.debugMode,
|
||||
"apiUrl": tm.apiURL,
|
||||
}
|
||||
}
|
||||
|
||||
func bgPatternCount() uint8 {
|
||||
func (tm *TemplateManager) bgPatternCount() uint8 {
|
||||
return uint8(time.Now().UnixNano() % 17)
|
||||
}
|
||||
|
||||
func debugMode() bool {
|
||||
return conf.DebugMode()
|
||||
func (tm *TemplateManager) debugMode() bool {
|
||||
return tm.debugModeEnabled
|
||||
}
|
||||
|
||||
func apiURL() string {
|
||||
return conf.ApiUrlExternal()
|
||||
func (tm *TemplateManager) apiURL() string {
|
||||
return tm.externalAPIEndpoint
|
||||
}
|
||||
|
61
webcontroller/templates/manager.go
Normal file
61
webcontroller/templates/manager.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package templates
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/Fornaxian/log"
|
||||
)
|
||||
|
||||
type TemplateManager struct {
|
||||
templates *template.Template
|
||||
|
||||
// Config
|
||||
templateDir string
|
||||
externalAPIEndpoint string
|
||||
debugModeEnabled bool
|
||||
}
|
||||
|
||||
func New(templateDir, externalAPIEndpoint string, debugMode bool) *TemplateManager {
|
||||
return &TemplateManager{
|
||||
templateDir: templateDir,
|
||||
externalAPIEndpoint: externalAPIEndpoint,
|
||||
debugModeEnabled: debugMode,
|
||||
}
|
||||
}
|
||||
|
||||
// ParseTemplates parses the templates in the template directory which is
|
||||
// defined in the config file
|
||||
func (tm *TemplateManager) ParseTemplates() {
|
||||
var templatePaths []string
|
||||
|
||||
filepath.Walk(tm.templateDir, func(path string, f os.FileInfo, err error) error {
|
||||
if f.IsDir() {
|
||||
return nil
|
||||
}
|
||||
templatePaths = append(templatePaths, path)
|
||||
log.Info("Template found: %s", path)
|
||||
return nil
|
||||
})
|
||||
|
||||
tpl := template.New("")
|
||||
|
||||
// Import template functions from funcs.go
|
||||
tpl = tpl.Funcs(tm.funcMap())
|
||||
|
||||
var err error
|
||||
tpl, err = tpl.ParseFiles(templatePaths...)
|
||||
if err != nil {
|
||||
log.Error("Template parsing failed: %v", err)
|
||||
}
|
||||
|
||||
// Swap out the old templates with the new templates, to minimize
|
||||
// modifications to the original variable.
|
||||
tm.templates = tpl
|
||||
}
|
||||
|
||||
// Get returns the templates, so they can be used to render views
|
||||
func (tm *TemplateManager) Get() *template.Template {
|
||||
return tm.templates
|
||||
}
|
@@ -1,46 +0,0 @@
|
||||
package templates
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"fornaxian.com/pixeldrain-web/conf"
|
||||
"fornaxian.com/pixeldrain-web/log"
|
||||
)
|
||||
|
||||
var templates *template.Template
|
||||
var templatePaths []string
|
||||
|
||||
// ParseTemplates parses the templates in the template directory which is
|
||||
// defined in the config file
|
||||
func ParseTemplates() {
|
||||
filepath.Walk(conf.TemplateDir(), func(path string, f os.FileInfo, err error) error {
|
||||
if f.IsDir() {
|
||||
return nil
|
||||
}
|
||||
templatePaths = append(templatePaths, path)
|
||||
log.Info("Template found: %s", path)
|
||||
return nil
|
||||
})
|
||||
|
||||
tpl := template.New("")
|
||||
|
||||
// Import template functions from funcs.go
|
||||
tpl = tpl.Funcs(funcMap)
|
||||
|
||||
var err error
|
||||
tpl, err = tpl.ParseFiles(templatePaths...)
|
||||
if err != nil {
|
||||
log.Error("Template parsing failed: %v", err)
|
||||
}
|
||||
|
||||
// Swap out the old templates with the new templates, to minimize
|
||||
// modifications to the original variable.
|
||||
templates = tpl
|
||||
}
|
||||
|
||||
// Get returns the templates, so they can be used to render views
|
||||
func Get() *template.Template {
|
||||
return templates
|
||||
}
|
81
webcontroller/webcontroller.go
Normal file
81
webcontroller/webcontroller.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package webcontroller
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"fornaxian.com/pixeldrain-web/init/conf"
|
||||
"fornaxian.com/pixeldrain-web/pixelapi"
|
||||
"fornaxian.com/pixeldrain-web/webcontroller/templates"
|
||||
"github.com/Fornaxian/log"
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
type WebController struct {
|
||||
conf *conf.PixelWebConfig
|
||||
api *pixelapi.PixelAPI
|
||||
templates *templates.TemplateManager
|
||||
staticResourceDir string
|
||||
}
|
||||
|
||||
func New(r *httprouter.Router, prefix string, conf *conf.PixelWebConfig) *WebController {
|
||||
var wc = &WebController{
|
||||
conf: conf,
|
||||
staticResourceDir: conf.StaticResourceDir,
|
||||
}
|
||||
wc.api = pixelapi.New(conf.APIURLInternal)
|
||||
wc.templates = templates.New(
|
||||
conf.TemplateDir,
|
||||
conf.APIURLExternal,
|
||||
conf.DebugMode,
|
||||
)
|
||||
wc.templates.ParseTemplates()
|
||||
|
||||
// Serve static files
|
||||
r.ServeFiles(prefix+"/res/*filepath", http.Dir(wc.staticResourceDir+"/res"))
|
||||
|
||||
r.GET(prefix+"/" /* */, wc.serveTemplate("home"))
|
||||
r.GET(prefix+"/favicon.ico" /* */, wc.serveFile("/favicon.ico"))
|
||||
r.GET(prefix+"/global.css" /* */, wc.globalCSSHandler)
|
||||
r.GET(prefix+"/api" /* */, wc.serveTemplate("apidoc"))
|
||||
r.GET(prefix+"/history" /* */, wc.serveTemplate("history-cookies"))
|
||||
r.GET(prefix+"/u/:id" /* */, wc.serveFileViewer)
|
||||
r.GET(prefix+"/u/:id/preview" /**/, wc.serveFilePreview)
|
||||
r.GET(prefix+"/l/:id" /* */, wc.serveListViewer)
|
||||
r.GET(prefix+"/t" /* */, wc.serveTemplate("paste"))
|
||||
|
||||
r.NotFound = http.HandlerFunc(wc.serveNotFound)
|
||||
|
||||
return wc
|
||||
}
|
||||
|
||||
func (wc *WebController) ReloadTemplates() {
|
||||
wc.templates.ParseTemplates()
|
||||
}
|
||||
|
||||
func (wc *WebController) serveTemplate(tpl string) httprouter.Handle {
|
||||
return func(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
p httprouter.Params,
|
||||
) {
|
||||
err := wc.templates.Get().ExecuteTemplate(w, tpl, nil)
|
||||
if err != nil {
|
||||
log.Error("Error executing template '%s': %s", tpl, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (wc *WebController) serveFile(path string) httprouter.Handle {
|
||||
return func(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
p httprouter.Params,
|
||||
) {
|
||||
http.ServeFile(w, r, wc.staticResourceDir+"/favicon.ico")
|
||||
}
|
||||
}
|
||||
|
||||
func (wc *WebController) serveNotFound(w http.ResponseWriter, r *http.Request) {
|
||||
log.Debug("Not Found: %s", r.URL)
|
||||
wc.templates.Get().ExecuteTemplate(w, "error", nil)
|
||||
}
|
Reference in New Issue
Block a user