119 lines
3.0 KiB
Go
119 lines
3.0 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 := 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("Menu").Order("position").Find(&menus)
|
|
var menu *web.Menu
|
|
for _, item := range menus {
|
|
if item.Shorturl == "" && menu == nil {
|
|
menu = item
|
|
}
|
|
if item.Shorturl == url {
|
|
menu = item
|
|
}
|
|
}
|
|
if menu.ID <= 0 {
|
|
logger.Warn("menu not found")
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
menus = web.BuildMenuTree(menus)
|
|
page := web.Page{WebsiteID: website.ID, MenuID: menu.ID}
|
|
dbconnection.Where(&page).Find(&page)
|
|
if page.ID <= 0 {
|
|
logger.Warn("page not found")
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
page.Menu = menu
|
|
|
|
unsafe := blackfriday.MarkdownCommon([]byte(page.Content))
|
|
//page.Content = string(bluemonday.UGCPolicy().SanitizeBytes(unsafe))
|
|
page.Content = string(unsafe)
|
|
i := TemplateInfo{
|
|
Website: website,
|
|
Host: r.Host,
|
|
URL: url,
|
|
Page: &page,
|
|
Menu: menus,
|
|
}
|
|
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)
|
|
}
|
|
}
|