youp0m/main.go

109 lines
3.2 KiB
Go
Raw Normal View History

2016-06-25 17:51:24 +00:00
package main
import (
"flag"
"log"
"net/http"
"os"
)
var mux = http.NewServeMux()
2016-06-25 17:51:24 +00:00
var PublishedImgDir string
var NextImgDir string
var ThumbsDir string
func main() {
2018-02-04 14:27:51 +00:00
bind := flag.String("bind", ":8080", "Bind port/socket")
2016-06-25 17:51:24 +00:00
htpasswd_file := flag.String("htpasswd", "", "Admin passwords file, Apache htpasswd format")
flag.StringVar(&PublishedImgDir, "publishedimgdir", "./images/published/", "Directory where save published pictures")
flag.StringVar(&NextImgDir, "nextimgdir", "./images/next/", "Directory where save pictures to review")
flag.StringVar(&ThumbsDir, "thumbsdir", "./images/thumbs/", "Directory where generate thumbs")
flag.Parse()
htpasswd := &Htpasswd{}
if htpasswd_file != nil && *htpasswd_file != "" {
log.Println("Reading htpasswd file...")
var err error
if htpasswd, err = NewHtpasswd(*htpasswd_file); htpasswd == nil {
log.Fatal("Unable to parse htpasswd:", err)
}
} else if NextImgDir == "./images/next/" {
log.Println("Disable admin interface, images will be published without moderation")
NextImgDir = PublishedImgDir
2016-06-25 17:51:24 +00:00
}
log.Println("Checking paths...")
if _, err := os.Stat(PublishedImgDir); os.IsNotExist(err) {
2018-02-04 14:26:50 +00:00
if err := os.MkdirAll(PublishedImgDir, 0755); err != nil {
log.Fatal(err)
}
2016-06-25 17:51:24 +00:00
}
if _, err := os.Stat(NextImgDir); os.IsNotExist(err) {
2018-02-04 14:26:50 +00:00
if err := os.MkdirAll(NextImgDir, 0755); err != nil {
log.Fatal(err)
}
2016-06-25 17:51:24 +00:00
}
if _, err := os.Stat(ThumbsDir); os.IsNotExist(err) {
2018-02-04 14:26:50 +00:00
if err := os.MkdirAll(ThumbsDir, 0755); err != nil {
log.Fatal(err)
}
2016-06-25 17:51:24 +00:00
}
log.Println("Registering handlers...")
2018-09-18 20:16:58 +00:00
authFunc := func(r *http.Request) *User { return Authenticate(*htpasswd, r) }
2016-06-25 17:51:24 +00:00
if err := sanitizeStaticOptions(); err != nil {
log.Fatal(err)
2018-02-16 10:41:03 +00:00
}
2017-10-11 16:42:51 +00:00
2018-09-18 20:16:58 +00:00
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
r.URL.Path = "/"
http.FileServer(Assets).ServeHTTP(w, r)
2018-09-18 20:16:58 +00:00
})
2016-06-26 15:54:08 +00:00
2016-06-26 11:03:15 +00:00
mux.Handle("/api/", http.StripPrefix("/api", ApiHandler(authFunc)))
mux.Handle("/images/", http.StripPrefix("/images", ImagesHandler(
"images",
2018-09-18 20:16:58 +00:00
func(*http.Request) bool { return true },
2016-06-26 11:03:15 +00:00
http.FileServer(http.Dir(PublishedImgDir)),
GetPublishedImage,
)))
mux.Handle("/images/next/", http.StripPrefix("/images/next", ImagesHandler(
"next",
2018-09-18 20:16:58 +00:00
func(r *http.Request) bool { return authFunc(r) != nil },
2016-06-26 11:03:15 +00:00
http.FileServer(http.Dir(NextImgDir)),
GetNextImage,
)))
mux.Handle("/images/thumbs/", http.StripPrefix("/images/thumbs", ImagesHandler(
"thumbs",
2018-09-18 20:16:58 +00:00
func(*http.Request) bool { return true },
2016-06-26 11:03:15 +00:00
http.FileServer(http.Dir(ThumbsDir)),
GetPublishedImage,
)))
mux.Handle("/images/next/thumbs/", http.StripPrefix("/images/next/thumbs", ImagesHandler(
"nexthumbs",
2018-09-18 20:16:58 +00:00
func(r *http.Request) bool { return authFunc(r) != nil },
2016-06-26 11:03:15 +00:00
http.FileServer(http.Dir(ThumbsDir)),
GetNextImage,
)))
2016-06-25 17:51:24 +00:00
2018-02-16 10:42:41 +00:00
mux.HandleFunc("/admin/", func(w http.ResponseWriter, r *http.Request) {
if authFunc(r) == nil {
w.Header().Set("WWW-Authenticate", "Basic realm=\"YouP0m\"")
2018-02-16 10:42:41 +00:00
http.Error(w, "You are not allowed to perform this request.", http.StatusUnauthorized)
} else {
r.URL.Path = "/admin.html"
http.FileServer(Assets).ServeHTTP(w, r)
2018-02-16 10:42:41 +00:00
}
})
2016-06-25 17:51:24 +00:00
log.Println("Ready, listening on", *bind)
if err := http.ListenAndServe(*bind, mux); err != nil {
log.Fatal("Unable to listen and serve: ", err)
}
}