aboutsummaryrefslogtreecommitdiff
path: root/cmd/_old/johaus/main.go
blob: c18928e9d7c80a1e470b4a121e4d2dde89f41907 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Command johaus wraps the lojban parser camxes and presents the results over HTTP.
package main

import (
	"encoding/json"
	"flag"
	"io"
	"log"
	"net/http"

	"tulpa.dev/cadey/jvozba"
	"within.website/johaus/parser"
	_ "within.website/johaus/parser/camxes"
	"within.website/johaus/pretty"
	"within.website/x/internal"
)

var (
	port    = flag.String("port", "9001", "TCP port to bind on for HTTP")
	dialect = flag.String("dialect", "camxes", "Lojban dialect to use")
)

func main() {
	internal.HandleStartup()

	log.Printf("Listening on http://0.0.0.0:%s", *port)

	http.HandleFunc("/tree", tree)
	http.HandleFunc("/braces", braces)
	http.HandleFunc("/lujvo", lujvo)

	http.ListenAndServe(":"+*port, nil)
}

func lujvo(w http.ResponseWriter, r *http.Request) {
	data, err := io.ReadAll(r.Body)
	if err != nil {
		http.Error(w, "can't read: "+err.Error(), http.StatusBadRequest)
		return
	}
	defer r.Body.Close()

	jvo, err := jvozba.Jvozba(string(data))
	if err != nil {
		http.Error(w, "can't read: "+err.Error(), http.StatusBadRequest)
		return
	}

	w.Header().Set("X-Content-Language", "jbo")
	http.Error(w, jvo, http.StatusOK)
}

func braces(w http.ResponseWriter, r *http.Request) {
	data, err := io.ReadAll(r.Body)
	if err != nil {
		http.Error(w, "can't parse: "+err.Error(), http.StatusBadRequest)
		return
	}
	defer r.Body.Close()

	tree, err := parser.Parse(*dialect, string(data))
	if err != nil {
		http.Error(w, "can't parse: "+err.Error(), http.StatusBadRequest)
		return
	}

	parser.RemoveMorphology(tree)
	parser.AddElidedTerminators(tree)
	parser.RemoveSpace(tree)
	parser.CollapseLists(tree)

	pretty.Braces(w, tree)
}

func tree(w http.ResponseWriter, r *http.Request) {
	data, err := io.ReadAll(r.Body)
	if err != nil {
		http.Error(w, "can't parse: "+err.Error(), http.StatusBadRequest)
		return
	}
	defer r.Body.Close()

	tree, err := parser.Parse(*dialect, string(data))
	if err != nil {
		http.Error(w, "can't parse: "+err.Error(), http.StatusBadRequest)
		return
	}

	parser.RemoveMorphology(tree)
	parser.AddElidedTerminators(tree)
	parser.RemoveSpace(tree)
	parser.CollapseLists(tree)

	json.NewEncoder(w).Encode(tree)
}