Add options for query log and disabling automatic directory index generation
This commit is contained in:
parent
015d185109
commit
1ba3c1b0e4
40
main.go
40
main.go
|
@ -10,39 +10,51 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type config struct {
|
type config struct {
|
||||||
Listen string
|
listen string
|
||||||
Port int
|
port int
|
||||||
Username string
|
username string
|
||||||
Password string
|
password string
|
||||||
|
noindex bool
|
||||||
|
log bool
|
||||||
}
|
}
|
||||||
|
|
||||||
var c config
|
var c config
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
flag.StringVar(&c.Listen, "listen", "0.0.0.0", "IP address to listen.")
|
flag.StringVar(&c.listen, "listen", "0.0.0.0", "IP address to listen.")
|
||||||
flag.IntVar(&c.Port, "port", 8000, "Listen port for interface (ports below 1024 may require super user privileges)")
|
flag.IntVar(&c.port, "port", 8000, "Listen port for interface (ports below 1024 may require super user privileges)")
|
||||||
flag.StringVar(&c.Username, "username", "", "Require this username tp access")
|
flag.StringVar(&c.username, "username", "", "Require this username tp access")
|
||||||
flag.StringVar(&c.Password, "password", "", "Require this password to access interface")
|
flag.StringVar(&c.password, "password", "", "Require this password to access interface")
|
||||||
|
flag.BoolVar(&c.noindex, "noindex", false, "Disable directory indexing")
|
||||||
|
flag.BoolVar(&c.log, "log", false, "Enable simple request log")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
r := mux.NewRouter()
|
r := mux.NewRouter()
|
||||||
r.Use(BasicAuth)
|
if c.username != "" && c.password != "" {
|
||||||
r.Use(QueryLog)
|
r.Use(BasicAuth)
|
||||||
|
}
|
||||||
|
if c.log {
|
||||||
|
r.Use(QueryLog)
|
||||||
|
}
|
||||||
|
|
||||||
r.PathPrefix("/").Handler(http.StripPrefix("/", enhancedfileserver.FileServer(http.Dir("./"))))
|
if c.noindex {
|
||||||
|
r.PathPrefix("/").Handler(http.StripPrefix("/", enhancedfileserver.FileServerNoIndex(http.Dir("./"))))
|
||||||
|
} else {
|
||||||
|
r.PathPrefix("/").Handler(http.StripPrefix("/", enhancedfileserver.FileServer(http.Dir("./"))))
|
||||||
|
}
|
||||||
|
|
||||||
a := fmt.Sprintf("%s:%d", c.Listen, c.Port)
|
a := fmt.Sprintf("%s:%d", c.listen, c.port)
|
||||||
|
|
||||||
log.Fatal(http.ListenAndServe(a, r))
|
log.Fatal(http.ListenAndServe(a, r))
|
||||||
}
|
}
|
||||||
|
|
||||||
func BasicAuth(next http.Handler) http.Handler {
|
func BasicAuth(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
if c.Username != "" && c.Password != "" {
|
if c.username != "" && c.password != "" {
|
||||||
user, pass, ok := r.BasicAuth()
|
user, pass, ok := r.BasicAuth()
|
||||||
if !ok || user != c.Username || pass != c.Password {
|
if !ok || user != c.username || pass != c.password {
|
||||||
w.Header().Set("WWW-Authenticate", `Basic realm="mydomains"`)
|
w.Header().Set("WWW-Authenticate", `Basic realm="mydomains"`)
|
||||||
|
|
||||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||||
|
|
Loading…
Reference in New Issue