How to create a Gin Web Service
Once Go is installed, you can create a Gin web service by issuing the following commands:
You can then create main.go with the following code:
go:
You can run the resulting code by issuing the command:
You can then access the resulting web application from your nearest web browser:
and you should see the pong response.
--------------------------------------------------------------------------------
Example of the /tables endpoint
The /tables endpoint calls main.apiTables, which returns an array of all of the available game tables, how many players they can support, and how many players are currently joined in.
This endpoint can be accessed from your development PC using a browser or a tool such as curl.
json
--------------------------------------------------------------------------------
Example of the /view endpoint
For the game field, 0 indicates no shot, 1 indicates a shot was fired into open water, 2 indicates a shot was fired, and a ship was hit.
json
Once Go is installed, you can create a Gin web service by issuing the following commands:
$ mkdir my-gin-app$ cd my-gin-app$ go mod init my-gin-app$ go get -u github.com/gin-gonic/ginYou can then create main.go with the following code:
go:
package mainimport ( "net/http" "github.com/gin-gonic/gin")func main() { // Create a Gin router with default middleware (logger, recovery) r := gin.Default() // Define a GET route at /ping r.GET("/ping", func(c *gin.Context) { // Send JSON response c.JSON(http.StatusOK, gin.H{ "message": "pong", }) }) // Start the server on port 8080 r.Run() // Default: http://localhost:8080}You can run the resulting code by issuing the command:
$ go run main.goYou can then access the resulting web application from your nearest web browser:
HTTP://localhost:8080/and you should see the pong response.
--------------------------------------------------------------------------------
Example of the /tables endpoint
The /tables endpoint calls main.apiTables, which returns an array of all of the available game tables, how many players they can support, and how many players are currently joined in.
This endpoint can be accessed from your development PC using a browser or a tool such as curl.
$ curl http://localhost:8080/tablesjson
[ { "Table":"r2", "Name":"High Seas", "CurPlayers":0, "MaxPlayers":4 }, { "Table":"r1", "Name":"Cape Fuji", "CurPlayers":0, "MaxPlayers":4 }, { "Table":"ai3", "Name":"AI - 3 Bots", "CurPlayers":0, "MaxPlayers":4 }, { "Table":"ai2", "Name":"AI - 2 Bots", "CurPlayers":0, "MaxPlayers":4 }, { "Table":"ai1", "Name":"AI - 1 on 1", "CurPlayers":0, "MaxPlayers":4 }]--------------------------------------------------------------------------------
Example of the /view endpoint
For the game field, 0 indicates no shot, 1 indicates a shot was fired into open water, 2 indicates a shot was fired, and a ship was hit.
$ curl http://localhost:8080/view?table=ai3&player=thomjson
{ "Status": 12, "ActivePlayer": 3, "PlayerStatus": 0, "MoveTime": 0, "LastAttackPos": 72, "Prompt": "", "Players": [ { "Name": "thom", "Gamefield": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], "ShipsLeft": [1, 1, 1, 1, 1] }, { "Name": "Clyd Bot", "Gamefield": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], "ShipsLeft": [1, 1, 1, 1, 1] }, { "Name": "Meg Bot", "Gamefield": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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