Skip to content

Commit

Permalink
infra:
Browse files Browse the repository at this point in the history
- docker
- gh workflows
- makefile
- protoc (wip)
- config examples
  • Loading branch information
amirylm committed Sep 18, 2023
1 parent 19609c0 commit e122178
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Lint

on:
push:
branches: [ "main", "dev" ]
pull_request:
branches: [ "*" ]

jobs:

golint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: '1.20'

- name: Format
run: make fmt

- name: Lint
run: make lint
22 changes: 22 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Test

on:
push:
branches: [ "main", "dev" ]
pull_request:
branches: [ "*" ]

jobs:

gotest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: '1.20'

- name: Test
run: make test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@

# Go workspace file
go.work

bin
33 changes: 33 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Build

FROM golang:1.20 AS builder

RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
git make g++ gcc-aarch64-linux-gnu wget \
&& rm -rf /var/lib/apt/lists/*

ARG APP_VERSION
ARG APP_NAME
ARG BUILD_TARGET

WORKDIR /p2pmq

COPY go.mod go.sum ./
RUN go mod download
COPY . .

RUN GOOS=linux CGO_ENABLED=0 go build -tags netgo -a -v -o ./bin/${BUILD_TARGET} ./cmd/${BUILD_TARGET}

# Runtime

FROM alpine:latest as runner

ARG BUILD_TARGET

RUN apk --no-cache --upgrade add ca-certificates bash

WORKDIR /p2pmq

COPY --from=builder /p2pmq/.env* ./
COPY --from=builder /p2pmq/bin/${BUILD_TARGET} ./app
39 changes: 39 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
APP_NAME?=p2pmq
BUILD_TARGET?=${APP_NAME}
BUILD_IMG?=${APP_NAME}
APP_VERSION?=$(git describe --tags $(git rev-list --tags --max-count=1) 2> /dev/null || echo "nightly")
CFG_PATH?=/route-p2p/router.json
TEST_PKG?=./...
TEST_TIMEOUT?=2m

lint:
@docker run --rm -v $(shell pwd):/app -w /app golangci/golangci-lint:latest golangci-lint run -v --timeout=5m ./...

fmt:
@go fmt ./...

test:
@go test -v -race -timeout=${TEST_TIMEOUT} `go list ./... | grep -v -E "cmd|scripts"`

test-pkg:
@go test -v -race -timeout=${TEST_TIMEOUT} ${TEST_PKG}

test-cov:
@go test -v -race -timeout=${TEST_TIMEOUT} -coverprofile cover.out `go list ./... | grep -v -E "cmd|scripts"`

test-open-cov:
@make test-cov
@go tool cover -html cover.out -o cover.html
open cover.html

keygen:
@go run ./cmd/keygen/main.go

build:
@go build -o "./bin/${BUILD_TARGET}" "./cmd/${BUILD_TARGET}"

docker-build:
@docker build -t "${APP_NAME}" --build-arg APP_VERSION="${APP_VERSION}" --build-arg APP_NAME="${APP_NAME}" --build-arg BUILD_TARGET="${BUILD_TARGET}" .

docker-run:
@docker run -d --restart unless-stopped --name "${APP_NAME}" -v "${PWD}/data/${APP_NAME}/:/p2pmq/.data" -p "${TCP_PORT}":"${TCP_PORT}" -p "${GRPC_PORT}":"${GRPC_PORT}" -e "GRPC_PORT=${GRPC_PORT}" -it "${BUILD_IMG}" /p2pmq/app -config=${CFG_PATH}
65 changes: 65 additions & 0 deletions proto/main.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
syntax = "proto3";

option go_package = "github.com/amirylm/p2pmq/proto";

package proto;

service Broadcast {
rpc Publish(PublishRequest) returns (PublishResponse);
}

message PublishRequest {
string topic = 1;
bytes data = 2;
}

message PublishResponse {
string message_id = 1;
}

service MsgValidation {
rpc Listen(stream Message) returns (stream ValidatedMessage) {}
}

service Subscriptions {
rpc Subscribe(SubscribeRequest) returns (SubscribeResponse);
rpc Unsubscribe(UnsubscribeRequest) returns (UnsubscribeResponse);
rpc Listen(ListenRequest) returns (stream Message) {}
}

message Message {
// TODO: use pubsub Message pb
string message_id = 1;
string topic = 2;
bytes data = 3;
}

enum ValidationResult {
ACCEPT = 0;
IGNORE = 1;
REJECT = 2;
}

message ValidatedMessage {
ValidationResult result = 1;
Message msg = 2;
}

message ListenRequest {
repeated string topics = 1;
int64 max_rate = 2;
}

message SubscribeRequest {
string topic = 1;
}

message SubscribeResponse {
string subscription_id = 1;
}

message UnsubscribeRequest {
string subscription_id = 1;
}

message UnsubscribeResponse {}
7 changes: 7 additions & 0 deletions resources/config/bootstrapper.p2pmq.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
listenAddrs:
- /ip4/0.0.0.0/tcp/5001
discovery:
serviceTag: p2pmq/kad
mode: bootstrapper
bootstrappers:
# pubsub:
8 changes: 8 additions & 0 deletions resources/config/default.p2pmq.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
listenAddrs:
- /ip4/0.0.0.0/tcp/0
discovery:
serviceTag: p2pmq/kad
mode: server
bootstrappers:
- /ip4/0.0.0.0/tcp/5001
pubsub:
7 changes: 7 additions & 0 deletions scripts/proto-gen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative ./proto/*.proto

protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative ./proto/**/*.proto

0 comments on commit e122178

Please sign in to comment.