diff options
| author | Julian Hurst <ark@mansus.space> | 2025-01-23 03:14:14 +0100 |
|---|---|---|
| committer | Julian Hurst <ark@mansus.space> | 2025-01-23 03:14:14 +0100 |
| commit | 0408d03a29c7aced3d4730df8fee1662cba4a4dd (patch) | |
| tree | 5797905a642e2f8368a796f783fe3a5791364629 | |
| download | box-0408d03a29c7aced3d4730df8fee1662cba4a4dd.tar.gz | |
Initial commit
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | go.mod | 3 | ||||
| -rw-r--r-- | main.go | 63 | ||||
| -rw-r--r-- | templates/index.html | 32 |
4 files changed, 100 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..697f5b3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +box +data @@ -0,0 +1,3 @@ +module simpleshare + +go 1.23.5 @@ -0,0 +1,63 @@ +package main + +import ( + "net/http" + "html/template" + "log" + "fmt" + "embed" + "io" + "os" + "path" + "path/filepath" +) + +//go:embed templates +var tmplFS embed.FS + +type BoxHandler struct { + dataPath string +} + +func serve(w http.ResponseWriter, views ...string) { + t, err := template.New("index.html").ParseFS(tmplFS, views...) + if err != nil { + log.Fatal(err) + } + if err := t.Execute(w, nil); err != nil { + log.Fatal(err) + } +} + +func (handler BoxHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case http.MethodGet: + serve(w, "templates/index.html") + return + case http.MethodPost: + filename := filepath.Join(handler.dataPath, path.Base(r.URL.Path)) + log.Printf("boxing %s\n", filename) + f, err := os.Create(filename) + if err != nil { + fmt.Fprint(w, err.Error()) + w.WriteHeader(http.StatusInternalServerError) + return + } + defer r.Body.Close() + io.Copy(f, r.Body) + log.Printf("boxed %s\n", filename) + default: + w.WriteHeader(http.StatusMethodNotAllowed) + } +} + +func main() { + boxHandler := BoxHandler { + "data", + } + err := os.MkdirAll(boxHandler.dataPath, 0750) + if err != nil { + log.Fatal(err) + } + log.Fatal(http.ListenAndServe(":8080", boxHandler)) +} diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..220c8c9 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,32 @@ +<!DOCTYPE HTML> +<html> + <head> + <meta name="viewport" content="width=device-width, initial-scale=1" /> + <title>box</title> + <script> + function upload() { + f = document.getElementById("file").files[0]; + const xhr = new XMLHttpRequest(); + xhr.upload.addEventListener("progress", (e) => { + p = document.getElementById("progress") + ratio = e.loaded / e.total; + perc = Math.floor(ratio * 100); + p.innerHTML = perc + "%"; + }); + xhr.open("POST", "/" + f.name, true); + xhr.send(f) + /*const response = await fetch("/" + name, { + method: "POST", + body: f, + });*/ + } + </script> + </head> + <body> + <h1>Box</h1> + <pre>Server for uploading files. Use the form here or send a POST request to /[filename] with the content of the file in the body.</pre> + <input type="file" id="file"/><br/><br/> + <button type="button" onclick="upload()">Upload</button><br/> + <span id="progress"></span> + </body> +</html> |
