summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorAlexander Neonxp Kiryukhin <i@neonxp.ru>2024-08-28 13:47:44 +0300
committerAlexander Neonxp Kiryukhin <i@neonxp.ru>2024-08-28 13:47:44 +0300
commit459b87480e76d9be275b0ee8a30e4b691af8aaba (patch)
tree6981c676d5c52c955240b464601d9311fb30a888 /main.go
initial
Diffstat (limited to 'main.go')
-rw-r--r--main.go98
1 files changed, 98 insertions, 0 deletions
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..b9154b8
--- /dev/null
+++ b/main.go
@@ -0,0 +1,98 @@
+package main
+
+import (
+ "context"
+ "flag"
+ "io/fs"
+ "log"
+ "net/http"
+ "os"
+ "os/signal"
+ "path/filepath"
+ "time"
+
+ "golang.org/x/sync/errgroup"
+)
+
+var (
+ host string
+ listen string
+ path 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(&path, "path", "./storage", "storage directory")
+ flag.DurationVar(&ttl, "ttl", 24*time.Hour, "time to delete uploaded file")
+}
+
+func main() {
+ ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
+ defer cancel()
+
+ flag.Parse()
+
+ r := http.NewServeMux()
+
+ r.Handle("POST /upload", http.HandlerFunc(uploadHandler))
+ r.Handle("/", http.FileServer(http.Dir(path)))
+
+ srv := http.Server{
+ Addr: listen,
+ Handler: r,
+ }
+
+ eg, egCtx := errgroup.WithContext(ctx)
+
+ eg.Go(func() error {
+ <-egCtx.Done()
+ srv.Close()
+
+ return nil
+ })
+
+ eg.Go(func() error {
+ if err := srv.ListenAndServe(); err != http.ErrServerClosed {
+ return err
+ }
+
+ return nil
+ })
+
+ eg.Go(func() error {
+ timer := time.NewTimer(1 * time.Minute)
+ defer timer.Stop()
+ for {
+ select {
+ case <-timer.C:
+ now := time.Now()
+ err := filepath.Walk(path, func(p string, info fs.FileInfo, err error) error {
+ if info == nil {
+ return nil
+ }
+ if info.IsDir() {
+ return nil
+ }
+ if now.Sub(info.ModTime()).Seconds() > ttl.Seconds() {
+ log.Println("delete old file:", p)
+ if err := os.Remove(p); err != nil {
+ return err
+ }
+ }
+ return nil
+ })
+ if err != nil {
+ log.Println(err)
+ }
+ case <-egCtx.Done():
+ return nil
+ }
+ }
+ })
+
+ if err := eg.Wait(); err != nil {
+ log.Fatal(err)
+ }
+}