diff --git a/main.go b/main.go index fde24bd..67f8401 100644 --- a/main.go +++ b/main.go @@ -19,8 +19,10 @@ 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") + 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() http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) { w.Write([]byte("

Error!

Check your webserver config, You should not see this!

")) @@ -32,9 +34,6 @@ func main() { 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) @@ -49,12 +48,16 @@ func main() { } log.Println("Saved!") + + if _, err := w.Write([]byte("OK")); err != nil { + log.Panic(err) + } }) 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 { + if err != nil { // First time data = []byte("{}") } if _, err := w.Write(data); err != nil { @@ -62,6 +65,7 @@ func main() { } log.Println("Sent!") }) + err := anyhttp.ListenAndServe(*addr, nil) if err != nil { log.Panic(err)