youp0m/main.go

87 lines
2.7 KiB
Go
Raw Normal View History

2016-06-25 17:51:24 +00:00
package main
import (
"flag"
"log"
"net/http"
"os"
2017-10-11 16:42:51 +00:00
"path/filepath"
2016-06-25 17:51:24 +00:00
)
var PublishedImgDir string
var NextImgDir string
var ThumbsDir string
func main() {
bind := flag.String("bind", "0.0.0.0:8080", "Bind port/socket")
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)
}
}
log.Println("Checking paths...")
if _, err := os.Stat(PublishedImgDir); os.IsNotExist(err) {
log.Fatal(err)
}
if _, err := os.Stat(NextImgDir); os.IsNotExist(err) {
log.Fatal(err)
}
if _, err := os.Stat(ThumbsDir); os.IsNotExist(err) {
log.Fatal(err)
}
log.Println("Registering handlers...")
authFunc := func (r *http.Request) (*User){ return Authenticate(*htpasswd, r) }
2017-10-11 16:42:51 +00:00
staticDir, _ := filepath.Abs(filepath.Join(filepath.Dir(os.Args[0]), "static"))
2016-06-25 17:51:24 +00:00
mux := http.NewServeMux()
2017-10-11 16:42:51 +00:00
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, filepath.Join(staticDir, "index.html")) })
mux.Handle("/css/", http.FileServer(http.Dir(staticDir)))
mux.Handle("/js/", http.FileServer(http.Dir(staticDir)))
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",
func (*http.Request) (bool){ return true; },
http.FileServer(http.Dir(PublishedImgDir)),
GetPublishedImage,
)))
mux.Handle("/images/next/", http.StripPrefix("/images/next", ImagesHandler(
"next",
func (r *http.Request) (bool){ return authFunc(r) != nil; },
http.FileServer(http.Dir(NextImgDir)),
GetNextImage,
)))
mux.Handle("/images/thumbs/", http.StripPrefix("/images/thumbs", ImagesHandler(
"thumbs",
func (*http.Request) (bool){ return true; },
http.FileServer(http.Dir(ThumbsDir)),
GetPublishedImage,
)))
mux.Handle("/images/next/thumbs/", http.StripPrefix("/images/next/thumbs", ImagesHandler(
"nexthumbs",
func (r *http.Request) (bool){ return authFunc(r) != nil; },
http.FileServer(http.Dir(ThumbsDir)),
GetNextImage,
)))
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)
}
}