Skip to content

a-belhadj/gophers-api

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gophers-api

This simple API handle a list of Gophers. It alllows to:

  • list the existing Gophers
  • display the information about a Gopher
  • create a new Gopher
  • delete a Gopher
  • update the path and the URL of a Gopher

yoda-gopher.png back-to-the-future-v2.png

Gitpod integration

Open in Gitpod

How to install

Prerequisites

Install Go in 1.16 version minimum.
Install Taskfile (optional):

brew install go-task/tap/go-task

Install go-swagger:

brew tap go-swagger/go-swagger
brew install go-swagger
swagger version

Build

go build -o bin/gophers-api internal/main.go

// or 

task build

Run app

go run internal/main.go

// or 

task run

Serve Swagger UI

This will open you browser on Swagger UI

task swagger.serve

Test the API

  • Get all Gophers:
curl localhost:8080/gophers

Response:

[{"displayname":"5th Element","name":"5th-element","url":"https://raw.githubusercontent.com/scraly/gophers/main/5th-element.png"}]
  • Get a Gopher with the input name
curl "localhost:8080/gopher?name=5th-element"

Response:

{"displayname":"5th Element","name":"5th-element","url":"https://raw.githubusercontent.com/scraly/gophers/main/5th-element.png"}

/!\ Returns a 404 HTTP Error Code if a Gopher have not been found for the given name.

  • Add a new Gopher
curl -X POST localhost:8080/gopher \
   -H "Content-Type: application/json" \
   -d '{"name":"yoda-gopher","displayname":"Yoda Gopher","url":"https://raw.githubusercontent.com/scraly/gophers/main/yoda-gopher.png"}'  

Response:

{"displayname":"Yoda Gopher.png","name":"yoda-gopher","url":"https://raw.githubusercontent.com/scraly/gophers/main/yoda-gopher.png"}

Add another Gopher:

curl -X POST localhost:8080/gopher \
   -H "Content-Type: application/json" \
   -d '{"name":"jurassic-park","displayname":"Gopher Park","url":"https://raw.githubusercontent.com/scraly/gophers/main/jurassic-park.png"}'  
  • Delete a Gopher
curl -X DELETE "localhost:8080/gopher?name=5th-element"
  • Update a Gopher
curl -X PUT localhost:8080/gopher \
   -H "Content-Type: application/json" \
   -d '{"name":"yoda-gopher","displayname":"El mejor Yoda Gopher","url":"https://raw.githubusercontent.com/scraly/gophers/main/yoda-gopher.png"}' 

Response:

{"displayname":"El mejor Yoda Gopher","name":"yoda-gopher","url":"https://raw.githubusercontent.com/scraly/gophers/main/yoda-gopher.png"}

Notes

This API use go-swagger

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%