github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/config/configuration.go (about)

     1  // Copyright 2023 IAC. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package config
    16  
    17  import (
    18  	"encoding/json"
    19  	"fmt"
    20  	"io/ioutil"
    21  
    22  	"github.com/mdaxf/iac/com"
    23  	"github.com/mdaxf/iac/integration/activemq"
    24  	"github.com/mdaxf/iac/integration/kafka"
    25  	"github.com/mdaxf/iac/integration/mqttclient"
    26  )
    27  
    28  type Controller struct {
    29  	Path      string     `json:"path"`
    30  	Module    string     `json:"module"`
    31  	Timeout   int        `json:"timeout"`
    32  	Endpoints []Endpoint `json:"endpoints"`
    33  }
    34  
    35  type PluginController struct {
    36  	Path      string     `json:"path"`
    37  	Endpoints []Endpoint `json:"endpoints"`
    38  }
    39  type Endpoint struct {
    40  	Path    string `json:"path"`
    41  	Method  string `json:"method"`
    42  	Handler string `json:"handler"`
    43  }
    44  type Config struct {
    45  	Port              int                `json:"port"`
    46  	Timeout           int                `json:"timeout"`
    47  	Controllers       []Controller       `json:"controllers"`
    48  	PluginControllers []PluginController `json:"plugins"`
    49  	Portal            Portal             `json:"portal"`
    50  	ApiKey            string             `json:"apikey"`
    51  	OpenAiKey         string             `json:"openaikey"`
    52  }
    53  
    54  type Portal struct {
    55  	Port  int    `json:"port"`
    56  	Path  string `json:"path"`
    57  	Home  string `json:"home"`
    58  	Logon string `json:"logon"`
    59  }
    60  
    61  var apiconfig = "apiconfig.json"
    62  var gconfig = "configuration.json"
    63  
    64  var MQTTClients map[string]*mqttclient.MqttClient
    65  var Kakfas map[string]*kafka.KafkaConsumer
    66  var ActiveMQs map[string]*activemq.ActiveMQ
    67  
    68  func LoadConfig() (*Config, error) {
    69  	data, err := ioutil.ReadFile(apiconfig)
    70  	if err != nil {
    71  		return nil, fmt.Errorf("failed to read configuration file: %v", err)
    72  	}
    73  
    74  	var config Config
    75  	if err := json.Unmarshal(data, &config); err != nil {
    76  		return nil, fmt.Errorf("failed to parse configuration file: %v", err)
    77  	}
    78  
    79  	ApiKey = config.ApiKey
    80  	OpenAiKey = config.OpenAiKey
    81  
    82  	fmt.Println("loaded portal and api configuration:", config)
    83  	return &config, nil
    84  }
    85  
    86  func LoadGlobalConfig() (*GlobalConfig, error) {
    87  	jsonFile, err := ioutil.ReadFile(gconfig)
    88  	if err != nil {
    89  		return nil, fmt.Errorf("failed to read configuration file: %v", err)
    90  	}
    91  
    92  	// Create a map to hold the JSON data
    93  	var jsonData GlobalConfig
    94  
    95  	// Unmarshal the JSON data into the map
    96  	if err := json.Unmarshal(jsonFile, &jsonData); err != nil {
    97  
    98  		return nil, fmt.Errorf("failed to parse configuration file: %v", err)
    99  	}
   100  	//fmt.Println(jsonFile, jsonData)
   101  
   102  	com.Instance = jsonData.Instance
   103  	com.InstanceType = jsonData.InstanceType
   104  	com.InstanceName = jsonData.InstanceName
   105  	com.SingalRConfig = jsonData.SingalRConfig
   106  	//fmt.Println(com.SingalRConfig, com.Instance)
   107  
   108  	Transaction := jsonData.Transaction
   109  
   110  	com.TransactionTimeout = com.ConverttoIntwithDefault(Transaction["timeout"], 15)
   111  	com.DBTransactionTimeout = com.ConverttoIntwithDefault(jsonData.DatabaseConfig["timeout"], 5)
   112  
   113  	fmt.Println("loaded global configuration:", jsonData)
   114  	return &jsonData, nil
   115  }