mirror of
https://github.com/yanislav-igonin/micrach
synced 2025-04-20 03:40:33 +03:00
feat: separate gateway folder
This commit is contained in:
parent
124c524e78
commit
77706851c0
36
gateway/connect.go
Normal file
36
gateway/connect.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package controllers
|
package gateway
|
||||||
|
|
||||||
import (
|
import (
|
||||||
Config "micrach/config"
|
Config "micrach/config"
|
30
main.go
30
main.go
@ -1,12 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"html/template"
|
"html/template"
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -20,6 +16,7 @@ import (
|
|||||||
Config "micrach/config"
|
Config "micrach/config"
|
||||||
Controllers "micrach/controllers"
|
Controllers "micrach/controllers"
|
||||||
Db "micrach/db"
|
Db "micrach/db"
|
||||||
|
Gateway "micrach/gateway"
|
||||||
Repositories "micrach/repositories"
|
Repositories "micrach/repositories"
|
||||||
Utils "micrach/utils"
|
Utils "micrach/utils"
|
||||||
)
|
)
|
||||||
@ -72,29 +69,8 @@ func main() {
|
|||||||
router.Static("/uploads", "./uploads")
|
router.Static("/uploads", "./uploads")
|
||||||
router.Static("/static", "./static")
|
router.Static("/static", "./static")
|
||||||
if Config.App.Gateway.Url != "" {
|
if Config.App.Gateway.Url != "" {
|
||||||
router.GET("/ping", Controllers.Ping)
|
router.GET("/ping", Gateway.Ping)
|
||||||
// make http request to gateway to tell the board id and description
|
Gateway.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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
router.GET("/", Controllers.GetThreads)
|
router.GET("/", Controllers.GetThreads)
|
||||||
router.POST("/", Controllers.CreateThread)
|
router.POST("/", Controllers.CreateThread)
|
||||||
|
Loading…
Reference in New Issue
Block a user