chore: update swag version
continuous-integration/drone/pr Build is passing
Details
continuous-integration/drone/pr Build is passing
Details
This commit is contained in:
parent
bbaea764d4
commit
e72881ba13
|
@ -116,4 +116,7 @@ issues:
|
|||
source: "^//go:generate "
|
||||
- linters:
|
||||
- lll
|
||||
source: "^// @Param"
|
||||
source: "^// @Param"
|
||||
- linters: ## conflict with swag fmt
|
||||
- gocritic
|
||||
source: "^} //@name "
|
||||
|
|
2
Makefile
2
Makefile
|
@ -22,7 +22,7 @@ install-counterfeiter:
|
|||
.PHONY: install-swag
|
||||
install-swag:
|
||||
@echo "Installing github.com/swaggo/swag..."
|
||||
@test -f $(SWAG_PATH) || (wget -qO- https://github.com/swaggo/swag/releases/download/v1.8.7/swag_1.8.7_$(GOOS)_$(OSARCH).tar.gz | tar -xz -C $(GOBIN)/)
|
||||
@test -f $(SWAG_PATH) || (wget -qO- https://github.com/swaggo/swag/releases/download/v1.8.12/swag_1.8.12_$(GOOS)_$(OSARCH).tar.gz | tar -xz -C $(GOBIN)/)
|
||||
|
||||
.PHONY: install-golangci-lint
|
||||
install-golangci-lint:
|
||||
|
|
7
go.mod
7
go.mod
|
@ -13,8 +13,8 @@ require (
|
|||
github.com/kelseyhightower/envconfig v1.4.0
|
||||
github.com/ory/dockertest/v3 v3.10.0
|
||||
github.com/stretchr/testify v1.8.2
|
||||
github.com/swaggo/http-swagger v1.3.3
|
||||
github.com/swaggo/swag v1.8.8
|
||||
github.com/swaggo/http-swagger/v2 v2.0.1
|
||||
github.com/swaggo/swag v1.8.12
|
||||
github.com/uptrace/bun v1.1.12
|
||||
github.com/uptrace/bun/dbfixture v1.1.12
|
||||
github.com/uptrace/bun/dialect/pgdialect v1.1.12
|
||||
|
@ -54,7 +54,7 @@ require (
|
|||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||
github.com/sirupsen/logrus v1.8.1 // indirect
|
||||
github.com/swaggo/files v0.0.0-20220610200504-28940afbdbfe // indirect
|
||||
github.com/swaggo/files/v2 v2.0.0 // indirect
|
||||
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
|
||||
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
|
||||
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
|
||||
|
@ -66,7 +66,6 @@ require (
|
|||
go.uber.org/multierr v1.7.0 // indirect
|
||||
golang.org/x/crypto v0.6.0 // indirect
|
||||
golang.org/x/mod v0.9.0 // indirect
|
||||
golang.org/x/net v0.8.0 // indirect
|
||||
golang.org/x/sys v0.7.0 // indirect
|
||||
golang.org/x/tools v0.7.0 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
|
|
17
go.sum
17
go.sum
|
@ -132,12 +132,12 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
|
|||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
|
||||
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/swaggo/files v0.0.0-20220610200504-28940afbdbfe h1:K8pHPVoTgxFJt1lXuIzzOX7zZhZFldJQK/CgKx9BFIc=
|
||||
github.com/swaggo/files v0.0.0-20220610200504-28940afbdbfe/go.mod h1:lKJPbtWzJ9JhsTN1k1gZgleJWY/cqq0psdoMmaThG3w=
|
||||
github.com/swaggo/http-swagger v1.3.3 h1:Hu5Z0L9ssyBLofaama21iYaF2VbWyA8jdohaaCGpHsc=
|
||||
github.com/swaggo/http-swagger v1.3.3/go.mod h1:sE+4PjD89IxMPm77FnkDz0sdO+p5lbXzrVWT6OTVVGo=
|
||||
github.com/swaggo/swag v1.8.8 h1:/GgJmrJ8/c0z4R4hoEPZ5UeEhVGdvsII4JbVDLbR7Xc=
|
||||
github.com/swaggo/swag v1.8.8/go.mod h1:ezQVUUhly8dludpVk+/PuwJWvLLanB13ygV5Pr9enSk=
|
||||
github.com/swaggo/files/v2 v2.0.0 h1:hmAt8Dkynw7Ssz46F6pn8ok6YmGZqHSVLZ+HQM7i0kw=
|
||||
github.com/swaggo/files/v2 v2.0.0/go.mod h1:24kk2Y9NYEJ5lHuCra6iVwkMjIekMCaFq/0JQj66kyM=
|
||||
github.com/swaggo/http-swagger/v2 v2.0.1 h1:mNOBLxDjSNwCKlMxcErjjvct/xhc9t2KIO48xzz/V/k=
|
||||
github.com/swaggo/http-swagger/v2 v2.0.1/go.mod h1:XYhrQVIKz13CxuKD4p4kvpaRB4jJ1/MlfQXVOE+CX8Y=
|
||||
github.com/swaggo/swag v1.8.12 h1:pctzkNPu0AlQP2royqX3apjKCQonAnf7KGoxeO4y64w=
|
||||
github.com/swaggo/swag v1.8.12/go.mod h1:lNfm6Gg+oAq3zRJQNEMBE66LIJKM44mxFqhEEgy2its=
|
||||
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
|
||||
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYmW5DyG0UqvY96Bu5QYsTLvCHdrgo=
|
||||
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc/go.mod h1:bciPuU6GHm1iF1pBvUfxfsH0Wmnc2VbpgvbI9ZWuIRs=
|
||||
|
@ -191,9 +191,6 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL
|
|||
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
|
||||
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
|
@ -207,7 +204,6 @@ golang.org/x/sys v0.0.0-20200831180312-196b9ba8737a/go.mod h1:h1NjWce9XRLGQEsW7w
|
|||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210906170528-6f6e22806c34/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20211116061358-0a5406a5449c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
|
@ -216,7 +212,6 @@ golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190624222133-a101b041ded4/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
|
|
|
@ -4,8 +4,8 @@ type APIError struct {
|
|||
//nolint:lll
|
||||
Code string `json:"code" enums:"entity-not-found,route-not-found,method-not-allowed,validation-error,unauthorized,already-exists,internal-server-error"`
|
||||
Message string `json:"message"`
|
||||
} // @name ApiError
|
||||
} //@name ApiError
|
||||
|
||||
type ErrorResp struct {
|
||||
Error APIError `json:"error"`
|
||||
} // @name ErrorResp
|
||||
} //@name ErrorResp
|
||||
|
|
|
@ -5,7 +5,7 @@ import "gitea.dwysokinski.me/twhelp/sessions/internal/domain"
|
|||
type Session struct {
|
||||
ServerKey string `json:"serverKey"`
|
||||
SID string `json:"sid"`
|
||||
} // @name Session
|
||||
} //@name Session
|
||||
|
||||
func NewSession(s domain.Session) Session {
|
||||
return Session{
|
||||
|
@ -16,7 +16,7 @@ func NewSession(s domain.Session) Session {
|
|||
|
||||
type GetSessionResp struct {
|
||||
Data Session `json:"data"`
|
||||
} // @name GetSessionResp
|
||||
} //@name GetSessionResp
|
||||
|
||||
func NewGetSessionResp(s domain.Session) GetSessionResp {
|
||||
return GetSessionResp{Data: NewSession(s)}
|
||||
|
|
|
@ -10,7 +10,7 @@ type User struct {
|
|||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
CreatedAt time.Time `json:"createdAt" format:"date-time"`
|
||||
} // @name User
|
||||
} //@name User
|
||||
|
||||
func NewUser(u domain.User) User {
|
||||
return User{
|
||||
|
@ -22,7 +22,7 @@ func NewUser(u domain.User) User {
|
|||
|
||||
type GetUserResp struct {
|
||||
Data User `json:"data"`
|
||||
} // @name GetUserResp
|
||||
} //@name GetUserResp
|
||||
|
||||
func NewGetUserResp(u domain.User) GetUserResp {
|
||||
return GetUserResp{Data: NewUser(u)}
|
||||
|
|
|
@ -15,10 +15,13 @@ type APIKeyVerifier interface {
|
|||
Verify(ctx context.Context, key string) (domain.User, error)
|
||||
}
|
||||
|
||||
//nolint:gosec
|
||||
const headerXApiKey = "X-API-Key"
|
||||
|
||||
func authMiddleware(verifier APIKeyVerifier) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
key := r.Header.Get("X-API-Key")
|
||||
key := r.Header.Get(headerXApiKey)
|
||||
if key == "" {
|
||||
renderJSON(w, http.StatusUnauthorized, model.ErrorResp{
|
||||
Error: model.APIError{
|
||||
|
|
|
@ -2,17 +2,17 @@ package rest
|
|||
|
||||
//go:generate swag init -g openapi.go -o "./internal/docs"
|
||||
|
||||
// @title Sessions API
|
||||
// @version 1.0
|
||||
// @title Sessions API
|
||||
// @version 1.0
|
||||
|
||||
// @contact.name Dawid Wysokiński
|
||||
// @contact.url https://dwysokinski.me
|
||||
// @contact.email contact@dwysokinski.me
|
||||
// @contact.name Dawid Wysokiński
|
||||
// @contact.url https://dwysokinski.me
|
||||
// @contact.email contact@dwysokinski.me
|
||||
|
||||
// @BasePath /api/v1
|
||||
// @BasePath /api/v1
|
||||
|
||||
// @query.collection.format multi
|
||||
// @query.collection.format multi
|
||||
|
||||
// @securityDefinitions.apikey ApiKeyAuth
|
||||
// @in header
|
||||
// @name X-Api-Key
|
||||
// @securityDefinitions.apikey ApiKeyAuth
|
||||
// @in header
|
||||
// @name X-Api-Key
|
||||
|
|
|
@ -10,7 +10,7 @@ import (
|
|||
"gitea.dwysokinski.me/twhelp/sessions/internal/router/rest/internal/model"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/cors"
|
||||
httpSwagger "github.com/swaggo/http-swagger"
|
||||
httpSwagger "github.com/swaggo/http-swagger/v2"
|
||||
)
|
||||
|
||||
//go:generate counterfeiter -generate
|
||||
|
@ -35,7 +35,7 @@ func New(
|
|||
AllowedOrigins: cfg.cors.AllowedOrigins,
|
||||
AllowCredentials: cfg.cors.AllowCredentials,
|
||||
AllowedMethods: cfg.cors.AllowedMethods,
|
||||
AllowedHeaders: []string{"Origin", "Content-Length", "Content-Type", "X-API-Key"},
|
||||
AllowedHeaders: []string{"Origin", "Content-Length", "Content-Type", headerXApiKey},
|
||||
MaxAge: cfg.cors.MaxAge,
|
||||
}))
|
||||
}
|
||||
|
|
|
@ -24,19 +24,21 @@ type sessionHandler struct {
|
|||
svc SessionService
|
||||
}
|
||||
|
||||
// @ID createOrUpdateSession
|
||||
// @Summary Create or update a session
|
||||
// @Description Create or update a session
|
||||
// @Tags users,sessions
|
||||
// @Accept plain
|
||||
// @Success 204
|
||||
// @Failure 400 {object} model.ErrorResp
|
||||
// @Failure 401 {object} model.ErrorResp
|
||||
// @Failure 500 {object} model.ErrorResp
|
||||
// @Security ApiKeyAuth
|
||||
// @Param serverKey path string true "Server key"
|
||||
// @Param request body string true "SID"
|
||||
// @Router /user/sessions/{serverKey} [put]
|
||||
// createOrUpdate creates or updates a session.
|
||||
//
|
||||
// @ID createOrUpdateSession
|
||||
// @Summary Create or update a session
|
||||
// @Description Create or update a session
|
||||
// @Tags users,sessions
|
||||
// @Accept plain
|
||||
// @Success 204
|
||||
// @Failure 400 {object} model.ErrorResp
|
||||
// @Failure 401 {object} model.ErrorResp
|
||||
// @Failure 500 {object} model.ErrorResp
|
||||
// @Security ApiKeyAuth
|
||||
// @Param serverKey path string true "Server key"
|
||||
// @Param request body string true "SID"
|
||||
// @Router /user/sessions/{serverKey} [put]
|
||||
func (h *sessionHandler) createOrUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
chiCtx := chi.RouteContext(ctx)
|
||||
|
@ -63,18 +65,20 @@ func (h *sessionHandler) createOrUpdate(w http.ResponseWriter, r *http.Request)
|
|||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
// @ID getCurrentUserSession
|
||||
// @Summary Get a session
|
||||
// @Description Get a session
|
||||
// @Tags users,sessions
|
||||
// @Success 200 {object} model.GetSessionResp
|
||||
// @Failure 400 {object} model.ErrorResp
|
||||
// @Failure 401 {object} model.ErrorResp
|
||||
// @Failure 404 {object} model.ErrorResp
|
||||
// @Failure 500 {object} model.ErrorResp
|
||||
// @Security ApiKeyAuth
|
||||
// @Param serverKey path string true "Server key"
|
||||
// @Router /user/sessions/{serverKey} [get]
|
||||
// getCurrentUser returns a session for the given server if any exists, otherwise 404 is returned.
|
||||
//
|
||||
// @ID getCurrentUserSession
|
||||
// @Summary Get a session
|
||||
// @Description Get a session
|
||||
// @Tags users,sessions
|
||||
// @Success 200 {object} model.GetSessionResp
|
||||
// @Failure 400 {object} model.ErrorResp
|
||||
// @Failure 401 {object} model.ErrorResp
|
||||
// @Failure 404 {object} model.ErrorResp
|
||||
// @Failure 500 {object} model.ErrorResp
|
||||
// @Security ApiKeyAuth
|
||||
// @Param serverKey path string true "Server key"
|
||||
// @Router /user/sessions/{serverKey} [get]
|
||||
func (h *sessionHandler) getCurrentUser(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
chiCtx := chi.RouteContext(ctx)
|
||||
|
|
|
@ -9,16 +9,18 @@ import (
|
|||
type userHandler struct {
|
||||
}
|
||||
|
||||
// @ID getCurrentUser
|
||||
// @Summary Get the authenticated user
|
||||
// @Description Get the authenticated user
|
||||
// @Tags users
|
||||
// @Produce json
|
||||
// @Success 200 {object} model.GetUserResp
|
||||
// @Failure 401 {object} model.ErrorResp
|
||||
// @Failure 500 {object} model.ErrorResp
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /user [get]
|
||||
// getCurrentUser returns the current user stored in the request context.
|
||||
//
|
||||
// @ID getCurrentUser
|
||||
// @Summary Get the authenticated user
|
||||
// @Description Get the authenticated user
|
||||
// @Tags users
|
||||
// @Produce json
|
||||
// @Success 200 {object} model.GetUserResp
|
||||
// @Failure 401 {object} model.ErrorResp
|
||||
// @Failure 500 {object} model.ErrorResp
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /user [get]
|
||||
func (h *userHandler) getCurrent(w http.ResponseWriter, r *http.Request) {
|
||||
u, _ := userFromContext(r.Context())
|
||||
renderJSON(w, http.StatusOK, model.NewGetUserResp(u))
|
||||
|
|
Loading…
Reference in New Issue