forked from fiatjaf/flowies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
54 lines (46 loc) · 1.34 KB
/
web.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 main
import (
"encoding/base64"
"os"
"strconv"
"strings"
"github.com/valyala/fasthttp"
)
func main() {
PORT := os.Getenv("PORT")
BASEDOMAIN := os.Getenv("BASEDOMAIN")
if BASEDOMAIN == "" {
BASEDOMAIN = "cantillon.alhur.es:" + PORT
}
full := os.Getenv("COUCHDB_URL")
frag := strings.Split(full, "/")[2]
userpass := strings.Split(frag, "@")[0]
COUCHDB_AUTH := base64.StdEncoding.EncodeToString([]byte(userpass))
COUCHDB_URL := "https://" + strings.Split(full, "@")[1]
client := fasthttp.Client{}
h := func(ctx *fasthttp.RequestCtx) {
domain := string(ctx.URI().Host())
if strings.HasSuffix(domain, BASEDOMAIN) {
domain = strings.Split(domain, ".")[0]
}
key := domain + "~" + string(ctx.Path())
url := COUCHDB_URL + "/_design/sites/_list/page/pages?key=\"" + key + "\""
req := fasthttp.AcquireRequest()
req.Header.Add("Authorization", "Basic "+COUCHDB_AUTH)
req.SetRequestURI(url)
resp := fasthttp.AcquireResponse()
err := client.Do(req, resp)
if err != nil {
ctx.Error("Error when loading '"+url+"': "+err.Error(), 503)
return
}
code := resp.StatusCode()
if code != fasthttp.StatusOK {
ctx.Error("Unexpected code when loading '"+url+"': "+strconv.Itoa(code), code)
return
}
ctx.SetContentType("text/html; charset=utf8")
ctx.SetBody(resp.Body())
}
fasthttp.ListenAndServe(":"+PORT, h)
}