Skip to content

Commit

Permalink
Bootstrap service (#95)
Browse files Browse the repository at this point in the history
## tl;dr

- Creates the initial command
- Boostraps the core server
- Bootstraps the API
- Proto generation
- Docker-compose configuration
  • Loading branch information
neekolas authored Jul 29, 2024
2 parents 6c5a7f0 + 076aca2 commit be31c0b
Show file tree
Hide file tree
Showing 26 changed files with 1,066 additions and 2 deletions.
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# build folder
build/
bin/

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

*.db
.DS_STORE

# Dependency directories (remove the comment below to include it)
# vendor/
9 changes: 9 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations.
// Extension identifier format: ${publisher}.${name}. Example: vscode.csharp

// List of extensions which should be recommended for users of this workspace.
"recommendations": ["bradymholt.pgformatter", "golang.go"],
// List of extensions recommended by VS Code that should not be recommended for users of this workspace.
"unwantedRecommendations": []
}
7 changes: 6 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
{
}
"pgFormatter.typeCase": "uppercase",
"pgFormatter.tabs": true,
"[sql]": {
"editor.defaultFormatter": "bradymholt.pgformatter"
}
}
116 changes: 116 additions & 0 deletions cmd/replication/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package main

import (
"context"
"log"
"os"
"os/signal"
"sync"
"syscall"

"github.com/jessevdk/go-flags"
"github.com/xmtp/xmtpd/pkg/registry"
"github.com/xmtp/xmtpd/pkg/server"
"github.com/xmtp/xmtpd/pkg/tracing"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

var Commit string

var options server.Options

func main() {
if _, err := flags.Parse(&options); err != nil {
if err, ok := err.(*flags.Error); !ok || err.Type != flags.ErrHelp {
fatal("Could not parse options: %s", err)
}
return
}
addEnvVars()

log, _, err := buildLogger(options)
if err != nil {
fatal("Could not build logger: %s", err)
}

ctx, cancel := context.WithCancel(context.Background())

var wg sync.WaitGroup
doneC := make(chan bool, 1)
tracing.GoPanicWrap(ctx, &wg, "main", func(ctx context.Context) {
s, err := server.New(ctx, log, options, registry.NewFixedNodeRegistry([]registry.Node{}))
if err != nil {
log.Fatal("initializing server", zap.Error(err))
}
s.WaitForShutdown()
doneC <- true
})

sigC := make(chan os.Signal, 1)
signal.Notify(sigC,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT,
)
select {
case sig := <-sigC:
log.Info("ending on signal", zap.String("signal", sig.String()))
case <-doneC:
}
cancel()
wg.Wait()
}

func addEnvVars() {
if connStr, hasConnstr := os.LookupEnv("WRITER_DB_CONNECTION_STRING"); hasConnstr {
options.DB.WriterConnectionString = connStr
}

if connStr, hasConnstr := os.LookupEnv("READER_DB_CONNECTION_STRING"); hasConnstr {
options.DB.WriterConnectionString = connStr
}

if privKey, hasPrivKey := os.LookupEnv("PRIVATE_KEY"); hasPrivKey {
options.PrivateKeyString = privKey
}
}

func fatal(msg string, args ...any) {
log.Fatalf(msg, args...)
}

func buildLogger(options server.Options) (*zap.Logger, *zap.Config, error) {
atom := zap.NewAtomicLevel()
level := zapcore.InfoLevel
err := level.Set(options.LogLevel)
if err != nil {
return nil, nil, err
}
atom.SetLevel(level)

cfg := zap.Config{
Encoding: options.LogEncoding,
Level: atom,
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stderr"},
EncoderConfig: zapcore.EncoderConfig{
MessageKey: "message",
LevelKey: "level",
EncodeLevel: zapcore.CapitalLevelEncoder,
TimeKey: "time",
EncodeTime: zapcore.ISO8601TimeEncoder,
NameKey: "caller",
EncodeCaller: zapcore.ShortCallerEncoder,
},
}
log, err := cfg.Build()
if err != nil {
return nil, nil, err
}

log = log.Named("replication")

return log, &cfg, nil
}
14 changes: 14 additions & 0 deletions dev/.golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
linters:
# Disable all linters.
disable-all: true
# Enable specific linter
# https://golangci-lint.run/usage/linters/#enabled-by-default-linters
enable:
- nakedret
- nilerr
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- unused
36 changes: 36 additions & 0 deletions dev/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# BUILD IMAGE --------------------------------------------------------
ARG GO_VERSION=unknown
FROM golang:${GO_VERSION}-alpine as builder

# Get build tools and required header files
RUN apk add --no-cache build-base

WORKDIR /app
COPY . .

# Build the final node binary
ARG GIT_COMMIT=unknown
RUN go build -ldflags="-X 'main.Commit=$GIT_COMMIT'" -o bin/xmtpd cmd/replication/main.go

# ACTUAL IMAGE -------------------------------------------------------

FROM alpine:3.12

ARG GIT_COMMIT=unknown

LABEL maintainer="[email protected]"
LABEL source="https://github.com/xmtp/xmtpd"
LABEL description="XMTP Node Software"
LABEL commit=$GIT_COMMIT

# color, nocolor, json
ENV GOLOG_LOG_FMT=nocolor

# go-waku default port
EXPOSE 9000

COPY --from=builder /app/bin/xmtpd /usr/bin/

ENTRYPOINT ["/usr/bin/xmtpd"]
# By default just show help if called without arguments
CMD ["--help"]
5 changes: 5 additions & 0 deletions dev/docker/compose
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
set -e
. dev/docker/env

docker_compose "$@"
14 changes: 14 additions & 0 deletions dev/docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
services:
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: xmtp
ports:
- 8765:5432

prometheus:
image: prom/prometheus
ports:
- 9090:9090
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
6 changes: 6 additions & 0 deletions dev/docker/env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
set -e

function docker_compose() {
docker-compose -f dev/docker/docker-compose.yml -p xmtpd "$@"
}
9 changes: 9 additions & 0 deletions dev/docker/prometheus.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
global:
scrape_interval: 10s

scrape_configs:
- job_name: prometheus
static_configs:
- targets:
- "host.docker.internal:8008"
- "host.docker.internal:8009"
6 changes: 6 additions & 0 deletions dev/docker/up
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
set -e
. dev/docker/env

docker_compose build
docker_compose up -d --remove-orphans
11 changes: 11 additions & 0 deletions dev/up
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash
set -e

go mod tidy

if ! which golangci-lint &>/dev/null; then brew install golangci-lint; fi
if ! which shellcheck &>/dev/null; then brew install shellcheck; fi
if ! which mockery &>/dev/null; then brew install mockery; fi

dev/generate
dev/docker/up
52 changes: 51 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,53 @@
module github.com/xmtp/xmtpd

go 1.21.11
go 1.22

require (
github.com/ethereum/go-ethereum v1.14.7
github.com/grpc-ecosystem/grpc-gateway/v2 v2.21.0
github.com/jessevdk/go-flags v1.6.1
github.com/pires/go-proxyproto v0.7.0
github.com/stretchr/testify v1.9.0
go.uber.org/zap v1.27.0
google.golang.org/genproto/googleapis/api v0.0.0-20240725223205-93522f1f2a9f
google.golang.org/grpc v1.65.0
google.golang.org/protobuf v1.34.2
gopkg.in/DataDog/dd-trace-go.v1 v1.66.0
)

require (
github.com/DataDog/appsec-internal-go v1.6.0 // indirect
github.com/DataDog/datadog-agent/pkg/obfuscate v0.48.0 // indirect
github.com/DataDog/datadog-agent/pkg/remoteconfig/state v0.48.1 // indirect
github.com/DataDog/datadog-go/v5 v5.3.0 // indirect
github.com/DataDog/go-libddwaf/v3 v3.2.1 // indirect
github.com/DataDog/go-tuf v1.0.2-0.5.2 // indirect
github.com/DataDog/sketches-go v1.4.5 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.2.1 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/ebitengine/purego v0.6.0-alpha.5 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/holiman/uint256 v1.3.0 // indirect
github.com/outcaste-io/ristretto v0.2.3 // indirect
github.com/philhofer/fwd v1.1.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/secure-systems-lab/go-securesystemslib v0.7.0 // indirect
github.com/tinylib/msgp v1.1.8 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240723171418-e6d459c13d2a // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit be31c0b

Please sign in to comment.