Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Latest commit

 

History

History

go-rest-api

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

go-rest-api

This repo contains a simple/basic HTTP server in Go, with a basic code organization. We use:

  • net/http package to start and serve HTTP server
  • Gorilla mux to handle routes
  • Swagger in order to serve a REST API compliant with OpenAPI specs

go-rest-api use Taskfile (a Makefile alternative).

Please read the Learning Go by examples: part 3 - Create an HTTP REST API Server in Go article in order to know more about this Git repository.

Pre-requisits

Install Go in 1.16 version minimum.

or:

Open in Gitpod

Build the app

$ go build -o bin/go-rest-api internal/main.go

or

$ task build

Run the app

$ ./bin/go-rest-api

or

$ task run

Test the app

$ curl http://localhost:8080/healthz
OK

$ curl http://localhost:8080/hello/aurelie
"Hello aurelie!"

$ curl -O localhost:8080/gopher/dr-who
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  992k    0  992k    0     0  9185k      0 --:--:-- --:--:-- --:--:-- 9185k

$ file dr-who
dr-who: PNG image data, 1700 x 1460, 8-bit/color RGBA, non-interlaced

Request & Response Examples

Swagger doc: go-rest-api

URL Port HTTP Method Operation
/healthz 8080 GET Test if the app is running
/hello/{name} 8080 GET Returns message with {name} provided in the query
/gopher/{name} 8080 GET Returns gopher image by {name} provided in the query

$ curl localhost:8080/hello/aurelie

Generate swagger files

After editing pkg/swagger/swagger.yml file you need to generate swagger files again:

$ task swagger.gen

Test swagger file validity

$ task swagger.validate

Generate swagger documentation

$ task swagger.doc