aboutsummaryrefslogtreecommitdiff
path: root/hswc/main.go
diff options
context:
space:
mode:
authorPřemysl Eric Janouch <p@janouch.name>2022-10-06 00:44:09 +0200
committerPřemysl Eric Janouch <p@janouch.name>2022-10-08 17:00:55 +0200
commit5ebc35ad11989328c0c417230805b4ff9ed7a045 (patch)
treef448ff5bb4a8c7b7728f46f41e3a300ebbeb564b /hswc/main.go
parentc3cc608570aa473d8777496e857e69637507da25 (diff)
downloadhaven-5ebc35ad11989328c0c417230805b4ff9ed7a045.tar.gz
haven-5ebc35ad11989328c0c417230805b4ff9ed7a045.tar.xz
haven-5ebc35ad11989328c0c417230805b4ff9ed7a045.zip
Add a basic tool for collecting web comments
Diffstat (limited to 'hswc/main.go')
-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))
+}