Compare commits
2 Commits
90f63f4c54
...
4ed73c3086
Author | SHA1 | Date | |
---|---|---|---|
4ed73c3086 | |||
468fb823e9 |
3
abyss.go
3
abyss.go
@ -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,
|
||||||
|
19
handlers.go
19
handlers.go
@ -20,9 +20,14 @@ type Application struct {
|
|||||||
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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user