Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
zoedsoupe committed Feb 15, 2024
0 parents commit 6aa377e
Show file tree
Hide file tree
Showing 14 changed files with 633 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export GOPATH="$HOME/go"
export CGO_ENABLED=0
export GOOS=darwin
export GOARCH=arm64
export POSTGRES_USER=$(whoami)
export POSTGRES_PASS=""
export IP_NODE=$(hostname)
export IP_V4_ADDRESS=$(hostname)

export SECRET_KEY_BASE=cyqpgMlaL59EKA4OQMFYT7JksQ6vTYMY
export DATABASE_URL=postgresql://zoedsoupe@localhost:5432/rinha_dev
15 changes: 15 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use flake

function transact {
args=("$@")
valor=$(printf '"valor": %d' ${args[2]})
tipo=$(printf '"tipo": "%s"' ${args[3]})
body=$(printf '{"descricao": "teste", %s, %s}' $valor $tipo)
url=$(printf 'http://localhost:4000/clientes/%d/transacoes' ${args[1]})

echo $valor
echo $tipo
echo $url

curl -X POST -H 'content-type: application/json' -d $body $url
}
41 changes: 41 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: ci

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:
runs-on: ubuntu-latest
env:
GHCR_USERNAME: ${{ github.actor }}
GHCR_TOKEN: ${{ secrets.GHCR_TOKEN }}
FORCE_COLOR: 1
steps:
- uses: actions/checkout@v3
- name: Put back the git branch into git (Earthly uses it for tagging)
run: |
branch=""
if [ -n "$GITHUB_HEAD_REF" ]; then
branch="$GITHUB_HEAD_REF"
else
branch="${GITHUB_REF##*/}"
fi
git checkout -b "$branch" || true
- name: Docker Login
run: docker login https://ghcr.io --username "$GHCR_USERNAME" --password "$GHCR_TOKEN"
- name: Download latest earthly
run: "sudo /bin/sh -c 'wget https://github.com/earthly/earthly/releases/latest/download/earthly-linux-amd64 -O /usr/local/bin/earthly && chmod +x /usr/local/bin/earthly'"
- name: Earthly version
run: earthly --version

- name: Run builder
run: earthly -P --ci +build

- name: Run tests
run: earthly -P --ci +test

- name: Publish image
run: earthly -P --ci --push +docker
29 changes: 29 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
FROM golang:1.22.0-alpine3.19 AS builder

RUN apk update && apk add --no-cache git

WORKDIR /app

COPY . /app

RUN go get -d -v

ENV CGO_ENABLED=0
ENV GOOS=linux
ENV GOARCH=amd64

# Build the binary.
RUN go build -a -installsuffix cgo -ldflags="-w -s" -o rinha .
RUN chmod +x ./rinha

FROM alpine:3.19

ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8

RUN apk add --no-cache ca-certificates

COPY --from=builder --chown=nobody:root /app/rinha /usr/bin/rinha

CMD ["/usr/bin/rinha"]
18 changes: 18 additions & 0 deletions Earthfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
VERSION 0.7
FROM golang:1.22.0-alpine3.19
WORKDIR /rinha

test:
COPY go.mod ./
COPY main.go ./
RUN go test

build:
COPY go.mod ./
COPY main.go ./
RUN go build

docker:
FROM DOCKERFILE .
ARG GITHUB_REPO=cciuenf/rinha-backend-2024-q1
SAVE IMAGE --push ghcr.io/$GITHUB_REPO:latest
39 changes: 39 additions & 0 deletions config/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
CREATE TABLE accounts (
id SERIAL PRIMARY KEY,
name VARCHAR(50) NOT NULL,
limit_amount INTEGER NOT NULL
);

CREATE TABLE transactions (
id SERIAL PRIMARY KEY,
account_id INTEGER NOT NULL,
amount INTEGER NOT NULL,
transaction_type CHAR(1) NOT NULL,
description VARCHAR(10) NOT NULL,
date TIMESTAMP NOT NULL DEFAULT NOW(),
CONSTRAINT fk_accounts_transactions_id
FOREIGN KEY (account_id) REFERENCES accounts(id)
);

