Skip to content

Commit bde5d67

Browse files
authored
Merge pull request #69 from numary/GoDoc
Add Swagger generator && Add Logger in JSON
2 parents 4202b2c + 2e4dca3 commit bde5d67

File tree

16 files changed

+645
-280
lines changed

16 files changed

+645
-280
lines changed

.github/workflows/main.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@ on:
55

66
name: Main
77
jobs:
8+
docs:
9+
name: 'Build Docs'
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v2
13+
- run: |
14+
wget https://github.com/swaggo/swag/releases/download/v1.7.4/swag_1.7.4_Linux_x86_64.tar.gz
15+
tar -zxvf swag_1.7.4_Linux_x86_64.tar.gz
16+
- run: ./swag init
17+
- uses: actions/upload-artifact@v2
18+
with:
19+
name: docs-dist
20+
path: ./docs/**
821
build_control:
922
name: 'Build Control'
1023
runs-on: ubuntu-latest
@@ -57,6 +70,11 @@ jobs:
5770
with:
5871
name: control-dist
5972
path: cmd/control/
73+
- name: fetch docs
74+
uses: actions/download-artifact@v2
75+
with:
76+
name: docs-dist
77+
path: docs/
6078
- name: OSXCross for CGO Support
6179
if: matrix.os == 'ubuntu-latest'
6280
run: |

.github/workflows/pr_open.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@ on:
33
pull_request:
44
types: [assigned, opened, synchronize, reopened]
55
jobs:
6+
docs:
7+
name: 'Build Docs'
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v2
11+
- run: |
12+
wget https://github.com/swaggo/swag/releases/download/v1.7.4/swag_1.7.4_Linux_x86_64.tar.gz
13+
tar -zxvf swag_1.7.4_Linux_x86_64.tar.gz
14+
- run: ./swag init
15+
- uses: actions/upload-artifact@v2
16+
with:
17+
name: docs-dist
18+
path: ./docs/**
619
build_control:
720
name: 'Build Control'
821
runs-on: ubuntu-latest

.github/workflows/release.yml

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,34 @@ on:
55
permissions:
66
contents: write
77
jobs:
8+
docs:
9+
name: 'Build Docs'
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v2
13+
- run: |
14+
wget https://github.com/swaggo/swag/releases/download/v1.7.4/swag_1.7.4_Linux_x86_64.tar.gz
15+
tar -zxvf swag_1.7.4_Linux_x86_64.tar.gz
16+
- run: ./swag init
17+
- uses: actions/upload-artifact@v2
18+
with:
19+
name: docs-dist
20+
path: ./docs/**
821
oas:
922
name: 'OAS Readme'
1023
runs-on: ubuntu-latest
24+
needs: docs
1125
steps:
1226
- uses: actions/checkout@v2
27+
- name: fetch docs
28+
uses: actions/download-artifact@v2
29+
with:
30+
name: docs-dist
31+
path: docs/
1332
- uses: readmeio/github-readme-sync@v2
1433
with:
1534
readme-oas-key: ${{ secrets.README_OAS_KEY }}
16-
oas-file-path: './spec/http.yml'
35+
oas-file-path: './docs/swagger.json'
1736
api-version: 'v1.0-beta' # TODO : Use Release Version
1837
build_control:
1938
name: 'Build Control'
@@ -35,7 +54,9 @@ jobs:
3554
path: ./**
3655
build:
3756
name: 'Build'
38-
needs: build_control
57+
needs:
58+
- build_control
59+
- docs
3960
runs-on: ${{ matrix.os }}
4061
strategy:
4162
matrix:
@@ -63,6 +84,11 @@ jobs:
6384
with:
6485
name: control-dist
6586
path: cmd/control/
87+
- name: fetch docs
88+
uses: actions/download-artifact@v2
89+
with:
90+
name: docs-dist
91+
path: docs/
6692
- name: Set up QEMU
6793
if: matrix.os == 'ubuntu-latest'
6894
uses: docker/setup-qemu-action@v1

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ coverage.out
44
cmd/control/*
55
!cmd/control/gitkeep
66
.DS_Store
7+
docs/swagger.json
8+
docs/swagger.yaml

Makefile

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,3 @@ test:
33

44
bench:
55
go test -test.bench=. -run=^a ./...
6-
7-
fetch-control:
8-
cd cmd/control && \
9-
curl -OL https://numary-control-releases.s3.eu-west-1.amazonaws.com/numary-control.tar.gz && \
10-
tar -zxvf numary-control.tar.gz && \
11-
rm numary-control.tar.gz

api/controllers/account_controller.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@ func NewAccountController() AccountController {
1919
return AccountController{}
2020
}
2121

22-
// GetAccounts -
22+
// GetAccounts godoc
23+
// @Summary List All Accounts
24+
// @Schemes
25+
// @Param ledger path string true "ledger"
26+
// @Accept json
27+
// @Produce json
28+
// @Success 200 {object} storage.Store{}
29+
// @Router /{ledger}/accounts [get]
2330
func (ctl *AccountController) GetAccounts(c *gin.Context) {
2431
l, _ := c.Get("ledger")
2532
cursor, err := l.(*ledger.Ledger).FindAccounts(
@@ -40,7 +47,15 @@ func (ctl *AccountController) GetAccounts(c *gin.Context) {
4047
)
4148
}
4249

43-
// GetAccount -
50+
// GetAccount godoc
51+
// @Summary List Account by Address
52+
// @Schemes
53+
// @Param ledger path string true "ledger"
54+
// @Param accountId path string true "accountId"
55+
// @Accept json
56+
// @Produce json
57+
// @Success 200 {string} string ""
58+
// @Router /{ledger}/accounts/{accountId} [get]
4459
func (ctl *AccountController) GetAccount(c *gin.Context) {
4560
l, _ := c.Get("ledger")
4661
acc, err := l.(*ledger.Ledger).GetAccount(c.Param("address"))
@@ -59,7 +74,15 @@ func (ctl *AccountController) GetAccount(c *gin.Context) {
5974
)
6075
}
6176

62-
// PostAccountMetadata -
77+
// PostAccountMetadata godoc
78+
// @Summary Add metadata to account
79+
// @Schemes
80+
// @Param ledger path string true "ledger"
81+
// @Param accountId path string true "accountId"
82+
// @Accept json
83+
// @Produce json
84+
// @Success 200 {string} string ""
85+
// @Router /{ledger}/accounts/{accountId}/metadata [post]
6386
func (ctl *AccountController) PostAccountMetadata(c *gin.Context) {
6487
l, _ := c.Get("ledger")
6588
var m core.Metadata

api/controllers/config_controller.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package controllers
22

33
import (
4+
"github.com/swaggo/swag"
45
"net/http"
56

67
"github.com/gin-gonic/gin"
78
"github.com/numary/ledger/config"
9+
_ "github.com/numary/ledger/docs"
810
"github.com/spf13/viper"
911
)
1012

@@ -18,7 +20,13 @@ func NewConfigController() ConfigController {
1820
return ConfigController{}
1921
}
2022

21-
// GetInfo -
23+
// GetInfo godoc
24+
// @Summary Server Info
25+
// @Schemes
26+
// @Accept json
27+
// @Produce json
28+
// @Success 200 {object} config.ConfigInfo{}
29+
// @Router /_info [get]
2230
func (ctl *ConfigController) GetInfo(c *gin.Context) {
2331
ctl.response(
2432
c,
@@ -35,3 +43,12 @@ func (ctl *ConfigController) GetInfo(c *gin.Context) {
3543
},
3644
)
3745
}
46+
47+
func (ctl *ConfigController) GetDocs(c *gin.Context) {
48+
doc, err := swag.ReadDoc("swagger")
49+
if err != nil {
50+
c.AbortWithStatus(http.StatusInternalServerError)
51+
return
52+
}
53+
c.Writer.Write([]byte(doc))
54+
}

api/controllers/ledger_controller.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ func NewLedgerController() LedgerController {
1717
return LedgerController{}
1818
}
1919

20-
// GetStats -
20+
// GetStats godoc
21+
// @Summary Get ledger stats (aggregate metrics on accounts and transactions)
22+
// @Schemes
23+
// @Description The stats for account
24+
// @Accept json
25+
// @Produce json
26+
// @Param ledger path string true "ledger"
27+
// @Success 200 {object} ledger.Stats{}
28+
// @Router /{ledger}/stats [get]
2129
func (ctl *LedgerController) GetStats(c *gin.Context) {
2230
l, _ := c.Get("ledger")
2331

api/controllers/script_controller.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,14 @@ func NewScriptController() ScriptController {
2222
return ScriptController{}
2323
}
2424

25-
// PostScript -
25+
// PostScript godoc
26+
// @Summary Execute a Numscript and commit transaction if any
27+
// @Schemes
28+
// @Param ledger path string true "ledger"
29+
// @Accept json
30+
// @Produce json
31+
// @Success 200 {string} string ""
32+
// @Router /{ledger}/script [post]
2633
func (ctl *ScriptController) PostScript(c *gin.Context) {
2734
l, _ := c.Get("ledger")
2835

api/controllers/transaction_controller.go

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,15 @@ func NewTransactionController() TransactionController {
1919
return TransactionController{}
2020
}
2121

22-
// GetTransactions -
22+
// GetTransactions godoc
23+
// @Summary Get Transactions
24+
// @Schemes
25+
// @Description List transactions
26+
// @Param ledger path string true "ledger"
27+
// @Accept json
28+
// @Produce json
29+
// @Success 200 {object} storage.Store{}
30+
// @Router /{ledger}/transactions [get]
2331
func (ctl *TransactionController) GetTransactions(c *gin.Context) {
2432
l, _ := c.Get("ledger")
2533
cursor, err := l.(*ledger.Ledger).FindTransactions(
@@ -42,7 +50,15 @@ func (ctl *TransactionController) GetTransactions(c *gin.Context) {
4250
)
4351
}
4452

45-
// PostTransaction -
53+
// PostTransactions godoc
54+
// @Summary Commit a new transaction to the ledger
55+
// @Schemes
56+
// @Description Commit a new transaction to the ledger
57+
// @Param ledger path string true "ledger"
58+
// @Accept json
59+
// @Produce json
60+
// @Success 200 {string} string ""
61+
// @Router /{ledger}/transactions [post]
4662
func (ctl *TransactionController) PostTransaction(c *gin.Context) {
4763
l, _ := c.Get("ledger")
4864

@@ -65,7 +81,15 @@ func (ctl *TransactionController) PostTransaction(c *gin.Context) {
6581
)
6682
}
6783

68-
// RevertTransaction -
84+
// RevertTransaction godoc
85+
// @Summary Revert transaction
86+
// @Schemes
87+
// @Param ledger path string true "ledger"
88+
// @Param reference path string true "reference"
89+
// @Accept json
90+
// @Produce json
91+
// @Success 200 {string} string ""
92+
// @Router /{ledger}/transactions/{reference}/revert [post]
6993
func (ctl *TransactionController) RevertTransaction(c *gin.Context) {
7094
l, _ := c.Get("ledger")
7195
err := l.(*ledger.Ledger).RevertTransaction(c.Param("transactionId"))
@@ -84,7 +108,15 @@ func (ctl *TransactionController) RevertTransaction(c *gin.Context) {
84108
)
85109
}
86110

87-
// PostTransactionMetadata -
111+
// PostTransactionMetadata godoc
112+
// @Summary Set metadata on transaction
113+
// @Schemes
114+
// @Param ledger path string true "ledger"
115+
// @Param reference path string true "reference"
116+
// @Accept json
117+
// @Produce json
118+
// @Success 200 {string} string ""
119+
// @Router /{ledger}/transactions/{reference}/metadata [post]
88120
func (ctl *TransactionController) PostTransactionMetadata(c *gin.Context) {
89121
l, _ := c.Get("ledger")
90122

0 commit comments

Comments
 (0)