A custom server program based on the Django framework designed to allow a programmer to directly move to the implementation of an application's features without having to torture himself with other time-consuming configuration or installation.
You can clone this repository everywhere you want in your machine, with the following command lines:
# ~$
git clone https://github.com/mokira3d48/cobra.git myapp && cd myapp
In this cloned directory, you will see the following structure:
.
├── LICENSE
├── README.md
├── requirements.txt
└── server
├── core
│ ├── asgi.py
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
2 directories, 9 files
This is the list of the installed features:
- Django REST Framework: it's a powerful and flexible toolkit for building Web APIs.
- drf-yasg: for the generation of a documentation of the API in real Swagger/OpenAPI 2.0 specifications from a Django Rest Framework API.
- Django CORS Headers: it's a security mechanism that allows one domain to access resources hosted on another domain.
Table des Contenus
We must install three (03) programs:
Python3
runtime;- Python virtual environment
venv
; - Database manager
PostgreSQL
; - Getting of project repository.
For Linux system users, you can install the following dependencies:
sudo apt install cmake
# ~$
sudo apt install python3 python3-pip
You have to make sure of the version of python that is installed.
The version of python used is python 3.10.9
.
You can install a python virtualenv program in two different ways.
# ~$
sudo apt install python3-venv
OR
# ~$
sudo pip3 install virtualenv
# ~$
sudo apt install postgresql postgresql-contrib
For using a spacial database, we can install the following extension:
# ~$
# PostGIS is an extension of PostgreSQL
# that allows to process the spacial data like the Polygons,
# the Points, ...
sudo apt install postgis
- Setting virtual environment;
- Creating and setting of PostgreSQL database;
- Dependencies installation.
- In your project root, if you have not already done so, run one of the following commands to create a virtual environment.
# ~$
python3 -m venv env
OR
# ~$
virtualenv env -p python3
- Launch environment
# ~$
source env/bin/activate
- You must execute the following command to install the basic dependences:
# ~$
make install
The following SQL
command lines allow to create a PostgreSQL
database for your application:
# ~$
# To connect to PostgreSQL with ROOT user:
sudo su - postgres
# ~$
# To connect to default database (postgres)
psql
Given your database name is cbrdb
and the username is cobra
.
CREATE DATABASE cbrdb;
CREATE USER cobra WITH ENCRYPTED PASSWORD 'your-secret-password-here';
ALTER ROLE cobra SET client_encoding TO 'utf8';
ALTER ROLE cobra SET default_transaction_isolation TO 'read committed';
ALTER ROLE cobra SET timezone TO 'Europe/Paris';
GRANT ALL PRIVILEGES ON DATABASE cbrdb TO cobra;
-- configuration for testing database for Django
ALTER USER cobra CREATEDB;
-- ALTER ROLE cobra SUPERUSER;
-- connect to cbrdb.
\c cbrdb;
Give the access of the public
schema to the user account of the application.
GRANT ALL ON SCHEMA public TO cobra;
For the spacial database, you must create the following extensions on it.
-- ...
-- Only you are using a spatial database
CREATE EXTENSION postgis;
Finally, disconnect from PostgreSQL by pressing CTRL + D
twice.
- You have to create a
.env
file in the root of the server from theserver/.env_example
:
cp server/.env_example server/.env
- Insert the following information into
.env
file:
FIELDS | VALUES |
---|---|
DB_NAME | cbrdb |
USERNAME | cobra |
PASSWORD | your-secret-password-here |
HOST | 127.0.0.1 |
PORT | 5432 |
Here are the contents of the file .env
:
DB_NAME=cbrdb
USERNAME=cobra
PASSWORD=your-secret-password-here
HOST=127.0.0.1
PORT=5432
If port
5432
does not work, then try port5433
.
- Execute the following command line, to apply the configs made in the
server/.env
file.
source ./server/.env
- Execute the following command lines to make migrations of models into
database. It's assumed that you are currently in project directory
root
cobra
.
# ~$
./server/manage.py makemigrations;\
./server/manage.py migrate
or
# ~$
make migrations
You will get the following result, if all works successfully :
- Then, create a superuser that will be used to connect to admin space.
# ~$
./server/manage.py createsuperuser
Or
# ~$
make csudo
To start server, you must execute the following command line:
# ~$
./server/manage.py runserver
The result is:
We cant go it at this local host link or .
You can change the IP address and the port of the server with the following command line:
# ~$
# With this command, we cant make the server listens
# on the IP address of your local network on the port 8080.
./server/manage.py runserver 0.0.0.0:8080
Or you can execute the following command contained in Makefile
.
# ~$
make run
You will see:
All work with successfully !
To access it in this cas, you must execute the following command line,
in first:
# ~$
# IF YOU ARE USING LINUX
# show your IP address of your machine, if it's connected
# to your local network for example.
ifconfig
For the people using Windows, use
ipconfig
insted of the command line above.
We cant go it at this local host http://yourip:8080.
Cette section est facultative. Mais, il peut arriver un jour où tu aurras besoin de néttoyer toutes
les tables de la base de données. Alors, c'est simple. Pour y parvcenir, tu peux simplement supprimer
tous les schémas que tu as créé. Dans cet exemple, il n'y a qu'un seul schéma que tu vas néttoyer : public
.
Connecte-toi en mode root
avec les deux commandes suivantes :
sudo su - postgres
psql
Ensuite connecte-toi en tent que user_name
à db_name
:
\c user_name db_name
Maintenant, tu peux supprimer le schéma :
DROP SCHEMA public CASCADE;
Ensuite tu le recrées avec la commande SQL suivante :
CREATE SCHEMA public;
Et enfin, il ne faut pas oublier de redonner les droits d'accès du schéma à l'utilisateur utilisé par ton application pour se connecter.
GRANT ALL ON SCHEMA public TO user_name;
GRANT ALL ON SCHEMA public TO public;
For the different usages, you can consult the different documentation available here.
- PostGIS
- Cross Origin Resource Sharing
- Usage example of Django REST Framework
- JWT authentication with Django REST Framework
- API documentation programming
- Using cache with apiview and viewsets