/website-backend

The hackclub backend server written in FastAPI

Primary LanguagePython

HackClub Website Backend

Table of Contents

Requirements

  • Docker(With compose): Container tool

Verify by

docker -v
docker-compose -v

Running the app

  • Create a email.env and cloudinary.env file to prevent docker errors. $ docker-compose up
  • Add --build to rebuild the image if needed.
  • Add -d to run in detached mode(No Output from uvicorn process)
  • ./app & ./alembic are mounted as volumes to refresh changes without rebuilding the image

pgAdmin

You can use pgAdmin by visiting localhost:9000.

  • Username: admin@hackclubrit.com
  • Password: password
  • Get DB container IP by running docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' hackclub-db
  • Add server by entering
    • Host: IP of docker container
    • Database User: user
    • Database Password: password
  • The name of the database is hackclubdb

Running Scripts

Various scripts for different purposes has been made in the ./scripts directory. They connect to the remote container from host and run commands inside the container.

Set your docker container name(if you changed it) in ./scripts/set_env.sh

Running Migrations

Make migrations using

sh scripts/make_migrations.sh "<NAME>"

Migrate the database using

sh scripts/migrate.sh

Run pylint

Pylint is a Python static code analysis tool which looks for programming errors, helps enforcing a coding standard, sniffs for code smells and offers simple refactoring suggestions.

sh scripts/lint.sh

Run tests

Run tests using pytest

sh scripts/test.sh

Environment Variables

NAME DESC TYPE DEFAULT REQUIRED
DATABASE_URL The database url Url String - YES
ALLOWED_ORIGINS List of allowed origins in production List as Json String [*] NO
SECRET_KEY 64 digit hexadecimal string used for encryption String Random Key NO
DEBUG Is Debug Mode Boolean as String true NO
ALLOW_RELOAD Pass --reload param to uvicorn run server cmd Boolean as String false NO
TEST_DB Test Database URL Url String - NO

Email Variables

These environment variables are stored in a separate email.env file(Ignored by git)

NAME DESC TYPE DEFAULT REQUIRED
EMAIL_USERNAME Username(Generally same as FROM mail) String - NO
EMAIL_PASSWORD Password String - NO
EMAIL_FROM Email from which the mail is sent Email test@test.com NO
EMAIL_PORT The email port Integer - NO
EMAIL_SERVER The email server url Url - NO
EMAIL_TLS Use TLS Boolean as String false NO
EMAIL_SSL Use SSL Boolean as String false NO

Cloudinary Variables

NAME DESC TYPE DEFAULT REQUIRED
CLOUDINARY_URL The url containing all cloudinary info Url - NO
CLOUDINARY_OVERRIDE Override default and use cloudinary for uploads Boolean as String false NO

Json Schemas

User Receive

{
    "email": STRING, 
    "role": ENUM(ADMIN, MODERATOR, USER), 
    "name": STRING, 
    "id": INTEGER, 
    "is_active": BOOLEAN
}

User Create

{
    "email": STRING, 
    "role": ENUM(ADMIN, MODERATOR, USER), 
    "name": STRING,
    "password": STRING
}

User Update

{
    "email": STRING[Optional],
    "name": STRING[Optional],
    "password": STRING[Optional]
}

ApplicationView

{
    "email": STRING,
    "data": JSON,
    "name": STRING,
    "id": INTEGER,
    "status": ENUM(PENDING, APPROVED, REJECTED),
    "created_date": DATE 
}

ApplicationCreate

{
  "email": STRING,
  "data": JSON,
  "name": STRING
}

FeedbackView

{
  "id": INTEGER,
  "content": STRING,
  "created_time": DATETIME
}

EventView

{
  "user": {
    "name": STRING,
    "id": INTEGER
  },
  "image": URL,
  "name": STRING,
  "registration_link": URL,
  "description": STRING,
  "date": ISODateTime,
  "image_id": INTEGER,
  "id": INTEGER
}

EventCreate

{
  "name": STRING,
  "registration_link": URL,
  "description": STRING,
  "date": ISODateTime,
  "image_id": INTEGER,
}

Endpoints

NOTE: All urls contain trailing /

URL DESCRIPTION METHOD PARAMS AUTHENTICATED RESPONSE
/auth/user/{user_id}/ Get User By ID GET - No User
/auth/user/ Create new User(DEBUG ONLY) POST UserCreate No User
/auth/user/{user_id}/ Update Existing User PATCH UserUpdate Yes User
/auth/user/{user_id}/ Soft Delete User By ID DELETE - Yes -
/auth/token/ Return token by submitting credentials POST as formdata/x-www-form-urlencoded {"email": STRING, "password": STRING} No {"access_token": "string", "token_type": "string"}
/application/ View all pending applications GET - Yes List(ApplicationView)
/application/{application_id}/ View application by ID GET - No ApplicationView
/application/{application_id}/ Approve/Reject Application PATCH {"approved": BOOLEAN} Yes -
/content/feedback/ Get all feedbacks GET - No List(FeedbackView)
/content/feedback/{feedback_id}/ View feedback by ID GET - No FeedbackView
/content/feedback/ Create a feedback POST {"content": STRING} No FeedbackView
/content/events/ List all events GET {"upcoming": BOOL as STRING} No List(EventView)
/content/events/{event_id}/ Get event by ID GET - No EventView
/content/events/ Create Event POST EventCreate Yes EventView
/content/events/{event_id}/ Edit Event PATCH EventView(all optional) Yes EventView
/content/events/{event_id}/ Delete Event DELETE - Yes -
/content/image/ Upload Image POST as multipart/formdata {"img": File} Yes {"id":INT, "url": URL}

Image Upload

Refer flowchart for process flow.

Setting up docker remote interpreter for IDEs

VS Code, PyCharm - Blog