-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
54 lines (42 loc) · 967 Bytes
/
api.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
package api
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/dreamspawn/ribbon-server/config"
)
var resource_dir string
func ConfigReady() {
resource_dir = config.Get("resource_dir")
}
var endpoints = map[string]func(string, url.Values, http.Request) (any, error){
"categories": categoriesAPI,
"translations": translationsAPI,
"glyphs": glyphsAPI,
}
func Handle(endpoint string, vars url.Values, request http.Request) string {
base, sub_path, _ := strings.Cut(endpoint, "/")
var data any
var err error
if function, ok := endpoints[base]; ok {
data, err = function(sub_path, vars, request)
} else {
panic(fmt.Errorf("endpoint %s is not defined", base))
}
if err != nil {
panic(err)
}
json_bytes, err := json.Marshal(data)
if err != nil {
panic(err)
}
var pretty bytes.Buffer
err = json.Indent(&pretty, json_bytes, "", " ")
if err != nil {
panic(err)
}
return pretty.String()
}