/
server.go
95 lines (73 loc) · 1.99 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
package appserver
import (
"io/ioutil"
"net/http"
"github.com/pkg/errors"
)
const (
HeaderAppSignature = "shopware-app-signature"
HeaderPayloadSignature = "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
}
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)
}
return srv
}
func WithCredentialStore(store CredentialStore) ServerOpt {
return func(s *Server) {
s.credentialStore = store
}
}
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 := ioutil.ReadAll(req.Body)
if err != nil {
return nil, errors.Wrap(err, "read body")
}
if err := req.Body.Close(); err != nil {
return nil, errors.Wrap(err, "close body")
}
return body, nil
}