Jean-Nicolas Louis
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # API setup in cPouta In order to create an restAPI in cPouta, one must get familiar with what you want to do with it and what do you want to do with it. This documentation details only one architecture that was already done but other architectures are possible. For a good API, you will need 1. a server 2. a database 3. an API design As a total beginner in the field, it took me about 2 weeks to go through all the steps. But you will need to get acquainted with sql relational database, remote connection, API design ## Server As a member of a research institution in Finland, we are allowed to use the CSC infrastructure to compute, store, and retrieve data. for more information about CSC see [CSC webpages](https://www.csc.fi) In order to setup your own space online, follow the [guidelines](https://docs.csc.fi/cloud/pouta/) from CSC. A set of video tutorial is available from their [support pages](https://docs.csc.fi/cloud/pouta/pouta-videos/) A good tutorial to start with and get all the tools you need was developed in our unit and can be found [here](https://hackmd.io/bCR_WaNJRJ2bO68eT0bpKA). NOTE: if you install ubuntu, do not try to connect via ``` ssh -i .ssh/keypair.pem cloud-user@hostIP ``` but instead ``` ssh -i .ssh/keypair.pem ubuntu@hostIP ``` By going through their materials you should be able to set it up quite nicely and setup also security measures to open and restrict the access to your virtual machine. ## Database The second step is to work with a database. A database is basically a set of table that are stored on your computer (localhost) or on a remote server (IP address). A common database tool is [PostgreSQL](https://www.postgresql.org), it is free, open-source, has a graphical interface but most likely you will just need the command line options. if you are a beginner and do not know where to start, one of the best step-by-step tutorial is available from [here](https://www.youtube.com/watch?v=qw--VYLpxG4&t=4306s). ### Installation right, let's assume now that you are aware of the tool and found out how to work with it, we need to install PostgreSQL on our virtual machine in cPouta. 1. connect to your VM 2. Install PostgreSQL ``` sudo apt install postgresql postgresql-contrib ``` 3. Allow remote access to server. Change commented #listen_address to listen_address = '*' ``` sudo vim /etc/postgresql/12/main/postgresql.conf listen_address = '*' ``` 4. Allow access with password from remote host. ``` sudo vim /etc/postgresql/12/main/pg_hba.conf ``` Add following line to the end of the file ``` host database_name user_name ip_of_remote_computer/32 md5 ``` 5. Add user ``` sudo -u postgres createuser --interactive ``` ``` username: user_name yes/no choises: no ``` 6. Create database ``` sudo -u postgres createdb database_name ``` 7. Add password to user ``` sudo -u postgres psql -c "alter user user_name with password 'example_password';" ``` 8. Change ownership of the database_name ``` sudo -u postgres psql -c "alter database database_name owner to user_name;" ``` 9. Restart PostgreSQL ``` sudo systemctl restart postgresql ``` After that you may need to open port 5432 from cPouta interface. You can test connection from client with command: ``` psql -h host_name-d database_name -U user_name ``` Change hostname, database_name, user_name and example_password to what you like, just remember them or store them in a safe place for future use. You are now ready to interact with your database remotely, store and query data remotely. But since this is impractical and only available to you, it would be good to open the database access publicly. To do that, you will need to setup your API. APIs will allow you to retrieve access, send query and obtain results from the database automatically. ## API design First, you will need to design your API. What is meant by designing an API is to define the roles, queries and under which forms queries can be made. This is also very important to document your APIs that others can access it without wondering how it is done. Designing the API can be done in multiple ways but these 2 platforms are usually favored: - [Stoplight](https://stoplight.io) - [Swagger](https://swagger.io) I have personally used stoplight since it has a nice user interface where you can create all your relationship dynamically and still output your YAML document. [^a]: YAML is a document format that stores all the data about your API. it works just like XML Since I was a beginner in this, I found [this lecture](https://www.youtube.com/watch?v=ROVI2G8eH78) quite useful for setting up the bases but the most important aspect is 1. to setup the Paths this means how to access certain type of data 2. set the responses 3. set each parameters that can be passed some of the parameters can be made mandatory and some not This part is crucial to understand how everything works and how you can relate your databases to your API. ## API implementation before the deployment, the API will need to be tested locally and then be deployed on the cPouta server You can try to implement the API with Python3, [Flask](https://flask.palletsprojects.com/en/2.0.x/installation/), [Flask-SQLAlchemy](https://flask-sqlalchemy.palletsprojects.com/en/2.x/) and SQLAlchemy. You can first create test environment in your own computer before you release it in cPouta server. going through the installation on Anaconda Working around with flask and tutorials [Tutorial1](https://programminghistorian.org/en/lessons/creating-apis-with-python-and-flask) ### Query design This is for example a shortened query that I designed to retrieve the latest entry in the database and select all entries between 2 dates. Both queries having optional parameters if needed. ```python import flask from flask import request import sqlalchemy as db import json app = flask.Flask(__name__) #app.config["DEBUG"] = True def loginaccess(tablenm): usrname = 'username' pswd = 'password' host = 'IP address of your host' port = 'database port, usually 5432' database = 'database_name' table_load = tablenm return usrname, pswd, host, port, database, table_load ##### Error management ##### @app.errorhandler(404) def page_not_found(e): return "<h1>404</h1><p>The resource could not be found.</p>", e def incorrect_arg(e): return "<h1>" + str(e) + "</h1><p>The resource could not be found. <br> Check the arguments if they were given correctly</p>", e ##### Error management ##### ### Three types of requests ### ###### GET ALL THE DATA ###### @app.route('/api/v1/resources/emissions/all', methods=['GET']) def api_all(): usrname, pswd, host, port, database, table_load = loginaccess('emissions') query = "SELECT * FROM " + table_load + ";" to_call = 'postgresql://' + usrname + ':' + pswd + '@' + host + ':' + port + '/' + database engine = db.create_engine(to_call) conn = engine.connect() all_books = conn.execution_options(isolation_level="SERIALIZABLE").execute(query).fetchall() result_list = [] for row in all_books: result_list.append({ 'id':row.id, 'date_time':row.date_time, 'country':row.country, 'emdb':row.emdb, 'em_prod':row.emissionintprod, 'em_cons':row.emissionintcons }) result_dict = {'results': result_list} jsonString = json.dumps(result_dict, indent=4, sort_keys=True, default=str) conn.close() return jsonString ###### GET ALL THE DATA ###### ###### GET DATA BY DATE###### @app.route('/api/v1/resources/emissions/findByDate', methods=['GET']) def api_filter_em_findByDate(): usrname, pswd, host, port, database, table_load = loginaccess('emissions') query_parameters = request.args country = query_parameters.get('country') EmDB = query_parameters.get('EmDB') startdate = query_parameters.get('startdate') enddate = query_parameters.get('enddate') query = "SELECT * FROM " + table_load + " WHERE" to_filter = [] if country: query += ' country=%s AND' to_filter.append(country) if EmDB: query += ' emdb=%s AND' to_filter.append(EmDB) if startdate: query += ' date_time>=%s AND' to_filter.append(startdate) if enddate: query += ' date_time<=%s AND' to_filter.append(enddate) if not (country and EmDB or startdate or enddate): return page_not_found(404) query = query[:-4] + ';' print(query) print(to_filter) to_call = 'postgresql://' + usrname + ':' + pswd + '@' + host + ':' + port + '/' + database engine = db.create_engine(to_call) conn = engine.connect() all_books = conn.execution_options(isolation_level="SERIALIZABLE").execute(query, to_filter).fetchall() if len(all_books): results = [list(row) for row in all_books] result_list=[] for row in all_books: result_list.append({ 'id':row.id, 'date_time':row.date_time, 'country':row.country, 'emdb':row.emdb, 'em_prod':row.emissionintprod, 'em_cons':row.emissionintcons }) result_dict = {'results': result_list} jsonString = json.dumps(result_dict, indent=4, sort_keys=True, default=str) conn.close() else: return incorrect_arg(404) return jsonString ###### GET DATA BY DATE###### ###### GET LATEST DATA###### @app.route('/api/v1/resources/emissions/latest', methods=['GET']) def api_filter_em_latest(): usrname, pswd, host, port, database, table_load = loginaccess('emissions') query_parameters = request.args ############ Load the last entries ############ query = "SELECT * FROM " + table_load + " ORDER BY date_time DESC LIMIT 1 ;" to_filter = [] to_call = 'postgresql://' + usrname + ':' + pswd + '@' + host + ':' + port + '/' + database engine = db.create_engine(to_call) conn = engine.connect() all_books = conn.execution_options(isolation_level="SERIALIZABLE").execute(query, to_filter).fetchall() results = [list(row) for row in all_books] query = [] ############ Load the last entries ############ query = "SELECT * FROM " + table_load + " WHERE" query += ' date_time=%s AND' to_filter.append(results[0][1].strftime("%Y-%m-%d %H:%M:%S")) country = query_parameters.get('country') EmDB = query_parameters.get('EmDB') if country: query += ' country=%s AND' to_filter.append(country) if EmDB: query += ' emdb=%s AND' to_filter.append(EmDB) # return page_not_found(404) query = query[:-4] + ';' print(query) print(to_filter) to_call = 'postgresql://' + usrname + ':' + pswd + '@' + host + ':' + port + '/' + database engine = db.create_engine(to_call) conn = engine.connect() all_books = conn.execution_options(isolation_level="SERIALIZABLE").execute(query, to_filter).fetchall() result_list=[] for row in all_books: result_list.append({ 'id':row.id, 'date_time':row.date_time, 'country':row.country, 'emdb':row.emdb, 'em_prod':row.emissionintprod, 'em_cons':row.emissionintcons }) result_dict = {'results': result_list} jsonString = json.dumps(result_dict, indent=4, sort_keys=True, default=str) #print(json.dumps(result_dict, indent=4, sort_keys=True, default=str)) conn.close() return jsonString ###### GET LATEST DATA###### ``` ## Deployment ### Python installations To deploy your flask app to the cPouta server, a few steps are required. First, we need to install Apache compatible with python ``` sudo apt-get install python3-pip apache2 libapache2-mod-wsgi-py3 ``` Add the api folder where we will store some of the files ``` mkdir -p /var/www/flask_api/api ``` Transfer the api.py file that you created earlier and rename it as '_init__.py' if you cannot load it directly jsut create the init file and copy/paste your api.py text in it. You must go in the api folder "var/www/flask_api/api/" ``` sudo touch __inti__.py ``` remove app.run() Then you need to install python and some dependency packages on the server ``` cd /var/www/flask_api python3 -m venv env . env/bin/activate pip install flask flask-sqlalchemy sqlalchemy psycopg2-binary ``` Within the flask_api folder, we declared a virtual environment *env* using python 3.X (depending on the python version available for the OS you are running). Within this virtual environment, we need to install other packages that are used in the script *flask*, *sqlalchemy*, *psycopg2*. If you have need for any other packages, you will need to install them at that point in the virtual environment. ### Apache deployment then we need to create the conf file for apache ``` sudo touch /etc/apache2/sites-available/flask_api.conf ``` and then you should edit the file using nano for example ``` sudo nano /etc/apache2/sites-available/flask_api.conf ``` once the file is created and you are in editing more, add the following lines to the file ``` <VirtualHost *:80> ServerName ip-address WSGIDaemonProcess api python-home="/var/www/flask_api/env" WSGIProcessGroup api WSGIApplicationGroup %{GLOBAL} WSGIScriptAlias / /var/www/flask_api/api.wsgi <Directory /var/www/flask_api/api/> Order allow,deny Allow from all </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log LogLevel warn CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> ``` Create api.wsgi file. This is to allow the server to know what it is going to run ``` sudo touch /var/www/flask_api/api.wsgi ``` edit your api.wsgi file in vim ``` sudo vim /var/www/flask_api/api.wsgi ``` and insert the following text instead ``` #!/var/www/flask_api/bin/python import sys import logging logging.basicConfig(stream=sys.stderr) sys.path.insert(0,"/var/www/flask_api") ``` Remember to save and exit ':wq' command in vim. enable site, type the following instructions in the terminal window: ``` a2ensite flask_api ``` restart apache ``` systemctl restart apache2 ``` ### PostgreSQL tuning Some of the information in the *pg_hba.conf* file might need to be fine-tuned in order to grant remote access to outside. ``` sudo vim /etc/postgresql/12/main/pg_hba.conf ``` within the *pg_hba.conf* file, you will need to add access to all users to the database. (top red highlight). Important to have it in the second row to potentially not shadow other access. ``` local all all md5 ``` ![conf_blurredv3](https://i.imgur.com/pBR9zOZ.png) and then remove the text or comment out the text in the second red rectangle (as shown in the picture above). after every change in the PostgreSQL, it is good to restart it every time ``` systemctl restart postgresql ``` ### Folder's rights You should create user to server and give ownership of /var/www/flask_api directory to it and allow user to read only access to database. This should remove database access issues. to add a user to the server and create a password you will remember for this user ``` sudo adduser someusername ``` check that the user was created successfully ``` grep '^someusername' /etc/passwd ``` change the owner ship of the folder and subfolder of flask_api ``` sudo chown -R guest_making_city: /var/www/flask_api ``` ### Server's rights On the server's side in CSC Pouta, you must open the port 80 for all users. The port 80 will be used to access the database remotely through query via the api we just created. Go to you security group, and add the following exceptions in the security group of your server, with the open IP 0.0.0.0/0 ![security_group80](https://i.imgur.com/Tn85CcA.png) ### API access If everything goes right and works, you can type in a browser the floating IP address of your server and you shall be able to see the description message you have written down in CSC portal. in my case, it looks something like this ![CSC_welcomemessage](https://i.imgur.com/8hjzLQC.png) then you can write your queries ``` IP/QUERY e.g. http://128.214.253.150/api/v1/resources/emissions/latest ``` In which case, my code returns a .json formatted document ![query_example](https://i.imgur.com/6H1cUgi.png) ### Some tips It is good to restart/reboot the PostgreSQL and Apache2 after you edit a file just to make sure that the new file are considered into the system If you have problems with python, one can debug the operation of the python file using the following command ```python . env/bin/activate tail -f /var/log/apache2/error.log ``` Within the virtual environment you created, you can see the error log file in real time of your Apache application that includes python errors. (ctrl+C to exit the tailing mode). Make sure there is no syntax error throughout the files that are declared in the conf, wsgi, or python script. ## Working example You can try an existing API using the design I created for the Making city project here: https://app.swaggerhub.com/apis/jean-nicolas.louis/emission-and_power_grid_status/1.0.0

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully