Skip to content

Commit 6cf6d08

Browse files
authored
Merge pull request #183 from netlify/chore/remove-commons
Remove usage of netlify-commons
2 parents e6d3d4d + 753862a commit 6cf6d08

File tree

8 files changed

+172
-100
lines changed

8 files changed

+172
-100
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
language: go
33

44
go:
5-
- "1.11"
5+
- "1.13"
66

77
env:
88
global:
@@ -11,4 +11,4 @@ env:
1111
install: make deps
1212
script: make all
1313
notifications:
14-
email: false
14+
email: false

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.11
1+
FROM golang:1.13
22

33
RUN useradd -m netlify
44

api/api.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ package api
33
import (
44
"context"
55
"net/http"
6+
"os"
7+
"os/signal"
68
"regexp"
9+
"syscall"
10+
"time"
711

812
"github.com/jinzhu/gorm"
913
"github.com/sebest/xff"
@@ -15,7 +19,6 @@ import (
1519
"github.com/go-chi/chi"
1620
"github.com/netlify/gocommerce/conf"
1721
gcontext "github.com/netlify/gocommerce/context"
18-
"github.com/netlify/netlify-commons/graceful"
1922
)
2023

2124
const (
@@ -38,13 +41,34 @@ type API struct {
3841
// ListenAndServe starts the REST API.
3942
func (a *API) ListenAndServe(hostAndPort string) {
4043
log := logrus.WithField("component", "api")
41-
server := graceful.NewGracefulServer(a.handler, log)
42-
if err := server.Bind(hostAndPort); err != nil {
43-
log.WithError(err).Fatal("http server bind failed")
44+
server := &http.Server{
45+
Addr: hostAndPort,
46+
Handler: a.handler,
4447
}
4548

46-
if err := server.Listen(); err != nil {
47-
log.WithError(err).Fatal("http server listen failed")
49+
done := make(chan struct{})
50+
defer close(done)
51+
go func() {
52+
waitForTermination(log, done)
53+
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
54+
defer cancel()
55+
server.Shutdown(ctx)
56+
}()
57+
58+
if err := server.ListenAndServe(); err != nil {
59+
log.WithError(err).Fatal("API server failed")
60+
}
61+
}
62+
63+
// WaitForShutdown blocks until the system signals termination or done has a value
64+
func waitForTermination(log logrus.FieldLogger, done <-chan struct{}) {
65+
signals := make(chan os.Signal, 1)
66+
signal.Notify(signals, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
67+
select {
68+
case sig := <-signals:
69+
log.Infof("Triggering shutdown from signal %s", sig)
70+
case <-done:
71+
log.Infof("Shutting down...")
4872
}
4973
}
5074

api/payments_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ func TestPaymentCreate(t *testing.T) {
427427
callCount := 0
428428
stripe.SetBackend(stripe.APIBackend, NewTrackingStripeBackend(func(method, path, key string, params stripe.ParamsContainer, v interface{}) {
429429
switch path {
430-
case "/charges":
430+
case "/v1/charges":
431431
payload := params.GetParams()
432432
fmt.Println("meta:", payload.Metadata)
433433
assert.Equal(t, test.Data.firstOrder.ID, payload.Metadata["order_id"])

conf/configuration.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
"github.com/joho/godotenv"
77
"github.com/kelseyhightower/envconfig"
8-
"github.com/netlify/netlify-commons/nconf"
98
)
109

1110
// DBConfiguration holds all the database related configuration.
@@ -39,7 +38,7 @@ type GlobalConfiguration struct {
3938
Endpoint string
4039
}
4140
DB DBConfiguration
42-
Logging nconf.LoggingConfig `envconfig:"LOG"`
41+
Logging LoggingConfig `envconfig:"LOG"`
4342
OperatorToken string `split_words:"true"`
4443
MultiInstanceMode bool
4544
SMTP SMTPConfiguration `json:"smtp"`
@@ -126,7 +125,7 @@ func LoadGlobal(filename string) (*GlobalConfiguration, error) {
126125
if err := envconfig.Process("gocommerce", config); err != nil {
127126
return nil, err
128127
}
129-
if _, err := nconf.ConfigureLogging(&config.Logging); err != nil {
128+
if _, err := ConfigureLogging(&config.Logging); err != nil {
130129
return nil, err
131130
}
132131
return config, nil

conf/logging.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package conf
2+
3+
import (
4+
"os"
5+
"time"
6+
7+
"github.com/sirupsen/logrus"
8+
)
9+
10+
type LoggingConfig struct {
11+
Level string `mapstructure:"log_level" json:"log_level"`
12+
File string `mapstructure:"log_file" json:"log_file"`
13+
DisableColors bool `mapstructure:"disable_colors" split_words:"true" json:"disable_colors"`
14+
QuoteEmptyFields bool `mapstructure:"quote_empty_fields" split_words:"true" json:"quote_empty_fields"`
15+
TSFormat string `mapstructure:"ts_format" json:"ts_format"`
16+
Fields map[string]interface{} `mapstructure:"fields" json:"fields"`
17+
UseNewLogger bool `mapstructure:"use_new_logger",split_words:"true"`
18+
}
19+
20+
func ConfigureLogging(config *LoggingConfig) (*logrus.Entry, error) {
21+
logger := logrus.New()
22+
23+
tsFormat := time.RFC3339Nano
24+
if config.TSFormat != "" {
25+
tsFormat = config.TSFormat
26+
}
27+
// always use the full timestamp
28+
logger.SetFormatter(&logrus.TextFormatter{
29+
FullTimestamp: true,
30+
DisableTimestamp: false,
31+
TimestampFormat: tsFormat,
32+
DisableColors: config.DisableColors,
33+
QuoteEmptyFields: config.QuoteEmptyFields,
34+
})
35+
36+
// use a file if you want
37+
if config.File != "" {
38+
f, errOpen := os.OpenFile(config.File, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0664)
39+
if errOpen != nil {
40+
return nil, errOpen
41+
}
42+
logger.SetOutput(f)
43+
logger.Infof("Set output file to %s", config.File)
44+
}
45+
46+
if config.Level != "" {
47+
level, err := logrus.ParseLevel(config.Level)
48+
if err != nil {
49+
return nil, err
50+
}
51+
logger.SetLevel(level)
52+
logger.Debug("Set log level to: " + logger.GetLevel().String())
53+
}
54+
55+
f := logrus.Fields{}
56+
for k, v := range config.Fields {
57+
f[k] = v
58+
}
59+
60+
return logger.WithFields(f), nil
61+
}

go.mod

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,50 @@
11
module github.com/netlify/gocommerce
22

33
require (
4-
cloud.google.com/go v0.0.0-20170822200954-98f5696b1026
4+
cloud.google.com/go v0.0.0-20170822200954-98f5696b1026 // indirect
55
github.com/GoogleCloudPlatform/cloudsql-proxy v0.0.0-20170623214735-571947b0f240
66
github.com/PuerkitoBio/goquery v1.1.0
7-
github.com/andybalholm/cascadia v0.0.0-20161224141413-349dd0209470
8-
github.com/davecgh/go-spew v1.1.1 // indirect
7+
github.com/andybalholm/cascadia v0.0.0-20161224141413-349dd0209470 // indirect
8+
github.com/denisenkom/go-mssqldb v0.0.0-20190906004059-62cf760a6c9e // indirect
99
github.com/dgrijalva/jwt-go v3.0.0+incompatible
10-
github.com/fsnotify/fsnotify v0.0.0-20170329110642-4da3e2cfbabc
10+
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 // indirect
1111
github.com/go-chi/chi v3.1.0+incompatible
1212
github.com/go-sql-driver/mysql v1.3.0
13-
github.com/golang/protobuf v0.0.0-20170816001514-ab9f9a6dab16
14-
github.com/hashicorp/hcl v0.0.0-20170509225359-392dba7d905e
15-
github.com/imdario/mergo v0.0.0-20160216103600-3e95a51e0639
16-
github.com/inconshreveable/mousetrap v1.0.0
13+
github.com/golang/protobuf v0.0.0-20170816001514-ab9f9a6dab16 // indirect
14+
github.com/inconshreveable/mousetrap v1.0.0 // indirect
1715
github.com/jinzhu/gorm v1.9.1
18-
github.com/jinzhu/inflection v0.0.0-20170102125226-1c35d901db3d
16+
github.com/jinzhu/inflection v0.0.0-20170102125226-1c35d901db3d // indirect
17+
github.com/jinzhu/now v1.0.1 // indirect
1918
github.com/joho/godotenv v0.0.0-20161216230537-726cc8b906e3
2019
github.com/kelseyhightower/envconfig v1.3.0
21-
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515
20+
github.com/kr/pretty v0.1.0 // indirect
2221
github.com/lib/pq v0.0.0-20170306183709-ca5bc43047f2
23-
github.com/magiconair/properties v1.7.3
22+
github.com/logpacker/PayPal-Go-SDK v2.0.5+incompatible // indirect
2423
github.com/mattes/vat v0.0.0-20160607175015-805d21ad0739
2524
github.com/mattn/go-sqlite3 v1.10.0
2625
github.com/mitchellh/mapstructure v0.0.0-20170523030023-d0303fe80992
27-
github.com/nats-io/nats v1.2.2
28-
github.com/nats-io/nuid v0.0.0-20170303150224-3cf34f9fca4e
2926
github.com/netlify/PayPal-Go-SDK v0.0.0-20180614154051-732c3d08bf8a
3027
github.com/netlify/mailme v0.0.0-20170821082834-c4a76ce443c1
31-
github.com/netlify/netlify-commons v0.4.5
3228
github.com/pariz/gountries v0.0.0-20171019111738-adb00f6513a3
3329
github.com/pborman/uuid v0.0.0-20160209185913-a97ce2ca70fa
34-
github.com/pelletier/go-toml v0.0.0-20170628012637-69d355db5304
3530
github.com/pkg/errors v0.8.0
36-
github.com/pmezard/go-difflib v1.0.0 // indirect
31+
github.com/plutov/paypal v2.0.5+incompatible // indirect
3732
github.com/rs/cors v0.0.0-20170608165155-8dd4211afb5d
38-
github.com/rybit/nats_logrus_hook v1.0.4
3933
github.com/sebest/xff v0.0.0-20160910043805-6c115e0ffa35
40-
github.com/signalfx/com_signalfx_metrics_protobuf v0.0.0-20170330202426-93e507b42f43
41-
github.com/signalfx/gohistogram v0.0.0-20160107210732-1ccfd2ff5083
42-
github.com/signalfx/golib v1.0.0
43-
github.com/sirupsen/logrus v0.0.0-20170713114250-a3f95b5c4235
44-
github.com/spf13/afero v0.0.0-20170217164146-9be650865eab
45-
github.com/spf13/cast v1.1.0
34+
github.com/sirupsen/logrus v1.4.2
4635
github.com/spf13/cobra v0.0.0-20170228191748-fcd0c5a1df88
47-
github.com/spf13/jwalterweatherman v0.0.0-20170523133247-0efa5202c046
48-
github.com/spf13/pflag v1.0.0
49-
github.com/spf13/viper v0.0.0-20170217163817-7538d73b4eb9
50-
github.com/streadway/amqp v0.0.0-20170707203015-2cbfe40c9341
36+
github.com/spf13/pflag v1.0.0 // indirect
5137
github.com/stretchr/testify v1.2.2
52-
github.com/stripe/stripe-go v52.0.0+incompatible
53-
golang.org/x/net v0.0.0-20170721033204-ab5485076ff3
54-
golang.org/x/oauth2 v0.0.0-20170807180024-9a379c6b3e95
55-
golang.org/x/sys v0.0.0-20170721163517-c4489faa6e5a
56-
golang.org/x/text v0.0.0-20170714085652-836efe42bb4a
57-
google.golang.org/api v0.0.0-20170821230356-dd6bdadc5852
58-
google.golang.org/appengine v0.0.0-20170814190942-d9a072cfa7b9
59-
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc
60-
gopkg.in/gomail.v2 v2.0.0-20150902115704-41f357289737
61-
gopkg.in/logfmt.v0 v0.3.0
62-
gopkg.in/stack.v1 v1.6.0
63-
gopkg.in/yaml.v2 v2.0.0-20170721122051-25c4ec802a7d
38+
github.com/stripe/stripe-go v62.9.0+incompatible
39+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 // indirect
40+
golang.org/x/oauth2 v0.0.0-20170807180024-9a379c6b3e95 // indirect
41+
golang.org/x/sync v0.0.0-20190423024810-112230192c58 // indirect
42+
google.golang.org/api v0.0.0-20170821230356-dd6bdadc5852 // indirect
43+
google.golang.org/appengine v0.0.0-20170814190942-d9a072cfa7b9 // indirect
44+
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
45+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
46+
gopkg.in/gomail.v2 v2.0.0-20150902115704-41f357289737 // indirect
47+
gopkg.in/yaml.v2 v2.0.0-20170721122051-25c4ec802a7d // indirect
6448
)
49+
50+
go 1.13

0 commit comments

Comments
 (0)