Creating the first endpoint of the game prediction project

Hi everybody Iordanis here. So this is the first part project creation with some actual code which is upload it to YouTube as a tutorial. In this post I will explain the architecture of the project and the first endpoint.

It will be a RESTful API where we will be hitting the endpoint to insert information (as the application have to learn to give back results) and get results. For each action we will have a user interface (probably it will be written with REACT) where you will be able to send information to the end points and receive information back. These end point will receive and return JSON so if you are not comfortable with it please check a tutorial (e.g tutorial )

Structure of the files (for what we have created until now)

src/
main.go
insertData/
insertData.go

You can find the code here : see the code here

Before starting the project you need to have Golang installed  here is the tutorial
Also you need to download an external library github.com/gorilla/mux. All you have to do is to run the get command from your terminal “go get github.com/gorilla/mux”

Explanation of the code:

The main package of the project is in the main.go file. At this moment we have two functions the main and the insertDataEndpoint. The main function is creating the server which is running in the port 8080 and defines the /insert end point which is connecting with the function insertDataEndpoint. This function set the content type to application/json, pass the body of the request to the insertData (will explain this one below) which returns the JSON response if the insert was successful or not. The insertData package contains 3 structs The first one is the “Result” which will be created from the JSON of the request to the /insert endpoint. The SuccessMessage struct will return a JSON which will give you the response’s success message to the request and the Error struct will return the JSON response’s error message.

Screen Shot 2018-05-19 at 22.14.52.pngScreen Shot 2018-05-19 at 21.55.49.pngScreen Shot 2018-05-19 at 21.56.04.png

The function getSuccessJsonBody creates the SuccessMessage struct and an []byte of the JSON response and the getErrorFromResult does the same but for the error message (and for the Error struct). The function isJSONCorrect checks the struct Result has all the values and if it looks good it returns true else false. This is used in the function InsertData to define if it will return a success JSON message or an error one. In the next post this function also will insert data to the database.

Screen Shot 2018-05-19 at 22.13.57.png

 

This endpoint will receive a JSON body (see below) check if the values are there and return a successful response or an error response. In the future it will also insert these information to the database ( will be the topic of the next post). Here is an example of a JSON body that this endpoint accepts.

Screen Shot 2018-05-19 at 19.50.13.png

 if you want to test the end point you can run these curl request from your terminal :
Request that will return a success message :
curl -X POST http://localhost:8080/insert -H ‘Cache-Control: no-cache’ -H ‘Content-Type: application/json’ -d ‘{“data”: [{“type”: “insertData”,”attributes”: {“teamOne”: “Real Madrid”,”teamTwo”: “Barcelona”,”teamOneGoals”: 4,”teamTwoGoals”: 3}}]}’
and one that will return an error message
curl -X POST http://localhost:8080/insert -H ‘Cache-Control: no-cache’ -H ‘Content-Type: application/json’ -d ‘{“data”: [{“type”: “insertData”,”attributes”: {“teamOne”: “Real Madrid”,”teamTwo”: “”,”teamOneGoals”: 4,”teamTwoGoals”: 3}}]}’
YouTube tutorials videos:



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s