HackClub Website Backend

June 7, 2021 ยท View on GitHub

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

NAMEDESCTYPEDEFAULTREQUIRED
DATABASE_URLThe database urlUrl String-YES
ALLOWED_ORIGINSList of allowed origins in productionList as Json String[*]NO
SECRET_KEY64 digit hexadecimal string used for encryptionStringRandom KeyNO
DEBUGIs Debug ModeBoolean as StringtrueNO
ALLOW_RELOADPass --reload param to uvicorn run server cmdBoolean as StringfalseNO
TEST_DBTest Database URLUrl String-NO

Email Variables

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

NAMEDESCTYPEDEFAULTREQUIRED
EMAIL_USERNAMEUsername(Generally same as FROM mail)String-NO
EMAIL_PASSWORDPasswordString-NO
EMAIL_FROMEmail from which the mail is sentEmailtest@test.comNO
EMAIL_PORTThe email portInteger-NO
EMAIL_SERVERThe email server urlUrl-NO
EMAIL_TLSUse TLSBoolean as StringfalseNO
EMAIL_SSLUse SSLBoolean as StringfalseNO

Cloudinary Variables

NAMEDESCTYPEDEFAULTREQUIRED
CLOUDINARY_URLThe url containing all cloudinary infoUrl-NO
CLOUDINARY_OVERRIDEOverride default and use cloudinary for uploadsBoolean as StringfalseNO

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 /

URLDESCRIPTIONMETHODPARAMSAUTHENTICATEDRESPONSE
/auth/user/{user_id}/Get User By IDGET-NoUser
/auth/user/Create new User(DEBUG ONLY)POSTUserCreateNoUser
/auth/user/{user_id}/Update Existing UserPATCHUserUpdateYesUser
/auth/user/{user_id}/Soft Delete User By IDDELETE-Yes-
/auth/token/Return token by submitting credentialsPOST as formdata/x-www-form-urlencoded{"email": STRING, "password": STRING}No{"access_token": "string", "token_type": "string"}
/application/View all pending applicationsGET-YesList(ApplicationView)
/application/{application_id}/View application by IDGET-NoApplicationView
/application/{application_id}/Approve/Reject ApplicationPATCH{"approved": BOOLEAN}Yes-
/content/feedback/Get all feedbacksGET-NoList(FeedbackView)
/content/feedback/{feedback_id}/View feedback by IDGET-NoFeedbackView
/content/feedback/Create a feedbackPOST{"content": STRING}NoFeedbackView
/content/events/List all eventsGET{"upcoming": BOOL as STRING}NoList(EventView)
/content/events/{event_id}/Get event by IDGET-NoEventView
/content/events/Create EventPOSTEventCreateYesEventView
/content/events/{event_id}/Edit EventPATCHEventView(all optional)YesEventView
/content/events/{event_id}/Delete EventDELETE-Yes-
/content/image/Upload ImagePOST 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