Minor cleanup and improve cli doc

This commit is contained in:
Balakrishnan Balasubramanian 2024-09-01 23:33:47 -04:00
parent 643b444b62
commit dfa1d08cdf

16
main.go
View File

@ -19,8 +19,10 @@ var html []byte
func main() { func main() {
addr := flag.String("address", "localhost:3242", "See go.balki.me/anyhttp for usage") addr := flag.String("address", "localhost:3242", "Listen address. See go.balki.me/anyhttp for usage")
dumpFile := flag.String("dumpfile", "dump.json", "local storage file path") dumpFile := flag.String("dumpfile", "dump.json", "Path where local storage is saved in server")
flag.Parse()
http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) { 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>")) w.Write([]byte("<h2>Error!</h2><p>Check your webserver config, You should not see this!</p>"))
@ -32,9 +34,6 @@ func main() {
http.HandleFunc("/wrap/saveLS", func(w http.ResponseWriter, r *http.Request) { 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) data, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
@ -49,12 +48,16 @@ func main() {
} }
log.Println("Saved!") log.Println("Saved!")
if _, err := w.Write([]byte("OK")); err != nil {
log.Panic(err)
}
}) })
http.HandleFunc("/wrap/loadLS", func(w http.ResponseWriter, _ *http.Request) { http.HandleFunc("/wrap/loadLS", func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
data, err := os.ReadFile(*dumpFile) data, err := os.ReadFile(*dumpFile)
if err != nil { if err != nil { // First time
data = []byte("{}") data = []byte("{}")
} }
if _, err := w.Write(data); err != nil { if _, err := w.Write(data); err != nil {
@ -62,6 +65,7 @@ func main() {
} }
log.Println("Sent!") log.Println("Sent!")
}) })
err := anyhttp.ListenAndServe(*addr, nil) err := anyhttp.ListenAndServe(*addr, nil)
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)