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 package main
import ( import (
"errors"
"fmt" "fmt"
"net/http" "net/http"
"os" "os"
@ -13,54 +14,60 @@ import (
web "dev.sum7.eu/sum7/warehost/modul/web" web "dev.sum7.eu/sum7/warehost/modul/web"
) )
//ProxyHost if this server is behind a proxy func getWebsite(host string) (*web.Website, error) {
const ProxyHost = "X-Real-Host" var err error
func getWebsite(host string) *web.Website {
website := &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) 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) { func handlerstatic(w http.ResponseWriter, r *http.Request) {
host := r.Header.Get(ProxyHost) logger := liblog.NewModulLog(r.Host).GetLog(r, r.URL.Path)
if len(host) <= 1 { website, err := getWebsite(r.Host)
host = r.Host if err != nil {
} http.NotFound(w, r)
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 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) http.NotFound(w, r)
} }
func handlerfiles(w http.ResponseWriter, r *http.Request) { func handlerfiles(w http.ResponseWriter, r *http.Request) {
host := r.Header.Get(ProxyHost) logger := liblog.NewModulLog(r.Host).GetLog(r, r.URL.Path)
if len(host) <= 1 { website, err := getWebsite(r.Host)
host = 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) 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() { if f, err := os.Stat(path); err == nil && !f.IsDir() {
http.ServeFile(w, r, path) http.ServeFile(w, r, path)
return return
} }
logger.Warn("file not found")
http.NotFound(w, r) http.NotFound(w, r)
} }
func handlerfunc(w http.ResponseWriter, r *http.Request) { 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:] url := r.URL.Path[1:]
logger := liblog.NewModulLog(host).GetLog(r, url) logger := liblog.NewModulLog(r.Host).GetLog(r, url)
website := getWebsite(host) 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) logger = logger.WithField("hID", website.ID)
var menus []*web.Menu var menus []*web.Menu
@ -74,9 +81,19 @@ func handlerfunc(w http.ResponseWriter, r *http.Request) {
menu = item menu = item
} }
} }
if menu.ID <= 0 {
logger.Warn("menu not found")
http.NotFound(w, r)
return
}
menus = web.BuildMenuTree(menus) menus = web.BuildMenuTree(menus)
page := &web.Page{WebsiteID: website.ID, MenuID: menu.ID} page := web.Page{WebsiteID: website.ID, MenuID: menu.ID}
dbconnection.Where(page).Find(page) dbconnection.Where(&page).Find(&page)
if page.ID <= 0 {
logger.Warn("page not found")
http.NotFound(w, r)
return
}
page.Menu = menu page.Menu = menu
unsafe := blackfriday.MarkdownCommon([]byte(page.Content)) unsafe := blackfriday.MarkdownCommon([]byte(page.Content))
@ -84,9 +101,9 @@ func handlerfunc(w http.ResponseWriter, r *http.Request) {
page.Content = string(unsafe) page.Content = string(unsafe)
i := TemplateInfo{ i := TemplateInfo{
Website: website, Website: website,
Host: host, Host: r.Host,
URL: url, URL: url,
Page: page, Page: &page,
Menu: menus, Menu: menus,
} }
t, err := template.ParseGlob(fmt.Sprintf("%s/%d/%s/%s", config.Webroot, website.ID, "tmpl", "*.tmpl")) 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 // GetLog with api request ip in log
func (m *ModulLog) GetLog(r *http.Request, request string) *log.Entry { 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 { if len(ip) <= 1 {
ip = r.RemoteAddr ip = r.RemoteAddr
} }