diff --git a/gateway/connect.go b/gateway/connect.go new file mode 100644 index 0000000..8608d33 --- /dev/null +++ b/gateway/connect.go @@ -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) + } +} diff --git a/controllers/gateway_controller.go b/gateway/controllers.go similarity index 94% rename from controllers/gateway_controller.go rename to gateway/controllers.go index 1cc7d22..b0b7a16 100644 --- a/controllers/gateway_controller.go +++ b/gateway/controllers.go @@ -1,4 +1,4 @@ -package controllers +package gateway import ( Config "micrach/config" diff --git a/main.go b/main.go index 1301dff..ba3cf1e 100644 --- a/main.go +++ b/main.go @@ -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)