default username if no password, don't export private functions
This commit is contained in:
parent
38a41d4b25
commit
73a3d635dd
18
main.go
18
main.go
|
@ -3,10 +3,11 @@ package main
|
|||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/gorilla/mux"
|
||||
"git.teamworkapps.com/shortcut/enhancedfileserver"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"git.teamworkapps.com/shortcut/enhancedfileserver"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
type config struct {
|
||||
|
@ -23,7 +24,7 @@ var c config
|
|||
func init() {
|
||||
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.StringVar(&c.username, "username", "", "Require this username tp access")
|
||||
flag.StringVar(&c.username, "username", "", "Require this username tp access. Default is 'admin' if you specify a password.")
|
||||
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")
|
||||
|
@ -32,11 +33,14 @@ func init() {
|
|||
|
||||
func main() {
|
||||
r := mux.NewRouter()
|
||||
if c.username != "" && c.password == "" {
|
||||
c.username = "admin"
|
||||
}
|
||||
if c.username != "" && c.password != "" {
|
||||
r.Use(BasicAuth)
|
||||
r.Use(basicAuth)
|
||||
}
|
||||
if c.log {
|
||||
r.Use(QueryLog)
|
||||
r.Use(queryLog)
|
||||
}
|
||||
|
||||
if c.noindex {
|
||||
|
@ -50,7 +54,7 @@ func main() {
|
|||
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) {
|
||||
if c.username != "" && c.password != "" {
|
||||
user, pass, ok := r.BasicAuth()
|
||||
|
@ -67,7 +71,7 @@ func BasicAuth(next http.Handler) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func QueryLog(next http.Handler) http.Handler {
|
||||
func queryLog(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("%s - %s", r.RemoteAddr, r.URL)
|
||||
next.ServeHTTP(w, r)
|
||||
|
|
Loading…
Reference in New Issue