98 lines
2.4 KiB
Go
98 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"text/template"
|
|
|
|
"github.com/microcosm-cc/bluemonday"
|
|
"github.com/russross/blackfriday"
|
|
|
|
log "dev.sum7.de/sum7/warehost/lib/log"
|
|
modul "dev.sum7.de/sum7/warehost/modul/web"
|
|
)
|
|
|
|
//ProxyHost if this server is behind a proxy
|
|
const ProxyHost = "X-Real-Host"
|
|
|
|
func getWebsite(host string) *modul.Website {
|
|
website := &modul.Website{}
|
|
dbconnection.Model(website).Joins("JOIN web_domain ON web_domain.website = web_website.id").Where("web_domain.name = ?", host).First(website)
|
|
return website
|
|
}
|
|
|
|
func handlerstatic(w http.ResponseWriter, r *http.Request) {
|
|
host := r.Header.Get(ProxyHost)
|
|
if len(host) <= 1 {
|
|
host = r.Host
|
|
}
|
|
website := getWebsite(host)
|
|
|
|
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() {
|
|
http.ServeFile(w, r, path)
|
|
return
|
|
}
|
|
|
|
http.NotFound(w, r)
|
|
}
|
|
func handlerfiles(w http.ResponseWriter, r *http.Request) {
|
|
host := r.Header.Get(ProxyHost)
|
|
if len(host) <= 1 {
|
|
host = r.Host
|
|
}
|
|
website := getWebsite(host)
|
|
|
|
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() {
|
|
http.ServeFile(w, r, path)
|
|
return
|
|
}
|
|
|
|
http.NotFound(w, r)
|
|
}
|
|
|
|
func handlerfunc(w http.ResponseWriter, r *http.Request) {
|
|
host := r.Header.Get(ProxyHost)
|
|
if len(host) <= 1 {
|
|
host = r.Host
|
|
}
|
|
url := r.URL.Path[1:]
|
|
logger := log.NewModulLog(host).GetLog(r, url)
|
|
website := getWebsite(host)
|
|
logger = logger.WithField("hID", website.ID)
|
|
|
|
var menus []*modul.Menu
|
|
dbconnection.Where("website = ?", website.ID).Preload("Menu").Find(&menus)
|
|
var menu *modul.Menu
|
|
for _, item := range menus {
|
|
if item.Shorturl == "" && menu == nil {
|
|
menu = item
|
|
}
|
|
if item.Shorturl == url {
|
|
menu = item
|
|
}
|
|
}
|
|
menus = modul.BuildMenuTree(menus)
|
|
page := &modul.Page{WebsiteID: website.ID, MenuID: menu.ID}
|
|
dbconnection.Where(page).Find(page)
|
|
|
|
unsafe := blackfriday.MarkdownCommon([]byte(page.Content))
|
|
page.Content = string(bluemonday.UGCPolicy().SanitizeBytes(unsafe))
|
|
i := TemplateInfo{
|
|
Website: website,
|
|
Host: 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)
|
|
}
|
|
}
|