Skip to content
Roshan Sharma edited this page Sep 8, 2020 · 2 revisions

Welcome to the blogstate-api wiki!

If you're trying to set up the API locally, please read along!

Configuring the system for testing

It is assumed here that all the dependencies are installed, and that the pip install ran successfully.

Now, the local database needs to be set up in order for the API to work. Below are the steps to be followed.

1. Creating the .env files

A .env file is meant to store your configuration variables, and is almost never made public. That's why it's not found in a Git repository.

This API needs two .env files - one under api/ and other the database/ directory.

  • The database/.env consists of everything the app needs to talk to the database on your system.
  • api/.env stores a token which authenticates incoming requests (i.e data is only returned if the agent sends a token in a request which is the same as stored in api/.env).

To create the files, follow the steps once you're in the project root.

Creating the database/.env file

$ cd database/
$ touch .env

Now, open the file in a text editor, and type in four lines as follows:

localhost    (default, assuming that's where your database runs)
root         (enter your MySQL username, usually "root")
password     (your MySQL installation password)
blogstate    (name of the database we are about to create)

Enter the above contents (without parentheses (...)). Make sure your database uses a password.

Note that the order of lines is important. Changing the order will definitely give an error.

Once done, the database/.env file should look something like this:

localhost
root
mypassword
blogstate

Of course yours will have different values, but the same structure.

Creating the api/.env file

Like before, cd into the api/ directory, and type touch .env. This creates a blank .env file, which you can open in your favourite text editor. Once done, enter a random string in it at the very start of the file. It literally can be "anything" you like, and of any length.

All incoming requests will be checked for this token (aka authentication token). The request will be processed only if it contains the authentication token.

2. Setting up the database

Now, we need to create a new database on our local machine. This is where all data lives - user credentials, blog posts, and so on.

Creating the database

The first thing here is to enter the MySQL console via the terminal (or in a GUI way like MySQL Workbench, phpmyadmin etc). To keep things minimal, let's create the database via the command line.

Open up the terminal, type:

$ mysql -u root -p

and enter your MySQL password.

If you're having trouble remembering your MySQL installation password, try looking up the Internet about it, or follow this article.

Next, the MySQL console should open up. Inside it, type the following couple of lines:

> CREATE DATABASE `blogstate`;
> exit;

This creates an empty database on your system, ready to be filled with tables to store information.

Creating tables inside the database

Make sure you are in the project root. Enter the Python console from here.

$ python

This opens up the Python interpreter. This is where peewee makes things easy.

>>> from api.models import *
>>> db.connect()

The db.connect() command must run successfully, and return True in the console. If that's not the case, there's perhaps an error in the credentials in the database/.env file. Modify them to the correct values, and you're good to go.

>>> db.connect()
# should return True.
# If it doesn't, go back and fix possible issues.

>>> db.create_tables([Credentials, Posts])
>>> exit()

Done! You now have the database and the tables configured properly.

Now, fire up any REST client (Insomnia, Postman), and read ahead to create some posts!

Important

Make sure to include the auth key (from api/.env) in the Authorization header when sending requests from the client.