Giter Club home page Giter Club logo

golangrestfulapiexample's Introduction

GolangRestfulApiExample

This personal project shows how to write a simple Restful Microservice API written in Go (Golang).

Prerequisites

This project uses the Go language to build the Miscroservice. The database connection is made with MongoDB. So both Go and MongoDB must be installed. The configuration is not externalized, so the connection with the database will be done int he same machine as the database server is running (localhost).

Installing

To run this project just run the main file and run the MongoDB server in another terminal.

Start the microservice:

go run main.go

Start the database server:

mongod

Exemple of the requests that can be done with the microservice. Create, Read, Update and Delete.

POST

  • The POST request will insert a new user in the database, all of the fields must have an value associated or it will not be accepted by the service.
  • The socialNumber must be unique within all of the users.

Body:

{
	"name": "JP",
	"age": "26",
	"phones": ["32511234", "999991991"],
	"socialNumber": "88877766600",
	"address":{
		"streetName": "Rua Principal",
		"streetNumber": "123",
		"zipCode": "18000000",
		"country": "Brasil",
		"state": "São Paulo"
	}
}

GET

  • To recover and see the information about a user, the GET request will only need a socialNumber associated with the user.
  • Each user has its own and unique socialNumber.

Header:

socialNumber: 88877766600

PATCH

  • To update or change some data from a user, just create a PATCH request.
  • The data send within the request must have at leat one completed field in the body and the socialNumber in the header to be accepted by the service.

Header:

socialNumber: 88877766600

Body:

{
	"name": "João Pedro",
	"age": "27",
	"phones": ["999991991"],
	"socialNumber": "88877766600",
	"address":{
		"streetName": "Rua Matriz",
		"streetNumber": "456",
		"zipCode": "18111111",
		"country": "Peru",
		"state": "Lima"
	}
}

DELETE

  • To delete all of the selected user`s data the DELETE request must be sent with only a socialNumber associated with the user in the header.
  • Each user has its own and unique socialNumber.

Header:

socialNumber: 88877766600

Running the tests

Unit and Integration Tests are being written within each service. A script is being made that the test and its verification with the database must pass it all.

Running on Docker

To run on Docker we can run the following commands:

To build a Go image in Linux (if you are in another system)

CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
go build -v

Change the built image to GolangRestfulApiExample To build a Docker Image with the Go image and run it in the background:

docker build -t golang/restful-api-example .
docker run -d -p 8080:80 golang/restful-api-example

That is it, the User CRUD Microservice built with Go and MongoDB is now running in a container and can be deployed in the cloud for easy access.

Thanks ;D

golangrestfulapiexample's People

Contributors

joaopmgd avatar

Watchers

James Cloos 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.