CREATE TABLE balances (
id SERIAL PRIMARY KEY,
account_id INTEGER NOT NULL,
amount INTEGER NOT NULL,
CONSTRAINT fk_accounts_balances_id
FOREIGN KEY (account_id) REFERENCES accounts(id)
);

DO $$
BEGIN
INSERT INTO accounts (name, limit_amount)
VALUES
('o barato sai caro', 1000 * 100),
('zan corp ltda', 800 * 100),
('les cruders', 10000 * 100),
('padaria joia de cocaia', 100000 * 100),
('kid mais', 5000 * 100);

INSERT INTO balances (account_id, amount)
SELECT id, 0 FROM accounts;
END;
$$;
22 changes: 22 additions & 0 deletions config/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
events {
worker_connections 1000;
}

http {
access_log off;
sendfile on;

upstream api {
server api01:4000;
server api02:4001;
}

server {
listen 9999;

location / {
proxy_pass http://api;
}
}
}

12 changes: 12 additions & 0 deletions config/postgresql.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# -----------------------------
# PostgreSQL configuration file
# -----------------------------

listen_addresses = '*'

# RESOURCE USAGE
max_connections = 30

# QUERY TUNING
random_page_cost = 1.1
effective_io_concurrency = 30
90 changes: 90 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
version: "3.5"

services:
api01: &api
image: ghcr.io/cciuenf/rinha-backend-2024-q1:latest
hostname: api01
environment:
- DATABASE_URL=postgresql://postgres:postgres@db:5432/rinha
- IP_V4_ADDRESS=192.0.1.11
- IP_NODE=192.0.1.12

ports:
- "4000:4000"
depends_on:
db:
condition: service_healthy
deploy:
resources:
limits:
cpus: "0.6"
memory: "200MB"
networks:
erlcluster:
ipv4_address: 192.0.1.11

api02:
<<: *api
hostname: api02
environment:
- DATABASE_URL=postgresql://postgres:postgres@db:5432/rinha
- IP_V4_ADDRESS=192.0.1.12
- IP_NODE=192.0.1.11
ports:
- "4000:4001"
networks:
erlcluster:
ipv4_address: 192.0.1.12

nginx:
image: nginx:latest
volumes:
- ./config/nginx.conf:/etc/nginx/nginx.conf:ro
depends_on:
- api01
- api02
ports:
- "9999:9999"
deploy:
resources:
limits:
cpus: "0.17"
memory: "10MB"
networks:
erlcluster:
ipv4_address: 192.0.1.13

db:
image: postgres:latest
hostname: db
environment:
- POSTGRES_PASSWORD=postgres
- POSTGRES_USER=postgres
- POSTGRES_DB=rinha
ports:
- "5432:5432"
volumes:
- ./config/init.sql:/docker-entrypoint-initdb.d/init.sql
- ./config/postgresql.conf:/etc/postgresql/postgresql.conf
command: postgres -c config_file=/etc/postgresql/postgresql.conf
deploy:
resources:
limits:
cpus: "0.13"
memory: "140MB"
healthcheck:
test: [ "CMD-SHELL", "pg_isready -U postgres -d rinha" ]
interval: 4s
timeout: 2s
retries: 5
networks:
erlcluster:
ipv4_address: 192.0.1.14

networks:
erlcluster:
driver: bridge
ipam:
driver: default
config:
- subnet: "192.0.1.0/24"
80 changes: 80 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs?rev=9a2dd8e4798be098877175e835eb8e23b85f2c33";
flake-parts.url = "github:hercules-ci/flake-parts";
systems.url = "github:nix-systems/default";
};

outputs = {
flake-parts,
systems,
...
} @ inputs:
flake-parts.lib.mkFlake {inherit inputs;} {
systems = import systems;
perSystem = {pkgs, ...}: {
devShells.default = with pkgs;
mkShell {
name = "rinha-go";
packages = with pkgs;
[go_1_22 nginx postgresql]
++ lib.optional stdenv.isLinux [inotify-tools]
++ lib.optional stdenv.isDarwin [
darwin.apple_sdk.frameworks.CoreServices
darwin.apple_sdk.frameworks.CoreFoundation
];
};
};
};
}
Loading

0 comments on commit 6aa377e

Please sign in to comment.