accesslog to stdout + new mux engine
This commit is contained in:
parent
a3d35642ab
commit
f83cc1b1ef
3 changed files with 24 additions and 18 deletions
|
@ -3,7 +3,6 @@ package main
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -36,6 +35,6 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
mu := megauploader.NewMegaUploader(cfg)
|
mu := megauploader.NewMegaUploader(cfg)
|
||||||
mu.SetupRoutes()
|
mux := mu.SetupRoutes()
|
||||||
graceful.Run(*addr, 15*time.Second, http.DefaultServeMux)
|
graceful.Run(*addr, 15*time.Second, mux)
|
||||||
}
|
}
|
||||||
|
|
34
http.go
34
http.go
|
@ -16,6 +16,8 @@ import (
|
||||||
|
|
||||||
rice "github.com/GeertJohan/go.rice"
|
rice "github.com/GeertJohan/go.rice"
|
||||||
"github.com/c2h5oh/datasize"
|
"github.com/c2h5oh/datasize"
|
||||||
|
"github.com/gorilla/handlers"
|
||||||
|
"github.com/gorilla/mux"
|
||||||
"github.com/unrolled/secure"
|
"github.com/unrolled/secure"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -58,27 +60,31 @@ func (mu *MegaUploader) confAcquire(inner http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(f)
|
return http.HandlerFunc(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mu *MegaUploader) privateMiddleware(inner func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
|
func (mu *MegaUploader) privateMiddleware(inner func(w http.ResponseWriter, r *http.Request)) http.Handler {
|
||||||
return mu.secureMW.Handler(mu.confAcquire(requireUserMiddleware(inner))).ServeHTTP
|
return mu.secureMW.Handler(mu.confAcquire(requireUserMiddleware(inner)))
|
||||||
}
|
}
|
||||||
func (mu *MegaUploader) publicMiddleware(inner func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
|
func (mu *MegaUploader) publicMiddleware(inner func(w http.ResponseWriter, r *http.Request)) http.Handler {
|
||||||
return mu.secureMW.Handler(requireUserMiddleware(inner)).ServeHTTP
|
return mu.secureMW.Handler(requireUserMiddleware(inner))
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetupRoutes adds API routes
|
// SetupRoutes adds API routes
|
||||||
func (mu *MegaUploader) SetupRoutes() {
|
func (mu *MegaUploader) SetupRoutes() http.Handler {
|
||||||
|
|
||||||
prefix := strings.TrimRight(mu.Conf.Global.RoutePrefix, "/")
|
prefix := strings.TrimRight(mu.Conf.Global.RoutePrefix, "/")
|
||||||
|
root := mux.NewRouter()
|
||||||
|
r := root.PathPrefix(prefix).Subrouter()
|
||||||
|
|
||||||
http.HandleFunc(prefix+"/", mu.privateMiddleware(mu.home))
|
r.Handle("/", mu.privateMiddleware(mu.home))
|
||||||
http.HandleFunc(prefix+"/upload/", mu.privateMiddleware(mu.uploadUI))
|
r.Handle("/upload/{share}", mu.privateMiddleware(mu.uploadUI))
|
||||||
static := rice.MustFindBox("res/static")
|
static := rice.MustFindBox("res/static")
|
||||||
http.HandleFunc(prefix+"/static/", mu.publicMiddleware(
|
r.PathPrefix("/static/").Handler(mu.publicMiddleware(
|
||||||
http.StripPrefix(prefix+"/static/", http.FileServer(static.HTTPBox())).ServeHTTP,
|
http.StripPrefix(prefix+"/static/", http.FileServer(static.HTTPBox())).ServeHTTP,
|
||||||
))
|
))
|
||||||
http.HandleFunc(prefix+"/api/share", mu.privateMiddleware(mu.listShares))
|
api := r.PathPrefix("/api/").Subrouter()
|
||||||
http.HandleFunc(prefix+"/api/share/", mu.privateMiddleware(mu.getShare))
|
api.Handle("/share", mu.privateMiddleware(mu.listShares))
|
||||||
http.HandleFunc(prefix+"/api/upload/", mu.privateMiddleware(mu.upload))
|
api.Handle("/share/{share}", mu.privateMiddleware(mu.getShare))
|
||||||
|
api.Handle("/upload/{share}", mu.privateMiddleware(mu.upload)).Methods(http.MethodPost)
|
||||||
|
|
||||||
|
return handlers.CombinedLoggingHandler(os.Stdout, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getUser(r *http.Request) (string, error) {
|
func getUser(r *http.Request) (string, error) {
|
||||||
|
@ -114,7 +120,7 @@ func (mu *MegaUploader) listShares(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mu *MegaUploader) getShare(w http.ResponseWriter, r *http.Request) {
|
func (mu *MegaUploader) getShare(w http.ResponseWriter, r *http.Request) {
|
||||||
sharename := r.URL.Path[strings.LastIndexByte(r.URL.Path, '/')+1:]
|
sharename := mux.Vars(r)["share"]
|
||||||
share, err := mu.Conf.GetShare(sharename)
|
share, err := mu.Conf.GetShare(sharename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, fmt.Sprintf("Share '%s' not found: %s", sharename, err), http.StatusNotFound)
|
http.Error(w, fmt.Sprintf("Share '%s' not found: %s", sharename, err), http.StatusNotFound)
|
||||||
|
@ -137,7 +143,7 @@ func (mu *MegaUploader) getShare(w http.ResponseWriter, r *http.Request) {
|
||||||
// on success, redirect to view URL
|
// on success, redirect to view URL
|
||||||
func (mu *MegaUploader) upload(w http.ResponseWriter, r *http.Request) {
|
func (mu *MegaUploader) upload(w http.ResponseWriter, r *http.Request) {
|
||||||
user, _ := getUser(r) // user is set: checked by middleware
|
user, _ := getUser(r) // user is set: checked by middleware
|
||||||
sharename := r.URL.Path[strings.LastIndexByte(r.URL.Path, '/')+1:]
|
sharename := mux.Vars(r)["share"]
|
||||||
|
|
||||||
share, err := mu.Conf.GetAuthShare(sharename, user)
|
share, err := mu.Conf.GetAuthShare(sharename, user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
rice "github.com/GeertJohan/go.rice"
|
rice "github.com/GeertJohan/go.rice"
|
||||||
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:generate rice embed-go
|
//go:generate rice embed-go
|
||||||
|
@ -55,7 +56,7 @@ func (mu *MegaUploader) home(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
func (mu *MegaUploader) uploadUI(w http.ResponseWriter, r *http.Request) {
|
func (mu *MegaUploader) uploadUI(w http.ResponseWriter, r *http.Request) {
|
||||||
user, _ := getUser(r) // user is set: checked by middleware
|
user, _ := getUser(r) // user is set: checked by middleware
|
||||||
sharename := r.URL.Path[strings.LastIndexByte(r.URL.Path, '/')+1:]
|
sharename := mux.Vars(r)["share"]
|
||||||
|
|
||||||
share, err := mu.Conf.GetAuthShare(sharename, user)
|
share, err := mu.Conf.GetAuthShare(sharename, user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in a new issue