sh-code-challenge

command module
v0.0.0-...-c91f845 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2023 License: MIT Imports: 3 Imported by: 0

README

Sword Health Code Challenge

made-with-Go go.mod LICENSE Go Report Card

This is a task management application written in Go that provides a REST API for authentication and authorization of users with different roles. The application and its dependencies can be run using Docker Compose.

Features

  • Task management: users can create, read, update, and delete tasks
  • Role-based authentication: users are assigned different roles (manager and technician) and are authorized to perform certain actions based on their role
  • REST API: the application provides a REST API for managing tasks, users and performing authentication and authorization
  • Docker Compose: the application and its dependencies can be run using Docker Compose for easy setup and configuration

Prerequisites

  • Docker and Docker Compose

Getting Started

Installation
  1. Clone the repository
git clone https://github.com/PixDale/sh-code-challenge.git
  1. Navigate to the project directory
cd sh-code-challenge
  1. Start the application using Docker Compose
docker-compose up

PS: If needed you can clean docker cache before start, using the command make docker_clean

API Endpoints

The API provides the following endpoints for task management and authentication:

  • POST /tasks: create a new task
{"summary": "Performed a task", "user_id": 1}

*the user_id must match the user id from token

  • GET /tasks: retrieve a list of tasks
    • For requests with the Manager role, all tasks will be retrieved
    • For requests with the Technician role, only the task from this user will be retrieved
  • GET /tasks/{id}: retrieve a single task by ID
    • For requests with the Manager role, the task is retrieved unconditionally
    • For requests with the Technician role, the task is retrieved only if it belongs to this user
  • PUT /tasks/{id}: update an existing task
    • For requests with the Manager role, the task is updated unconditionally
    • For requests with the Technician role, the task is updated only if it belongs to this user
{"summary": "Performed a task", "user_id": 1}

*the user_id must match the user id from token

  • DELETE /tasks/{id}: delete a task
    • Requests must have a user token with the Manager role

All user management requests must have a user token with the Manager role

  • POST /users: register a new user
{"name": "Felipe", "email": "[email protected]", "password": "123", "role": 1}
  • GET /users: retrieve a list of users
  • GET /users/{id}: retrieve a single user by ID
  • PUT /users/{id}: update an existing user
{"name": "Felipe", "email": "[email protected]", "password": "123", "role": 1}
  • DELETE /users/{id}: delete a user

  • POST /login: login to retrieve a JWT token

    • Return the JWT Token containing information such as User ID and Role
Authentication

Access to all endpoints except for login, requires a JSON Web Token (JWT) for authentication. The token must be included in the Authorization header of the request in the following format:

Authorization: Bearer [JWT_TOKEN]
Authorization

There is role based authorization implemented for the API requests. Existing roles:

Manager Role: 1
Technician Role: 2
Notification

This project contains 2 apps, in addition to the main API app, there is also a notification app, which can be found in the root of the repository.

This notification app is responsible for receiving and handling the notification sent by the main API app. It currently only prints the notification to standard output, but it can easily be replaced with another means of generating a notification.

In the main API app, notifications are implemented as a middleware, checking whether the current request comes from a technician, if so, it sends a message to the notification queue, containing the action that this user is performing.

Tests

To run the test environment along with the unit tests run the command:

docker-compose -f docker-compose.test.yml up --build --abort-on-container-exit
Doc

To check the Golang documentation, first you need to have godoc cmd. To install it run:

go install -v golang.org/x/tools/cmd/godoc@latest

After installed, run:

godoc --http=localhost:6060

then access: http://127.0.0.1:6060/pkg/github.com/PixDale/sh-code-challenge/

Kubernetes

To start the kubernetes deployment, first you need to have minikube and kubectl installed, then run:

make kube_start
make kube_apply

After that if you want to stop, run:

make kube_stop

License

This project is licensed under the MIT License.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
api
Package api provides the main server for the application
Package api provides the main server for the application
auth
Package auth contains the rule of authentication and authorization used by the application
Package auth contains the rule of authentication and authorization used by the application
controllers
Pakcage controllers implements several handler for the RestAPI app
Pakcage controllers implements several handler for the RestAPI app
middlewares
Package middlewares contains the middlewares used on the project
Package middlewares contains the middlewares used on the project
models
Package models implements the database operations and user management functions
Package models implements the database operations and user management functions
notification
Package notification implements the access to RabbitMQ with the notification queue
Package notification implements the access to RabbitMQ with the notification queue
responses
Package responses provides structured HTTP responses for a Fiber application
Package responses provides structured HTTP responses for a Fiber application
seed
Package seed provides seed data for the database and functions to load the data into the database
Package seed provides seed data for the database and functions to load the data into the database
utils/encryption
Package encryption contains funtions to encrypt and decrypt text used by the application
Package encryption contains funtions to encrypt and decrypt text used by the application
utils/formaterror
Package formaterror provides custom errors to be used by the application
Package formaterror provides custom errors to be used by the application

Jump to

Keyboard shortcuts

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