2016-08-27 02:18:41 +02:00
|
|
|
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)
|
|
|
|
|
2016-09-01 22:19:39 +02:00
|
|
|
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)
|
|
|
|
|
2016-08-27 02:18:41 +02:00
|
|
|
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)
|
|
|
|
}
|
2016-09-01 22:19:39 +02:00
|
|
|
|
2016-08-27 02:18:41 +02:00
|
|
|
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
|
2016-09-12 20:34:09 +02:00
|
|
|
dbconnection.Where("website = ?", website.ID).Preload("Menu").Find(&menus)
|
2016-08-27 02:18:41 +02:00
|
|
|
var menu *modul.Menu
|
|
|
|
for _, item := range menus {
|
|
|
|
if item.Shorturl == "" && menu == nil {
|
|
|
|
menu = item
|
|
|
|
}
|
|
|
|
if item.Shorturl == url {
|
|
|
|
menu = item
|
|
|
|
}
|
|
|
|
}
|
2016-09-12 20:34:09 +02:00
|
|
|
menus = modul.BuildMenuTree(menus)
|
2016-08-27 02:18:41 +02:00
|
|
|
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{
|
2016-09-12 20:34:09 +02:00
|
|
|
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)
|
2016-08-27 02:18:41 +02:00
|
|
|
}
|
|
|
|
}
|