57 lines
1.2 KiB
Go
57 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"html/template"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
|
|
"meutel.net/meutel/go-examples/photoblog/admin"
|
|
"meutel.net/meutel/go-examples/photoblog/photo"
|
|
|
|
"github.com/gorilla/sessions"
|
|
)
|
|
|
|
func main() {
|
|
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()
|
|
|
|
admin := admin.AuthCookie{
|
|
Templates: tpl,
|
|
Store: sessions.NewCookieStore([]byte("flQ6QzM/c3Jtdl9ycDx6OXRIfFgK")),
|
|
}
|
|
app := photo.PhotoBlog{
|
|
AuthCookie: &admin,
|
|
Templates: tpl,
|
|
DataDir: data,
|
|
}
|
|
|
|
fileServer := http.FileServer(http.Dir(data.Name()))
|
|
|
|
http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
|
|
fmt.Println(req.RequestURI)
|
|
if strings.HasPrefix(req.RequestURI, "/data") {
|
|
http.StripPrefix("/data/", fileServer).ServeHTTP(res, req)
|
|
} else {
|
|
app.HomePage(res, req)
|
|
}
|
|
})
|
|
http.HandleFunc("/upload", app.UploadPage)
|
|
http.HandleFunc("/login", app.LoginPage)
|
|
http.HandleFunc("/logout", app.LogoutPage)
|
|
http.ListenAndServeTLS(":9443", "../cert.pem", "../key.pem", nil)
|
|
}
|