Giter Club home page Giter Club logo

golang-test-api's Introduction

License Gitpod Ready-to-Code Go CI Docker Image CI

golang-test-api

A simple CRUD API made with Go, Postgres, FIber, Gorm and Docker.

  • Cloning the repository

    To clone the repository run the following command:

    $ git clone https://github.com/WeDias/golang-test-api.git
  • How to install and run (docker)

    Inside the newly cloned project folder run the following command:

    $ docker compose up
    # or
    $ docker-compose up
  • How to install and run (manually)

  • Installing the dependencies and setup env

    Inside the newly cloned project folder run the following command to install the dependencies:

    $ go mod download

    To configure the environment run this command:

    $ bash setup-env.sh

    It will generate a .env file with the variables below, you can edit to adapt with your database settings.

    PG_HOST=localhost
    PG_PASS=postgres
    PG_USER=postgres
    PG_DBNM=postgres
    PG_PORT=5432
    
    API_PORT=:3000

    After that, run the ddl-database.sql file in your database which is inside the resources folder.

  • Running the application

    Inside the project, run the following command to run the application:

    $ go run main.go
  • API

    Create a new product:

    # POST /api/v1/product
    
    $ curl --request POST \
    --url http://localhost:3000/api/v1/product \
    --header 'Content-Type: application/json' \
    --data '{
      "name": "product1",
      "price": 10.99,
      "stock": 10
    }'
    
    # > {"id":1,"name":"product1","price":10.99,"stock":10}

    Get all products:

    # GET /api/v1/product
    
    $ curl --request GET \
    --url http://localhost:3000/api/v1/product
    
    # > [{"id":1,"name":"product1","price":10.99,"stock":10}]

    Get a product by id:

    # GET /api/v1/product/1
    
    $ curl --request GET \
    --url http://localhost:3000/api/v1/product/1
    
    # > {"id":1,"name":"product1","price":10.99,"stock":10}

    Update a product by id:

    # PUT /api/v1/product/1
    
    $ curl --request PUT \
    --url http://localhost:3000/api/v1/product/1 \
    --header 'Content-Type: application/json' \
    --data '{
      "name": "a product",
      "price": 8.99,
      "stock": 5
    }'
    
    # > {"id":1,"name":"a product","price":8.99,"stock":5}

    Delete a product by id:

    # DELETE /api/v1/product/1
    
    $ curl --request DELETE \
    --url http://localhost:3000/api/v1/product/1
    
    # > {"id":1,"name":"a product","price":8.99,"stock":5}

Some useful commands

  • Download dependencies
go mod tidy
go mod init

Understand about dependencies

golang-test-api's People

Contributors

wedias avatar haithai91 avatar

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.