Commit 31226550 authored by Vitali Stupin's avatar Vitali Stupin
Browse files

Cleanup

parent 6e79f913
# Central Server API
## Developing
In order to develop on your machine and test on central server you need to add your public SSH key to the server:
```bash
ssh-keygen
ssh-copy-id riajenk@jan-center2.ci.kit
```
## API description
Prepare server:
```bash
sudo apt-get install python3.4-venv
```
API is described using OpenAPI specification: [openapi-definition.yaml](openapi-definition.yaml)
Then create a "Run Configuration" that executes `remote_exec.sh`
## Systemd configuration
Testing:
Add service description `systemd/csapi.service` to `/etc/systemd/system/csapi.service`. Then start and enable automatic startup:
```bash
curl -i -d '{"member_code": "XX000002", "member_name": "XX Test 2", "member_class": "GOV"}' -X POST jan-center2.ci.kit:5444/member
sudo systemctl daemon-reload
sudo systemctl start csapi
sudo systemctl enable csapi
```
## API description
API is described using OpenAPI specification: [openapi-definition.yaml](openapi-definition.yaml)
## Nginx configuration
Create a certificate for nginx (already installed to Central Server):
Create a certificate for nginx:
```bash
mkdir -p /etc/nginx/csapi
cd /etc/nginx/csapi
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout csapi.key -out csapi.crt
```
Cert info:
Cert info (NB! CN should be the domain name of your central server):
```
Country Name (2 letter code) [AU]:EE
State or Province Name (full name) [Some-State]:Harjumaa
Locality Name (eg, city) []:Tallinn
Organization Name (eg, company) [Internet Widgits Pty Ltd]:RIA
Organizational Unit Name (eg, section) []:CSAPI
Common Name (e.g. server FQDN or YOUR name) []:jan-center2.ci.kit
Common Name (e.g. server FQDN or YOUR name) []:central-server.ci.kit
Email Address []:
```
......@@ -66,20 +55,11 @@ Common Name (e.g. server FQDN or YOUR name) []:
Email Address []:
```
Copy client.srt to nginx machine: `/etc/nginx/csapi/client.crt`
Copy client.crt to nginx machine: `/etc/nginx/csapi/client.crt`
For testing copy nginx csapi.crt to client and issue curl command:
For testing copy nginx `csapi.crt` to client and issue curl command:
```bash
curl --cert client.crt --key client.key --cacert csapi.crt -i -d '{"member_code": "XX000003", "member_name": "XX Test 3", "member_class": "GOVXXX"}' -X POST https://jan-center2.ci.kit:5443/member
```
Add nginx configuration from this repository: `nginx/csapi` to nginx server: `/etc/nginx/sites-enabled/csapi`
## Systemd
Add service description `csapi.service` to `/etc/systemd/system/csapi.service`. Then start and enable automatic startup:
```bash
sudo systemctl daemon-reload
sudo systemctl start csapi
sudo systemctl enable csapi
```
#!/usr/bin/env bash
set -e
if [[ ! -d "venv" ]]; then
python3 -m venv venv > /dev/null
fi
source venv/bin/activate
pip install -r requirements.txt > /dev/null
#!/usr/bin/env bash
set -e
SERVER=riajenk@jan-center2.ci.kit
ssh $SERVER "mkdir -p project"
rsync -r -p -l --exclude=\.* --exclude=venv . ${SERVER}:project/
ssh $SERVER "cd project; ./prepare_venv.sh; sudo -u xroad ./run_in_venv.sh"
#!/usr/bin/env bash
set -e
source venv/bin/activate
# Flask (Werkzeug) server
#python server_dev.py
# Production ready Gunicorn server
gunicorn -w 4 -b 127.0.0.1:5444 server:app
#!/usr/bin/env python3
import logging
from flask import Flask, request
from flask_restful import Resource, Api
from member import MemberApi
# NB! For developing only
class StopApi(Resource):
@staticmethod
def get():
func = request.environ.get('werkzeug.server.shutdown')
if func is None:
raise RuntimeError('Not running with the Werkzeug Server')
func()
return 'Server shutting down...'
handler = logging.FileHandler('/var/log/xroad/csapi.log')
handler.setFormatter(logging.Formatter('%(asctime)s - %(process)d - %(levelname)s: %(message)s'))
# Member module logger
logger_m = logging.getLogger('member')
logger_m.setLevel(logging.INFO)
logger_m.addHandler(handler)
# Application logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
logger.addHandler(handler)
app = Flask(__name__)
api = Api(app)
api.add_resource(MemberApi, '/member')
logger.info('Starting Central Server API')
if __name__ == '__main__':
# Flask logger
logger_f = logging.getLogger('werkzeug')
logger_f.setLevel(logging.INFO)
logger_f.addHandler(handler)
# Running Flask (Werkzeug) server for development
api.add_resource(StopApi, '/stop')
app.run(debug=False, host='0.0.0.0', port=5444)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment