-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.go
72 lines (58 loc) · 1.4 KB
/
api.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
package payments
import (
"fmt"
"log"
"net/http"
"github.com/manyminds/api2go"
"github.com/michaljemala/payments-sample/pkg/domain"
"github.com/michaljemala/payments-sample/pkg/internal/store/sql"
)
type Config struct {
Prefix string
Driver string
DSN string
Logger *log.Logger
}
type API struct {
config Config
db *sql.DB
handler http.Handler
}
const jsonApiContentType = "application/vnd.api+json"
func NewAPI(c Config) (*API, error) {
db, err := sql.Connect(sql.Config{
Driver: c.Driver,
DSN: c.DSN,
})
if err != nil {
return nil, fmt.Errorf("unable to connect to store: %v", err)
}
txManager := sql.NewTxManager(db)
paymentStore := newPaymentStore()
enumStore := newEnumStore()
service := newPaymentService(txManager, paymentStore, enumStore, c.Logger)
api := newAPI(c, service)
api.db = db
return api, nil
}
func newAPI(c Config, service paymentService) *API {
api := api2go.NewAPI(c.Prefix)
api.ContentType = jsonApiContentType
api.AddResource(&domain.Payment{}, newResource(service))
return &API{config: c, handler: api.Handler()}
}
func (api *API) Prefix() string {
return api.config.Prefix
}
func (api *API) ServeHTTP(w http.ResponseWriter, r *http.Request) {
api.handler.ServeHTTP(w, r)
}
func (api *API) Close() error {
if api.db != nil {
err := api.db.Close()
if err != nil {
return fmt.Errorf("unable to close store: %v", err)
}
}
return nil
}