-
Notifications
You must be signed in to change notification settings - Fork 1
/
header.go
54 lines (45 loc) · 1.33 KB
/
header.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/json"
"fmt"
"os"
"time"
"github.com/http-wasm/http-wasm-guest-tinygo/handler"
"github.com/http-wasm/http-wasm-guest-tinygo/handler/api"
)
// Config the plugin configuration.
type Config struct {
Headers map[string]string `json:"headers,omitempty"`
}
type Middleware struct {
Config *Config
Start time.Time
}
var mw = &Middleware{}
func init() {
err := json.Unmarshal(handler.Host.GetConfig(), &mw.Config)
if err != nil {
handler.Host.Log(api.LogLevelError, fmt.Sprintf("Could not load config %v", err))
os.Exit(1)
}
}
func main() {
handler.HandleRequestFn = mw.handleRequest
handler.HandleResponseFn = mw.handleResponse
}
// handleRequest implements a simple request middleware.
func (mw *Middleware) handleRequest(req api.Request, resp api.Response) (next bool, reqCtx uint32) {
handler.Host.Log(api.LogLevelDebug, "hello from handleRequest debug")
mw.Start = time.Now()
handler.Host.Log(api.LogLevelDebug, "time is "+mw.Start.String())
for k, v := range mw.Config.Headers {
req.Headers().Add(k, v)
}
// proceed to the next handler on the host.
next = true
return
}
// handleResponse implements a simple response middleware.
func (mw *Middleware) handleResponse(_ uint32, req api.Request, resp api.Response, _ bool) {
handler.Host.Log(api.LogLevelDebug, time.Since(mw.Start).String())
}