2017-07-29 09:22:34 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"html/template"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2017-07-29 11:54:23 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2017-07-29 09:22:34 +00:00
|
|
|
|
|
|
|
"meutel.net/meutel/go-examples/photoblog/admin"
|
2017-07-29 11:54:23 +00:00
|
|
|
"meutel.net/meutel/go-examples/photoblog/photo"
|
2017-07-29 09:22:34 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/sessions"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
tpl, err := template.ParseGlob("templates/*.html")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
2017-07-29 11:54:23 +00:00
|
|
|
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()
|
|
|
|
|
2017-07-29 17:22:11 +00:00
|
|
|
// FIXME config file
|
2017-07-29 11:54:23 +00:00
|
|
|
app := photo.PhotoBlog{
|
2017-07-29 17:22:11 +00:00
|
|
|
admin.AuthCookie{
|
|
|
|
Templates: tpl,
|
2017-07-29 14:51:12 +00:00
|
|
|
Store: sessions.NewCookieStore([]byte("flQ6QzM/c3Jtdl9ycDx6OXRIfFgK")),
|
|
|
|
DataDir: data,
|
|
|
|
PasswordSecret: "d2xnNSwoREQhfSxBVDQ0bF0yb2AK",
|
|
|
|
},
|
2017-07-29 09:22:34 +00:00
|
|
|
}
|
2017-07-29 11:54:23 +00:00
|
|
|
|
|
|
|
http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
|
|
|
|
if strings.HasPrefix(req.RequestURI, "/data") {
|
2017-07-29 17:22:11 +00:00
|
|
|
http.StripPrefix("/data/", http.FileServer(http.Dir(data.Name()))).ServeHTTP(res, req)
|
2017-07-29 11:54:23 +00:00
|
|
|
} else {
|
|
|
|
app.HomePage(res, req)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
http.HandleFunc("/upload", app.UploadPage)
|
2017-07-29 09:22:34 +00:00
|
|
|
http.HandleFunc("/login", app.LoginPage)
|
|
|
|
http.HandleFunc("/logout", app.LogoutPage)
|
|
|
|
http.ListenAndServeTLS(":9443", "../cert.pem", "../key.pem", nil)
|
|
|
|
}
|