-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
128 lines (103 loc) · 2.71 KB
/
server.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
package appserver
import (
"fmt"
"io"
"net"
"net/http"
"runtime"
"time"
)
const (
AppSignatureKey = "shopware-app-signature"
ShopSignatureKey = "shopware-shop-signature"
)
type ServerOpt func(s *Server)
type Server struct {
confirmationURL string
appName string
appSecret string
webhooks map[string]WebhookHandler
actions map[string]ActionHandler
credentialStore CredentialStore
tokenStore *tokenStore
httpClient *http.Client
}
type Credentials struct {
APIKey string `json:"apiKey"`
SecretKey string `json:"secretKey"`
Timestamp string `json:"timestamp" query:"timestamp"`
ShopURL string `json:"shopUrl" query:"shop-url"`
ShopID string `json:"shopId" query:"shop-id"`
ShopSecret string `json:"shopSecret"`
}
type Source struct {
ShopID string `json:"shopId"`
ShopURL string `json:"url"`
AppVersion string `json:"appVersion"`
}
type AppRequest struct {
Source Source `json:"source"`
}
func NewServer(appName string, appSecret string, confirmationURL string, opts ...ServerOpt) *Server {
credentialStore := NewMemoryCredentialStore()
srv := &Server{
webhooks: make(map[string]WebhookHandler),
actions: make(map[string]ActionHandler),
credentialStore: credentialStore,
tokenStore: newTokenStore(),
confirmationURL: confirmationURL,
appName: appName,
appSecret: appSecret,
}
for _, o := range opts {
o(srv)
}
if srv.httpClient == nil {
srv.httpClient = createDefaultHTTPClient()
}
return srv
}
func WithCredentialStore(store CredentialStore) ServerOpt {
return func(s *Server) {
s.credentialStore = store
}
}
func WithHTTPClient(client *http.Client) ServerOpt {
return func(s *Server) {
s.httpClient = client
}
}
func (srv *Server) Event(event string, handler WebhookHandler) {
srv.webhooks[event] = handler
}
func (srv *Server) Action(entity string, action string, handler ActionHandler) {
srv.actions[entity+action] = handler
}
func extractBody(req *http.Request) ([]byte, error) {
body, err := io.ReadAll(req.Body)
if err != nil {
return nil, fmt.Errorf("read body: %w", err)
}
if err := req.Body.Close(); err != nil {
return nil, fmt.Errorf("close body: %w", err)
}
return body, nil
}
func createDefaultHTTPClient() *http.Client {
return &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
ForceAttemptHTTP2: true,
MaxIdleConnsPerHost: runtime.GOMAXPROCS(0) + 1,
},
}
}