🔥 Golang Rest Api with basic JWT Authentication and Basic Crud Operations.

Golang Food Data REST API 🚀

🔥 Golang Rest Api with basic JWT Authentication and Basic Crud Operations.

Development Setup

First of all create a new .env file in the root of the project directory. see example.env file for all required environment variables.

After setting up environment variables, run the following command to start development and database servers.

docker-compose up

before running this command you must have docker and docker-compose installed in your system.

Production

In order to deploy this application to production environment, create a docker image from Dockerfile and run it on your production server.

Technology

  • Language (golang)
  • Database (mysql)

Libraries

  • Router (gorilla/mux)
  • Server (net/http)
  • JWT (dgrijalva/go-jwt)
  • Password Encryption (bcrypt)
  • Database ORM (gorm)
  • Live Reload (cosmtrek/air)

Features

  • Monlith
  • Authentication
  • endpoint protection with middleware
  • Authorization - Role based Access Control (RBAC)

API Documentation

Authentication

POST /auth/login

Login with username/email and password.

Body
{
    "id": "abc123",
    "password": "abc123",
}

Output

{
    "jwt": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
    "user": {
        "id": "123e4567-e89b-12d3-a456-426614174000",
        "name": "ABC 123",
        "username": "abc123",
        "email": "[email protected]",
    }
}

POST /auth/signup

Create a new user in the database.

Body
{
    "name": "ABC 123",
    "username": "abc123",
    "email": "[email protected]",
    "password": "abc123",
}

Output

{
    "jwt": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
    "user": {
        "id": "123e4567-e89b-12d3-a456-426614174000",
        "name": "ABC 123",
        "username": "abc123",
        "email": "[email protected]",
    }
}

Data Manipulation

All endpoints are protected, must send valid jwt as Authorization header with each request.

GET   /food/all

Get All Food Items

Output

[
    {
        "id": "123e4567-e89b-12d3-a456-426614174000",
        "name": "Apples",
        "quantity": 100,
        "selling_price": "100 USD",
    },
    {
        "id": "123e4567-e89b-12d3-a456-426614174000",
        "name": "Mangos",
        "quantity": 97,
        "selling_price": "120 USD",
    }
]

GET   /food/

Get single Food Item by its name. name should be lowercase (e.g /food/apples)

Output

{
    "id": "123e4567-e89b-12d3-a456-4265674174000",
    "name": "Apples",
    "quantity": 100,
    "selling_price": "100 USD",
}

POST   /food

Add a new food item to the database.

Body
{
    "name": "Oranges",
    "quantity": 44,
    "selling_price": "80 USD",
}

Output

{
    "id": "123e4567-e89b-12d3-a456-426614174000",
    "name": "Oranges",
    "quantity": 44,
    "selling_price": "80 USD",
}

DELETE   /food/

Delete one Food Item from the database.

Output

{
    "id": "123e4567-e89b-12d3-a456-426614174000",
    "name": "Oranges",
    "quantity": 44,
    "selling_price": "80 USD",
}
Owner
Junaid Javed
👨‍💻 full-stack JavaScript Developer | 👨‍🎓 CS student | 🚀 Freelancer | ☁️ DevOps Engineer | 💻 Tech Enthusiast.
Junaid Javed
Similar Resources

A simple passwordless authentication middleware that uses only email as the authentication provider

A simple passwordless authentication middleware that uses only email as the authentication provider

email auth A simple passwordless authentication middleware that uses only email as the authentication provider. Motivation I wanted to restrict access

Jul 27, 2022

Basic and Digest HTTP Authentication for golang http

HTTP Authentication implementation in Go This is an implementation of HTTP Basic and HTTP Digest authentication in Go language. It is designed as a si

Dec 22, 2022

X3 - A template for using HTTP Basic Authentication in Go

HTTP Basic Auth in Go This is a template for using HTTP Basic Auth in a Go appli

Sep 28, 2022

