Skip to content

Commit

Permalink
Merge pull request #69 from writeas/resource-dirs-config
Browse files Browse the repository at this point in the history
Support configuring resource directories
  • Loading branch information
thebaer committed Jan 19, 2019
2 parents 47b2155 + 5c19d24 commit cb1bd37
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 32 deletions.
13 changes: 9 additions & 4 deletions app.go
Expand Up @@ -19,6 +19,7 @@ import (
"net/url"
"os"
"os/signal"
"path/filepath"
"regexp"
"strings"
"syscall"
Expand All @@ -41,7 +42,7 @@ import (
)

const (
staticDir = "static/"
staticDir = "static"
assumedTitleLen = 80
postsPerPage = 10

Expand Down Expand Up @@ -336,11 +337,15 @@ func Serve() {
isSingleUser = app.cfg.App.SingleUser
app.cfg.Server.Dev = *debugPtr

initTemplates()
err := initTemplates(app.cfg)
if err != nil {
log.Error("load templates: %s", err)
os.Exit(1)
}

// Load keys
log.Info("Loading encryption keys...")
err := initKeys(app)
err = initKeys(app)
if err != nil {
log.Error("\n%s\n", err)
}
Expand Down Expand Up @@ -395,7 +400,7 @@ func Serve() {
}

// Handle static files
fs := http.FileServer(http.Dir(staticDir))
fs := http.FileServer(http.Dir(filepath.Join(app.cfg.Server.StaticParentDir, staticDir)))
shttp.Handle("/", fs)
r.PathPrefix("/").Handler(fs)

Expand Down
5 changes: 5 additions & 0 deletions config/config.go
Expand Up @@ -30,6 +30,11 @@ type (
TLSCertPath string `ini:"tls_cert_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:"-"`
}

Expand Down
12 changes: 12 additions & 0 deletions keys.go
Expand Up @@ -38,16 +38,28 @@ func initKeys(app *app) error {
var err error
app.keys = &keychain{}

emailKeyPath = filepath.Join(app.cfg.Server.KeysParentDir, emailKeyPath)
if debugging {
log.Info(" %s", emailKeyPath)
}
app.keys.emailKey, err = ioutil.ReadFile(emailKeyPath)
if err != nil {
return err
}

cookieAuthKeyPath = filepath.Join(app.cfg.Server.KeysParentDir, cookieAuthKeyPath)
if debugging {
log.Info(" %s", cookieAuthKeyPath)
}
app.keys.cookieAuthKey, err = ioutil.ReadFile(cookieAuthKeyPath)
if err != nil {
return err
}

cookieKeyPath = filepath.Join(app.cfg.Server.KeysParentDir, cookieKeyPath)
if debugging {
log.Info(" %s", cookieKeyPath)
}
app.keys.cookieKey, err = ioutil.ReadFile(cookieKeyPath)
if err != nil {
return err
Expand Down
56 changes: 28 additions & 28 deletions templates.go
Expand Up @@ -11,10 +11,10 @@
package writefreely

import (
"fmt"
"github.com/dustin/go-humanize"
"github.com/writeas/web-core/l10n"
"github.com/writeas/web-core/log"
"github.com/writeas/writefreely/config"
"html/template"
"io"
"io/ioutil"
Expand Down Expand Up @@ -54,53 +54,53 @@ func showUserPage(w http.ResponseWriter, name string, obj interface{}) {
}
}

func initTemplate(name string) {
func initTemplate(parentDir, name string) {
if debugging {
log.Info(" %s%s%s.tmpl", templatesDir, string(filepath.Separator), name)
log.Info(" " + filepath.Join(parentDir, templatesDir, name+".tmpl"))
}

files := []string{
filepath.Join(templatesDir, name+".tmpl"),
filepath.Join(templatesDir, "include", "footer.tmpl"),
filepath.Join(templatesDir, "base.tmpl"),
filepath.Join(parentDir, templatesDir, name+".tmpl"),
filepath.Join(parentDir, templatesDir, "include", "footer.tmpl"),
filepath.Join(parentDir, templatesDir, "base.tmpl"),
}
if name == "collection" || name == "collection-tags" {
// 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" {
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...))
}

func initPage(path, key string) {
func initPage(parentDir, path, key string) {
if debugging {
log.Info(" %s", key)
log.Info(" [%s] %s", key, path)
}

pages[key] = template.Must(template.New("").Funcs(funcMap).ParseFiles(
path,
filepath.Join(templatesDir, "include", "footer.tmpl"),
filepath.Join(templatesDir, "base.tmpl"),
filepath.Join(parentDir, templatesDir, "include", "footer.tmpl"),
filepath.Join(parentDir, templatesDir, "base.tmpl"),
))
}

func initUserPage(path, key string) {
func initUserPage(parentDir, path, key string) {
if debugging {
log.Info(" %s", key)
log.Info(" [%s] %s", key, path)
}

userPages[key] = template.Must(template.New(key).Funcs(funcMap).ParseFiles(
path,
filepath.Join(templatesDir, "user", "include", "header.tmpl"),
filepath.Join(templatesDir, "user", "include", "footer.tmpl"),
filepath.Join(parentDir, templatesDir, "user", "include", "header.tmpl"),
filepath.Join(parentDir, templatesDir, "user", "include", "footer.tmpl"),
))
}

func initTemplates() error {
func initTemplates(cfg *config.Config) error {
log.Info("Loading templates...")
tmplFiles, err := ioutil.ReadDir(templatesDir)
tmplFiles, err := ioutil.ReadDir(filepath.Join(cfg.Server.TemplatesParentDir, templatesDir))
if err != nil {
return err
}
Expand All @@ -109,35 +109,35 @@ func initTemplates() error {
if !f.IsDir() && !strings.HasPrefix(f.Name(), ".") {
parts := strings.Split(f.Name(), ".")
key := parts[0]
initTemplate(key)
initTemplate(cfg.Server.TemplatesParentDir, key)
}
}

log.Info("Loading pages...")
// 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(), ".") {
parts := strings.Split(path, string(filepath.Separator))
key := i.Name()
if len(parts) > 2 {
key = fmt.Sprintf("%s%s%s", parts[1], string(filepath.Separator), i.Name())
}
initPage(path, key)
initPage(cfg.Server.PagesParentDir, path, key)
}

return nil
})

log.Info("Loading user pages...")
// 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(), ".") {
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()
if len(parts) > 2 {
key = filepath.Join(parts[1], f.Name())
}
initUserPage(path, key)
initUserPage(cfg.Server.TemplatesParentDir, path, key)
}

return nil
Expand Down

0 comments on commit cb1bd37

Please sign in to comment.