Compare commits
No commits in common. "4ed73c30863f9d909e822084927710d2f1c20a5a" and "90f63f4c545963765d394882ce99d17983df6705" have entirely different histories.
4ed73c3086
...
90f63f4c54
3
abyss.go
3
abyss.go
@ -51,12 +51,11 @@ func main() {
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/", app.indexHandler)
|
||||
mux.HandleFunc("/", app.fileHandler)
|
||||
mux.HandleFunc(
|
||||
"/tree/",
|
||||
app.basicAuth(app.treeHandler),
|
||||
)
|
||||
mux.HandleFunc("/last", app.lastHandler)
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: app.port,
|
||||
|
27
handlers.go
27
handlers.go
@ -16,18 +16,13 @@ type Application struct {
|
||||
username string
|
||||
password string
|
||||
}
|
||||
url string
|
||||
key string
|
||||
filesDir string
|
||||
port string
|
||||
lastUploadedFile string
|
||||
url string
|
||||
key string
|
||||
filesDir string
|
||||
port string
|
||||
}
|
||||
|
||||
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) {
|
||||
func (app *Application) fileHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == http.MethodPost {
|
||||
app.uploadHandler(w, r)
|
||||
return
|
||||
@ -44,16 +39,12 @@ func (app *Application) indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if fileInfo, err := os.Stat(path); err == nil && !fileInfo.IsDir() {
|
||||
http.ServeFile(w, r, path)
|
||||
} else {
|
||||
http.StripPrefix("/", http.FileServer(http.Dir("static"))).ServeHTTP(w, r)
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
func (app *Application) lastHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if app.lastUploadedFile == "" {
|
||||
http.Error(w, "No new files uploaded yet", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
http.ServeFile(w, r, app.lastUploadedFile)
|
||||
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) {
|
||||
@ -96,8 +87,6 @@ func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "Error copying the file", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
app.lastUploadedFile = filepath
|
||||
|
||||
if app.url == "" {
|
||||
fmt.Fprintf(w, "http://localhost%s/%s\n", app.port, filename)
|
||||
} else {
|
||||
|
Loading…
Reference in New Issue
Block a user