-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
187 lines (171 loc) · 5.55 KB
/
main.go
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/julienschmidt/httprouter"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
palitrans "github.com/siongui/pali-transliteration"
pli "github.com/tassa-yoniso-manasi-karoto/pali-transliteration"
)
var (
scheme = map[string]func(string) string{
"latin🠮latin": func(s string) string { return s },
"latin🠮thai2": func(s string) string { return palitrans.RomanToThai(strings.ReplaceAll(s, "’", "")) },
"thai1🠮latin": func(s string) string { return pli.ThaiToLatin(s, 1) },
"thai1🠮thai2": func(s string) string { return "Not available: colloquial thai is a lossy encoding of pali!" },
"thai2🠮latin": func(s string) string { return pli.ThaiToLatin(s, 2) },
"thai2🠮thai2": func(s string) string { return s },
}
index string = `<!DOCTYPE html>
<html>
<head>
<title>Pali Transliteration</title>
<style>
* { font-size: 115%; }
.container { display: flex; }
.input-group, .output-group {
display: flex;
flex-direction: column;
width: 50%;
}
select, textarea {
width: 100%;
padding: 15px;
box-sizing: border-box;
}
textarea { height: 100vh; resize: none; }
select {
text-align: center;
font-weight: bold;
height: auto;
min-height: 2.5em;
align-self: stretch;
}
</style>
</head>
<body>
<div class="container">
<div class="input-group">
<select id="inputSelect">
<option value="latin">Latin/Roman</option>
<option value="thai1">Thai (Colloquial) // อักษรไทย</option>
<option value="thai2">Thai (Pintu) // แบบพินทุ</option>
</select>
<textarea spellcheck="false" id="inputTextArea"></textarea>
</div>
<div class="output-group">
<select id="outputSelect">
<option value="latin">Latin/Roman</option>
<option value="thai2">Thai (Pintu) // แบบพินทุ</option>
</select>
<textarea spellcheck="false" id="outputTextArea" readonly></textarea>
</div>
</div>
<script>
const inputSelect = document.getElementById("inputSelect");
const outputSelect = document.getElementById("outputSelect");
const inputTextArea = document.getElementById("inputTextArea");
const outputTextArea = document.getElementById("outputTextArea");
const apiEndpoint = "http://localhost:8080/process";
async function callAPI() {
const inputValue = inputTextArea.value;
const inputSelection = inputSelect.value;
const outputSelection = outputSelect.value;
console.log(inputSelection+"🠮"+outputSelection);
try {
const response = await fetch(apiEndpoint, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
text: inputValue,
inputSelection: inputSelection,
outputSelection: outputSelection
})
});
if (!response.ok) throw new Error("API request failed 🤖");
const processedText = await response.text();
outputTextArea.value = processedText;
} catch (error) {
console.error(error);
outputTextArea.value = "Couldn't reach server";
}
}
inputTextArea.addEventListener("input", callAPI);
inputSelect.addEventListener("change", callAPI);
outputSelect.addEventListener("change", callAPI);
</script>
</body>
</html>`
)
func init() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
func processTextHandler(w http.ResponseWriter, r *http.Request) {
log.Debug().Msg("Received text processing request")
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Error().Err(err).Msg("Error reading request body")
http.Error(w, "Error reading request body", http.StatusBadRequest)
return
}
//inputText := string(body)
//fmt.Println("resp=", inputText)
var data struct {
Text string `json:"text"`
In string `json:"inputSelection"`
Out string `json:"outputSelection"`
}
err = json.Unmarshal(body, &data)
if err != nil {
log.Error().Err(err).Msg("Invalid JSON data")
http.Error(w, "Invalid JSON data", http.StatusBadRequest)
return
}
//pp.Println(data)
var processedText string
if f, ok := scheme[data.In+"🠮"+data.Out]; ok {
processedText = f(data.Text)
} else {
processedText = "func not found"
}
w.Header().Set("Access-Control-Allow-Origin", "http://localhost:8080")
w.Header().Set("Access-Control-Allow-Methods", "POST")
w.Header().Set("Access-Control-Allow-Headers", "*")
log.Debug().Msg("Processed text.")
fmt.Fprintf(w, processedText)
}
func indexHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
exeDir, err := os.Executable()
if err != nil {
log.Error().Err(err).Msg("Error getting executable path")
}
templatePath := filepath.Join(filepath.Dir(exeDir), "index.html")
tmpl := template.New("index")
if _, err := os.Stat(templatePath); os.IsNotExist(err) {
log.Info().Msg("Serving built-in index.html")
tmpl, err = tmpl.Parse(index)
} else {
log.Info().Msgf("Serving index.html from: %s", templatePath)
tmpl, err = template.ParseFiles(templatePath)
}
if err != nil {
log.Error().Err(err).Msg("Error parsing template")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tmpl.Execute(w, nil)
}
func main() {
router := httprouter.New()
router.GET("/", indexHandler)
router.HandlerFunc("POST", "/process", processTextHandler)
fmt.Println("Server running, exposing http://localhost:8080/")
log.Fatal().Err(http.ListenAndServe(":8080", router))
}