initial commit
This commit is contained in:
commit
643b444b62
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
remote-local-storage
|
||||
dump.json
|
5
go.mod
Normal file
5
go.mod
Normal file
@ -0,0 +1,5 @@
|
||||
module go.balki.me/remote-local-storage
|
||||
|
||||
go 1.23.0
|
||||
|
||||
require go.balki.me/anyhttp v0.3.0
|
2
go.sum
Normal file
2
go.sum
Normal file
@ -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=
|
69
main.go
Normal file
69
main.go
Normal file
@ -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("<h2>Error!</h2><p>Check your webserver config, You should not see this!</p>"))
|
||||
})
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
46
wrap.html
Normal file
46
wrap.html
Normal file
@ -0,0 +1,46 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<link rel="icon" href="data:;base64,iVBORw0KGgo=" />
|
||||
<script>
|
||||
|
||||
(async () => {
|
||||
const resp = await fetch("loadLS");
|
||||
const lsItems = JSON.parse(await resp.text());
|
||||
for (const key in lsItems) {
|
||||
localStorage.setItem(key, lsItems[key])
|
||||
}
|
||||
console.log("Loaded local storage");
|
||||
})();
|
||||
|
||||
function postLocalStorage() {
|
||||
(async () => {
|
||||
const resp = await fetch("saveLS", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(localStorage),
|
||||
});
|
||||
console.log(await resp.text());
|
||||
})();
|
||||
}
|
||||
|
||||
setInterval(postLocalStorage, 10 * 60 * 1000) // 10 mins
|
||||
|
||||
</script>
|
||||
<style>
|
||||
body {
|
||||
margin: 0;
|
||||
}
|
||||
iframe {
|
||||
width: 100vw;
|
||||
height: 100vh;
|
||||
border: none;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<iframe src="/"></iframe>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user