Skip to content

Commit

Permalink
Decouple API from pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
tolyo committed Dec 8, 2023
1 parent ee45b48 commit b6ff596
Show file tree
Hide file tree
Showing 63 changed files with 544 additions and 132 deletions.
31 changes: 4 additions & 27 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
include api/api.mk

.DEFAULT_GOAL := help
.PHONY: help
Expand Down Expand Up @@ -27,43 +28,19 @@ lint:
go vet ./...

include ./pkg/conf/dev.env
DBDSN:="host=$(POSTGRES_HOST) user=$(POSTGRES_USER) password=$(POSTGRES_PASSWORD) dbname=$(POSTGRES_DB) port=$(POSTGRES_PORT) sslmode=disable"
DB_DSN:="host=$(POSTGRES_HOST) user=$(POSTGRES_USER) password=$(POSTGRES_PASSWORD) dbname=$(POSTGRES_DB) port=$(POSTGRES_PORT) sslmode=disable"
MIGRATE_OPTIONS=-allow-missing -dir="./sql"

db-up: ## Migrate down on database
goose -v $(MIGRATE_OPTIONS) postgres $(DBDSN) up
goose -v $(MIGRATE_OPTIONS) postgres $(DB_DSN) up

db-down: ## Migrate up on database
goose -v $(MIGRATE_OPTIONS) postgres $(DBDSN) reset
goose -v $(MIGRATE_OPTIONS) postgres $(DB_DSN) reset

db-rebuild: ## Reset the database
make db-down
make db-up

OUTPUT_YAML:="./pkg/static/api.yaml"

bundle-api:
npx @redocly/cli bundle \
pkg/api/openapi.yaml \
--output $(OUTPUT_YAML)

validate-api:
make bundle-api
npx @redocly/cli lint \
$(OUTPUT_YAML) \
--format=checkstyle

generate-api: ## Generate server bindings, move model files, fix imports
make validate-api
npx @openapitools/openapi-generator-cli generate \
-i $(OUTPUT_YAML) \
-g go-server \
-o pkg/rest \
--additional-properties=packageName=api \
--additional-properties=sourceFolder=api \
--additional-properties=outputAsLibrary=true
make lint

help:
grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) \
| sed -n 's/^\(.*\): \(.*\)##\(.*\)/\1\3/p'
26 changes: 26 additions & 0 deletions api/api.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# API management make

INPUT_YAML:="./api/openapi.yaml"
OUTPUT_YAML:="../pkg/static/api.yaml"

bundle-api:
npx @redocly/cli bundle \
$(INPUT_YAML) \
--output $(OUTPUT_YAML)

validate-api:
make bundle-api
npx @redocly/cli lint \
$(OUTPUT_YAML) \
--format=checkstyle

generate-api: ## Generate server bindings, move model files, fix imports
make validate-api
npx @openapitools/openapi-generator-cli generate \
-i $(OUTPUT_YAML) \
-g go-server \
-o pkg/rest \
--additional-properties=packageName=api \
--additional-properties=sourceFolder=api \
--additional-properties=outputAsLibrary=true
make lint
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ components:
- PARTIALLY_CANCELLED
- PARTIALLY_REJECTED
- FILLED
- REJECTED

