summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMateus Cruz <mateuscolvr@gmail.com>2024-02-04 23:10:30 -0300
committerMateus Cruz <mateuscolvr@gmail.com>2024-02-04 23:10:57 -0300
commitf3ac99a84497868aded8ee7ec2822d1b12960fd7 (patch)
treea870e9bb38259e0641c282b2ba524ac9abe85346
initial commit
-rw-r--r--.envrc1
-rw-r--r--.gitignore2
-rw-r--r--.ocamlformat1
-rw-r--r--bin/dune21
-rw-r--r--bin/main.ml1
-rw-r--r--docker-compose.yml55
-rw-r--r--dune-project26
-rw-r--r--flake.lock113
-rw-r--r--flake.nix70
-rw-r--r--lib/dune2
-rw-r--r--nginx.conf21
-rw-r--r--rinhadebackend.opam31
-rw-r--r--script.sql33
-rw-r--r--test/dune2
-rw-r--r--test/test_rinhadebackend.ml0
15 files changed, 379 insertions, 0 deletions
diff --git a/.envrc b/.envrc
new file mode 100644
index 0000000..3550a30
--- /dev/null
+++ b/.envrc
@@ -0,0 +1 @@
+use flake
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..296fc8c
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+.direnv
+_build
diff --git a/.ocamlformat b/.ocamlformat
new file mode 100644
index 0000000..0619e80
--- /dev/null
+++ b/.ocamlformat
@@ -0,0 +1 @@
+profile = janestreet
diff --git a/bin/dune b/bin/dune
new file mode 100644
index 0000000..0716639
--- /dev/null
+++ b/bin/dune
@@ -0,0 +1,21 @@
+(executable
+ (public_name rinhadebackend)
+ (name main)
+ (flags
+ (:standard -cclib -static -cclib -no-pie))
+ (libraries
+ rinhadebackend
+
+ piaf
+ routes
+ eio_main
+ caqti-driver-postgresql
+ logs.fmt
+ fmt.tty
+ logs.threaded
+ caqti-eio
+ caqti
+ ppx_rapper_eio)
+
+ (preprocess
+ (pps ppx_rapper)))
diff --git a/bin/main.ml b/bin/main.ml
new file mode 100644
index 0000000..7bf6048
--- /dev/null
+++ b/bin/main.ml
@@ -0,0 +1 @@
+let () = print_endline "Hello, World!"
diff --git a/docker-compose.yml b/docker-compose.yml
new file mode 100644
index 0000000..28897d0
--- /dev/null
+++ b/docker-compose.yml
@@ -0,0 +1,55 @@
+version: '3.5'
+
+services:
+ api01: &api
+ image: rinhadebackend:latest
+ environment:
+ - DB_HOST=db
+ ports:
+ - 8081:8080
+ depends_on:
+ - db
+ deploy:
+ resources:
+ limits:
+ cpus: "0.45"
+ memory: "200mb"
+
+ api02:
+ <<: *api
+ environment:
+ - DB_HOST=db
+ ports:
+ - 8082:8080
+
+ nginx:
+ image: nginx:latest
+ volumes:
+ - ./nginx.conf:/etc/nginx/nginx.conf:ro
+ depends_on:
+ - api01
+ - api02
+ ports:
+ - "9999:9999"
+ deploy:
+ resources:
+ limits:
+ cpus: "0.17"
+ memory: "10MB"
+
+ db:
+ image: postgres:latest
+ hostname: db
+ environment:
+ - POSTGRES_PASSWORD=123
+ - POSTGRES_USER=admin
+ - POSTGRES_DB=rinha
+ ports:
+ - "5432:5432"
+ volumes:
+ - ./script.sql:/docker-entrypoint-initdb.d/script.sql
+ deploy:
+ resources:
+ limits:
+ cpus: "0.13"
+ memory: "140MB"
diff --git a/dune-project b/dune-project
new file mode 100644
index 0000000..2e174ca
--- /dev/null
+++ b/dune-project
@@ -0,0 +1,26 @@
+(lang dune 3.13)
+
+(name rinhadebackend)
+
+(generate_opam_files true)
+
+(source
+ (github username/reponame))
+
+(authors "Author Name")
+
+(maintainers "Maintainer Name")
+
+(license LICENSE)
+
+(documentation https://url/to/documentation)
+
+(package
+ (name rinhadebackend)
+ (synopsis "A short synopsis")
+ (description "A longer description")
+ (depends ocaml dune)
+ (tags
+ (topics "to describe" your project)))
+
+; See the complete stanza docs at https://dune.readthedocs.io/en/stable/dune-files.html#dune-project
diff --git a/flake.lock b/flake.lock
new file mode 100644
index 0000000..ed1eb1b
--- /dev/null
+++ b/flake.lock
@@ -0,0 +1,113 @@
+{
+ "nodes": {
+ "flake-utils": {
+ "inputs": {
+ "systems": "systems"
+ },
+ "locked": {
+ "lastModified": 1705309234,
+ "narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=",
+ "owner": "numtide",
+ "repo": "flake-utils",
+ "rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26",
+ "type": "github"
+ },
+ "original": {
+ "owner": "numtide",
+ "repo": "flake-utils",
+ "type": "github"
+ }
+ },
+ "flake-utils_2": {
+ "inputs": {
+ "systems": "systems_2"
+ },
+ "locked": {
+ "lastModified": 1705309234,
+ "narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=",
+ "owner": "numtide",
+ "repo": "flake-utils",
+ "rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26",
+ "type": "github"
+ },
+ "original": {
+ "owner": "numtide",
+ "repo": "flake-utils",
+ "type": "github"
+ }
+ },
+ "nixpkgs": {
+ "inputs": {
+ "flake-utils": "flake-utils_2",
+ "nixpkgs": "nixpkgs_2"
+ },
+ "locked": {
+ "lastModified": 1705609824,
+ "narHash": "sha256-ARrxzdKiiMsAs2zQzxe+XkxTo/eg1FpmvWhFoDgYB/A=",
+ "owner": "nix-ocaml",
+ "repo": "nix-overlays",
+ "rev": "0477e3c5a2c05351707a3e36c462aef4c78f769f",
+ "type": "github"
+ },
+ "original": {
+ "owner": "nix-ocaml",
+ "repo": "nix-overlays",
+ "type": "github"
+ }
+ },
+ "nixpkgs_2": {
+ "locked": {
+ "lastModified": 1705429789,
+ "narHash": "sha256-7gQju9WiToi7wI6oahTXiqwJu2RZoV0cg8OGa9YhEvw=",
+ "owner": "NixOS",
+ "repo": "nixpkgs",
+ "rev": "cc3ab0e45687d15cb21663a95f5a53a05abd39e4",
+ "type": "github"
+ },
+ "original": {
+ "owner": "NixOS",
+ "repo": "nixpkgs",
+ "rev": "cc3ab0e45687d15cb21663a95f5a53a05abd39e4",
+ "type": "github"
+ }
+ },
+ "root": {
+ "inputs": {
+ "flake-utils": "flake-utils",
+ "nixpkgs": "nixpkgs"
+ }
+ },
+ "systems": {
+ "locked": {
+ "lastModified": 1681028828,
+ "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
+ "owner": "nix-systems",
+ "repo": "default",
+ "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
+ "type": "github"
+ },
+ "original": {
+ "owner": "nix-systems",
+ "repo": "default",
+ "type": "github"
+ }
+ },
+ "systems_2": {
+ "locked": {
+ "lastModified": 1681028828,
+ "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
+ "owner": "nix-systems",
+ "repo": "default",
+ "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
+ "type": "github"
+ },
+ "original": {
+ "owner": "nix-systems",
+ "repo": "default",
+ "type": "github"
+ }
+ }
+ },
+ "root": "root",
+ "version": 7
+}
diff --git a/flake.nix b/flake.nix
new file mode 100644
index 0000000..83eb5a3
--- /dev/null
+++ b/flake.nix
@@ -0,0 +1,70 @@
+{
+ inputs = {
+ flake-utils.url = "github:numtide/flake-utils";
+ nixpkgs.url = "github:nix-ocaml/nix-overlays";
+ };
+
+ outputs = { self, nixpkgs, flake-utils }:
+ flake-utils.lib.eachDefaultSystem (system:
+ let
+ pkgs = nixpkgs.legacyPackages.${system}.appendOverlays [
+ (self: super: {
+ ocamlPackages = super.ocaml-ng.ocamlPackages_5_1.overrideScope'
+ (oself: osuper: {
+ pg_query = osuper.pg_query.overrideAttrs (prev: {
+ propagatedBuildInputs = prev.propagatedBuildInputs ++ [ osuper.cmdliner ];
+ });
+ });
+ })
+ ];
+
+ pkgs' = pkgs.pkgsCross.musl64;
+
+ rinhadebackend = pkgs'.ocamlPackages.buildDunePackage {
+ pname = "rinhadebackend";
+ version = "0.0.1";
+ src = ./.;
+ buildInputs = with pkgs'.ocamlPackages; [
+ ppx_rapper
+ ppx_rapper_eio
+ yojson
+ eio_main
+ piaf
+ routes
+ caqti-driver-postgresql
+ ppx_expect
+
+ logs
+ ];
+ };
+
+ in {
+ devShells.default = pkgs'.mkShell rec {
+ nativeBuildInputs = with pkgs'.ocamlPackages; [
+ dune_3
+ findlib
+ ocaml
+ ocaml-lsp
+ ocamlformat
+ ];
+
+ buildInputs = rinhadebackend.buildInputs
+ ++ (with pkgs'.ocamlPackages; [ utop ]);
+ };
+
+ packages.default = rinhadebackend;
+
+ packages.docker = pkgs'.dockerTools.buildImage {
+ name = "ghcr.io/molvrr/rinhadebackend";
+ tag = "latest";
+ copyToRoot = pkgs'.buildEnv {
+ name = "image-root";
+ paths = [ pkgs'.bashInteractive pkgs'.coreutils pkgs'.curl rinhadebackend ];
+ pathsToLink = [ "/bin" ];
+ };
+ config = { Cmd = [ "rinhadebackend" ]; };
+ };
+
+ formatter = pkgs.nixfmt;
+ });
+}
diff --git a/lib/dune b/lib/dune
new file mode 100644
index 0000000..45a8131
--- /dev/null
+++ b/lib/dune
@@ -0,0 +1,2 @@
+(library
+ (name rinhadebackend))
diff --git a/nginx.conf b/nginx.conf
new file mode 100644
index 0000000..60b677f
--- /dev/null
+++ b/nginx.conf
@@ -0,0 +1,21 @@
+events {
+ worker_connections 1000;
+}
+
+http {
+ access_log off;
+ sendfile on;
+
+ upstream api {
+ server api01:8080;
+ server api02:8080;
+ }
+
+ server {
+ listen 9999; # Lembra da porta 9999 obrigatória?
+
+ location / {
+ proxy_pass http://api;
+ }
+ }
+}
diff --git a/rinhadebackend.opam b/rinhadebackend.opam
new file mode 100644
index 0000000..3b957d2
--- /dev/null
+++ b/rinhadebackend.opam
@@ -0,0 +1,31 @@
+# This file is generated by dune, edit dune-project instead
+opam-version: "2.0"
+synopsis: "A short synopsis"
+description: "A longer description"
+maintainer: ["Maintainer Name"]
+authors: ["Author Name"]
+license: "LICENSE"
+tags: ["topics" "to describe" "your" "project"]
+homepage: "https://github.com/username/reponame"
+doc: "https://url/to/documentation"
+bug-reports: "https://github.com/username/reponame/issues"
+depends: [
+ "ocaml"
+ "dune" {>= "3.13"}
+ "odoc" {with-doc}
+]
+build: [
+ ["dune" "subst"] {dev}
+ [
+ "dune"
+ "build"
+ "-p"
+ name
+ "-j"
+ jobs
+ "@install"
+ "@runtest" {with-test}
+ "@doc" {with-doc}
+ ]
+]
+dev-repo: "git+https://github.com/username/reponame.git"
diff --git a/script.sql b/script.sql
new file mode 100644
index 0000000..f5da315
--- /dev/null
+++ b/script.sql
@@ -0,0 +1,33 @@
+CREATE TABLE clients (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(50) NOT NULL,
+ limit INTEGER NOT NULL
+);
+
+CREATE TYPE transaction_type AS ENUM ('c', 'd');
+
+CREATE TABLE transactions (
+ id SERIAL PRIMARY KEY,
+ client_id REFERENCES clients,
+ value INTEGER NOT NULL,
+ type transaction_type NOT NULL,
+ description VARCHAR(10) NOT NULL,
+ created_at TIMESTAMP NOT NULL DEFAULT NOW()
+);
+
+CREATE TABLE balances (
+ id SERIAL PRIMARY KEY,
+ client_id REFERENCES clients,
+ value INTEGER NOT NULL
+);
+
+BEGIN
+ INSERT INTO clients (name, limit)
+ VALUES
+ ('naruto', 1000 * 100),
+ ('mob', 800 * 100),
+ ('jojo', 10000 * 100),
+ ('hellboy', 5000 * 100);
+ INSERT INTO balances (client_id, value)
+ SELECT id, 0 FROM clients;
+END;
diff --git a/test/dune b/test/dune
new file mode 100644
index 0000000..9c10282
--- /dev/null
+++ b/test/dune
@@ -0,0 +1,2 @@
+(test
+ (name test_rinhadebackend))
diff --git a/test/test_rinhadebackend.ml b/test/test_rinhadebackend.ml
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/test_rinhadebackend.ml