<p align="center"><img width="100%" src="img/logo.svg" alt="Silence logo"></p>
<p align="center">
<a href="https://github.com/DEAL-US/Silence/actions/workflows/ci_test.yml"><img src="https://github.com/DEAL-US/Silence/actions/workflows/ci_test.yml/badge.svg"></a>
<a href="https://pypi.org/project/Silence/"><img src="https://img.shields.io/pypi/v/Silence.svg?label=Version"></a>
<a href="https://pepy.tech/project/silence"><img src="https://static.pepy.tech/personalized-badge/silence?period=total&units=international_system&left_color=grey&right_color=orange&left_text=Downloads"></a></p>
<hr>
# Introduction
Silence is a framework that allows for a rapid deployment of a RESTful API and a Web application around a relational database. Silence revolves around the concept of projects, which contain everything needed to deploy the previously mentioned elements and can be moved and/or shared between computers or systems with ease.
Silence has been built by the [**DEAL research group**](https://deal.us.es/) as an educational and teaching tool for its use in several courses of the Degree in Computer Science at the University of Seville.
# Contents
- [Installation](#installation)
- [Creating a new project](#creating-a-new-project)
- [Project templates](#project-templates)
- [Running your project](#running-your-project)
- [Configuring your project](#configuring-your-project)
- [Creating the database](#creating-the-database)
- [Defining your API endpoints](#defining-your-api-endpoints)
* [GET operations](#get-operations)
* [POST/PUT operations](#post-put-operations)
* [DELETE operations](#delete-operations)
- [Endpoint and API files auto-generation](#endpoint-and-api-files-auto-generation)
- [Default endpoints and other utilities](#default-endpoints-and-other-utilities)
* [Summary endpoint](#summary-endpoint)
* [/register endpoint](#-register-endpoint)
* [/login endpoint](#-login-endpoint)
* [Restricting endpoints to logged users](#restricting-endpoints-to-logged-users)
* [Restricting endpoints to certain user roles](#restricting-endpoints-to-certain-user-roles)
* [Using the ID of the currently logged in user](#using-the-id-of-the-currently-logged-in-user)
* [URL query parameters in GET requests](#url-query-parameters-in-get-requests)
* [Banning or deactivating users](#banning-or-deactivating-users)
- [Static web server](#static-web-server)
- [Changelog](#changelog)
- [Contributions](#contributions)
- [Disclaimer](#disclaimer)
- [License](#license)
# Installation
Silence is available in the Python Package Index (PyPI) for Python ≥ 3.6. To install Silence, run:
`pip install Silence`
Silence also requires a connection to a MySQL/MariaDB database.
# Creating a new project
Once Silence has been installed via pip, the `silence` command becomes available. To create a new project, run `silence new <name>`, where `<name>` is the name of the new project.
This will download an example project template that you can adapt to your needs. There are many different templates available. If you wish to download a different one, you can use `silence new <name> --template <template-name>`.
Alternatively, you can use `silence new <name> --blank` to download a blank template, or `silence new --url <url-to-a-repo>` to download a project hosted in the specified repository.
# Project templates
We have a variety of different examples implemented using Silence, which we call templates. You can find a list of all available templates using `silence list-templates`, and download one as shown previously.
# Running your project
Once you have configured your project, database and defined your endpoints, you can launch the web server with:
`silence run`
Access logs, debug messages (if allowed) and uncontained exceptions will be logged directly to the console.
# Configuring your project
The project settings can be found in `settings.py`. The available configuration parameters are:
- `SECRET_KEY` Random string used for signing session tokens and Flask security. Generated automatically upon project creation when using `silence new`. **No default is provided** and not setting one will result in an error.
- `DEBUG_ENABLED` Controls whether debug messages and Flask's debug mode are active (bool, default: `False`)
- `LISTEN_ADDRESS` IP address in which the web server will listen to requests (str, default: `"127.0.0.1"`)
- `HTTP_PORT` Port in which the web server will listen to requests (int, default: `8080`)
- `SQL_SCRIPTS` Sequence of files inside the `sql/` folder to run when the `silence createdb` command is issued (list[str], default: `[]`)
- `API_PREFIX` URL prefix for API requests (str, default: `/api`, do not set empty)
- `DB_CONN` Connection details to the MySQL/MariaDB database
- `host` IP or name of the server (str, default: `'127.0.0.1'`)
- `port` Port the SQL server is listening to (int, default: `3306`)
- `username` User in the SQL server to use (str, default: `default_username`)
- `password` Password for the previous user (str, default: `default_password`)
- `database` Name of the schema to use, the user should have privileges to create and destroy tables in this schema (str, default: `default_database`)
- `RUN_API` Deploy the API endpoints (bool, default: `True`)
- `RUN_WEB` Deploy the static web server (bool, default: `True`)
- `ENABLE_LOGIN` Enables the /login endpoint (bool, default: `True`)
- `ENABLE_REGISTER` Enables the /register endpoint (bool, default: `True`)
- `ENABLE_SUMMARY` Enables the API summary endpoint (`GET API_PREFIX`) (bool, default: `True`)
- `SHOW_ENDPOINT_LIST` Controls whether the list of all available endpoints is displayed when using `silence run` (bool, default: `True`)
- `COLORED_OUTPUT` Enables colors in the console output (bool, default: `True`)
- `DECIMALS_AS_STRINGS` Controls whether Decimal types are serialized as `str` instead of `float` (bool, default: `False`)
- `USER_AUTH_DATA` Configures which information to use for login and register
- `table` Name of the table containing your users (str, default: `users`)
- `identifier` Column of this table containing the unique identifiers used for login (str, default: `username`)
- `password` Column of this table containing the hashed passwords (str, default: `password`)
- `role` Column of this table containing the role of the user (**optional**, str, default: `role`)
- `active_status` Column of this table containing a boolean value, representing if the user is allowed to log in or not (**optional**, no default).
- `ALLOW_CLEAR_PASSWORDS` Allows clear text passwords stored in the users table to be used for login (bool, default: `False`)
- `DEFAULT_ROLE_REGISTER` Role to assign to the users that register via the `/register` endpoint (str, default: `None`)
- `HTTP_CACHE_TIME` Sets the `max-age` value in the `Cache-Control` HTTP header for static files sent by the server. In practice, this controls for how long these files are cached by the web browser. (int, default: `0` for development purposes)
- `MAX_TOKEN_AGE` Time in seconds during which a session token is valid after it has been issued (int, default: `86400`)
- `CHECK_USER_IS_ACTIVE` Whether to check if a user is active when logging in (bool, default: `True`). Note that this only works if `USER_AUTH_DATA.active_status` is also set to the name of the corresponding column.
- `DEFAULT_ACTIVE_STATUS` Default value for the column that determines whether a user is active. Used when registering new users, only applies when the activity check is on and no value has been provided during register (bool, default: `True`)
- `CHECK_FOR_UPDATES` Whether to check for new Silence versions when using `silence run` (bool, default: `True`)
- `ENABLE_ENDPOINT_AUTO_GENERATION` Allows for the use of the `silence createapi` command (bool, default:`True`)
# Creating the database
Silence provides the `silence createdb` command to automatically execute any number of SQL scripts to create your database and/o