From bf3b56d242742141c6b437d818afecff97753864 Mon Sep 17 00:00:00 2001 From: William Petit Date: Mon, 15 Jun 2020 14:58:59 +0200 Subject: [PATCH] Mise en place du projet --- Makefile | 17 ++ README.md | 58 +++++ backend/config/allow.list | 0 backend/config/dev.yml | 203 ++++++++++++++++++ backend/config/migrations/0_init.sql | 17 ++ backend/config/prod.yml | 67 ++++++ backend/config/seed.js | 19 ++ docker-compose.yml | 30 +++ misc/containers/super-graph/Dockerfile | 36 ++++ .../super-graph/docker-entrypoint.sh | 13 ++ 10 files changed, 460 insertions(+) create mode 100644 Makefile create mode 100644 README.md create mode 100644 backend/config/allow.list create mode 100644 backend/config/dev.yml create mode 100644 backend/config/migrations/0_init.sql create mode 100644 backend/config/prod.yml create mode 100644 backend/config/seed.js create mode 100644 docker-compose.yml create mode 100644 misc/containers/super-graph/Dockerfile create mode 100644 misc/containers/super-graph/docker-entrypoint.sh diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c522d77 --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ +build: + docker-compose build + +up: build + USER_ID=$(shell id -u) docker-compose up + +sg: + docker-compose exec -u $(shell id -u) super-graph sh + +sgr: + docker-compose run -u $(shell id -u) super-graph sh + +down: + docker-compose down -v --remove-orphans + +db-shell: + docker-compose exec postgres psql -Usupergraph \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..a429024 --- /dev/null +++ b/README.md @@ -0,0 +1,58 @@ +# Daddy + +Application de gestion des Dossiers d'Aide à la Décision (D.A.D.) à Cadoles. + +## Démarrage + +### Avec les sources + +#### Dépendances + +- docker +- docker-compose +- nodejs avec npm/yarn + +#### Procédure + +```bash +git clone https://forge.cadoles.com/Cadoles/daddy.git # Cloner le projet +cd daddy # Se placer dans le répertoire +make up # Démarrer l'environnement de développement +``` + +Les services suivants devraient être disponibles après démarrage de l'environnement: + +|Service|Type|Accès|Description| +|-------|----|-----|-----------| +|Application React|HTTP (UI)|http://localhost:8081/|Page d'accueil de l'application React (serveur Webpack)| +|Serveur GraphQL|HTTP (GraphQL)|http://localhost:8080/api/v1/graphql|Point d'entrée de l'API GraphQL| +|Interface Web GraphQL|HTTP (UI)|http://localhost:8080/|Interface Web de développement de l'API GraphQL| +|Serveur PostgreSQL|TCP/IP (PostgreSQL)|`127.0.0.1:5432`|Port de connexion à la base de données PostgreSQL de développement| + +#### Fichiers/répertoires notables + +|Répertoire/Fichier|Description| +|------------------|-----------| +|`docker-compose.yml`|Configuration de l'environnement Docker Compose| +|`frontend/src`|Sources du frontend ([React](https://reactjs.org))| +|`backend/config/migrations`|Migrations SQL pour le backend, voir [la documentation de SuperGraph à ce sujet](https://supergraph.dev/docs/start#migrations)| + +#### Commandes utiles + +##### `make up` + +Démarrer l'environnement de développement + +`Ctrl+C` pour stopper l'environnement. + +##### `make down` + +Stopper et supprimer l'environnement de développement + +##### `make db-shell` + +Ouvrir une console `psql` sur la base de données de développement. + +## Licence + +AGPL-3.0 diff --git a/backend/config/allow.list b/backend/config/allow.list new file mode 100644 index 0000000..e69de29 diff --git a/backend/config/dev.yml b/backend/config/dev.yml new file mode 100644 index 0000000..185f4ce --- /dev/null +++ b/backend/config/dev.yml @@ -0,0 +1,203 @@ +app_name: "Test Development" +host_port: 0.0.0.0:8080 +web_ui: true + +# debug, error, warn, info +log_level: "info" + +# enable or disable http compression (uses gzip) +http_compress: true + +# When production mode is 'true' only queries +# from the allow list are permitted. +# When it's 'false' all queries are saved to the +# the allow list in ./config/allow.list +production: false + +# Throw a 401 on auth failure for queries that need auth +auth_fail_block: false + +# Latency tracing for database queries and remote joins +# the resulting latency information is returned with the +# response +enable_tracing: true + +# Watch the config folder and reload Super Graph +# with the new configs when a change is detected +reload_on_config_change: true + +# File that points to the database seeding script +# seed_file: seed.js + +# Path pointing to where the migrations can be found +# this must be a relative path under the config path +migrations_path: ./migrations + +# Secret key for general encryption operations like +# encrypting the cursor data +secret_key: supercalifajalistics + +# CORS: A list of origins a cross-domain request can be executed from. +# If the special * value is present in the list, all origins will be allowed. +# An origin may contain a wildcard (*) to replace 0 or more +# characters (i.e.: http://*.domain.com). +cors_allowed_origins: ["*"] + +# Debug Cross Origin Resource Sharing requests +cors_debug: false + +# Postgres related environment Variables +# SG_DATABASE_HOST +# SG_DATABASE_PORT +# SG_DATABASE_USER +# SG_DATABASE_PASSWORD + +# Auth related environment Variables +# SG_AUTH_RAILS_COOKIE_SECRET_KEY_BASE +# SG_AUTH_RAILS_REDIS_URL +# SG_AUTH_RAILS_REDIS_PASSWORD +# SG_AUTH_JWT_PUBLIC_KEY_FILE + +# inflections: +# person: people +# sheep: sheep + +auth: + # Can be 'rails', 'jwt' or 'header' + type: jwt + cookie: _supergraph_session + + # Comment this out if you want to disable setting + # the user_id via a header for testing. + # Disable in production + creds_in_header: true + + # jwt: + # provider: auth0 + # secret: abc335bfcfdb04e50db5bb0a4d67ab9 + # public_key_file: /secrets/public_key.pem + # public_key_type: ecdsa #rsa + + # header: + # name: dnt + # exists: true + # value: localhost:8080 + +# You can add additional named auths to use with actions +# In this example actions using this auth can only be +# called from the Google Appengine Cron service that +# sets a special header to all it's requests +auths: + - name: from_taskqueue + type: header + header: + name: X-Appengine-Cron + exists: true + +database: + type: postgres + host: db + port: 5432 + dbname: daddy + user: daddy + password: daddy + + #schema: "public" + #pool_size: 10 + #max_retries: 0 + #log_level: "debug" + + # Set session variable "user.id" to the user id + # Enable this if you need the user id in triggers, etc + set_user_id: false + + # database ping timeout is used for db health checking + ping_timeout: 1m + + # Define additional variables here to be used with filters + variables: + #admin_account_id: "5" + admin_account_id: "sql:select id from users where admin = true limit 1" + + + # Field and table names that you wish to block + blocklist: + - ar_internal_metadata + - schema_migrations + - secret + - password + - encrypted + - token + +# Create custom actions with their own api endpoints +# For example the below action will be available at /api/v1/actions/refresh_leaderboard_users +# A request to this url will execute the configured SQL query +# which in this case refreshes a materialized view in the database. +# The auth_name is from one of the configured auths +actions: + - name: refresh_leaderboard_users + sql: REFRESH MATERIALIZED VIEW CONCURRENTLY "leaderboard_users" + auth_name: from_taskqueue + +tables: + - name: customers + remotes: + - name: payments + id: stripe_id + url: http://rails_app:3000/stripe/$id + path: data + # debug: true + pass_headers: + - cookie + set_headers: + - name: Host + value: 0.0.0.0 + # - name: Authorization + # value: Bearer + + - # You can create new fields that have a + # real db table backing them + name: me + table: users + + +#roles_query: "SELECT * FROM users WHERE id = $user_id" + +roles: + - name: anon + tables: + - name: users + query: + limit: 10 + + - name: user + tables: + - name: users + query: + filters: ["{ id: { _eq: $user_id } }"] + + - name: products + query: + limit: 50 + filters: ["{ user_id: { eq: $user_id } }"] + disable_functions: false + + insert: + filters: ["{ user_id: { eq: $user_id } }"] + presets: + - user_id: "$user_id" + - created_at: "now" + + update: + filters: ["{ user_id: { eq: $user_id } }"] + presets: + - updated_at: "now" + + delete: + block: true + + # - name: admin + # match: id = 1000 + # tables: + # - name: users + # filters: [] diff --git a/backend/config/migrations/0_init.sql b/backend/config/migrations/0_init.sql new file mode 100644 index 0000000..17d993c --- /dev/null +++ b/backend/config/migrations/0_init.sql @@ -0,0 +1,17 @@ +-- Write your migrate up statements here + +CREATE TABLE public.users ( + id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, + full_name text, + email text UNIQUE NOT NULL CHECK (length(email) < 255), + created_at timestamptz NOT NULL NOT NULL DEFAULT NOW(), + updated_at timestamptz NOT NULL NOT NULL DEFAULT NOW() +); + +---- create above / drop below ---- + +-- Write your down migrate statements here. If this migration is irreversible +-- then delete the separator line above. + +DROP TABLE public.users + diff --git a/backend/config/prod.yml b/backend/config/prod.yml new file mode 100644 index 0000000..5987ea3 --- /dev/null +++ b/backend/config/prod.yml @@ -0,0 +1,67 @@ +# Inherit config from this other config file +# so I only need to overwrite some values +inherits: dev + +app_name: "Backend Production" +host_port: 0.0.0.0:8080 +web_ui: false + +# debug, info, warn, error, fatal, panic, disable +log_level: "warn" + +# enable or disable http compression (uses gzip) +http_compress: true + +# When production mode is 'true' only queries +# from the allow list are permitted. +# When it's 'false' all queries are saved to the +# the allow list in ./config/allow.list +production: true + +# Throw a 401 on auth failure for queries that need auth +auth_fail_block: true + +# Latency tracing for database queries and remote joins +# the resulting latency information is returned with the +# response +enable_tracing: true + +# File that points to the database seeding script +# seed_file: seed.js + +# Path pointing to where the migrations can be found +# migrations_path: migrations + +# Secret key for general encryption operations like +# encrypting the cursor data +# secret_key: supercalifajalistics + +# Postgres related environment Variables +# SG_DATABASE_HOST +# SG_DATABASE_PORT +# SG_DATABASE_USER +# SG_DATABASE_PASSWORD + +# Auth related environment Variables +# SG_AUTH_RAILS_COOKIE_SECRET_KEY_BASE +# SG_AUTH_RAILS_REDIS_URL +# SG_AUTH_RAILS_REDIS_PASSWORD +# SG_AUTH_JWT_PUBLIC_KEY_FILE + +database: + type: postgres + host: db + port: 5432 + dbname: backend_development + user: postgres + password: postgres + #pool_size: 10 + #max_retries: 0 + #log_level: "debug" + + # Set session variable "user.id" to the user id + # Enable this if you need the user id in triggers, etc + set_user_id: false + + # database ping timeout is used for db health checking + ping_timeout: 5m \ No newline at end of file diff --git a/backend/config/seed.js b/backend/config/seed.js new file mode 100644 index 0000000..e027af3 --- /dev/null +++ b/backend/config/seed.js @@ -0,0 +1,19 @@ +// Example script to seed database + +var users = []; + +for (i = 0; i < 10; i++) { + var data = { + full_name: fake.name(), + email: fake.email() + } + + var res = graphql(" \ + mutation { \ + user(insert: $data) { \ + id \ + } \ + }", { data: data }) + + users.push(res.user) +} \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..bbad58e --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,30 @@ +version: '2.4' +services: + super-graph: + build: + context: ./misc/containers/super-graph + args: + - HTTP_PROXY=${HTTP_PROXY} + - HTTPS_PROXY=${HTTPS_PROXY} + - http_proxy=${http_proxy} + - https_proxy=${https_proxy} + environment: + - SG_DATABASE_HOST=postgres + - SG_DATABASE_USER=daddy + - SG_DATABASE_PASSWORD=daddy + - USER_ID=${USER_ID} + - GO_ENV=dev + volumes: + - ./backend:/app + links: + - postgres + ports: + - 8080:8080 + postgres: + image: postgres:12-alpine + environment: + - POSTGRES_PASSWORD=daddy + - POSTGRES_USER=daddy + - POSTGRES_DB=daddy + ports: + - 5432:5432 \ No newline at end of file diff --git a/misc/containers/super-graph/Dockerfile b/misc/containers/super-graph/Dockerfile new file mode 100644 index 0000000..2e2ce94 --- /dev/null +++ b/misc/containers/super-graph/Dockerfile @@ -0,0 +1,36 @@ +FROM alpine:edge AS build + +ARG HTTP_PROXY= +ARG HTTPS_PROXY= +ARG http_proxy= +ARG https_proxy= + +ARG SUPERGRAPH_VERSION=v0.14.17 +ARG WAITFORIT_VERSION=v2.4.1 + +RUN apk add --no-cache go make git curl bash ca-certificates + +RUN git clone https://github.com/dosco/super-graph \ + && export PATH="$PATH:/root/go/bin" \ + && export CGO_ENABLED=0 \ + && cd super-graph \ + && git checkout ${SUPERGRAPH_VERSION} \ + && make SHELL='bash -x' build + +RUN curl -sL \ + -o /usr/local/bin/waitforit \ + https://github.com/maxcnunes/waitforit/releases/download/${WAITFORIT_VERSION}/waitforit-linux_amd64 + +FROM alpine:3.11 + +COPY --from=build /super-graph/super-graph /usr/local/bin/super-graph +COPY --from=build /usr/local/bin/waitforit /usr/local/bin/waitforit + +RUN chmod +x /usr/local/bin/waitforit + +WORKDIR /app + +COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint +RUN chmod +x /usr/local/bin/docker-entrypoint + +CMD ["/usr/local/bin/docker-entrypoint"] \ No newline at end of file diff --git a/misc/containers/super-graph/docker-entrypoint.sh b/misc/containers/super-graph/docker-entrypoint.sh new file mode 100644 index 0000000..c0bea55 --- /dev/null +++ b/misc/containers/super-graph/docker-entrypoint.sh @@ -0,0 +1,13 @@ +#!/bin/sh + +set -eo pipefail + +if [ ! -f /container-lifecycle/first_run ]; then + waitforit -debug -host $SG_DATABASE_HOST -port 5432 + super-graph db:migrate up + super-graph db:seed + mkdir /container-lifecycle + touch /container-lifecycle/first_run +fi + +super-graph serv