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)
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.
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.
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.