summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Dockerfile1
-rw-r--r--Makefile5
-rw-r--r--go.mod3
-rw-r--r--go.sum4
-rw-r--r--html/index.html39
-rw-r--r--main.go24
-rw-r--r--storage/index.html36
-rw-r--r--upload.go2
8 files changed, 69 insertions, 45 deletions
diff --git a/Dockerfile b/Dockerfile
index 1035e2b..1dc5eb6 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -14,6 +14,7 @@ FROM scratch
WORKDIR /app
COPY --from=go /app/nixshare ./nixshare
+COPY --from=go /app/html ./html
EXPOSE 8000
diff --git a/Makefile b/Makefile
index db4ded9..5621cdf 100644
--- a/Makefile
+++ b/Makefile
@@ -6,5 +6,6 @@ deploy:
docker push gitrepo.ru/neonxp/nixshare:latest
docker context use curie
docker pull gitrepo.ru/neonxp/nixshare:latest
- docker rm nixshare || true
- docker run --name nixshare -d -p 8095:8000 -v nixshare:/app/storage gitrepo.ru/neonxp/nixshare \ No newline at end of file
+ docker rm -f nixshare || true
+ docker volume rm -f nixshare_html || true
+ docker run --name nixshare -d -p 8095:8000 -v nixshare:/app/storage -v nixshare_html:/app/html gitrepo.ru/neonxp/nixshare \ No newline at end of file
diff --git a/go.mod b/go.mod
index ad3e9c2..13d2043 100644
--- a/go.mod
+++ b/go.mod
@@ -4,5 +4,8 @@ go 1.23.0
require (
github.com/google/uuid v1.6.0
+ go.neonxp.ru/mux v0.0.0-20240729221623-376839b264c7
golang.org/x/sync v0.8.0
)
+
+require go.neonxp.ru/objectid v0.0.2 // indirect
diff --git a/go.sum b/go.sum
index b1eeaf3..520d67f 100644
--- a/go.sum
+++ b/go.sum
@@ -1,4 +1,8 @@
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
+go.neonxp.ru/mux v0.0.0-20240729221623-376839b264c7 h1:b/AgHRmzwI+FMNrf/HYEILmntTPSRDarOMTYEmL7AS8=
+go.neonxp.ru/mux v0.0.0-20240729221623-376839b264c7/go.mod h1:5OlVmN/OgaWjghQMV8oOzUQC0lUqAoplpQsUiqL5Pgc=
+go.neonxp.ru/objectid v0.0.2 h1:Z/G6zvBxmUq0NTq681oGH8pTbBWwi6VA22YOYludIPs=
+go.neonxp.ru/objectid v0.0.2/go.mod h1:s0dRi//oe1liiKcor1KmWx09WzkD6Wtww8ZaIv+VLBs=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
diff --git a/html/index.html b/html/index.html
new file mode 100644
index 0000000..3d3b20e
--- /dev/null
+++ b/html/index.html
@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <meta charset="UTF-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <title>NixShare</title>
+</head>
+<body>
+ <h1>Nixshare</h1>
+ <h2>Usage:</h2>
+ <h3>Upload file:</h3>
+ <pre>
+ curl -d @FILENAME https://nixshare.ru/upload
+ </pre>
+ <h3>Upload clipboard:</h3>
+ <p>X:</p>
+ <pre>
+ xclip -o | curl -d @- https://nixshare.ru/upload
+ </pre>
+ <p>Wayland (<code>wl-clipboard</code> required):</p>
+ <pre>
+ wl-paste | curl -d @- https://nixshare.ru/upload
+ </pre>
+ <h3>Shell alias:</h3>
+ <p>You can add shell aliases to .bashrc or .zshrc file:</p>
+ <p>X:</p>
+ <pre>
+ alias nsh="xclip -o | curl -d @- https://nixshare.ru/upload 2>/dev/null | xclip"
+ </pre>
+ <p>Wayland (<code>wl-clipboard</code> required):</p>
+ <pre>
+ alias nsh="wl-paste | curl -d @- https://nixshare.ru/upload 2>/dev/null | wl-copy"
+ </pre>
+ <h2>Author:</h2>
+ <p>Alexander NeonXP Kiryukhin &lt;<a href="mailto:a.kiryukhin@mail.ru">a.kiryukhin@mail.ru</a>&gt;</p>
+ <h2>Source code:</h2>
+ <a href="https://gitrepo.ru/neonxp/nixshare">gitrepo.ru/neonxp/nixshare</a>
+</body>
+</html> \ No newline at end of file
diff --git a/main.go b/main.go
index b9154b8..2c15ed6 100644
--- a/main.go
+++ b/main.go
@@ -5,25 +5,30 @@ import (
"flag"
"io/fs"
"log"
+ "log/slog"
"net/http"
"os"
"os/signal"
"path/filepath"
"time"
+ "go.neonxp.ru/mux"
+ "go.neonxp.ru/mux/middleware"
"golang.org/x/sync/errgroup"
)
var (
- host string
- listen string
- path string
- ttl time.Duration
+ host string
+ listen string
+ path string
+ htmlPath string
+ ttl time.Duration
)
func init() {
flag.StringVar(&host, "host", "https://nixshare.ru/", "host of nixshare")
flag.StringVar(&listen, "listen", ":8000", "port to listen")
+ flag.StringVar(&htmlPath, "html_path", "./html", "html directory")
flag.StringVar(&path, "path", "./storage", "storage directory")
flag.DurationVar(&ttl, "ttl", 24*time.Hour, "time to delete uploaded file")
}
@@ -37,11 +42,16 @@ func main() {
r := http.NewServeMux()
r.Handle("POST /upload", http.HandlerFunc(uploadHandler))
- r.Handle("/", http.FileServer(http.Dir(path)))
+ r.Handle("/", http.FileServer(http.Dir("html")))
+ r.Handle("/s/", http.StripPrefix("/s/", http.FileServer(http.Dir(path))))
srv := http.Server{
- Addr: listen,
- Handler: r,
+ Addr: listen,
+ Handler: mux.Use(r,
+ middleware.Recover(slog.Default()),
+ middleware.RequestID,
+ middleware.Logger(slog.Default()),
+ ),
}
eg, egCtx := errgroup.WithContext(ctx)
diff --git a/storage/index.html b/storage/index.html
index 4cfaf0e..a0c2040 100644
--- a/storage/index.html
+++ b/storage/index.html
@@ -1,35 +1 @@
-<!DOCTYPE html>
-<html lang="en">
-<head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>NixShare</title>
-</head>
-<body>
- <h1>Nixshare</h1>
- <h2>Usage:</h2>
- <h3>Upload file:</h3>
- <pre>
- curl -d @FILENAME https://nixshare.ru/upload
- </pre>
- <h3>Upload clipboard:</h3>
- <p>X:</p>
- <pre>
- xclip -o | curl -d @- https://nixshare.ru/upload
- </pre>
- <p>Wayland (<code>wl-clipboard</code> required):</p>
- <pre>
- wl-paste | curl -d @- https://nixshare.ru/upload
- </pre>
- <h3>Shell alias:</h3>
- <p>You can add shell aliases to .bashrc or .zshrc file:</p>
- <p>X:</p>
- <pre>
- alias nsh="xclip -o | curl -d @- https://nixshare.ru/upload 2>/dev/null | xclip"
- </pre>
- <p>Wayland (<code>wl-clipboard</code> required):</p>
- <pre>
- alias nsh="wl-paste | curl -d @- https://nixshare.ru/upload 2>/dev/null | wl-copy"
- </pre>
-</body>
-</html> \ No newline at end of file
+nothing here \ No newline at end of file
diff --git a/upload.go b/upload.go
index 719e519..95a6913 100644
--- a/upload.go
+++ b/upload.go
@@ -36,7 +36,7 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
h.Set("Content-Type", "text/plain; charset=utf-8")
h.Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(http.StatusCreated)
- u, err := url.JoinPath(host, section, uid)
+ u, err := url.JoinPath(host, "s", section, uid)
if err != nil {
log.Println(err)
http.Error(w, "can't get file url", http.StatusInternalServerError)