Skip to content

Commit ae8c197

Browse files
feat: Add swagger (#2)
* feat: add swagger Signed-off-by: Thomas Poignant <thomas.poignant@gofeatureflag.org> * add sponsors link Signed-off-by: Thomas Poignant <thomas.poignant@gofeatureflag.org> --------- Signed-off-by: Thomas Poignant <thomas.poignant@gofeatureflag.org>
1 parent 9e2a73d commit ae8c197

18 files changed

+1707
-75
lines changed

.github/FUNDING.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# These are supported funding model platforms
2+
github: thomaspoignant

.github/workflows/ci.yaml

+15-15
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,18 @@ jobs:
7070
env:
7171
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
7272

73-
# swagger-change:
74-
# name: Swagger Change
75-
# runs-on: ubuntu-latest
76-
# steps:
77-
# - name: Checkout repository
78-
# uses: actions/checkout@v4
79-
# with:
80-
# fetch-depth: 1
81-
# - name: Setup go
82-
# uses: actions/setup-go@v5
83-
# with:
84-
# go-version-file: go.mod
85-
# check-latest: true
86-
# - run: make swagger
87-
# - run: git diff --exit-code --quiet
73+
swagger-change:
74+
name: Swagger Change
75+
runs-on: ubuntu-latest
76+
steps:
77+
- name: Checkout repository
78+
uses: actions/checkout@v4
79+
with:
80+
fetch-depth: 1
81+
- name: Setup go
82+
uses: actions/setup-go@v5
83+
with:
84+
go-version-file: go.mod
85+
check-latest: true
86+
- run: make swagger
87+
- run: git diff --exit-code --quiet

.github/workflows/pr-lint.yaml

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: "Lint PR title"
2+
3+
on:
4+
pull_request_target:
5+
types:
6+
- opened
7+
- edited
8+
- synchronize
9+
10+
permissions:
11+
contents: read
12+
13+
jobs:
14+
main:
15+
permissions:
16+
# for amannn/action-semantic-pull-request to analyze PR titles
17+
# for marocchino/sticky-pull-request-comment to add comments to the PR
18+
pull-requests: write
19+
statuses: write # for amannn/action-semantic-pull-request to mark status of analyzed PR
20+
name: Validate PR title
21+
runs-on: ubuntu-latest
22+
steps:
23+
- uses: amannn/action-semantic-pull-request@v5
24+
env:
25+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
26+
27+
- uses: marocchino/sticky-pull-request-comment@v2
28+
# When the previous steps fails, the workflow would stop. By adding this
29+
# condition you can continue the execution with the populated error message.
30+
if: always() && (steps.lint_pr_title.outputs.error_message != null)
31+
with:
32+
header: pr-title-lint-error
33+
message: |
34+
Hey there and thank you for opening this pull request! 👋🏼
35+
36+
We require pull request titles to follow the [Conventional Commits specification](https://www.conventionalcommits.org/en/v1.0.0/) and it looks like your proposed title needs to be adjusted.
37+
Details:
38+
39+
```
40+
${{ steps.lint_pr_title.outputs.error_message }}
41+
```
42+
43+
# Delete a previous comment when the issue has been resolved
44+
- if: ${{ steps.lint_pr_title.outputs.error_message == null }}
45+
uses: marocchino/sticky-pull-request-comment@v2
46+
with:
47+
header: pr-title-lint-error
48+
delete: true

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ vendor: ## Copy of all packages needed to support builds and tests in the vendor
3333
## Dev:
3434
swagger: ## Build swagger documentation
3535
$(GOCMD) install github.com/swaggo/swag/cmd/swag@latest
36-
cd cmd/relayproxy && swag init --parseDependency --parseDepth=1 --parseInternal --markdownFiles docs
36+
swag init --parseInternal --markdownFiles docs
3737

3838
setup-env:
3939
docker stop goff || true

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ This repository is a work in progress initiative to create an API to manage your
1717
- Postgres database using `sqlx` and `pq` as driver.
1818

1919

20-
2120
## Contributing
2221
⚠️ Since this it is a work in progress initiative please come to the [Slack channel](https://gofeatureflag.org/slack) first before contributing.
2322

@@ -36,4 +35,6 @@ To start the API:
3635
```shell
3736
make build
3837
./out/bin/goff-api
39-
```
38+
```
39+
40+
When started you can access the swagger UI at [http://localhost:3001/swagger/](http://localhost:3001/swagger/).

api/server.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package api
2+
3+
import (
4+
_ "github.com/go-feature-flag/app-api/docs"
5+
"github.com/go-feature-flag/app-api/handler"
6+
"github.com/labstack/echo/v4"
7+
"github.com/labstack/echo/v4/middleware"
8+
"github.com/swaggo/echo-swagger"
9+
)
10+
11+
// New creates a new instance of the API server
12+
func New(serverAddress string, flagHandlers handler.Flags) *Server {
13+
return &Server{
14+
flagHandlers: flagHandlers,
15+
apiEcho: echo.New(),
16+
serverAddress: serverAddress,
17+
}
18+
}
19+
20+
// Server is the struct that represents the API server
21+
type Server struct {
22+
flagHandlers handler.Flags
23+
apiEcho *echo.Echo
24+
serverAddress string
25+
}
26+
27+
// Start starts the API server
28+
func (s *Server) Start() {
29+
// config echo
30+
s.apiEcho.HideBanner = true
31+
s.apiEcho.HidePort = true
32+
33+
// Middlewares
34+
s.apiEcho.Use(middleware.CORSWithConfig(middleware.DefaultCORSConfig))
35+
36+
// init API routes
37+
groupV1 := s.apiEcho.Group("/v1")
38+
groupV1.GET("/flags", s.flagHandlers.GetAllFeatureFlags)
39+
groupV1.GET("/flags/:id", s.flagHandlers.GetFeatureFlagsByID)
40+
groupV1.POST("/flags", s.flagHandlers.CreateNewFlag)
41+
groupV1.PUT("/flags/:id", s.flagHandlers.UpdateFlagByID)
42+
groupV1.DELETE("/flags/:id", s.flagHandlers.DeleteFlagByID)
43+
groupV1.PATCH("/flags/:id/status", s.flagHandlers.UpdateFeatureFlagStatus)
44+
45+
// TODO: conditionally enable swagger based on configuration
46+
s.apiEcho.GET("/swagger/*", echoSwagger.WrapHandler)
47+
48+
// start the server
49+
s.apiEcho.Logger.Fatal(s.apiEcho.Start(s.serverAddress))
50+
}
51+
52+
// Stop stops the API server
53+
func (s *Server) Stop() error {
54+
return s.apiEcho.Close()
55+
}

dao/flags.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type Flags interface {
1010
// GetFlags return all the flags
1111
GetFlags(ctx context.Context) ([]model.FeatureFlag, error)
1212

13-
// GetFlagById return a flag by its ID
13+
// GetFlagByID return a flag by its ID
1414
GetFlagByID(ctx context.Context, id string) (model.FeatureFlag, error)
1515

1616
// GetFlagByName return a flag by its name

dao/postgres_impl.go

+20
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,26 @@ func (m *pgFlagImpl) UpdateFlag(ctx context.Context, flag model.FeatureFlag) err
262262
}
263263
}
264264

265+
// Update the flag
266+
if _, errTx := tx.NamedExecContext(
267+
ctx,
268+
`UPDATE feature_flags SET
269+
name=:name,
270+
description=:description,
271+
variations=:variations,
272+
bucketing_key=:bucketing_key,
273+
metadata=:metadata,
274+
track_events=:track_events,
275+
disable=:disable,
276+
version=:version,
277+
last_updated_date=:last_updated_date,
278+
last_modified_by=:last_modified_by
279+
WHERE id = :id`,
280+
dbQuery); err != nil {
281+
_ = tx.Rollback
282+
return errTx
283+
}
284+
265285
return tx.Commit()
266286
}
267287

docs/api.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Introduction
2+
3+
This API is documented in **OpenAPI format** and describe the REST API of the **`GO Feature Flag configuration API`**.
4+
5+
The goal of this micro-service is to offer a way to configure your feature flags in a more centralized and convenient way than a file.

0 commit comments

Comments
 (0)