-
Notifications
You must be signed in to change notification settings - Fork 2
/
demo.go
131 lines (114 loc) · 3.66 KB
/
demo.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
// Package plugindemo a demo plugin.
package main
import (
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"os"
"time"
"github.com/http-wasm/http-wasm-guest-tinygo/handler"
"github.com/http-wasm/http-wasm-guest-tinygo/handler/api"
_ "github.com/stealthrocket/net/http"
"github.com/stealthrocket/net/wasip1"
)
func main() {}
func init() {
// Because there is no file mounted in the plugin by default, we configure insecureSkipVerify to avoid having to load rootCas
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} //nolint:gosec
// Because there is no file mounted in the plugin by default, we configure a default resolver to 1.1.1.1
net.DefaultResolver = &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := wasip1.Dialer{
Timeout: time.Millisecond * time.Duration(3000), //nolint:mnd
}
return d.DialContext(ctx, "udp", "1.1.1.1:53")
},
}
var config Config
err := json.Unmarshal(handler.Host.GetConfig(), &config)
if err != nil {
handler.Host.Log(api.LogLevelError, fmt.Sprintf("Could not load config %v", err))
os.Exit(1)
}
mw, err := New(config)
if err != nil {
handler.Host.Log(api.LogLevelError, fmt.Sprintf("Could not load config %v", err))
os.Exit(1)
}
handler.HandleRequestFn = mw.handleRequest
}
// Config the plugin configuration.
type Config struct {
HeaderName string `json:"headerName,omitempty"`
Timezone string `json:"timezone,omitempty"`
}
// Demo a Demo plugin.
type Demo struct {
header string
timezone string
}
type WorldTime struct {
Abbreviation string `json:"abbreviation"`
ClientIP string `json:"client_ip"`
Datetime time.Time `json:"datetime"`
DayOfWeek int `json:"day_of_week"`
DayOfYear int `json:"day_of_year"`
Dst bool `json:"dst"`
DstFrom time.Time `json:"dst_from"`
DstOffset int `json:"dst_offset"`
DstUntil time.Time `json:"dst_until"`
RawOffset int `json:"raw_offset"`
Timezone string `json:"timezone"`
Unixtime int `json:"unixtime"`
UtcDatetime time.Time `json:"utc_datetime"`
UtcOffset string `json:"utc_offset"`
WeekNumber int `json:"week_number"`
}
// New created a new Demo plugin.
func New(config Config) (*Demo, error) {
if config.HeaderName == "" {
return nil, errors.New("header name cannot be empty")
}
timezone := "Europe/Paris"
if config.Timezone != "" {
timezone = config.Timezone
}
return &Demo{
header: config.HeaderName,
timezone: timezone,
}, nil
}
func (a *Demo) handleRequest(req api.Request, resp api.Response) (next bool, reqCtx uint32) {
response, err := http.Get("http://worldtimeapi.org/api/timezone/" + a.timezone)
if err != nil {
resp.SetStatusCode(http.StatusInternalServerError)
handler.Host.Log(api.LogLevelError, fmt.Sprintf("Could not fetch time %v", err))
return false, reqCtx
}
if response.StatusCode != http.StatusOK {
resp.SetStatusCode(http.StatusInternalServerError)
handler.Host.Log(api.LogLevelError, fmt.Sprintf("Could not fetch time %s instead of 200", response.Status))
return false, reqCtx
}
body, err := io.ReadAll(response.Body)
if err != nil {
resp.SetStatusCode(http.StatusInternalServerError)
handler.Host.Log(api.LogLevelError, fmt.Sprintf("Could not read body %v", err))
return
}
worldTime := WorldTime{}
err = json.Unmarshal(body, &worldTime)
if err != nil {
resp.SetStatusCode(http.StatusInternalServerError)
handler.Host.Log(api.LogLevelError, fmt.Sprintf("Could not parse body %v", err))
return
}
req.Headers().Set(a.header, worldTime.Datetime.String())
return true, 0
}