From 166e8791bbe16dafc2502bfbe8e4367f1e9159e8 Mon Sep 17 00:00:00 2001 From: Martin Geno Date: Thu, 22 Dec 2016 23:16:41 +0100 Subject: [PATCH] better logging and fix host --- cmd/warehost-web/handler.go | 75 +++++++++++++++++++++++-------------- lib/log/main.go | 2 +- 2 files changed, 47 insertions(+), 30 deletions(-) diff --git a/cmd/warehost-web/handler.go b/cmd/warehost-web/handler.go index f2f0326..c1ffdc5 100644 --- a/cmd/warehost-web/handler.go +++ b/cmd/warehost-web/handler.go @@ -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")) diff --git a/lib/log/main.go b/lib/log/main.go index 6717f17..7fd9986 100644 --- a/lib/log/main.go +++ b/lib/log/main.go @@ -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 }