feat: separate gateway folder

This commit is contained in:
Yanislav Igonin 2022-02-10 19:44:04 +02:00
parent 124c524e78
commit 77706851c0
3 changed files with 40 additions and 28 deletions

36
gateway/connect.go Normal file
View File

@ -0,0 +1,36 @@
package gateway
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
Config "micrach/config"
)
// Make http request to gateway to tell the board id and description
func Connect() {
requestBody, _ := json.Marshal(map[string]string{
"id": Config.App.Gateway.BoardId,
"description": Config.App.Gateway.BoardDescription,
})
url := Config.App.Gateway.Url + "/boards"
req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
if err != nil {
log.Panicln(err)
}
req.Header.Set("Authorization", Config.App.Gateway.ApiKey)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Panicln(err)
}
//We Read the response body on the line below.
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
log.Panicln(err)
}
}

View File

@ -1,4 +1,4 @@
package controllers
package gateway
import (
Config "micrach/config"

30
main.go
View File

@ -1,12 +1,8 @@
package main
import (
"bytes"
"encoding/json"
"html/template"
"io/ioutil"
"log"
"net/http"
"strconv"
"time"
@ -20,6 +16,7 @@ import (
Config "micrach/config"
Controllers "micrach/controllers"
Db "micrach/db"
Gateway "micrach/gateway"
Repositories "micrach/repositories"
Utils "micrach/utils"
)
@ -72,29 +69,8 @@ func main() {
router.Static("/uploads", "./uploads")
router.Static("/static", "./static")
if Config.App.Gateway.Url != "" {
router.GET("/ping", Controllers.Ping)
// make http request to gateway to tell the board id and description
requestBody, _ := json.Marshal(map[string]string{
"id": Config.App.Gateway.BoardId,
"description": Config.App.Gateway.BoardDescription,
})
url := Config.App.Gateway.Url + "/boards"
req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
if err != nil {
log.Panicln(err)
}
req.Header.Set("Authorization", Config.App.Gateway.ApiKey)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Panicln(err)
}
//We Read the response body on the line below.
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
log.Panicln(err)
}
router.GET("/ping", Gateway.Ping)
Gateway.Connect()
}
router.GET("/", Controllers.GetThreads)
router.POST("/", Controllers.CreateThread)