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 @@ + + + + + + NixShare + + +

Nixshare

+

Usage:

+

Upload file:

+
+        curl -d @FILENAME https://nixshare.ru/upload
+    
+

Upload clipboard:

+

X:

+
+        xclip -o | curl -d @- https://nixshare.ru/upload
+    
+

Wayland (wl-clipboard required):

+
+        wl-paste | curl -d @- https://nixshare.ru/upload
+    
+

Shell alias:

+

You can add shell aliases to .bashrc or .zshrc file:

+

X:

+
+        alias nsh="xclip -o | curl -d @- https://nixshare.ru/upload 2>/dev/null | xclip"
+    
+

Wayland (wl-clipboard required):

+
+        alias nsh="wl-paste | curl -d @- https://nixshare.ru/upload 2>/dev/null | wl-copy"
+    
+

Author:

+

Alexander NeonXP Kiryukhin <a.kiryukhin@mail.ru>

+

Source code:

+ gitrepo.ru/neonxp/nixshare + + \ 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 @@ - - - - - - NixShare - - -

Nixshare

-

Usage:

-

Upload file:

-
-        curl -d @FILENAME https://nixshare.ru/upload
-    
-

Upload clipboard:

-

X:

-
-        xclip -o | curl -d @- https://nixshare.ru/upload
-    
-

Wayland (wl-clipboard required):

-
-        wl-paste | curl -d @- https://nixshare.ru/upload
-    
-

Shell alias:

-

You can add shell aliases to .bashrc or .zshrc file:

-

X:

-
-        alias nsh="xclip -o | curl -d @- https://nixshare.ru/upload 2>/dev/null | xclip"
-    
-

Wayland (wl-clipboard required):

-
-        alias nsh="wl-paste | curl -d @- https://nixshare.ru/upload 2>/dev/null | wl-copy"
-    
- - \ 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)