Goauth - Basic username password cookie based authentication with Go Lang

goauth [WIP] Basic username password cookie based authentication with Go Lang Overview Use a Postgres DB to store Sign-in and Sign-up info Redis for c

Jan 4, 2022

Go-Guardian is a golang library that provides a simple, clean, and idiomatic way to create powerful modern API and web authentication.

❗ Cache package has been moved to libcache repository Go-Guardian Go-Guardian is a golang library that provides a simple, clean, and idiomatic way to

Dec 23, 2022

:key: Secure alternative to JWT. Authenticated Encrypted API Tokens for Go.

branca branca is a secure alternative to JWT, This implementation is written in pure Go (no cgo dependencies) and implements the branca token specific

Dec 29, 2022

Golang implementation of JWT and Refresh Token

Fiber and JWT with Refresh Token Repo ini adalah demostrasi JWT support refresh token tanpa menggunakan storage Branch Main: unlimited refresh token R

Dec 18, 2022

Golang with JWT, Go Gin and MongoDB

User authentication in Golang with JWT, Go Gin and MongoDB Golang backend application that uses JWT tokens for users Locally Up Setup your .env file,

Sep 16, 2022

Golang based User creation and Management application. GORM, Fiber, JWT

User Creation and Management app (BACK-END) Auth Features: Create Clients (regular password + 6 one-time passwords + (optional) QR code and Secret for

Dec 2, 2022
Authentication Plugin for implementing Form-Based, Basic, Local, LDAP, OpenID Connect, OAuth 2.0, SAML Authentication
Authentication Plugin for implementing Form-Based, Basic, Local, LDAP, OpenID Connect, OAuth 2.0, SAML Authentication

Authentication Plugin for implementing Form-Based, Basic, Local, LDAP, OpenID Connect, OAuth 2.0, SAML Authentication

Jan 8, 2023
Account-jwt-go - Simple JWT api with go, gorm, gin
Account-jwt-go - Simple JWT api with go, gorm, gin

Account JWT on Go Go, gorm, Gin web framework 를 활용하여 만든 간단한 JWT API 입니다. Dajngo의

Apr 14, 2022
JWT wrapper library which makes it simple to use ECDSA based JWT signing

JWT JWT wrapper library which makes it simple to user ECDSA based JWT signing. Usage package main import ( "context" "github.com/infiniteloopcloud

Feb 10, 2022
Krakend-jwt-header-rewriter - Kraken Plugin - JWT Header Rewriter

Kraken Plugin - JWT Header Rewriter 1 Plugin Configuration Name Desciption Defau

Feb 15, 2022
A simple authentication web application in Golang (using jwt)

Simple Authentication WebApp A simple authentication web app in Go (using JWT) Routes Path Method Data /api/v1/auth/register POST {"firstname":,"lastn

Feb 6, 2022
A demo of authentication and authorization using jwt
A demo of authentication and authorization using jwt

Nogopy Hi, this a demo of how to use jwt for authentication in microservices Keep in mind that this is a demo of how to authenticate using jwt, we don

Nov 1, 2021
Backend Development Rest Api Project for book management system. Used Features like redis, jwt token,validation and authorization.

Golang-restapi-project Simple Rest Api Project with Authentication, Autherization,Validation and Connection with redis File Structure ├── cache │ ├──

May 25, 2022
rEST API to test JWT on RS256 and HS256 algorithm.

JWT Check - Mock para tests This repo is just a simple example of JWT token generation using RS256 algorithm and HS256 algorithm. This api is responsi

Aug 19, 2022
Authorization and authentication. Learning go by writing a simple authentication and authorization service.

Authorization and authentication. Learning go by writing a simple authentication and authorization service.

Aug 5, 2022
Authelia: an open-source authentication and authorization server providing two-factor authentication
Authelia: an open-source authentication and authorization server providing two-factor authentication

Authelia is an open-source authentication and authorization server providing two

Jan 5, 2022