TradeOrderList:
type: array
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion openapitools.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"$schema": "./node_modules/@openapitools/openapi-generator-cli/config.schema.json",
"spaces": 2,
"generator-cli": {
"version": "7.0.0"
"version": "7.1.0"
}
}
2 changes: 1 addition & 1 deletion pkg/rest/.openapi-generator-ignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
#
**/api_public_service.go
#**/api_public_service.go
#**/helpers.go
1 change: 1 addition & 0 deletions pkg/rest/.openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ api/api.go
api/api_admin.go
api/api_admin_service.go
api/api_public.go
api/api_public_service.go
api/api_user.go
api/api_user_service.go
api/error.go
Expand Down
2 changes: 1 addition & 1 deletion pkg/rest/.openapi-generator/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.0.0
7.1.0
2 changes: 1 addition & 1 deletion pkg/rest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ To see how to make this your own, look here:
[README](https://openapi-generator.tech)

- API version: 1.0.0
- Build date: 2023-12-07T21:19:37.002526+02:00[Europe/Riga]
- Build date: 2023-12-08T19:14:53.772694+02:00[Europe/Riga]


### Running the server
Expand Down
22 changes: 11 additions & 11 deletions pkg/rest/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ type UserAPIRouter interface {
// and updated with the logic required for the API.
type AdminAPIServicer interface {
CreateAdminPayment(context.Context) (ImplResponse, error)
GetAdminPaymentById(context.Context, interface{}) (ImplResponse, error)
GetAdminPaymentById(context.Context, string) (ImplResponse, error)
GetAppEntities(context.Context) (ImplResponse, error)
GetAppEntity(context.Context, interface{}) (ImplResponse, error)
GetAppEntity(context.Context, string) (ImplResponse, error)
}

// PublicAPIServicer defines the api actions for the PublicAPI service
Expand All @@ -67,20 +67,20 @@ type PublicAPIServicer interface {
GetCurrencies(context.Context) (ImplResponse, error)
GetFxInstruments(context.Context) (ImplResponse, error)
GetInstruments(context.Context) (ImplResponse, error)
GetOrderBook(context.Context, interface{}) (ImplResponse, error)
GetOrderBook(context.Context, string) (ImplResponse, error)
}

// UserAPIServicer defines the api actions for the UserAPI service
// This interface intended to stay up to date with the openapi yaml used to generate it,
// while the service implementation can be ignored with the .openapi-generator-ignore file
// and updated with the logic required for the API.
type UserAPIServicer interface {
CreateTrade(context.Context, interface{}, CreateTradeRequest) (ImplResponse, error)
DeleteTradeById(context.Context, interface{}, interface{}) (ImplResponse, error)
GetBookOrders(context.Context, interface{}) (ImplResponse, error)
GetPaymentAccounts(context.Context, interface{}) (ImplResponse, error)
GetTradeById(context.Context, interface{}, interface{}) (ImplResponse, error)
GetTradeOrders(context.Context, interface{}) (ImplResponse, error)
GetTrades(context.Context, interface{}) (ImplResponse, error)
GetTradingAccount(context.Context, interface{}) (ImplResponse, error)
CreateTrade(context.Context, string, CreateTradeRequest) (ImplResponse, error)
DeleteTradeById(context.Context, string, string) (ImplResponse, error)
GetBookOrders(context.Context, string) (ImplResponse, error)
GetPaymentAccounts(context.Context, string) (ImplResponse, error)
GetTradeById(context.Context, string, string) (ImplResponse, error)
GetTradeOrders(context.Context, string) (ImplResponse, error)
GetTrades(context.Context, string) (ImplResponse, error)
GetTradingAccount(context.Context, string) (ImplResponse, error)
}
8 changes: 8 additions & 0 deletions pkg/rest/api/api_admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ func (c *AdminAPIController) CreateAdminPayment(w http.ResponseWriter, r *http.R
func (c *AdminAPIController) GetAdminPaymentById(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
paymentIdParam := params["payment_id"]
if paymentIdParam == "" {
c.errorHandler(w, r, &RequiredError{"payment_id"}, nil)
return
}
result, err := c.service.GetAdminPaymentById(r.Context(), paymentIdParam)
// If an error occurred, encode the error with the status code
if err != nil {
Expand All @@ -114,6 +118,10 @@ func (c *AdminAPIController) GetAppEntities(w http.ResponseWriter, r *http.Reque
func (c *AdminAPIController) GetAppEntity(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
appEntityIdParam := params["app_entity_id"]
if appEntityIdParam == "" {
c.errorHandler(w, r, &RequiredError{"app_entity_id"}, nil)
return
}
result, err := c.service.GetAppEntity(r.Context(), appEntityIdParam)
// If an error occurred, encode the error with the status code
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions pkg/rest/api/api_admin_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (s *AdminAPIService) CreateAdminPayment(ctx context.Context) (ImplResponse,
}

// GetAdminPaymentById - Get payment
func (s *AdminAPIService) GetAdminPaymentById(ctx context.Context, paymentId interface{}) (ImplResponse, error) {
func (s *AdminAPIService) GetAdminPaymentById(ctx context.Context, paymentId string) (ImplResponse, error) {
// TODO - update GetAdminPaymentById with the required logic for this service method.
// Add api_admin_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.

Expand All @@ -59,8 +59,8 @@ func (s *AdminAPIService) GetAppEntities(ctx context.Context) (ImplResponse, err
// TODO - update GetAppEntities with the required logic for this service method.
// Add api_admin_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(200, []AppEntity{}) or use other options such as http.Ok ...
// return Response(200, []AppEntity{}), nil

// TODO: Uncomment the next line to return response Response(404, {}) or use other options such as http.Ok ...
// return Response(404, nil),nil
Expand All @@ -69,7 +69,7 @@ func (s *AdminAPIService) GetAppEntities(ctx context.Context) (ImplResponse, err
}

// GetAppEntity - Get application entity
func (s *AdminAPIService) GetAppEntity(ctx context.Context, appEntityId interface{}) (ImplResponse, error) {
func (s *AdminAPIService) GetAppEntity(ctx context.Context, appEntityId string) (ImplResponse, error) {
// TODO - update GetAppEntity with the required logic for this service method.
// Add api_admin_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.

Expand Down
4 changes: 4 additions & 0 deletions pkg/rest/api/api_public.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ func (c *PublicAPIController) GetInstruments(w http.ResponseWriter, r *http.Requ
func (c *PublicAPIController) GetOrderBook(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
instrumentNameParam := params["instrument_name"]
if instrumentNameParam == "" {
c.errorHandler(w, r, &RequiredError{"instrument_name"}, nil)
return
}
result, err := c.service.GetOrderBook(r.Context(), instrumentNameParam)
// If an error occurred, encode the error with the status code
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions pkg/rest/api/api_public_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func (s *PublicAPIService) GetCurrencies(ctx context.Context) (ImplResponse, err
res := make([]Currency, 0)
for _, v := range currencies {
cur := Currency{
Name: NewInterface(v.Name),
Precision: NewInterface(v.Precision),
Name: string(v.Name),
Precision: int32(v.Precision),
}
res = append(res, cur)
}
Expand All @@ -47,8 +47,8 @@ func (s *PublicAPIService) GetFxInstruments(ctx context.Context) (ImplResponse,
// TODO - update GetFxInstruments with the required logic for this service method.
// Add api_public_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(200, []FxInstrument{}) or use other options such as http.Ok ...
// return Response(200, []FxInstrument{}), nil

// TODO: Uncomment the next line to return response Response(404, {}) or use other options such as http.Ok ...
// return Response(404, nil),nil
Expand All @@ -61,8 +61,8 @@ func (s *PublicAPIService) GetInstruments(ctx context.Context) (ImplResponse, er
// TODO - update GetInstruments with the required logic for this service method.
// Add api_public_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(200, []Instrument{}) or use other options such as http.Ok ...
// return Response(200, []Instrument{}), nil

// TODO: Uncomment the next line to return response Response(404, {}) or use other options such as http.Ok ...
// return Response(404, nil),nil
Expand All @@ -71,7 +71,7 @@ func (s *PublicAPIService) GetInstruments(ctx context.Context) (ImplResponse, er
}

// GetOrderBook - Get order book
func (s *PublicAPIService) GetOrderBook(ctx context.Context, instrumentName interface{}) (ImplResponse, error) {
func (s *PublicAPIService) GetOrderBook(ctx context.Context, instrumentName string) (ImplResponse, error) {
// TODO - update GetOrderBook with the required logic for this service method.
// Add api_public_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.

Expand Down
40 changes: 40 additions & 0 deletions pkg/rest/api/api_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ func (c *UserAPIController) Routes() Routes {
func (c *UserAPIController) CreateTrade(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
tradingAccountIdParam := params["trading_account_id"]
if tradingAccountIdParam == "" {
c.errorHandler(w, r, &RequiredError{"trading_account_id"}, nil)
return
}
createTradeRequestParam := CreateTradeRequest{}
d := json.NewDecoder(r.Body)
d.DisallowUnknownFields()
Expand Down Expand Up @@ -126,7 +130,15 @@ func (c *UserAPIController) CreateTrade(w http.ResponseWriter, r *http.Request)
func (c *UserAPIController) DeleteTradeById(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
tradingAccountIdParam := params["trading_account_id"]
if tradingAccountIdParam == "" {
c.errorHandler(w, r, &RequiredError{"trading_account_id"}, nil)
return
}
tradeIdParam := params["trade_id"]
if tradeIdParam == "" {
c.errorHandler(w, r, &RequiredError{"trade_id"}, nil)
return
}
result, err := c.service.DeleteTradeById(r.Context(), tradingAccountIdParam, tradeIdParam)
// If an error occurred, encode the error with the status code
if err != nil {
Expand All @@ -141,6 +153,10 @@ func (c *UserAPIController) DeleteTradeById(w http.ResponseWriter, r *http.Reque
func (c *UserAPIController) GetBookOrders(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
tradingAccountIdParam := params["trading_account_id"]
if tradingAccountIdParam == "" {
c.errorHandler(w, r, &RequiredError{"trading_account_id"}, nil)
return
}
result, err := c.service.GetBookOrders(r.Context(), tradingAccountIdParam)
// If an error occurred, encode the error with the status code
if err != nil {
Expand All @@ -155,6 +171,10 @@ func (c *UserAPIController) GetBookOrders(w http.ResponseWriter, r *http.Request
func (c *UserAPIController) GetPaymentAccounts(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
appEntityIdParam := params["app_entity_id"]
if appEntityIdParam == "" {
c.errorHandler(w, r, &RequiredError{"app_entity_id"}, nil)
return
}
result, err := c.service.GetPaymentAccounts(r.Context(), appEntityIdParam)
// If an error occurred, encode the error with the status code
if err != nil {
Expand All @@ -169,7 +189,15 @@ func (c *UserAPIController) GetPaymentAccounts(w http.ResponseWriter, r *http.Re
func (c *UserAPIController) GetTradeById(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
tradingAccountIdParam := params["trading_account_id"]
if tradingAccountIdParam == "" {
c.errorHandler(w, r, &RequiredError{"trading_account_id"}, nil)
return
}
tradeIdParam := params["trade_id"]
if tradeIdParam == "" {
c.errorHandler(w, r, &RequiredError{"trade_id"}, nil)
return
}
result, err := c.service.GetTradeById(r.Context(), tradingAccountIdParam, tradeIdParam)
// If an error occurred, encode the error with the status code
if err != nil {
Expand All @@ -184,6 +212,10 @@ func (c *UserAPIController) GetTradeById(w http.ResponseWriter, r *http.Request)
func (c *UserAPIController) GetTradeOrders(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
tradingAccountIdParam := params["trading_account_id"]
if tradingAccountIdParam == "" {
c.errorHandler(w, r, &RequiredError{"trading_account_id"}, nil)
return
}
result, err := c.service.GetTradeOrders(r.Context(), tradingAccountIdParam)
// If an error occurred, encode the error with the status code
if err != nil {
Expand All @@ -198,6 +230,10 @@ func (c *UserAPIController) GetTradeOrders(w http.ResponseWriter, r *http.Reques
func (c *UserAPIController) GetTrades(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
tradingAccountIdParam := params["trading_account_id"]
if tradingAccountIdParam == "" {
c.errorHandler(w, r, &RequiredError{"trading_account_id"}, nil)
return
}
result, err := c.service.GetTrades(r.Context(), tradingAccountIdParam)
// If an error occurred, encode the error with the status code
if err != nil {
Expand All @@ -212,6 +248,10 @@ func (c *UserAPIController) GetTrades(w http.ResponseWriter, r *http.Request) {
func (c *UserAPIController) GetTradingAccount(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
tradingAccountIdParam := params["trading_account_id"]
if tradingAccountIdParam == "" {
c.errorHandler(w, r, &RequiredError{"trading_account_id"}, nil)
return
}
result, err := c.service.GetTradingAccount(r.Context(), tradingAccountIdParam)
// If an error occurred, encode the error with the status code
if err != nil {
Expand Down
Loading

0 comments on commit b6ff596

Please sign in to comment.