Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options for configuring the server #79

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
Expand Down
9 changes: 8 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package main

import (
"os"

"github.com/brave/go-sync/server"
)

func main() {
server.StartServer()
server.StartServer(
server.Opts{
SentryDSN: os.Getenv("SENTRY_DSN"),
Addr: ":8295",
},
)
}
18 changes: 11 additions & 7 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ var (
buildTime string
)

// Opts provide configuration options for the server.
type Opts struct {
SentryDSN string
Addr string
}

func setupLogger(ctx context.Context) (context.Context, *zerolog.Logger) {
ctx = context.WithValue(ctx, appctx.EnvironmentCTXKey, os.Getenv("ENV"))
ctx = context.WithValue(ctx, appctx.LogLevelCTXKey, zerolog.WarnLevel)
Expand Down Expand Up @@ -88,15 +94,14 @@ func setupRouter(ctx context.Context, logger *zerolog.Logger) (context.Context,
return ctx, r
}

// StartServer starts the translate proxy server on port 8195
func StartServer() {
// StartServer starts the translate proxy server.
func StartServer(opts Opts) {
serverCtx, logger := setupLogger(context.Background())

// Setup Sentry.
sentryDsn := os.Getenv("SENTRY_DSN")
if sentryDsn != "" {
if opts.SentryDSN != "" {
err := sentry.Init(sentry.ClientOptions{
Dsn: sentryDsn,
Dsn: opts.SentryDSN,
Release: fmt.Sprintf("go-sync@%s-%s", commit, buildTime),
})
if err != nil {
Expand All @@ -109,8 +114,7 @@ func StartServer() {

serverCtx, r := setupRouter(serverCtx, logger)

port := ":8295"
srv := http.Server{Addr: port, Handler: chi.ServerBaseContext(serverCtx, r)}
srv := http.Server{Addr: opts.Addr, Handler: chi.ServerBaseContext(serverCtx, r)}
err := srv.ListenAndServe()
if err != nil {
sentry.CaptureException(err)
Expand Down