Skip to content

Setup a development environment

Logan Davidson edited this page Feb 12, 2025 · 28 revisions

If you attempted/completed these steps and you have any questions or feedback, please contact me at iamlogandavidson@gmail.com.

Operating system

This guide is mainly aimed at Windows users, but the frontend and backend apps both run on Linux systems in production, so getting it running on UNIX systems like Linux or macOS should be pretty straightforward.

Visual Studio Code

The project contains a Visual Studio Code workspace file called roronline.code-workspace. If you use VS Code as your editor, I recommend opening the project via the workspace rather than the folder.

How to setup a new local development environment

Prerequisites

Steps

  1. Clone this repository.

Backend

  1. Create a Python 3.11 virtual environment called env in the backend folder.

    • This can be done by opening a terminal or command prompt in the backend folder and executing this command. Remember to replace C:\Python\Python38\python with the correct path to your Python 3.11 executable.

      C:\Python\Python38\python -m venv env
      
  2. Activate the virtual environment.

    • Now that you've created a Python virtual environment, you must understand how to activate and deactivate it. To execute any Python commands in this project you will need to do so within the virtual environment. This guide covers activation and deactivation of the virtual environment: https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/.

    • Remember to always activate your virtual environment in the backend folder. This is where you will execute all your Python commands.

      From here on out, every time this guide instructs you to enter a command starting with python or pip, it is implied that you should enter the command in the activated virtual environment in the backend folder.

    • Ensure that your virtual environment is using the correct version of Python by entering this command. The command should return Python 3.11.4.

      python --version
      
  3. Update pip using this command.

    python -m pip install --upgrade pip
    
  4. Install packages from requirements.txt using this command.

    pip install -r requirements.txt
    
  5. Create a Google Auth Platform client for Google authentication.

    • In the Google Cloud Platform online management console, create a new project then navigate to Google Auth Platform.

    • Configure Google Auth Platform by creating a new app named "Republic of Rome Online".

    • In the Data Access tab, add scopes for ./auth/userinfo.email, ./auth/userinfo.profile and openid.

    • In the Clients tab, create a new client with the following settings:

      Application type: Web application
      Name: Local development web client
      
      Authorized redirect URIs:
      URIs 1: http://localhost:8000/accounts/google/login/callback/
      URIs 2: http://127.0.0.1:8000/accounts/google/login/callback/
      
    • Take note of the Client ID and Client secret.

  6. In the backend folder, create a new file called .env with this content.

    ALLOWED_HOSTS=127.0.0.1
    FRONTEND_ORIGINS=http://127.0.0.1:3000
    DEBUG=True
    PARENT_DOMAIN=127.0.0.1
    RDS_HOSTNAME=localhost
    RDS_NAME=postgres
    RDS_PASSWORD=rdspassword
    RDS_PORT=5432
    RDS_USERNAME=postgres
    REDIS_HOSTNAME=127.0.0.1
    SECRET_KEY=abc123
    SOCIALACCOUNT_GOOGLE_CLIENT_ID=googleclientid
    SOCIALACCOUNT_GOOGLE_SECRET=googlesecret
    
    • Replace rdspassword with the password you created for the postgres superuser during PostgreSQL installation.
    • Replace googleclientid with the client ID for your Google Auth Platform client.
    • Replace googlesecret with the secret for your Google Auth Platform client.
  7. Ensure that the PostgreSQL server (a background service) is running.

    • If you have just installed PostgreSQL, the server may not be running.

    • On Windows, you can use Services.msc to check which services are running, manually start the server and configure automatic startup behaviour. Look for the service whose name begins with "postgresql". This guide covers basic usage: https://www.thewindowsclub.com/open-windows-services

  8. Syncronize the database with Django's models and migrations using this command.

    python manage.py migrate
    
  9. Create an application superuser with this command.

    python manage.py createsuperuser
    
    • You will need to provide a username and password. The email address is optional.
  10. Start a Redis instance inside a docker container using this command.

    docker run -p 6379:6379 -d redis:5
    
  11. Start the backend development server using this command.

    python manage.py runserver
    
    • Ensure that the backend server is running by visiting http://127.0.0.1:8000. If you are able to access the web browsable API and the Django admin portal, that means you have successfully setup up the backend.

Frontend

  1. Now in the frontend folder, create a file called .env with this content.

    BACKEND_URL=http://127.0.0.1:8000/
    
  2. Install packages and start the frontend server using npm.

    • In the frontend folder, open a terminal or command prompt and enter these commands. The first command installs required packages and the second command starts the frontend development server.

      npm install
      
      npm run dev
      
    • Ensure that the frontend server is running by visiting http://localhost:3000. Login in using the credentials you provided in step 9. If that works, congratulations! You have successfully set up the Republic of Rome frontend and backend development servers on your local machine!

How to update an existing local development environment

Prerequisites

  • The steps in "How to setup a new local development environment" have been completed previously.

  • The Python virtual environment has been upgraded to use the version of Python specified above.

  • Environment variable names and values are up to date.

Steps

  1. Update the backend by executing these commands in the backend folder, using the Python virtual environment.

    • Install packages from requirements.txt using this command.

      pip install -r requirements.txt
      
    • Syncronize the database with Django's models and migrations using this command.

      python manage.py migrate
      
  2. Update the frontend by executing this command in the frontend folder.

    npm install