Skip to content

Commit

Permalink
events-api
Browse files Browse the repository at this point in the history
  • Loading branch information
0xluk committed Oct 2, 2024
1 parent 249a8fd commit 4392b30
Show file tree
Hide file tree
Showing 18 changed files with 3,936 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/push-docker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Deploy prod images to GHCR

on:
push:
tags:
- 'v[0-9]+.[0-9]+.[0-9]+'

jobs:
push-store-image:
runs-on: ubuntu-latest
steps:
- name: 'Checkout GitHub Action'
uses: actions/checkout@main

- name: 'Login to GitHub Container Registry'
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{github.actor}}
password: ${{secrets.GITHUB_TOKEN}}

- name: 'Build Inventory Image'
run: |
docker build . --tag ghcr.io/qubic/go-events:${{github.ref_name}}
docker push ghcr.io/qubic/go-events:${{github.ref_name}}
25 changes: 25 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
on: [push, pull_request]
name: Go Test
jobs:
test-nocache:
strategy:
matrix:
go-version: [1.21.x]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
cache: false
- run: go test ./...

test-cache:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: 1.21.x
- run: go test ./...
22 changes: 22 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM golang:1.22 AS builder
ENV CGO_ENABLED=0

WORKDIR /src
COPY . /src

RUN go mod tidy
WORKDIR /src/app/events-api
RUN go build

# We don't need golang to run binaries, just use alpine.
FROM ubuntu:22.04
RUN apt-get update && apt-get install -y ca-certificates
COPY --from=builder /src/app/events-api/events-api /app/events-api
RUN chmod +x /app/events-api

EXPOSE 8000
EXPOSE 8001

WORKDIR /app

ENTRYPOINT ["./events-api"]
146 changes: 146 additions & 0 deletions app/events-api/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package main

import (
"fmt"
"github.com/cockroachdb/pebble"
"github.com/qubic/go-events/processor"
"github.com/qubic/go-events/server"
"github.com/qubic/go-events/store"
"github.com/qubic/go-qubic/connector"
"log"
"os"
"os/signal"
"syscall"
"time"

"github.com/ardanlabs/conf"
"github.com/pkg/errors"
)

const prefix = "QUBIC_EVENTS"

func main() {
if err := run(); err != nil {
log.Fatalf("main: exited with error: %s", err.Error())
}
}

func run() error {
var cfg struct {
Server struct {
ReadTimeout time.Duration `conf:"default:5s"`
WriteTimeout time.Duration `conf:"default:5s"`
ShutdownTimeout time.Duration `conf:"default:5s"`
HttpHost string `conf:"default:0.0.0.0:8000"`
GrpcHost string `conf:"default:0.0.0.0:8001"`
NodeSyncThreshold int `conf:"default:3"`
ChainTickFetchUrl string `conf:"default:http://127.0.0.1:8080/max-tick"`
}
Pool struct {
SingleNodeIP string `conf:"default:127.0.0.1"`
NodePasscode []uint64 `conf:"default:1;1;1;1"`
NodeFetcherUrl string `conf:"default:http://127.0.0.1:8080/status"`
NodeFetcherTimeout time.Duration `conf:"default:2s"`
InitialCap int `conf:"default:5"`
MaxIdle int `conf:"default:20"`
MaxCap int `conf:"default:30"`
IdleTimeout time.Duration `conf:"default:15s"`
}
Qubic struct {
NodePort string `conf:"default:21841"`
StorageFolder string `conf:"default:store"`
ProcessTickTimeout time.Duration `conf:"default:120s"`
}
}

if err := conf.Parse(os.Args[1:], prefix, &cfg); err != nil {
switch {
case errors.Is(err, conf.ErrHelpWanted):
usage, err := conf.Usage(prefix, &cfg)
if err != nil {
return errors.Wrap(err, "generating config usage")
}
fmt.Println(usage)
return nil
case errors.Is(err, conf.ErrVersionWanted):
version, err := conf.VersionString(prefix, &cfg)
if err != nil {
return errors.Wrap(err, "generating config version")
}
fmt.Println(version)
return nil
}
return errors.Wrap(err, "parsing config")
}

out, err := conf.String(&cfg)
if err != nil {
return errors.Wrap(err, "generating config for output")
}
log.Printf("main: Config :\n%v\n", out)

connectorConfig := connector.Config{
ConnectionPort: cfg.Qubic.NodePort,
ConnectionTimeout: 5 * time.Second,
HandlerRequestTimeout: 5 * time.Second,
}

conn, err := connector.NewConnector(cfg.Pool.SingleNodeIP, connectorConfig)
if err != nil {
return errors.Wrap(err, "creating connector")
}

levelOptions := pebble.LevelOptions{
BlockRestartInterval: 16,
BlockSize: 4096,
BlockSizeThreshold: 90,
Compression: pebble.ZstdCompression,
FilterPolicy: nil,
FilterType: pebble.TableFilter,
IndexBlockSize: 4096,
TargetFileSize: 2097152,
}

pebbleOptions := pebble.Options{
Levels: []pebble.LevelOptions{levelOptions},
}

db, err := pebble.Open(cfg.Qubic.StorageFolder, &pebbleOptions)
if err != nil {
return errors.Wrap(err, "opening db with zstd compression")
}
defer db.Close()

eventsStore := store.NewStore(db)

var passcode [4]uint64
copy(passcode[:], cfg.Pool.NodePasscode)
proc := processor.NewProcessor(conn, eventsStore, cfg.Qubic.ProcessTickTimeout, passcode)

srv := server.NewServer(cfg.Server.GrpcHost, cfg.Server.HttpHost, eventsStore)
err = srv.Start()
if err != nil {
return errors.Wrap(err, "starting server")
}

shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, os.Interrupt, syscall.SIGTERM)

procErrors := make(chan error, 1)

// Start the service listening for requests.
go func() {
procErrors <- proc.Start()
}()

for {
select {
case <-shutdown:
return errors.New("shutting down")
case err := <-procErrors:
return errors.Wrap(err, "events service error")
}
}

return nil
}
43 changes: 43 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,46 @@
module github.com/qubic/go-events

go 1.22.2

require (
github.com/ardanlabs/conf v1.5.0
github.com/cockroachdb/pebble v1.1.2
github.com/grpc-ecosystem/grpc-gateway/v2 v2.21.0
github.com/pkg/errors v0.9.1
github.com/qubic/go-qubic v0.1.4-0.20241002202021-958a2fbb2cb4
google.golang.org/genproto/googleapis/api v0.0.0-20240730163845-b1a4ccb954bf
google.golang.org/grpc v1.65.0
google.golang.org/protobuf v1.34.2
)

require (
github.com/DataDog/zstd v1.4.5 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cloudflare/circl v1.3.8 // indirect
github.com/cockroachdb/errors v1.11.3 // indirect
github.com/cockroachdb/fifo v0.0.0-20240606204812-0bbfbd93a7ce // indirect
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
github.com/cockroachdb/redact v1.1.5 // indirect
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect
github.com/getsentry/sentry-go v0.27.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/klauspost/compress v1.16.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/prometheus/client_golang v1.12.0 // indirect
github.com/prometheus/client_model v0.2.1-0.20210607210712-147c58e9608a // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/silenceper/pool v1.0.0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df // 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
google.golang.org/genproto/googleapis/rpc v0.0.0-20240725223205-93522f1f2a9f // indirect
)
Loading

0 comments on commit 4392b30

Please sign in to comment.