Giter Club home page Giter Club logo

danielkolbespecial / backends Goto Github PK

View Code? Open in Web Editor NEW

This project forked from orbiting/backends

0.0 0.0 0.0 10.67 MB

monorepo for republik, publikator and assets backends based on NodeJS providing the GraphQL API used to power republik.ch.

Home Page: https://api.republik.ch/graphiql

License: GNU Affero General Public License v3.0

JavaScript 80.88% PLSQL 0.03% PLpgSQL 2.33% HTML 15.21% Shell 0.01% SQLPL 0.05% Dockerfile 0.02% HCL 0.01% TSQL 1.46%

backends's Introduction

@orbiting/backends Build Status Coverage Status

This repo contains all the backend code in use at Republik. For easier development the previously separate repos republik-backend, publikator-backend, assets-backend and backend-modules where merged into this monorepo.

For a guide on how to start the the frontends see: docs/how-to-run

Components

This repo is devided between servers and packages.

  • servers: this directory contains runnable servers
  • packages: contains code shared between the servers

How to run / Development

1. Clone

git clone [email protected]:orbiting/backends.git && cd backends

2. Prerequisites

You must have Node.js (10+), yarn, docker and docker-compose installed (alternatively to docker you can install the external services natively).

Docker

The included docker-compose.yml starts all external-services. Currently that's: postgresql, redis, elasticsearch (and kibana). The data is persisted in ./docker-data/.

docker-compose up [-d]
Postgresql in docker

We recommend you install the postgresql client tools on your machine to interact with the database. The tests scripts also depend on the clients being installed.

# linux
sudo apt install postgresql-client-10

When postgresql in running in docker client tools like psql or createdb/dropdb don't automatically connect to it. They try to access postgresql via a local socket, when instead you want them to connect via network to localhost. To make your life easier, you can add the following environment variables to ~/.bashrc / ~/.zshrc so the client tools connect to localhost per default.

export PGHOST=127.0.0.1
export PGUSER=postgres

alternativ: install natively

show more

As an alternative to docker(-compose) you can install the external-services natively:

On macOS with homebrew:

brew install postgresql redis nvm elasticsearch
nvm install 12
nvm alias default 12
npm install -g [email protected]
brew services start postgresql
brew services start redis
brew services start elasticsearch

Docker Kibana accessing an native Elasticsearch

docker run -p 5601:5601 -e ELASTICSEARCH_HOSTS=http://host.docker.internal:9200 docker.elastic.co/kibana/kibana-oss:6.7.0

Note:

  • Elasticsearch and Kibana versions must match, ckeck ES version at http://localhost:9200/
  • ELASTICSEARCH_HOSTS must be accessible within docker.

2. ENVs

Copy the .env.example files to .env (in root and server/*/). The default values should be enough to get started.

cp .env.example .env
cp servers/republik/.env.example servers/republik/.env
cp servers/publikator/.env.example servers/publikator/.env
cp servers/assets/.env.example servers/assets/.env

3. Install

yarn install

4. Initialize

createdb republik
yarn run db:migrate:up
yarn run db:seed

5. Run

yarn run dev

This kicks on foreman which then launches all the servers locally. All servers greets you with Cannot GET / on the root route. The API servers have a graphical API explorer available at /graphiql:

Next steps

more about ENVs

In development environment variables are loaded from ./.env, ../../.env (in this order, relative to node's cwd). This is done with the help of backend-modules-env and enables you to define a .env file at the root of this repo for common variables and also store server specific envs at servers/*/.env. ./.env takes precedence over ../../.env because existing env variables are never overwritten. No ENV variabeles are loaded from any file in production, you yourself are responsible to set all required ENVs in the production environment.

Checkout .env.example, servers/republik/.env.example, servers/publikator/.env.example, servers/assets/.env.example for which ENVs are required and their descriptions. Check the packages' README for further config options.

You will quickly run into errors and limitations if you run with the example envs. You probably want to do the following three rather soon:

  1. Setup GitHub
    • GITHUB_* in the root .env
  2. MailChimp and Mandrill
    • MAILCHIMP_URL, MAILCHIMP_API_KEY, MANDRILL_API_KEY in the root .env
    • MAILCHIMP_* in servers/republik/.env (less important)
  3. S3 Bucket
    • AWS_* in the root .env

Special setup: develop on two hosts

The following setup enables to start the servers (backends and republik-frontend) on one machine (A) and access it from another (B). This can come handy if you want to develop the backend on A and the app on B (where B can be a physical device).

Please not that due to how "Docker for Mac" works (docker is run in a hidden VM), it's not possible to bind containers to the host's network-interface, therefore this setup only works on linux.

Machine A (servers)

  1. Get the IP of your machine in the local network, use it in the next step as LOCAL_IP
ip addr
  1. Adapt hostnames in the environment variables:
  • in backends/.env
FRONTEND_BASE_URL=http://republik.test
ASSETS_SERVER_BASE_URL=http://assets.republik.test

LOCAL_IP=192.168.1.88
  • in backends/servers/republik/.env
CORS_WHITELIST_URL=http://republik.test
COOKIE_DOMAIN=.republik.test
  • in republik-frontend/.env
API_URL=http://api.republik.test/graphql
API_WS_URL=ws://api.republik.test/graphql
API_ASSETS_BASE_URL=http://republik.test
PUBLIC_BASE_URL=http://republik.test
  1. Start the DNS-Server and reverse proxy:
docker-compose -f docker-compose-test-network.yml up [-d]
  • bind: You now have a DNS server running locally. It resolves all requests of *.republik.test to LOCAL_IP.
  • traefik: routes requests based on SNI (check: traefik.toml)
    • http://republik.test -> http://localhost:3010
    • http://api.republik.test -> http://localhost:5000
  1. Run backend services with docker (in backends/):
docker-compose up [-d]
  1. Refresh published articles, due to changed ASSETS_SERVER_BASE_URL (in backends/):
yarn run pull:elasticsearch
redis-cli
> FLUSHALL
  1. Run the backend servers (in backends/):
yarn run dev
  1. Run the frontend server (in republik-frontend/):
npm run dev
  1. Test You should be able to access http://api.republik.test/graphiql and http://republik.test

Machine B (app)

  1. To resolve the hostnames:
  • Find the IP of Machine A, let's say its 192.168.1.88
  • change your network config to use this IP as your DNS resolver.
  1. Adapt hostnames in the environment variables (in app/.env.dev):
API_URL=http://api.republik.test/graphql
API_WS_URL=ws://api.republik.test/graphql
FRONTEND_BASE_URL=http://republik.test
  1. Test

You should be able to access http://api.republik.test/graphiql and http://republik.test

  1. Setup simulators/emulators

At least the android emulator doesn't use the hosts dns resolver

  • configure the DNS resolver manually (see step 1) inside the simulator
  • test in the webbrowser that http://republik.test is accessible.
  • run the app as usual (in app/)
yarn run run-android

Licensing

The source code and it's documentation is licensed under GNU AGPLv3+.

backends's People

Contributors

annatraussnig avatar clarajeanne avatar dependabot[bot] avatar eightam avatar garamond avatar lukasbuenger avatar patrickvenetz avatar patte avatar pozylon avatar ruggedly avatar tpreusse avatar uxengine avatar werehamster avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.