Photoblog: refactoring config

This commit is contained in:
Meutel 2017-07-29 20:21:27 +02:00
parent 16fa08d2a1
commit 5088b4b531
3 changed files with 47 additions and 16 deletions

1
.gitignore vendored
View File

@ -65,3 +65,4 @@ cert.pem
key.pem
photoblog/photoblog
photoblog/data
photoblog/photoblog.json

View File

@ -1,6 +1,7 @@
package main
import (
"encoding/json"
"html/template"
"log"
"net/http"
@ -13,34 +14,61 @@ import (
"github.com/gorilla/sessions"
)
type PhotoBlogConfig struct {
SessionSecret string `json:"session-secret"`
PasswordSecret string `json:"password-secret"`
DataDir string `json:"data-dir"`
}
func ReadConfig(file *os.File) (*PhotoBlogConfig, error) {
conf := new(PhotoBlogConfig)
err := json.NewDecoder(file).Decode(conf)
return conf, err
}
func main() {
// load config
confPath := "photoblog.json"
if len(os.Args) > 1 {
confPath = os.Args[1]
}
confFile, err := os.Open(confPath)
if err != nil {
log.Fatalln("Invalid conf file", err)
}
defer confFile.Close()
conf, err := ReadConfig(confFile)
if err != nil {
log.Fatalln("Configuration error", err)
}
dataInfo, err := os.Stat(conf.DataDir)
if !dataInfo.IsDir() {
log.Fatalln("Invalid data directory")
}
data, err := os.Open(conf.DataDir)
if err != nil {
log.Fatalln("cannot open data directory", err)
}
defer data.Close()
// load templates
tpl, err := template.ParseGlob("templates/*.html")
if err != nil {
log.Fatalln(err)
}
dataInfo, err := os.Stat("data")
if !dataInfo.IsDir() {
log.Fatalln("data is not a directory")
}
data, err := os.Open("data")
if err != nil {
log.Fatalln(err)
}
defer data.Close()
// FIXME config file
app := photo.PhotoBlog{
admin.AuthCookie{
Templates: tpl,
Store: sessions.NewCookieStore([]byte("flQ6QzM/c3Jtdl9ycDx6OXRIfFgK")),
Store: sessions.NewCookieStore([]byte(conf.SessionSecret)),
DataDir: data,
PasswordSecret: "d2xnNSwoREQhfSxBVDQ0bF0yb2AK",
PasswordSecret: conf.PasswordSecret,
},
}
http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
if strings.HasPrefix(req.RequestURI, "/data") {
http.StripPrefix("/data/", http.FileServer(http.Dir(data.Name()))).ServeHTTP(res, req)
if strings.HasPrefix(req.RequestURI, photo.DATA) {
http.StripPrefix(photo.DATA, http.FileServer(http.Dir(data.Name()))).ServeHTTP(res, req)
} else {
app.HomePage(res, req)
}

View File

@ -21,6 +21,7 @@ const (
var (
PHOTOEXT = [3]string{".jpg", ".jpeg", ".png"}
PHOTOMIME = [2]string{"image/png", "image/jpeg"}
DATA = "/data/"
)
// Application
@ -148,8 +149,9 @@ func (app *PhotoBlog) AddPhoto(res http.ResponseWriter, req *http.Request) (stri
func (app *PhotoBlog) HomePage(res http.ResponseWriter, req *http.Request) {
photos := make([]*TimedFile, 0)
filepath.Walk(app.DataDir.Name(), func(path string, info os.FileInfo, err error) error {
if !info.IsDir() && IsPhoto(path) {
photos = append(photos, NewTimedFile(info.ModTime(), path))
relPath, err := filepath.Rel(app.DataDir.Name(), path)
if !info.IsDir() && IsPhoto(path) && err == nil {
photos = append(photos, NewTimedFile(info.ModTime(), DATA+relPath))
}
return nil
})