feat: add static webpage

This commit is contained in:
jabuxas 2024-09-18 09:55:26 -03:00
parent 90f63f4c54
commit 468fb823e9
2 changed files with 7 additions and 7 deletions

View File

@ -51,7 +51,7 @@ func main() {
} }
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/", app.fileHandler) mux.HandleFunc("/", app.indexHandler)
mux.HandleFunc( mux.HandleFunc(
"/tree/", "/tree/",
app.basicAuth(app.treeHandler), app.basicAuth(app.treeHandler),

View File

@ -22,7 +22,11 @@ type Application struct {
port string port string
} }
func (app *Application) fileHandler(w http.ResponseWriter, r *http.Request) { func (app *Application) treeHandler(w http.ResponseWriter, r *http.Request) {
http.StripPrefix("/tree/", http.FileServer(http.Dir(app.filesDir))).ServeHTTP(w, r)
}
func (app *Application) indexHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost { if r.Method == http.MethodPost {
app.uploadHandler(w, r) app.uploadHandler(w, r)
return return
@ -39,14 +43,10 @@ func (app *Application) fileHandler(w http.ResponseWriter, r *http.Request) {
if fileInfo, err := os.Stat(path); err == nil && !fileInfo.IsDir() { if fileInfo, err := os.Stat(path); err == nil && !fileInfo.IsDir() {
http.ServeFile(w, r, path) http.ServeFile(w, r, path)
} else { } else {
http.NotFound(w, r) http.StripPrefix("/", http.FileServer(http.Dir("static"))).ServeHTTP(w, r)
} }
} }
func (app *Application) treeHandler(w http.ResponseWriter, r *http.Request) {
http.StripPrefix("/tree/", http.FileServer(http.Dir(app.filesDir))).ServeHTTP(w, r)
}
func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) { func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" { if r.URL.Path != "/" {
http.Error(w, "Method not allowed", http.StatusUnauthorized) http.Error(w, "Method not allowed", http.StatusUnauthorized)