148 lines
3.9 KiB
Go
148 lines
3.9 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"text/template"
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
liblog "dev.sum7.eu/sum7/warehost/lib/log"
|
|
web "dev.sum7.eu/sum7/warehost/modul/web"
|
|
)
|
|
|
|
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)
|
|
if website.ID <= 0 {
|
|
err = errors.New("No Website found")
|
|
}
|
|
return website, err
|
|
}
|
|
|
|
func handlerstatic(w http.ResponseWriter, r *http.Request) {
|
|
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
|
|
}
|
|
|
|
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.Info("done")
|
|
http.ServeFile(w, r, path)
|
|
return
|
|
}
|
|
logger.Warn("file not found")
|
|
http.NotFound(w, r)
|
|
}
|
|
func handlerfiles(w http.ResponseWriter, r *http.Request) {
|
|
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
|
|
}
|
|
|
|
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() {
|
|
logger.Info("done")
|
|
http.ServeFile(w, r, path)
|
|
return
|
|
}
|
|
logger.Warn("file not found")
|
|
http.NotFound(w, r)
|
|
}
|
|
|
|
func handlerfunc(w http.ResponseWriter, r *http.Request) {
|
|
url := web.FixPath(r.URL.Path[1:])
|
|
urlParts := strings.Split(url, "/")
|
|
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
|
|
dbconnection.Where("website = ?", website.ID).Preload("URL").Order("position").Find(&menus)
|
|
|
|
var urls []*web.URL
|
|
dbconnection.Where("website = ?", website.ID).Find(&urls)
|
|
var urlObj *web.URL
|
|
for _, item := range urls {
|
|
if item.Path == "" && urlObj == nil {
|
|
urlObj = item
|
|
}
|
|
if item.Path == strings.Join(urlParts[0:len(urlParts)-1], "/") && (urlObj == nil || urlObj.Path == "") {
|
|
urlObj = item
|
|
}
|
|
if len(urlParts) > 2 && item.Path == strings.Join(urlParts[0:len(urlParts)-3], "/") && (urlObj == nil || urlObj.Path == "") {
|
|
urlObj = item
|
|
}
|
|
if item.Path == url {
|
|
urlObj = item
|
|
}
|
|
}
|
|
|
|
menus = web.BuildMenuTree(menus)
|
|
|
|
page := web.Page{
|
|
WebsiteID: website.ID,
|
|
URLID: urlObj.ID,
|
|
}
|
|
blog := web.Blog{
|
|
WebsiteID: website.ID,
|
|
URLID: urlObj.ID,
|
|
}
|
|
blogpost := web.BlogPost{}
|
|
if !dbconnection.Where(page).First(&page).RecordNotFound() {
|
|
page.URL = urlObj
|
|
} else {
|
|
if !dbconnection.Where(blog).Preload("Posts", func(db *gorm.DB) *gorm.DB {
|
|
return db.Order("createat DESC")
|
|
}).Preload("Posts.Preview").First(&blog).RecordNotFound() {
|
|
blogpost.BlogID = blog.ID
|
|
if (blog.PostURL == 0 && len(urlParts) > 1) || (blog.PostURL == 1 && len(urlParts) > 2) {
|
|
db := dbconnection.Where(blogpost).Preload("Preview").Where("LOWER(title) = LOWER(?)", urlParts[len(urlParts)-1])
|
|
if blog.PostURL == 1 {
|
|
db = db.Where("to_char(createat,'YYYY-MM') = ?", strings.Join(urlParts[len(urlParts)-3:len(urlParts)-1], "-"))
|
|
}
|
|
db.First(&blogpost)
|
|
blogpost.Blog = &blog
|
|
}
|
|
for _, post := range blog.Posts {
|
|
post.Blog = &blog
|
|
}
|
|
blog.URL = urlObj
|
|
}
|
|
}
|
|
|
|
i := TemplateInfo{
|
|
Website: website,
|
|
Host: r.Host,
|
|
URL: url,
|
|
Menu: menus,
|
|
Page: &page,
|
|
Blog: &blog,
|
|
BlogPost: &blogpost,
|
|
}
|
|
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("<h1>Error on rendering Template</h1>\n%s", err)))
|
|
} else {
|
|
t.Execute(w, i)
|
|
}
|
|
}
|