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 := http.NewServeMux()
|
||||||
mux.HandleFunc("/", app.indexHandler)
|
mux.HandleFunc("/", app.fileHandler)
|
||||||
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,
|
||||||
|
27
handlers.go
27
handlers.go
@ -16,18 +16,13 @@ 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) treeHandler(w http.ResponseWriter, r *http.Request) {
|
func (app *Application) fileHandler(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
|
||||||
@ -44,16 +39,12 @@ func (app *Application) indexHandler(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.StripPrefix("/", http.FileServer(http.Dir("static"))).ServeHTTP(w, r)
|
http.NotFound(w, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *Application) lastHandler(w http.ResponseWriter, r *http.Request) {
|
func (app *Application) treeHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if app.lastUploadedFile == "" {
|
http.StripPrefix("/tree/", http.FileServer(http.Dir(app.filesDir))).ServeHTTP(w, r)
|
||||||
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) {
|
||||||
@ -96,8 +87,6 @@ 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