move requestclient credentials to env

main
alex 2023-09-13 19:53:45 +02:00
parent 21d8d18ecc
commit e75988106a
5 changed files with 72 additions and 64 deletions

View File

@ -16,6 +16,7 @@ type Config struct {
Port string
FolderPaths FolderPaths
MariaDB MariaDB
InvexAPI InvexAPI
}
type FolderPaths struct {
@ -34,6 +35,11 @@ type MariaDB struct {
DatabaseName string
}
type InvexAPI struct {
Base string
Token string
}
func LoadConfig() {
// used to determine server was startet in docker or not
if os.Getenv("DOCKER") == "" {
@ -60,6 +66,11 @@ func LoadConfig() {
Port: os.Getenv("MARIADB_PORT"),
Username: os.Getenv("MARIADB_USERNAME"),
Password: os.Getenv("MARIADB_PASSWORD"),
DatabaseName: os.Getenv("MARIADB_DATABASE_NAME")},
DatabaseName: os.Getenv("MARIADB_DATABASE_NAME"),
},
InvexAPI: InvexAPI{
Base: os.Getenv("INVEX_API_BASE"),
Token: os.Getenv("INVEX_API_TOKEN"),
},
}
}

View File

@ -128,7 +128,7 @@ func GetEquipmentDocumentations(stockItemId string, query structs.PageQuery, c *
if len(documentations) == 0 {
// there are no documentations for this equipment on the our database
// so there will be checked on invex if the stock item exists
statusCode, _, err = requestclient.InvexApiRequestClient(fiber.MethodGet, requestclient.ApiBase+"/stock/"+stockItemId+"/")
statusCode, _, err = requestclient.InvexApiRequestClient(fiber.MethodGet, config.Cfg.InvexAPI.Base+"/api/stock/"+stockItemId+"/")
if err != nil {
log.Error().Msgf("Invex api request error: %s", err)
@ -264,7 +264,7 @@ func isInList(fileName string, notes []Notes) bool {
// fetching the thumbnail from the invex server and sending it back to the client
func GetEquipmentInvexThumbnail(c *fiber.Ctx, stockItemId string) error {
// first request to /api/stock/:stockItemId/ to get the thumbnail url
_, body, err := requestclient.InvexApiRequestClient(fiber.MethodGet, requestclient.ApiBase+"/stock/"+stockItemId+"/")
_, body, err := requestclient.InvexApiRequestClient(fiber.MethodGet, config.Cfg.InvexAPI.Base+"/api/stock/"+stockItemId+"/")
if err != nil {
log.Error().Msgf("Invex api request error: %s", err)
@ -283,7 +283,7 @@ func GetEquipmentInvexThumbnail(c *fiber.Ctx, stockItemId string) error {
thumbnail := partDetail["thumbnail"].(string)
// second request to /media/part_images/:thumbnail to get the thumbnail image
_, body, err = requestclient.InvexApiRequestClient(fiber.MethodGet, requestclient.Base+"/"+thumbnail)
_, body, err = requestclient.InvexApiRequestClient(fiber.MethodGet, config.Cfg.InvexAPI.Base+"/"+thumbnail)
if err != nil {
log.Error().Msgf("Invex api request error: %s", err)

View File

@ -1,6 +1,7 @@
package requestclient
import (
"jannex/admin-dashboard-backend/modules/config"
"jannex/admin-dashboard-backend/modules/logger"
"strconv"
@ -8,14 +9,13 @@ import (
"github.com/rs/zerolog/log"
)
const Base = "https://inv.ex.umbach.dev"
const ApiBase = Base + "/api"
const ApiToken = "1367f15d21935e4eb540f897946fb5cd98485c3f"
//const Base = "https://inv.ex.umbach.dev"
//const ApiBase = Base + "/api"
func InvexApiRequestClient(requestMethod string, url string) (statusCode int, body []byte, err error) {
a := fiber.AcquireAgent()
a.Add("Authorization", "Token "+ApiToken)
a.Add("Authorization", "Token "+config.Cfg.InvexAPI.Token)
req := a.Request()
req.Header.SetMethod(requestMethod)

View File

@ -2,6 +2,7 @@ package machines
import (
"encoding/json"
"jannex/admin-dashboard-backend/modules/config"
"jannex/admin-dashboard-backend/modules/requestclient"
"jannex/admin-dashboard-backend/modules/structs"
"jannex/admin-dashboard-backend/modules/utils"
@ -30,11 +31,7 @@ func GetMachines(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusBadRequest)
}
log.Info().Msgf("body %v", body)
statusCode, reqBody, err := requestclient.InvexApiRequestClient(fiber.MethodGet, requestclient.ApiBase+"/stock/?search=&offset=0&limit=50&location="+strconv.Itoa(body.Location)+"&part_detail=true&in_stock=0")
log.Info().Msgf("statuscode %v", statusCode)
_, reqBody, err := requestclient.InvexApiRequestClient(fiber.MethodGet, config.Cfg.InvexAPI.Base+"/api/stock/?search=&offset=0&limit=50&location="+strconv.Itoa(body.Location)+"&part_detail=true&in_stock=0")
if err != nil {
log.Error().Msgf("Invex api request error: %s", err)