github.com/diadata-org/diadata@v1.4.593/internal/pkg/datasource/exchange.go (about)

     1  package datasource
     2  
     3  import (
     4  	"encoding/json"
     5  	"go/build"
     6  	"io/ioutil"
     7  	"os"
     8  
     9  	"github.com/diadata-org/diadata/pkg/dia"
    10  )
    11  
    12  type Source struct {
    13  	Exchanges []dia.Exchange `json:"exchanges"`
    14  }
    15  
    16  //InitSource Read exchange.json file and put exchange metadata in Source struct
    17  func InitSource() (source *Source, err error) {
    18  
    19  	var (
    20  		jsonFile  *os.File
    21  		fileBytes []byte
    22  	)
    23  	executionMode := os.Getenv("EXEC_MODE")
    24  
    25  	if executionMode == "production" {
    26  		jsonFile, err = os.Open("/config/exchanges.json")
    27  	} else {
    28  		gopath := os.Getenv("GOPATH")
    29  		if gopath == "" {
    30  			jsonFile, err = os.Open(build.Default.GOPATH)
    31  		} else {
    32  			jsonFile, err = os.Open(os.Getenv("GOPATH") + "/src/github.com/diadata-org/diadata/config/exchanges.json")
    33  		}
    34  	}
    35  	if err != nil {
    36  		return
    37  	}
    38  	defer func() {
    39  		cerr := jsonFile.Close()
    40  		if err == nil {
    41  			err = cerr
    42  		}
    43  	}()
    44  
    45  	fileBytes, err = ioutil.ReadAll(jsonFile)
    46  	if err != nil {
    47  		return
    48  	}
    49  	err = json.Unmarshal(fileBytes, &source)
    50  	if err != nil {
    51  		return
    52  	}
    53  	return
    54  }
    55  
    56  //GetExchanges Get map of exchane name and exchange metadata
    57  func (s *Source) GetExchanges() (exchanges map[string]dia.Exchange) {
    58  	exchanges = make(map[string]dia.Exchange)
    59  	for _, exchange := range s.Exchanges {
    60  		exchanges[exchange.Name] = exchange
    61  	}
    62  	return
    63  }