93 lines
2.3 KiB
Go
93 lines
2.3 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 = ? AND menu IS NULL", 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
|
|
}
|
|
}
|
|
page := &modul.Page{WebsiteID: website.ID, MenuID: menu.ID}
|
|
dbconnection.Where(page).Find(page)
|
|
logger.Info(page.ID)
|
|
|
|
unsafe := blackfriday.MarkdownCommon([]byte(page.Content))
|
|
page.Content = string(bluemonday.UGCPolicy().SanitizeBytes(unsafe))
|
|
i := TemplateInfo{
|
|
Host: host,
|
|
URL: url,
|
|
Page: page,
|
|
Menu: menus,
|
|
}
|
|
t, _ := template.ParseGlob(fmt.Sprintf("%s/%d/%s/%s", config.Webroot, website.ID, "tmpl", "*.tmpl"))
|
|
logger.Info("")
|
|
t.Execute(w, i)
|
|
}
|