-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kantoku.go
125 lines (102 loc) · 3.19 KB
/
kantoku.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
package main
import (
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
"github.com/gorilla/mux"
"github.com/nats-io/nats.go"
"github.com/oasisprotocol/curve25519-voi/primitives/ed25519"
"github.com/sirupsen/logrus"
)
const version = "dev"
func main() {
k := &Kantoku{
Logger: logrus.New(),
}
var err error
if err = k.loadConfig(); err != nil {
k.Logger.Fatal("failed to load config: ", err)
}
/* configure logging */
formatter := Formatter{
TimestampFormat: k.Config.Kantoku.Logging.TimeFormat,
PrintColors: true,
}
k.Logger.SetFormatter(formatter)
level, err := logrus.ParseLevel(k.Config.Kantoku.Logging.Level)
if err != nil {
k.Logger.Warnln("unable to parse configured log level:", err)
level = logrus.InfoLevel
}
k.Logger.SetLevel(level)
/* decode public key */
if k.PublicKey, err = hex.DecodeString(k.Config.Kantoku.PublicKey); err != nil {
k.Logger.Fatal("failed to decode public key: ", err)
}
/* prepare no_responders reply. */
if k.Config.Kantoku.Nats.NoResponders != nil {
b, err := json.Marshal(map[string]any{
"type": 4,
"data": k.Config.Kantoku.Nats.NoResponders,
})
if err != nil {
k.Logger.Warnln("unable to encode 'no_responders' reply: ", err)
} else {
k.NoResponders = b
}
}
/* prepare nats client */
nc, err := nats.Connect(strings.Join(k.Config.Kantoku.Nats.Servers, ", "))
if err != nil {
k.Logger.Fatal("connecting to NATS server failed: ", err)
}
k.NatsConn = nc
k.Logger.Infoln("connected to NATS server!")
/* starting Server */
k.Logger.Infof("starting w/ version: %s...", version)
defer k.Logger.Infoln("stopping...")
handler := mux.NewRouter()
/* setup router middleware */
handler.Use(func(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Powered-By", "catboys")
handler.ServeHTTP(w, r)
})
})
handler.Use(func(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
lw := NewLogResponseWriter(w)
start := time.Now()
handler.ServeHTTP(lw, r)
stop := time.Now()
k.Logger.Infof("%s %s %s %d %s", r.RemoteAddr, r.Method, r.URL, lw.StatusCode, stop.Sub(start).String())
})
})
/* expose routes */
handler.HandleFunc("/v1", k.GetIndex).Methods(http.MethodGet)
handler.HandleFunc("/v1/info", k.GetInfo).Methods(http.MethodGet)
handler.HandleFunc("/v1/interactions", k.PostInteractions).Methods(http.MethodPost)
if k.Config.Kantoku.Server.ExposeTestRoute {
k.Logger.Warnln("the interaction testing route has been exposed, interactions using any public-key can be published.")
handler.HandleFunc("/v1/interactions-test", k.PostInteractionsTest).Methods(http.MethodPost)
}
addr := fmt.Sprintf("%s:%d", k.Config.Kantoku.Server.Host, k.Config.Kantoku.Server.Port)
server := &http.Server{
Addr: addr,
Handler: handler,
}
k.Logger.Infoln("listening on", addr)
if err = server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
k.Logger.Fatal("error while running server: ", err)
}
}
type Kantoku struct {
NatsConn *nats.Conn
NoResponders []byte
Config Config
Logger *logrus.Logger
PublicKey ed25519.PublicKey
}