2024-08-30 18:16:40 -04:00
|
|
|
// 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() {
|
|
|
|
|
2024-09-01 23:33:47 -04:00
|
|
|
addr := flag.String("address", "localhost:3242", "Listen address. See go.balki.me/anyhttp for usage")
|
|
|
|
dumpFile := flag.String("dumpfile", "dump.json", "Path where local storage is saved in server")
|
|
|
|
|
|
|
|
flag.Parse()
|
2024-08-30 18:16:40 -04:00
|
|
|
|
|
|
|
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) {
|
|
|
|
|
|
|
|
data, err := io.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
2024-09-18 10:37:32 -04:00
|
|
|
if err := os.WriteFile(*dumpFile, data, 0o600); err != nil {
|
2024-08-30 18:16:40 -04:00
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Saved!")
|
2024-09-01 23:33:47 -04:00
|
|
|
|
|
|
|
if _, err := w.Write([]byte("OK")); err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2024-08-30 18:16:40 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
http.HandleFunc("/wrap/loadLS", func(w http.ResponseWriter, _ *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
data, err := os.ReadFile(*dumpFile)
|
2024-09-01 23:33:47 -04:00
|
|
|
if err != nil { // First time
|
2024-08-30 18:16:40 -04:00
|
|
|
data = []byte("{}")
|
|
|
|
}
|
|
|
|
if _, err := w.Write(data); err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
log.Println("Sent!")
|
|
|
|
})
|
2024-09-01 23:33:47 -04:00
|
|
|
|
2024-08-30 18:16:40 -04:00
|
|
|
err := anyhttp.ListenAndServe(*addr, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
}
|