Skip to content

qiusiyuan/go-swagger-example-in-gitpod

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-swagger-example-in-gitpod

A example go server using go-swagger simple server tutorial with gitpod one-click environment setup.

Content and Goal

This project will setup a go http server using go-swagger's simple server tutorial with full implementation using map data structure as suggested in the tutorial. You may notice that this project only contains

  • swagger.yml which is core file to go-swagger
  • restapi configure file, which contains the main logic of http server's controller
  • .gitpod.yml and .gitpod.dockerfile, which is gitpod's configuration for single-click setup

Goals of this project are

  • Getting familiar of how go-swagger works
  • Be able to play with a go-swagger http server
  • Having gitpod Single-click setup for go-swagger
  • Guides on how to setup a go-swagger http server from only a swagger.yml file

Setup

There are 2 ways to run this project either using gitpod for go-swagger-example or clone this repo and setup yourself. I really suggest to using gitpod directly, this is simple and stable. If you would like to know more, please refer gitpod doc

1. gitpod

  1. Click this link gitpod for go-swagger-example, and follow the instruction of login if you haven't used gitpod before.
  2. Wait for process done, it will take 1-2 min for the first time access.
  3. Once the workspace is opened, terminals opened by gitpod will automatically
  • Swagger generate server code from swagger.yml
  • go get all the dependencies
  • go install the server
  • run server's binary on port 8765 Please wait for all the process done, you will see a message below to indicate this process is done.
2019/11/01 18:24:29 Serving a todo list application at http://127.0.0.1:8765
  1. Now you can play with the server, choose Terminal on the top of the window and choose New Terminal. test3

With opened new terminal, you can now play with it using curl.

  • list all the items
curl -i localhost:8765
  • add one item into list
curl -i localhost:8765 -d "{\"description\":\"message $RANDOM\"}" -H 'Content-Type: application/io.goswagger.examples.todo-list.v1+json'
  • delete an item (delete the first item for example localhost:8765/1)
curl -i localhost:8765/1 -X DELETE -H 'Content-Type: application/io.goswagger.examples.todo-list.v1+json'
  • update an item
curl -i localhost:8765/2 -X PUT -H 'Content-Type: application/io.goswagger.examples.todo-list.v1+json' -d '{"description":"go shopping"}'

Refer to simple server tutorial for more info.

  1. You can stop the server in the first terminal, Ctrl + c to terminate the server. The server binary is located in the $GOPATH/bin/

You may want to change some of the code to play with. The core code resides in restapi/configure_a_todo_list_application.go

To restart the server, you can either

  1. Install binary and run
go install ./cmd/a-todo-list-application-server/ && $GOPATH/bin/a-todo-list-application-server --port 8765
  1. Run the main directly
go run ./cmd/a-todo-list-application-server/main.go --port 8765

2. Clone and Setup

  1. Firstly make sure you have go setup in your PC.
  2. Download go-swagger binary into your $GOROOT directory. Example of how I did it
download_url=$(curl -s https://api.github.com/repos/go-swagger/go-swagger/releases/latest | \
  jq -r '.assets[] | select(.name | contains("'"$(uname | tr '[:upper:]' '[:lower:]')"'_amd64")) | .browser_download_url') \
  && curl -o $GOROOT/bin/swagger -L'#' "$download_url" \
  && chmod +x $GOROOT/bin/swagger

Other ways in go swagger install 3. Git clone this repo to your $GOPATH/src/ 4. Go into this project root directory 5. Swagger generate server code

swagger generate server -f swagger.yml
  1. Install dependencies
go get ./...
  1. Install the application
go install ./cmd/a-todo-list-application-server

The binary will be installed as $GOPATH/bin/a-todo-list-application-server 8. Run the application on port (ex. 8765)

$GOPATH/bin/a-todo-list-application-server --port 8765
  1. Now you can access the service through localhost:8765

About

A example go server using go-swagger and development in gitpod

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published