Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Spark-Project-Pulse/codehive-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

462 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pulse (Backend)

Contributors Forks Stargazers Issues MIT License


Pulse

Buzz buzz!

Table of Contents
  1. About The Project
  2. Getting Started
  3. License
  4. Contact

About The Project

image

CodeHive aims to be a tool to help aspiring developers get fast, reliable, and contextual feedback on their questions. This is achieved by enabling seamless project integration and providing holistic context to related questions.

(back to top)

Built With

  • Next

(back to top)

Getting Started

This is an example of how you may give instructions on setting up your project locally. To get a local copy up and running follow these simple example steps.

Prerequisites

This is an example of how to list things you need to use the software and how to install them.

  • pip

     python -m pip install --upgrade pip

Installation

  1. Clone the repo

    git clone https://github.com/Spark-Project-Pulse/backend.git
    

Secret Management

Accessing Secrets locally

  1. Go to the Google Cloud Console, navigate to IAM & Admin > Service Accounts, and and download the already existing json key from the service acount called Secret Accessor Service Account. Go to the Keys tab and click Add Key then Create New Key then Create (use json format). Your key will download as pulse-random-letters-and-numbers.json in the root of the project.
  2. Add a .env.local file to the root of the project with the following contents:
GOOGLE_CLOUD_PROJECT=google_cloud_project_id
GOOGLE_APPLICATION_CREDENTIALS=pulse-random-letters-and-numbers.json
  1. Use the get_secret function in services/secret_manager.py to access secrets stored in Google Secret Manager locally. The function takes the secret name as an argument and returns the secret value.

Updating Secrets

To ensure they will be available in production and consistent across all environments and between developers, secrets should be stored in Google Secret Manager. To update a secret:

  1. Navigate to the Google Cloud Console
  2. Select the project Pulse
  3. Navigate to Secret Manager
  4. Select the secret you want to update
  5. Click Delete to delete the secret
  6. Now recreate the secret with the new value (I am aware this is kind of a pain, but I haven't researched how versions work yet)

Creating New Secrets

For the frontend, there are three kinds of secrets: local, local docker, and production. When creating a new secret, make sure to add the local version by appending _LOCAL, the local docker version by append _LOCAL_DOCKER to the secret name, and the production version by appending _PRODUCTION to the secret name. Even if the secret is the same for all environments, it should be added for each environment to ensure that the secret is available in all environments. The get_secret function handles the logic of which secret to return based on the environment, so after adding the secret to Google Secret Manager, it should be accessible via the function.

Steps for creating a new secret via the Google Cloud Console UI

  1. Navigate to the Google Cloud Console
  2. Select the project Pulse
  3. Navigate to Secret Manager
  4. Select the secret you want to update
  5. Click Create Secret
  6. Enter the secret's name and value
  7. Click Create Secret

Run locally

Prequisites

In order to run the backend locally, you must ensure that you have installed the Supabase CLI and started the database:

  1. Install Supabase CLI

  2. Make sure the Docker daemon is running (open Docker Desktop)

  3. Start supabase db

    supabase start
  4. Proceed to running the backend (note that all db updates will only be saved on your machine)

  5. Stop supabase when finished

    supabase stop

Docker

Running the backend
  1. Make sure the Docker daemon is running (open Docker Desktop)

  2. Navigate to the backend repository

  3. Use this command to build and run the backend container:

    docker compose up --build
  4. Navigate to http://localhost:8000/questions/getAll to see an example of an API response

Running the frontend & backend in one command
  1. Make sure the Docker daemon is running (open Docker Desktop)

  2. Navigate to the backend repository

  3. Make sure the backend repository is in the same directory as the frontend repository

  4. Use this command to run the project locally:

    docker compose --profile frontend-and-backend up --build
  5. This will run the frontend and backend in separate containers

Django

  1. Install Pipenv (if not already installed):

    pip install pipenv
  2. Install the project dependencies using Pipenv:

    pipenv install --dev
  3. Activate the Pipenv virtual environment:

    pipenv shell
  4. Run the Django development server locally:

    python manage.py runserver

Working with the database

This project uses a combination of Django's built in ORM and supabase to store/edit data. In order to edit/add/remove any database tables, be sure to follow these steps:

  1. Make needed changes to pulse/models.py

  2. Stage the migrations:

    python manage.py makemigrations
  3. Execute the migrations (DISCLAIMER: this will NOT apply changes directly to the hosted database, they will only be applied locally, and will take effect in the hosted database when your PR is merged into main)

    python manage.py migrate
  4. Run the following SQL commands in your SQL Editor on your local Supabase DB

    CREATE EXTENSION IF NOT EXISTS pg_trgm;
    CREATE EXTENSION IF NOT EXISTS unaccent;
    -- create profile-images bucket
    insert into storage.buckets
     (id, name, public)
    values
     ('profile-images', 'profile-images', true);
    -- create hive-avatars bucket
    insert into storage.buckets
     (id, name, public)
    values
     ('hive-avatars', 'hive-avatars', true);
    -- enable uploading to the profile-images bucklet
    CREATE POLICY "Allow all on profile-images" 
    ON storage.objects
    FOR ALL
    USING (bucket_id = 'profile-images');
    -- enable uploading to the hive-avatars bucklet
    CREATE POLICY "Allow all on hive-avatars" 
    ON storage.objects
    FOR ALL
    USING (bucket_id = 'hive-avatars');
    -- enable public operations on all buckets (this is needed to avoid RLS issues)
    CREATE POLICY "Allow all bucket operations"
    ON storage. buckets
    FOR ALL USING (true) ;

(back to top)

Usage

Use this space to show useful examples of how a project can be used. Additional screenshots, code examples and demos work well in this space. You may also link to more resources.

For more examples, please refer to the Documentation

(back to top)

Top contributors

contrib.rocks image

License

Distributed under the MIT License. See LICENSE.txt for more information.

(back to top)

Contact

CodeHive - [email protected]

Project Link: https://github.com/Spark-Project-Pulse/backend

(back to top)

About

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors