commit 643b444b6247b8da17e910b1f95912138beafa55 Author: Balakrishnan Balasubramanian Date: Fri Aug 30 18:16:40 2024 -0400 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5035580 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +remote-local-storage +dump.json diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ba50663 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module go.balki.me/remote-local-storage + +go 1.23.0 + +require go.balki.me/anyhttp v0.3.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..50912eb --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +go.balki.me/anyhttp v0.3.0 h1:WtBQ0rnkg567sX/O4ij/+qBbdCIUt5VURSe718sITBY= +go.balki.me/anyhttp v0.3.0/go.mod h1:JhfekOIjgVODoVqUCficjpIgmB3wwlB7jhN0eN2EZ/s= diff --git a/main.go b/main.go new file mode 100644 index 0000000..fde24bd --- /dev/null +++ b/main.go @@ -0,0 +1,69 @@ +// Save and serve HTML local storage in server +package main + +import ( + "bytes" + _ "embed" + "flag" + "io" + "log" + "net/http" + "os" + "time" + + "go.balki.me/anyhttp" +) + +//go:embed wrap.html +var html []byte + +func main() { + + addr := flag.String("address", "localhost:3242", "See go.balki.me/anyhttp for usage") + dumpFile := flag.String("dumpfile", "dump.json", "local storage file path") + + http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) { + w.Write([]byte("

Error!

Check your webserver config, You should not see this!

")) + }) + + http.HandleFunc("/wrap/", func(w http.ResponseWriter, r *http.Request) { + http.ServeContent(w, r, "index.html", time.Now(), bytes.NewReader(html)) + }) + + http.HandleFunc("/wrap/saveLS", func(w http.ResponseWriter, r *http.Request) { + + if _, err := w.Write([]byte("OK")); err != nil { + log.Panic(err) + } + data, err := io.ReadAll(r.Body) + if err != nil { + log.Panic(err) + } + + if err := os.WriteFile(*dumpFile, data, 0o400); err != nil { + log.Panic(err) + } + + if err != nil { + log.Panic(err) + } + + log.Println("Saved!") + }) + + http.HandleFunc("/wrap/loadLS", func(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Type", "application/json") + data, err := os.ReadFile(*dumpFile) + if err != nil { + data = []byte("{}") + } + if _, err := w.Write(data); err != nil { + log.Panic(err) + } + log.Println("Sent!") + }) + err := anyhttp.ListenAndServe(*addr, nil) + if err != nil { + log.Panic(err) + } +} diff --git a/wrap.html b/wrap.html new file mode 100644 index 0000000..c17254c --- /dev/null +++ b/wrap.html @@ -0,0 +1,46 @@ + + + + + + + + + + +