added uploads
This commit is contained in:
parent
b6bd406d31
commit
5ba960613c
55
main.go
55
main.go
@ -3,14 +3,28 @@ package main
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const postBody = `
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<form enctype="multipart/form-data" action="/upload/" method="POST">
|
||||||
|
<input name="file" type="file" /><br />
|
||||||
|
<input type="submit" value="Upload File" />
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`
|
||||||
|
|
||||||
var port = flag.Int("port", 8000, "port from which to serve")
|
var port = flag.Int("port", 8000, "port from which to serve")
|
||||||
var hidden = flag.Bool("hidden", false, "allow serving hidden dirs")
|
var hidden = flag.Bool("hidden", false, "allow serving hidden dirs")
|
||||||
|
var canUpload = flag.Bool("upload", false, "enable upload interface")
|
||||||
|
|
||||||
func logger(h http.Handler) http.Handler {
|
func logger(h http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -24,6 +38,38 @@ func logger(h http.Handler) http.Handler {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func upload(w http.ResponseWriter, r *http.Request) {
|
||||||
|
switch r.Method {
|
||||||
|
case "GET":
|
||||||
|
log.Printf("upload get")
|
||||||
|
fmt.Fprintf(w, postBody)
|
||||||
|
case "POST":
|
||||||
|
file, header, err := r.FormFile("file")
|
||||||
|
if err != nil {
|
||||||
|
msg := fmt.Sprintf("problem picking off file from request: %v", err)
|
||||||
|
http.Error(w, msg, http.StatusBadRequest)
|
||||||
|
log.Printf(msg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Printf("upload for: %v", header.Filename)
|
||||||
|
defer file.Close()
|
||||||
|
f, err := os.Create(header.Filename)
|
||||||
|
if err != nil {
|
||||||
|
msg := fmt.Sprintf("problem creating upload file: %v", err)
|
||||||
|
http.Error(w, msg, http.StatusInternalServerError)
|
||||||
|
log.Printf(msg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if _, err := io.Copy(f, file); err != nil {
|
||||||
|
msg := fmt.Sprintf("problem copying file: %v", err)
|
||||||
|
http.Error(w, msg, http.StatusInternalServerError)
|
||||||
|
log.Printf(msg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
hostname, err := os.Hostname()
|
hostname, err := os.Hostname()
|
||||||
@ -32,8 +78,13 @@ func main() {
|
|||||||
}
|
}
|
||||||
log.Printf("serving on: http://%s:%d/", hostname, *port)
|
log.Printf("serving on: http://%s:%d/", hostname, *port)
|
||||||
addr := fmt.Sprintf(":%d", *port)
|
addr := fmt.Sprintf(":%d", *port)
|
||||||
fh := http.FileServer(http.Dir("./"))
|
|
||||||
if err := http.ListenAndServe(addr, logger(fh)); err != nil {
|
http.Handle("/", logger(http.FileServer(http.Dir("./"))))
|
||||||
|
if *canUpload {
|
||||||
|
log.Printf("WARNING: uploading enabled")
|
||||||
|
http.HandleFunc("/upload/", upload)
|
||||||
|
}
|
||||||
|
if err := http.ListenAndServe(addr, nil); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user