Skip to content

Commit

Permalink
feat: Add swagger (#2)
Browse files Browse the repository at this point in the history
* feat: add swagger

Signed-off-by: Thomas Poignant <[email protected]>

* add sponsors link

Signed-off-by: Thomas Poignant <[email protected]>

---------

Signed-off-by: Thomas Poignant <[email protected]>
  • Loading branch information
thomaspoignant authored Oct 2, 2024
1 parent 9e2a73d commit ae8c197
Show file tree
Hide file tree
Showing 18 changed files with 1,707 additions and 75 deletions.
2 changes: 2 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# These are supported funding model platforms
github: thomaspoignant
30 changes: 15 additions & 15 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,18 @@ jobs:
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

# swagger-change:
# name: Swagger Change
# runs-on: ubuntu-latest
# steps:
# - name: Checkout repository
# uses: actions/checkout@v4
# with:
# fetch-depth: 1
# - name: Setup go
# uses: actions/setup-go@v5
# with:
# go-version-file: go.mod
# check-latest: true
# - run: make swagger
# - run: git diff --exit-code --quiet
swagger-change:
name: Swagger Change
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 1
- name: Setup go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
check-latest: true
- run: make swagger
- run: git diff --exit-code --quiet
48 changes: 48 additions & 0 deletions .github/workflows/pr-lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: "Lint PR title"

on:
pull_request_target:
types:
- opened
- edited
- synchronize

permissions:
contents: read

jobs:
main:
permissions:
# for amannn/action-semantic-pull-request to analyze PR titles
# for marocchino/sticky-pull-request-comment to add comments to the PR
pull-requests: write
statuses: write # for amannn/action-semantic-pull-request to mark status of analyzed PR
name: Validate PR title
runs-on: ubuntu-latest
steps:
- uses: amannn/action-semantic-pull-request@v5
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- uses: marocchino/sticky-pull-request-comment@v2
# When the previous steps fails, the workflow would stop. By adding this
# condition you can continue the execution with the populated error message.
if: always() && (steps.lint_pr_title.outputs.error_message != null)
with:
header: pr-title-lint-error
message: |
Hey there and thank you for opening this pull request! 👋🏼
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.
Details:
```
${{ steps.lint_pr_title.outputs.error_message }}
```
# Delete a previous comment when the issue has been resolved
- if: ${{ steps.lint_pr_title.outputs.error_message == null }}
uses: marocchino/sticky-pull-request-comment@v2
with:
header: pr-title-lint-error
delete: true
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ vendor: ## Copy of all packages needed to support builds and tests in the vendor
## Dev:
swagger: ## Build swagger documentation
$(GOCMD) install github.com/swaggo/swag/cmd/swag@latest
cd cmd/relayproxy && swag init --parseDependency --parseDepth=1 --parseInternal --markdownFiles docs
swag init --parseInternal --markdownFiles docs

setup-env:
docker stop goff || true
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ This repository is a work in progress initiative to create an API to manage your
- Postgres database using `sqlx` and `pq` as driver.



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

Expand All @@ -36,4 +35,6 @@ To start the API:
```shell
make build
./out/bin/goff-api
```
```

When started you can access the swagger UI at [http://localhost:3001/swagger/](http://localhost:3001/swagger/).
55 changes: 55 additions & 0 deletions api/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package api

import (
_ "github.com/go-feature-flag/app-api/docs"
"github.com/go-feature-flag/app-api/handler"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/swaggo/echo-swagger"
)

// New creates a new instance of the API server
func New(serverAddress string, flagHandlers handler.Flags) *Server {
return &Server{
flagHandlers: flagHandlers,
apiEcho: echo.New(),
serverAddress: serverAddress,
}
}

// Server is the struct that represents the API server
type Server struct {
flagHandlers handler.Flags
apiEcho *echo.Echo
serverAddress string
}

// Start starts the API server
func (s *Server) Start() {
// config echo
s.apiEcho.HideBanner = true
s.apiEcho.HidePort = true

// Middlewares
s.apiEcho.Use(middleware.CORSWithConfig(middleware.DefaultCORSConfig))

// init API routes
groupV1 := s.apiEcho.Group("/v1")
groupV1.GET("/flags", s.flagHandlers.GetAllFeatureFlags)
groupV1.GET("/flags/:id", s.flagHandlers.GetFeatureFlagsByID)
groupV1.POST("/flags", s.flagHandlers.CreateNewFlag)
groupV1.PUT("/flags/:id", s.flagHandlers.UpdateFlagByID)
groupV1.DELETE("/flags/:id", s.flagHandlers.DeleteFlagByID)
groupV1.PATCH("/flags/:id/status", s.flagHandlers.UpdateFeatureFlagStatus)

// TODO: conditionally enable swagger based on configuration
s.apiEcho.GET("/swagger/*", echoSwagger.WrapHandler)

// start the server
s.apiEcho.Logger.Fatal(s.apiEcho.Start(s.serverAddress))
}

// Stop stops the API server
func (s *Server) Stop() error {
return s.apiEcho.Close()
}
2 changes: 1 addition & 1 deletion dao/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Flags interface {
// GetFlags return all the flags
GetFlags(ctx context.Context) ([]model.FeatureFlag, error)

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

// GetFlagByName return a flag by its name
Expand Down
20 changes: 20 additions & 0 deletions dao/postgres_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,26 @@ func (m *pgFlagImpl) UpdateFlag(ctx context.Context, flag model.FeatureFlag) err
}
}

// Update the flag
if _, errTx := tx.NamedExecContext(
ctx,
`UPDATE feature_flags SET
name=:name,
description=:description,
variations=:variations,
bucketing_key=:bucketing_key,
metadata=:metadata,
track_events=:track_events,
disable=:disable,
version=:version,
last_updated_date=:last_updated_date,
last_modified_by=:last_modified_by
WHERE id = :id`,
dbQuery); err != nil {
_ = tx.Rollback
return errTx
}

return tx.Commit()
}

Expand Down
5 changes: 5 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Introduction

This API is documented in **OpenAPI format** and describe the REST API of the **`GO Feature Flag configuration API`**.

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.
Loading

0 comments on commit ae8c197

Please sign in to comment.