Categorygithub.com/sreekesari-vangeepuram/golang-api
modulepackage
0.1.0
Repository: https://github.com/sreekesari-vangeepuram/golang-api.git
Documentation: pkg.go.dev

# README

About golang-api

A simple API written in Golang, which basically maintains
a database of users. These users can be added to the DB
using CRUD operations. This API uses faunaDB as its
database server. The JSON structure of the API request
should look like:

{
	"id": null,
	"name": "User",
	"dob": "2001-10-03T05:08:06.880755794+05:30",
	"address": "Canada",
	"description": "I am a user",
	"createdAt": null
}

The fields id and createdAt will be auto-generated by
the API on POST request, during the user's registration.
id, dob and createdAt cannot be changed after the
registration process of the user. name, address and
description are the only fields which can be changed any
number of times. The API accepts four http methods, which
are: POST, GET, PATCH and DELETE for CRUD operations.

CRUD operations

  • CREATE

To create a new user using POST request:

$ curl --data '{
        "id": null,
        "name": "Test User",
        "dob": "2002-01-30T05:08:06.880755794",
        "address": "Berlin, Germany",
        "description": "I am a test user",
        "createdAt": null
    }' --header 'Content-Type: application/json' -X POST http://localhost:3000/api/users
  • Output:
{
	"id": "322165181465494089",
	"name": "Test User",
	"dob": "2002-01-30T05:08:06.880755794",
	"address": "Berlin, Germany",
	"description": "I am a test user",
	"createdAt": "2022-01-30T05:08:29.409137402Z"
}
  • READ

To read the details of an existing user using GET request:

$ curl -X GET http://localhost:3000/api/users/322165181465494089
  • Output:
{
	"id": "322165181465494089",
	"name": "Test User",
	"dob": "2002-01-30T05:08:06.880755794",
	"address": "Berlin, Germany",
	"description": "I am a test user",
	"createdAt": "2022-01-30T05:08:29.409137402Z"
}
  • UPDATE

To update the details of an existing user using PATCH request:

$ curl --data '{
        "name": "Anonymous",
		"description": "I am a Golang developer"
    }' --header 'Content-Type: application/json' -X PATCH http://localhost:3000/api/users/322165181465494089
  • Output:
{
	"id": "322165181465494089",
	"name": "Anonymous",
	"dob": "2002-01-30T05:08:06.880755794",
	"address": "Berlin, Germany",
	"description": "I am a Golang developer",
	"createdAt": "2022-01-30T05:08:29.409137402Z"
}
  • DELETE

To delete an existing user using DELETE request:

$ curl -X DELETE http://localhost:3000/api/users/322165181465494089
  • Output:
{"message": "322165181465494089: user successfully deleted"}

Usage

To use this API, follow the instructions accordingly:

  1. Initially, create a faunaDB account to access it dashboard and create a database with name: golang-api.
  2. Then, after creating the DB, click on golang-api on dashboard and go to Security section.
  3. Create a new access-key to integrate FaunaDB to the API and select admin role for key.
  4. Copy the access key and save it in .env file at the root of the API directory as:
$ cat .env
FAUNADB_ADMIN_SECRET=<your-access-token-here>
  1. Finally, run the API and start working with it.

HTTP methods

All these requests much match the JSON struct as show above:

POST      - http://localhost:3000/api/users 
GET       - http://localhost:3000/api/users/[:id]
PATCH     - http://localhost:3000/api/users/[:id]
DELETE    - http://localhost:3000/api/users/[:id]

LICENSE

Released under MIT License

# Structs

No description provided by the author