Code Monkey home page Code Monkey logo

go-rest-api's Introduction

Go REST API

This project is a secure REST API with JWT authentication, storing access tokens in cookies, and protecting sensitive routes through middleware.


Dependencies


Database

Tried with SQL Server but can't connect to port 1433 using TelNet so I use PostgreSQL

issue: hashicorp/vault#4167


Setting up the project

Follow the steps below to set up the project.

Environment Variables

create a .env file in the root directory and add the following:

PORT=8080
DB_URL="host=localhost user=gorm password=gorm dbname=gorm port=5432 sslmode=disable"
SECRET="Your_Super_Secret_Key"

PORT is the port where the API will run (default: 8080). You can change it if you want.

DB_URL is the connection string for the database. Change it according to your database credentials.

SECRET is the secret key for JWT authentication.


Run

Run the following command to start the server:

go run ./cmd/main.go

Running with Air (optional)

Air is a tool for running Go applications in the background, refreshing whenever it is modified.

If you got air installed, run the following command

air

Testing

You can use Postman to test the API.

Base address: http://localhost:{port}/

ps: Check the port in .env file and feel free to change it.


Endpoints

  • [POST] ~/signup
  • [POST] ~/login
  • [GET] ~/posts
  • [GET] ~/posts/{id}
  • [POST] ~/posts (Requires authentication)
  • [PUT] ~/posts/{id} (Requires authentication)
  • [DELETE] ~/posts/{id} (Requires authentication)

go-rest-api's People

Contributors

gblw1 avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

rowzzy

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.