diff --git a/main.go b/main.go index 1316852..08a519a 100644 --- a/main.go +++ b/main.go @@ -5,9 +5,8 @@ import ( "open-outcry/pkg/conf" "open-outcry/pkg/db" "open-outcry/pkg/models" - "open-outcry/pkg/services" + "open-outcry/pkg/rest" "os" - "sync" log "github.com/sirupsen/logrus" ) @@ -30,26 +29,9 @@ func main() { models.LoadFees(fees) } - // sample code to generate 100 trades - _, tradingAccount1 := services.Acc("test") - _, tradingAccount2 := services.Acc("test2") - - var mu sync.Mutex - var wg sync.WaitGroup - wg.Add(100) - - for i := 0; i < 100; i++ { - - go func() { - mu.Lock() - log.Info(services.ProcessTradeOrder(tradingAccount1, "BTC_EUR", "LIMIT", "SELL", 1, 10, "GTC")) - log.Info(services.ProcessTradeOrder(tradingAccount2, "BTC_EUR", "MARKET", "BUY", 0, 10, "GTC")) - mu.Unlock() - - wg.Done() - }() + server := rest.NewServer() + err := server.ListenAndServe() + if err != nil { + log.Fatal(err) } - - wg.Wait() - } diff --git a/pkg/api.yaml b/pkg/api.yaml index b490702..a496f74 100644 --- a/pkg/api.yaml +++ b/pkg/api.yaml @@ -15,7 +15,7 @@ info: version: 1.0.0 title: OPEN OUTCRY API servers: - - url: https://your.public.url + - url: http://localhost:4000 security: - basicAuth: [] paths: diff --git a/pkg/conf/conf.go b/pkg/conf/conf.go index 1db68dc..0558630 100644 --- a/pkg/conf/conf.go +++ b/pkg/conf/conf.go @@ -34,6 +34,7 @@ var mapper = map[EnvName]string{ type configuration struct { DBDsn string UpdateFees bool + RestPort string } var config *configuration @@ -67,6 +68,7 @@ func LoadConfig(conf string) (*configuration, error) { viper.GetString("POSTGRES_PORT"), ), UpdateFees: viper.GetBool("UPDATE_FEES"), + RestPort: viper.GetString("REST_PORT"), } return config, nil diff --git a/pkg/conf/dev.env b/pkg/conf/dev.env index b1f4649..bad7bc4 100644 --- a/pkg/conf/dev.env +++ b/pkg/conf/dev.env @@ -4,4 +4,5 @@ POSTGRES_PASSWORD=postgres POSTGRES_PORT=5432 POSTGRES_HOST=localhost +REST_PORT=4000 UPDATE_FEE=true diff --git a/pkg/conf/docker.env b/pkg/conf/docker.env index 7368dd2..8e442c7 100644 --- a/pkg/conf/docker.env +++ b/pkg/conf/docker.env @@ -1,2 +1,3 @@ +REST_PORT=4000 ENV=PROD POSTGRES_HOST=db \ No newline at end of file diff --git a/pkg/models/currency.go b/pkg/models/currency.go index 153d8f3..d4412a0 100644 --- a/pkg/models/currency.go +++ b/pkg/models/currency.go @@ -1,5 +1,7 @@ package models +import "open-outcry/pkg/db" + type CurrencyName string type CurrencyPrecision int @@ -8,3 +10,9 @@ type Currency struct { Name CurrencyName Precision CurrencyPrecision } + +// GetCurrencies returns a list of available currencies +func GetCurrencies() []Currency { + res := db.QueryList[Currency](`SELECT * FROM currency`) + return res +} diff --git a/pkg/models/currency_test.go b/pkg/models/currency_test.go new file mode 100644 index 0000000..75de562 --- /dev/null +++ b/pkg/models/currency_test.go @@ -0,0 +1,6 @@ +package models + +func (assert *ModelsTestSuite) TestGetCurrencies() { + // expect currenceis to be popullated + assert.GreaterOrEqual(3, len(GetCurrencies())) +} diff --git a/pkg/rest/.openapi-generator-ignore b/pkg/rest/.openapi-generator-ignore index 19f3b1e..d5ef0f1 100644 --- a/pkg/rest/.openapi-generator-ignore +++ b/pkg/rest/.openapi-generator-ignore @@ -21,3 +21,5 @@ #docs/*.md # Then explicitly reverse the ignore rule for a single file: #!docs/README.md + +api_currencies_service.go \ No newline at end of file diff --git a/pkg/rest/api/api_currencies_service.go b/pkg/rest/api/api_currencies_service.go index bcf7f5c..7b4a1b6 100644 --- a/pkg/rest/api/api_currencies_service.go +++ b/pkg/rest/api/api_currencies_service.go @@ -11,8 +11,8 @@ package api import ( "context" - "errors" "net/http" + "open-outcry/pkg/models" ) // CurrenciesAPIService is a service that implements the logic for the CurrenciesAPIServicer @@ -28,14 +28,15 @@ func NewCurrenciesAPIService() CurrenciesAPIServicer { // GetCurrencies - currencies list func (s *CurrenciesAPIService) GetCurrencies(ctx context.Context) (ImplResponse, error) { - // TODO - update GetCurrencies with the required logic for this service method. - // Add api_currencies_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation. - - // TODO: Uncomment the next line to return response Response(200, interface{}{}) or use other options such as http.Ok ... - // return Response(200, interface{}{}), nil - - // TODO: Uncomment the next line to return response Response(500, {}) or use other options such as http.Ok ... - // return Response(500, nil),nil - - return Response(http.StatusNotImplemented, nil), errors.New("GetCurrencies method not implemented") + currencies := models.GetCurrencies() + res := make([]Currency, 0) + for _, v := range currencies { + cur := Currency{ + Name: NewInterface(v.Name), + Precision: NewInterface(v.Precision), + } + res = append(res, cur) + } + + return Response(http.StatusOK, res), nil } diff --git a/pkg/rest/api/helpers.go b/pkg/rest/api/helpers.go index 7b296b7..ae5631c 100644 --- a/pkg/rest/api/helpers.go +++ b/pkg/rest/api/helpers.go @@ -58,3 +58,9 @@ func AssertRecurseValueRequired[T any](value reflect.Value, callback func(T) err } return nil } + +// NewInterface creates a new empty interface with a given value. +func NewInterface(value interface{}) *interface{} { + v := value + return &v +} diff --git a/pkg/rest/rest.go b/pkg/rest/rest.go new file mode 100644 index 0000000..81bfccf --- /dev/null +++ b/pkg/rest/rest.go @@ -0,0 +1,17 @@ +package rest + +import ( + "net/http" + "open-outcry/pkg/conf" + "open-outcry/pkg/rest/api" +) + +func NewServer() http.Server { + router := api.NewRouter( + api.NewCurrenciesAPIController(api.NewCurrenciesAPIService()), + ) + return http.Server{ + Addr: ":" + conf.Get().RestPort, + Handler: router, + } +}