Skip to content

SharpKoi/basic-golang-backend

Repository files navigation

Advanced Golang Backend - Social Media

💡 Info: You're in advanced example which implemented more ideas. To see the original example please switch to basic branch.

This project is written in Go. After learning from Boot.dev Golang courses, trying to implement a simple RESTful backend API with golang standard library net/http, and using json PostgreSQL as the database.

To run this project, please clone this project to your local machine first:

git clone https://github.com/SharpKoi/basic-golang-backend.git
cd basic-golang-backend

And you need to set environment variables DATABASE_URL and JWTSecret:

export database_url=${your postgreSQL database URL}
export jwtsecret=${your jwt key}

Type the command below to run the project (make sure you have docker and docker compose installed):

docker-compose up --build

It will create a server binded on docker default bridge network address, either 127.17.0.0 or 0.0.0.0, with port 8080.

You can use Postman or curl or any else to test this API by sending requests to server address.

API

Method URL Description Permission
POST /api/users/login Login to retrieve your json web token everyone
GET /api/users Get all user accounts from database admin
GET /api/users/${email} Get the user account by user's email owner+
POST /api/users Create an user account by given request body admin
PUT /api/users/${email} Update the user account by the given email and request body owner+
DELETE /api/users/t${email} Delete user account by the given email owner+
GET /api/posts/${email} Get all the posts created by the user with the given email everyone
POST /api/posts Create a post by given request body owner
DELETE /api/posts/${uuid} Delete a post by the given uuid owner+

Here's also a full testing example created at Postman. You can use the given examples to test this API.

TODO

These Ideas below are from the final section of the course, for extending the backend program.

  • Use PostgreSQL instead of a JSON file for the database layer
    • Used pgx as PostgreSQL Driver
    • Used database/sql to implement SQL operations
    • Alternatively there are more powerful tools like migrate, gorm to implement SQL database layer.
  • Add proper authentication to each request, may use the password validator designed by Lane
  • Allow users to save other data with their posts
  • Add more unit tests
  • Deploy the API on AWS, GCP, or Digital Ocean
  • Dockerize it
    • Used docker-compose to build golang and PostgreSQL containers
      • docker-compose is a tool that help us to build multiple containers conveniently.
    • Used bridge network to connect the two containers so that they can communicate with each other
  • Add documentation using markdown files
  • Write a frontend that interacts with the API, maybe a webpage or a mobile app

And these ideas below are mine

  • Write a full guide for the backend API designing
  • Use gin to implement backend API, go-oauth2 to implement authentications, and gorm to implement SQL database layer
  • Integrate with discord bot
  • Use sentimental analysis to detect the emotion of each post

About

The social media backend example written in golang.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published