sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
warehost/cmd/warehost-web/handler.go

120 lines
2.9 KiB
Go

package main
import (
"errors"
"fmt"
"net/http"
"os"
"text/template"
// "github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday"
liblog "dev.sum7.eu/sum7/warehost/lib/log"
web "dev.sum7.eu/sum7/warehost/modul/web"
)
func getWebsite(host string) (*web.Website, error) {
var err error
website := &web.Website{}
dbconnection.Model(website).Joins("JOIN web_domain ON web_domain.website = web_website.id").Where("web_domain.name = ?", host).First(website)
if website.ID <= 0 {
err = errors.New("No Website found")
}
return website, err
}
func handlerstatic(w http.ResponseWriter, r *http.Request) {
logger := liblog.NewModulLog(r.Host).GetLog(r, r.URL.Path)
website, err := getWebsite(r.Host)
if err != nil {
logger.Warn("website not found: ", r.Host)
http.NotFound(w, r)
return
}
path := fmt.Sprintf("%s/%d/%s/%s", config.Webroot, website.ID, "static", r.URL.Path)
if f, err := os.Stat(path); err == nil && !f.IsDir() {
logger.Info("done")
http.ServeFile(w, r, path)
return
}
logger.Warn("file not found")
http.NotFound(w, r)
}
func handlerfiles(w http.ResponseWriter, r *http.Request) {
logger := liblog.NewModulLog(r.Host).GetLog(r, r.URL.Path)
website, err := getWebsite(r.Host)
if err != nil {
logger.Warn("website not found: ", r.Host)
http.NotFound(w, r)
return
}
path := fmt.Sprintf("%s/%d/%s/%s", config.Webroot, website.ID, "files", r.URL.Path)
if f, err := os.Stat(path); err == nil && !f.IsDir() {
logger.Info("done")
http.ServeFile(w, r, path)
return
}
logger.Warn("file not found")
http.NotFound(w, r)
}
func handlerfunc(w http.ResponseWriter, r *http.Request) {
url := web.FixPath(r.URL.Path[1:])
logger := liblog.NewModulLog(r.Host).GetLog(r, url)
website, err := getWebsite(r.Host)
if err != nil {
logger.Warn("website not found: ", r.Host)
http.NotFound(w, r)
return
}
logger = logger.WithField("hID", website.ID)
var menus []*web.Menu
dbconnection.Where("website = ?", website.ID).Preload("URL").Order("position").Find(&menus)
var urls []*web.URL
dbconnection.Where("website = ?", website.ID).Find(&urls)
var urlObj *web.URL
for _, item := range urls {
if item.Path == "" && urlObj == nil {
urlObj = item
}
if item.Path == url {
urlObj = item
}
}
menus = web.BuildMenuTree(menus)
page := web.Page{
WebsiteID: website.ID,
URLID: urlObj.ID,
}
dbconnection.Where(page).FirstOrInit(&page)
if page.ID > 0 {
unsafe := blackfriday.MarkdownCommon([]byte(page.Content))
//page.Content = string(bluemonday.UGCPolicy().SanitizeBytes(unsafe))
page.Content = string(unsafe)
page.URL = urlObj
}
i := TemplateInfo{
Website: website,
Host: r.Host,
URL: url,
Menu: menus,
Page: &page,
}
t, err := template.ParseGlob(fmt.Sprintf("%s/%d/%s/%s", config.Webroot, website.ID, "tmpl", "*.tmpl"))
logger.Info("done")
if err != nil {
w.Write([]byte(fmt.Sprintf("<h1>Error on rendering Template</h1>\n%s", err)))
} else {
t.Execute(w, i)
}
}