Compare commits

..

2 Commits

Author SHA1 Message Date
4ed73c3086 feat: add /last for checking last uploaded file 2024-09-18 10:03:53 -03:00
468fb823e9 feat: add static webpage 2024-09-18 09:55:26 -03:00
2 changed files with 21 additions and 9 deletions

View File

@ -51,11 +51,12 @@ 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),
) )
mux.HandleFunc("/last", app.lastHandler)
srv := &http.Server{ srv := &http.Server{
Addr: app.port, Addr: app.port,

View File

@ -16,13 +16,18 @@ type Application struct {
username string username string
password string password string
} }
url string url string
key string key string
filesDir string filesDir string
port string port string
lastUploadedFile 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,12 +44,16 @@ 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) { func (app *Application) lastHandler(w http.ResponseWriter, r *http.Request) {
http.StripPrefix("/tree/", http.FileServer(http.Dir(app.filesDir))).ServeHTTP(w, r) if app.lastUploadedFile == "" {
http.Error(w, "No new files uploaded yet", http.StatusNotFound)
return
}
http.ServeFile(w, r, app.lastUploadedFile)
} }
func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) { func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) {
@ -87,6 +96,8 @@ func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Error copying the file", http.StatusInternalServerError) http.Error(w, "Error copying the file", http.StatusInternalServerError)
} }
app.lastUploadedFile = filepath
if app.url == "" { if app.url == "" {
fmt.Fprintf(w, "http://localhost%s/%s\n", app.port, filename) fmt.Fprintf(w, "http://localhost%s/%s\n", app.port, filename)
} else { } else {