sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0

better logging and fix host

This commit is contained in:
Martin Geno 2016-12-22 23:16:41 +01:00
parent 449960098a
commit 166e8791bb
2 changed files with 47 additions and 30 deletions

View File

@ -1,6 +1,7 @@
package main
import (
"errors"
"fmt"
"net/http"
"os"
@ -13,54 +14,60 @@ import (
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 {
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)
return website
if website.ID <= 0 {
err = errors.New("No Website found")
}
return website, err
}
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)
logger := liblog.NewModulLog(r.Host).GetLog(r, r.URL.Path)
website, err := getWebsite(r.Host)
if err != nil {
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.Warn("website not found: ", r.Host)
http.ServeFile(w, r, path)
return
}
logger.Warn("file not found")
http.NotFound(w, r)
}
func handlerfiles(w http.ResponseWriter, r *http.Request) {
host := r.Header.Get(ProxyHost)
if len(host) <= 1 {
host = r.Host
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
}
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
}
logger.Warn("file not found")
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 := 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
@ -74,9 +81,19 @@ func handlerfunc(w http.ResponseWriter, r *http.Request) {
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)
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))
@ -84,9 +101,9 @@ func handlerfunc(w http.ResponseWriter, r *http.Request) {
page.Content = string(unsafe)
i := TemplateInfo{
Website: website,
Host: host,
Host: r.Host,
URL: url,
Page: page,
Page: &page,
Menu: menus,
}
t, err := template.ParseGlob(fmt.Sprintf("%s/%d/%s/%s", config.Webroot, website.ID, "tmpl", "*.tmpl"))

View File

@ -47,7 +47,7 @@ func NewModulLog(modul string) *ModulLog {
// GetLog with api request ip in log
func (m *ModulLog) GetLog(r *http.Request, request string) *log.Entry {
ip := r.Header.Get("X-Real-IP")
ip := r.Header.Get("X-Forwarded-For")
if len(ip) <= 1 {
ip = r.RemoteAddr
}