mirror of
https://github.com/writeas/writefreely
synced 2025-02-01 18:46:51 +01:00
Merge pull request #69 from writeas/resource-dirs-config
Support configuring resource directories
This commit is contained in:
commit
cb1bd37f64
13
app.go
13
app.go
@ -19,6 +19,7 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
@ -41,7 +42,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
staticDir = "static/"
|
staticDir = "static"
|
||||||
assumedTitleLen = 80
|
assumedTitleLen = 80
|
||||||
postsPerPage = 10
|
postsPerPage = 10
|
||||||
|
|
||||||
@ -336,11 +337,15 @@ func Serve() {
|
|||||||
isSingleUser = app.cfg.App.SingleUser
|
isSingleUser = app.cfg.App.SingleUser
|
||||||
app.cfg.Server.Dev = *debugPtr
|
app.cfg.Server.Dev = *debugPtr
|
||||||
|
|
||||||
initTemplates()
|
err := initTemplates(app.cfg)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("load templates: %s", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
// Load keys
|
// Load keys
|
||||||
log.Info("Loading encryption keys...")
|
log.Info("Loading encryption keys...")
|
||||||
err := initKeys(app)
|
err = initKeys(app)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("\n%s\n", err)
|
log.Error("\n%s\n", err)
|
||||||
}
|
}
|
||||||
@ -395,7 +400,7 @@ func Serve() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Handle static files
|
// Handle static files
|
||||||
fs := http.FileServer(http.Dir(staticDir))
|
fs := http.FileServer(http.Dir(filepath.Join(app.cfg.Server.StaticParentDir, staticDir)))
|
||||||
shttp.Handle("/", fs)
|
shttp.Handle("/", fs)
|
||||||
r.PathPrefix("/").Handler(fs)
|
r.PathPrefix("/").Handler(fs)
|
||||||
|
|
||||||
|
@ -30,6 +30,11 @@ type (
|
|||||||
TLSCertPath string `ini:"tls_cert_path"`
|
TLSCertPath string `ini:"tls_cert_path"`
|
||||||
TLSKeyPath string `ini:"tls_key_path"`
|
TLSKeyPath string `ini:"tls_key_path"`
|
||||||
|
|
||||||
|
TemplatesParentDir string `ini:"templates_parent_dir"`
|
||||||
|
StaticParentDir string `ini:"static_parent_dir"`
|
||||||
|
PagesParentDir string `ini:"pages_parent_dir"`
|
||||||
|
KeysParentDir string `ini:"keys_parent_dir"`
|
||||||
|
|
||||||
Dev bool `ini:"-"`
|
Dev bool `ini:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
12
keys.go
12
keys.go
@ -38,16 +38,28 @@ func initKeys(app *app) error {
|
|||||||
var err error
|
var err error
|
||||||
app.keys = &keychain{}
|
app.keys = &keychain{}
|
||||||
|
|
||||||
|
emailKeyPath = filepath.Join(app.cfg.Server.KeysParentDir, emailKeyPath)
|
||||||
|
if debugging {
|
||||||
|
log.Info(" %s", emailKeyPath)
|
||||||
|
}
|
||||||
app.keys.emailKey, err = ioutil.ReadFile(emailKeyPath)
|
app.keys.emailKey, err = ioutil.ReadFile(emailKeyPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cookieAuthKeyPath = filepath.Join(app.cfg.Server.KeysParentDir, cookieAuthKeyPath)
|
||||||
|
if debugging {
|
||||||
|
log.Info(" %s", cookieAuthKeyPath)
|
||||||
|
}
|
||||||
app.keys.cookieAuthKey, err = ioutil.ReadFile(cookieAuthKeyPath)
|
app.keys.cookieAuthKey, err = ioutil.ReadFile(cookieAuthKeyPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cookieKeyPath = filepath.Join(app.cfg.Server.KeysParentDir, cookieKeyPath)
|
||||||
|
if debugging {
|
||||||
|
log.Info(" %s", cookieKeyPath)
|
||||||
|
}
|
||||||
app.keys.cookieKey, err = ioutil.ReadFile(cookieKeyPath)
|
app.keys.cookieKey, err = ioutil.ReadFile(cookieKeyPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
56
templates.go
56
templates.go
@ -11,10 +11,10 @@
|
|||||||
package writefreely
|
package writefreely
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/dustin/go-humanize"
|
"github.com/dustin/go-humanize"
|
||||||
"github.com/writeas/web-core/l10n"
|
"github.com/writeas/web-core/l10n"
|
||||||
"github.com/writeas/web-core/log"
|
"github.com/writeas/web-core/log"
|
||||||
|
"github.com/writeas/writefreely/config"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@ -54,53 +54,53 @@ func showUserPage(w http.ResponseWriter, name string, obj interface{}) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func initTemplate(name string) {
|
func initTemplate(parentDir, name string) {
|
||||||
if debugging {
|
if debugging {
|
||||||
log.Info(" %s%s%s.tmpl", templatesDir, string(filepath.Separator), name)
|
log.Info(" " + filepath.Join(parentDir, templatesDir, name+".tmpl"))
|
||||||
}
|
}
|
||||||
|
|
||||||
files := []string{
|
files := []string{
|
||||||
filepath.Join(templatesDir, name+".tmpl"),
|
filepath.Join(parentDir, templatesDir, name+".tmpl"),
|
||||||
filepath.Join(templatesDir, "include", "footer.tmpl"),
|
filepath.Join(parentDir, templatesDir, "include", "footer.tmpl"),
|
||||||
filepath.Join(templatesDir, "base.tmpl"),
|
filepath.Join(parentDir, templatesDir, "base.tmpl"),
|
||||||
}
|
}
|
||||||
if name == "collection" || name == "collection-tags" {
|
if name == "collection" || name == "collection-tags" {
|
||||||
// These pages list out collection posts, so we also parse templatesDir + "include/posts.tmpl"
|
// These pages list out collection posts, so we also parse templatesDir + "include/posts.tmpl"
|
||||||
files = append(files, filepath.Join(templatesDir, "include", "posts.tmpl"))
|
files = append(files, filepath.Join(parentDir, templatesDir, "include", "posts.tmpl"))
|
||||||
}
|
}
|
||||||
if name == "collection" || name == "collection-tags" || name == "collection-post" || name == "post" {
|
if name == "collection" || name == "collection-tags" || name == "collection-post" || name == "post" {
|
||||||
files = append(files, filepath.Join(templatesDir, "include", "post-render.tmpl"))
|
files = append(files, filepath.Join(parentDir, templatesDir, "include", "post-render.tmpl"))
|
||||||
}
|
}
|
||||||
templates[name] = template.Must(template.New("").Funcs(funcMap).ParseFiles(files...))
|
templates[name] = template.Must(template.New("").Funcs(funcMap).ParseFiles(files...))
|
||||||
}
|
}
|
||||||
|
|
||||||
func initPage(path, key string) {
|
func initPage(parentDir, path, key string) {
|
||||||
if debugging {
|
if debugging {
|
||||||
log.Info(" %s", key)
|
log.Info(" [%s] %s", key, path)
|
||||||
}
|
}
|
||||||
|
|
||||||
pages[key] = template.Must(template.New("").Funcs(funcMap).ParseFiles(
|
pages[key] = template.Must(template.New("").Funcs(funcMap).ParseFiles(
|
||||||
path,
|
path,
|
||||||
filepath.Join(templatesDir, "include", "footer.tmpl"),
|
filepath.Join(parentDir, templatesDir, "include", "footer.tmpl"),
|
||||||
filepath.Join(templatesDir, "base.tmpl"),
|
filepath.Join(parentDir, templatesDir, "base.tmpl"),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
func initUserPage(path, key string) {
|
func initUserPage(parentDir, path, key string) {
|
||||||
if debugging {
|
if debugging {
|
||||||
log.Info(" %s", key)
|
log.Info(" [%s] %s", key, path)
|
||||||
}
|
}
|
||||||
|
|
||||||
userPages[key] = template.Must(template.New(key).Funcs(funcMap).ParseFiles(
|
userPages[key] = template.Must(template.New(key).Funcs(funcMap).ParseFiles(
|
||||||
path,
|
path,
|
||||||
filepath.Join(templatesDir, "user", "include", "header.tmpl"),
|
filepath.Join(parentDir, templatesDir, "user", "include", "header.tmpl"),
|
||||||
filepath.Join(templatesDir, "user", "include", "footer.tmpl"),
|
filepath.Join(parentDir, templatesDir, "user", "include", "footer.tmpl"),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
func initTemplates() error {
|
func initTemplates(cfg *config.Config) error {
|
||||||
log.Info("Loading templates...")
|
log.Info("Loading templates...")
|
||||||
tmplFiles, err := ioutil.ReadDir(templatesDir)
|
tmplFiles, err := ioutil.ReadDir(filepath.Join(cfg.Server.TemplatesParentDir, templatesDir))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -109,20 +109,16 @@ func initTemplates() error {
|
|||||||
if !f.IsDir() && !strings.HasPrefix(f.Name(), ".") {
|
if !f.IsDir() && !strings.HasPrefix(f.Name(), ".") {
|
||||||
parts := strings.Split(f.Name(), ".")
|
parts := strings.Split(f.Name(), ".")
|
||||||
key := parts[0]
|
key := parts[0]
|
||||||
initTemplate(key)
|
initTemplate(cfg.Server.TemplatesParentDir, key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info("Loading pages...")
|
log.Info("Loading pages...")
|
||||||
// Initialize all static pages that use the base template
|
// Initialize all static pages that use the base template
|
||||||
filepath.Walk(pagesDir, func(path string, i os.FileInfo, err error) error {
|
filepath.Walk(filepath.Join(cfg.Server.PagesParentDir, pagesDir), func(path string, i os.FileInfo, err error) error {
|
||||||
if !i.IsDir() && !strings.HasPrefix(i.Name(), ".") {
|
if !i.IsDir() && !strings.HasPrefix(i.Name(), ".") {
|
||||||
parts := strings.Split(path, string(filepath.Separator))
|
|
||||||
key := i.Name()
|
key := i.Name()
|
||||||
if len(parts) > 2 {
|
initPage(cfg.Server.PagesParentDir, path, key)
|
||||||
key = fmt.Sprintf("%s%s%s", parts[1], string(filepath.Separator), i.Name())
|
|
||||||
}
|
|
||||||
initPage(path, key)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -130,14 +126,18 @@ func initTemplates() error {
|
|||||||
|
|
||||||
log.Info("Loading user pages...")
|
log.Info("Loading user pages...")
|
||||||
// Initialize all user pages that use base templates
|
// Initialize all user pages that use base templates
|
||||||
filepath.Walk(filepath.Join(templatesDir, "user"), func(path string, f os.FileInfo, err error) error {
|
filepath.Walk(filepath.Join(cfg.Server.TemplatesParentDir, templatesDir, "user"), func(path string, f os.FileInfo, err error) error {
|
||||||
if !f.IsDir() && !strings.HasPrefix(f.Name(), ".") {
|
if !f.IsDir() && !strings.HasPrefix(f.Name(), ".") {
|
||||||
parts := strings.Split(path, string(filepath.Separator))
|
corePath := path
|
||||||
|
if cfg.Server.TemplatesParentDir != "" {
|
||||||
|
corePath = corePath[len(cfg.Server.TemplatesParentDir)+1:]
|
||||||
|
}
|
||||||
|
parts := strings.Split(corePath, string(filepath.Separator))
|
||||||
key := f.Name()
|
key := f.Name()
|
||||||
if len(parts) > 2 {
|
if len(parts) > 2 {
|
||||||
key = filepath.Join(parts[1], f.Name())
|
key = filepath.Join(parts[1], f.Name())
|
||||||
}
|
}
|
||||||
initUserPage(path, key)
|
initUserPage(cfg.Server.TemplatesParentDir, path, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
x
Reference in New Issue
Block a user