This repository has been archived by the owner on Aug 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
72 lines (58 loc) · 1.54 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
package getresidence
import (
"encoding/base64"
"net/http"
"github.com/efficientgo/core/errcapture"
"github.com/efficientgo/core/errors"
"github.com/hako/branca"
"github.com/stripe/stripe-go/v75/client"
"github.com/uptrace/bunrouter"
)
type server struct {
db grDb
branca *branca.Branca
stripe *client.API
}
func New(stripe *client.API, dbUrl string, brancaKeyBase64 string) (*server, error) {
db, err := newDb(dbUrl)
if err != nil {
return nil, errors.New("new db")
}
brancaKey, err := base64.StdEncoding.DecodeString(brancaKeyBase64)
if err != nil {
return nil, errors.Wrap(err, "decode branca key")
}
branca := branca.NewBranca(string(brancaKey))
s := &server{
db,
branca,
stripe,
}
return s, nil
}
func errorHandler(next bunrouter.HandlerFunc) bunrouter.HandlerFunc {
return func(w http.ResponseWriter, r bunrouter.Request) error {
if err := next(w, r); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return nil
}
}
func (s *server) Routes() *bunrouter.Router {
r := bunrouter.New(bunrouter.Use(errorHandler))
r.GET("/", s.getRoot)
r.GET("/privacy", s.getPrivacy)
r.GET("/dubai", s.getDubai())
r.PUT("/dubai/name", s.putDubaiName())
r.PUT("/dubai/phone", s.putDubaiPhone())
r.PUT("/dubai/email", s.putDubaiEmail())
r.POST("/dubai/checkout", s.postDubaiCheckout())
return r
}
func (s *server) RegisterDomainRoutes(m map[string]*bunrouter.Router) {
m["getresidence.org"] = s.Routes()
}
func (s *server) Close() (err error) {
errcapture.Do(&err, s.db.Close, "close db")
return
}