Skip to content

collinder/py-rest-service

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 

Repository files navigation

About

Job test project to:

  1. Set up postgresql database.
  2. Populate it with some tables and records.
  3. Launch python service, which lets us grab records from database.

Terminal promts is for Arch Linux. If you have another Linux distribution, feel free to google 'how to install pip', 'how to install with pip' and 'how to install postgresql' for your distribution!

1. Set up postgresql database.

Install

sudo pacman -S postgresql

Init db

sudo su - postgres
initdb --locale en_US.UTF-8 -D /var/lib/postgres/data
exit

Start postgresql and check how it is running

sudo systemctl start postgresql
sudo systemctl status postgresql 
  • check if service is actually running

Create user and database, grant privileges

sudo -u postgres psql
CREATE USER uno WITH ENCRYPTED PASSWORD '8.B(P8pDQeMH!';
CREATE DATABASE prueba;
GRANT ALL PRIVILEGES ON DATABASE prueba TO uno;
\c prueba postgres
GRANT ALL PRIVILEGES ON SCHEMA public TO uno;
\q

do not configure your db for remote access if you exposed your password somewhere!

  • restart postgresql
sudo systemctl restart postgresql
  • connect to postgresql
psql -h localhost -d prueba -U uno -p 5432
\q

2. Populate it with some tables and records.

install pip, Flask and psycopg

sudo pacman -S python-pip
python -m pip install --break-system-packages psycopg
python -m pip install --break-system-packages flask
python -m pip install --break-system-packages jsonify

create schema, tables and populate tables with good books

git clone "https://github.com/collinder/py-rest-service.git" ~/sencillo
cd ~/sencillo
python serv.py init
  • CTRL - C

3. Launch python service, which lets us grab records from database.

launch service

cd ~/sencillo
python serv.py

check how it is working

  1. Open your browser
  2. Go to http://127.0.0.1:5000/ - can you see all authors in json format?
  3. Go to http://127.0.0.1:5000/writers/1 - can you see all books of that author?

Warnings

do not configure your db for remote access if you exposed your password somewhere!

that service may be vulnerable to sql injection - we are injecting string parameter from GET request into sql query directly

About

Job test task

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages