socialnets-api

module
v0.0.0-...-0749ddc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 13, 2024 License: MIT

README

Socialnets-API

A social network API with the functionalities (so far) of user registration, following users, search, posting, and liking posts.

Requirements

If you use docker, here you will find a complete environment container.

Installation

Clone or download this repository.

Copy .env.example to .env.

[!IMPORTANT] You must set SECRET_KEY in this file, which can literally be any string.

However, you can generate a SECRET_KEY with the following Go code, which will print it in the prompt. After that, simply copy the generated hash to the .env file.

package main

import (
	"crypto/rand"
	"encoding/base64"
	"fmt"
	"log"
)

func main() {
	key := make([]byte, 64)
	if _, err := rand.Read(key); err != nil {
		log.Fatal(err)
	}

	secret := base64.StdEncoding.EncodeToString(key)
	fmt.Println(secret)
}

After that, choose one of the following options (Docker or Local).

Docker

At the root of the project there is a Dockerfile and docker-compose.yml files with requirements to run the project. You can run with docker by building the image and running it.

Execute:

docker-compose up

You'll have API running on http://localhost

Local

Set database configuration on .env file and create the database. In docker folder, you will found a SQL script that will create database and tables.

# Install dependencies
go mod tidy

# Start application
go run ./cmd/api/main.go

If you want to generate the executable, use:

# Install dependencies
go mod tidy

# Generate executable
go build ./cmd/api

# Start application
./api

You'll have API running on http://localhost:8000 (if you don't change API_PORT on .env).

Migrations

This project uses pressly/goose to manage migrations, which are located in the build/migrations folder. To execute them, install Goose:

go install github.com/pressly/goose/v3/cmd/goose@latest

Change variables and run:

goose -dir build/migrations postgres "postgresql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:5432/{DATABASE_NAME}?sslmode=disable" up

For convenience, you can set the database driver and connection string as environment variables:

export GOOSE_DRIVER=postgres
export GOOSE_DBSTRING=postgresql://db_user:your_password@db_host:5432/socialnets?sslmode=disable

And then, you can run the migration using a short command:

goose -dir build/migrations up

Usage

After starting application, you'll have access to following routes:

Method URI Authentication Description
GET /health No Application status health check
POST /api/login No User login
POST /api/user No Create an user
GET /api/user Yes Search for users
GET /api/user/{userId} Yes Get an user data
PUT /api/user/{userId} Yes Update an user data
DELETE /api/user/{userId} Yes Delete an user
POST /api/user/{userId}/follow Yes Logged user follows an user
POST /api/user/{userId}/unfollow Yes Logged user unfollows an user
GET /api/user/{userId}/followers Yes Get all followers by an user
GET /api/user/{userId}/following Yes Gets all users who are following a user
POST /api/user/{userId}/update-password Yes Update password user
POST /api/post Yes Create a post
GET /api/post Yes Get all post for a logged user
GET /api/post/{postId} Yes Get a post
PUT /api/post/{postId} Yes Update a post
DELETE /api/post/{postId} Yes Delete a post
GET /api/user/{userId}/posts Yes Gets all posts from a user
POST /api/post/{postId}/like Yes Like a user post
POST /api/post/{postId}/unlike Yes Unlike a user post

Authentication, once performed with login (email) and password on /api/login, is maintained via a JWT Bearer Token.

You can find more information, like payload and responses in the application swagger (coming soon).

So API is already to use.

Tests

To run tests run on root of the project:

go test ./...
Coverage

Run at the root of the project to see tests coverage:

go test ./... --cover

API documentation

🚧

Soon a swagger doc will be added.

TODO

  • Add GORM to the project
  • Improve tests
  • Add Swagger documentation
  • Implements UUID for user's Id
  • Register who liked a post, so that each user can only like each post once, in addition to having information on who liked each post.
    • Add likes table
    • Control who like or unlike a post.
  • Implements migrations
  • Password recovery

License

MIT

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL