aboutsummaryrefslogtreecommitdiff
path: root/hswc
diff options
context:
space:
mode:
Diffstat (limited to 'hswc')
-rw-r--r--hswc/main.go68
1 files changed, 68 insertions, 0 deletions
diff --git a/hswc/main.go b/hswc/main.go
new file mode 100644
index 0000000..99aa3c6
--- /dev/null
+++ b/hswc/main.go
@@ -0,0 +1,68 @@
+package main
+
+import (
+ "fmt"
+ "log"
+ "net/http"
+ "os"
+ "regexp"
+ "strings"
+ "sync"
+ "time"
+)
+
+var (
+ long = regexp.MustCompile(`(.{72}\S*)\s+`)
+ file *os.File
+ m sync.Mutex
+)
+
+func wrap(s string) string {
+ return strings.ReplaceAll(long.ReplaceAllString(
+ strings.ReplaceAll(s, "\r", ""), "$1\n"), "\n", "\n ")
+}
+
+func handler(w http.ResponseWriter, r *http.Request) {
+ defer r.Body.Close()
+ if err := r.ParseForm(); err != nil {
+ w.WriteHeader(http.StatusBadRequest)
+ return
+ }
+ text := r.FormValue("text")
+ if len(text) > 64<<10 {
+ w.WriteHeader(http.StatusBadRequest)
+ return
+ }
+
+ m.Lock()
+ defer m.Unlock()
+
+ if s, err := file.Stat(); err != nil {
+ log.Fatalln(err)
+ } else if s.Size()+int64(len(text)) > 64<<20 {
+ w.WriteHeader(http.StatusInternalServerError)
+ } else {
+ fmt.Fprintf(file, "%s %s\n %s\n",
+ time.Now().Local().Format(time.RFC1123), r.RequestURI, wrap(text))
+ if err := file.Sync(); err != nil {
+ log.Fatalln(err)
+ }
+
+ fmt.Fprintln(w, "Saved.")
+ }
+}
+
+func main() {
+ if len(os.Args) != 3 {
+ log.Fatalf("Usage: %s BIND DB\n", os.Args[0])
+ }
+
+ var err error
+ if file, err = os.OpenFile(
+ os.Args[2], os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644); err != nil {
+ log.Fatalln(err)
+ }
+
+ http.HandleFunc("/", handler)
+ log.Fatalln(http.ListenAndServe(os.Args[1], nil))
+}