sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
warehost/cmd/warehost-web/handler.go

148 lines
3.9 KiB
Go
Raw Normal View History

2016-08-27 02:18:41 +02:00
package main
import (
2016-12-22 23:16:41 +01:00
"errors"
2016-08-27 02:18:41 +02:00
"fmt"
"net/http"
"os"
"strings"
2016-08-27 02:18:41 +02:00
"text/template"
"github.com/jinzhu/gorm"
2016-10-11 20:16:24 +02:00
liblog "dev.sum7.eu/sum7/warehost/lib/log"
web "dev.sum7.eu/sum7/warehost/modul/web"
2016-08-27 02:18:41 +02:00
)
2016-12-22 23:16:41 +01:00
func getWebsite(host string) (*web.Website, error) {
var err error
2016-10-11 20:16:24 +02:00
website := &web.Website{}
2016-08-27 02:18:41 +02:00
dbconnection.Model(website).Joins("JOIN web_domain ON web_domain.website = web_website.id").Where("web_domain.name = ?", host).First(website)
2016-12-22 23:16:41 +01:00
if website.ID <= 0 {
err = errors.New("No Website found")
}
return website, err
2016-08-27 02:18:41 +02:00
}
func handlerstatic(w http.ResponseWriter, r *http.Request) {
2016-12-22 23:16:41 +01:00
logger := liblog.NewModulLog(r.Host).GetLog(r, r.URL.Path)
website, err := getWebsite(r.Host)
if err != nil {
2016-12-29 07:53:27 +01:00
logger.Warn("website not found: ", r.Host)
2016-12-22 23:16:41 +01:00
http.NotFound(w, r)
return
2016-08-27 02:18:41 +02:00
}
2016-09-01 22:19:39 +02:00
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() {
2016-12-29 07:53:27 +01:00
logger.Info("done")
2016-09-01 22:19:39 +02:00
http.ServeFile(w, r, path)
return
}
2016-12-22 23:16:41 +01:00
logger.Warn("file not found")
2016-09-01 22:19:39 +02:00
http.NotFound(w, r)
}
func handlerfiles(w http.ResponseWriter, r *http.Request) {
2016-12-22 23:16:41 +01:00
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
2016-09-01 22:19:39 +02:00
}
2016-08-27 02:18:41 +02:00
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() {
2016-12-29 07:53:27 +01:00
logger.Info("done")
2016-08-27 02:18:41 +02:00
http.ServeFile(w, r, path)
return
}
2016-12-22 23:16:41 +01:00
logger.Warn("file not found")
2016-08-27 02:18:41 +02:00
http.NotFound(w, r)
}
2016-09-01 22:19:39 +02:00
2016-08-27 02:18:41 +02:00
func handlerfunc(w http.ResponseWriter, r *http.Request) {
2017-02-20 18:58:53 +01:00
url := web.FixPath(r.URL.Path[1:])
urlParts := strings.Split(url, "/")
2016-12-22 23:16:41 +01:00
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
}
2016-08-27 02:18:41 +02:00
logger = logger.WithField("hID", website.ID)
2016-10-11 20:16:24 +02:00
var menus []*web.Menu
2017-02-20 18:58:53 +01:00
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
2016-08-27 02:18:41 +02:00
}
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
}
2017-02-20 18:58:53 +01:00
if item.Path == url {
urlObj = item
2016-08-27 02:18:41 +02:00
}
}
2017-02-20 18:58:53 +01:00
2016-10-11 20:16:24 +02:00
menus = web.BuildMenuTree(menus)
2017-02-20 18:58:53 +01:00
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() {
2017-02-20 18:58:53 +01:00
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
}
2016-12-22 23:16:41 +01:00
}
2016-08-27 02:18:41 +02:00
i := TemplateInfo{
Website: website,
Host: r.Host,
URL: url,
Menu: menus,
Page: &page,
Blog: &blog,
BlogPost: &blogpost,
2016-09-12 20:34:09 +02:00
}
t, err := template.ParseGlob(fmt.Sprintf("%s/%d/%s/%s", config.Webroot, website.ID, "tmpl", "*.tmpl"))
logger.Info("done")
2017-02-20 18:58:53 +01:00
2016-09-12 20:34:09 +02:00
if err != nil {
w.Write([]byte(fmt.Sprintf("<h1>Error on rendering Template</h1>\n%s", err)))
} else {
t.Execute(w, i)
2016-08-27 02:18:41 +02:00
}
}