package main import ( "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" ) //ProxyHost if this server is behind a proxy const ProxyHost = "X-Real-Host" func getWebsite(host string) *web.Website { website := &web.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 := liblog.NewModulLog(host).GetLog(r, url) website := getWebsite(host) 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 } } menus = web.BuildMenuTree(menus) page := &web.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("

Error on rendering Template

\n%s", err))) } else { t.Execute(w, i) } }