API: Swagger: Add endpoint description

This commit is contained in:
Arthur Jamet
2024-05-25 13:30:38 +02:00
parent 41c89a0476
commit 395f0b0484
6 changed files with 63 additions and 7 deletions
+1 -1
View File
@@ -4,7 +4,7 @@ WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN swag init -d cmd -o ./cmd/docs
RUN swag init --parseDependency --parseInternal --parseDepth 1 -q -d cmd -o ./cmd/docs
RUN go build -o ./vex
EXPOSE 1597
+1 -1
View File
@@ -7,4 +7,4 @@ COPY go.mod go.sum ./
RUN go mod download
EXPOSE 1597
CMD wgo -xdir ./cmd/docs swag init -d cmd -o ./cmd/docs :: go run ./cmd
CMD wgo -xdir ./cmd/docs swag init --parseDependency --parseInternal --parseDepth 1 -q -d cmd -o ./cmd/docs :: go run ./cmd
+13
View File
@@ -16,6 +16,12 @@ type ChangeEntryStatusDto struct {
IsIgnored bool `json:"isIgnored"`
}
// @Tags Entries
// @Summary Get entries
// @Produce json
// @Success 200 {object} vex.Entry
// @Router /entries [get]
// @Security JWT
func (h *Handler) GetEntries(c echo.Context) error {
user, err := GetCurrentUserId(c)
if err != nil {
@@ -28,6 +34,13 @@ func (h *Handler) GetEntries(c echo.Context) error {
return c.JSON(200, ret)
}
// @Tags Entries
// @Summary Change status
// @Produce json
// @Param DTO body ChangeEntryStatusDto true " "
// @Success 200 {object} vex.Entry
// @Router /entries [patch]
// @Security JWT
func (h *Handler) ChangeUserStatus(c echo.Context) error {
user, err := GetCurrentUserId(c)
if err != nil {
+16 -2
View File
@@ -2,10 +2,10 @@ package main
import (
"fmt"
"github.com/labstack/echo/v4"
_ "github.com/zoriya/vex"
"log"
"net/http"
"github.com/labstack/echo/v4"
)
type AddFeedDto struct {
@@ -13,6 +13,11 @@ type AddFeedDto struct {
Tags []string `json:"tags" validate:"required"`
}
// @Tags Feeds
// @Summary Get Feeds
// @Produce json
// @Success 200 {array} vex.Feed
// @Router /feeds [get]
func (h *Handler) GetFeeds(c echo.Context) error {
ret, err := h.feeds.ListFeeds()
if err != nil {
@@ -21,6 +26,15 @@ func (h *Handler) GetFeeds(c echo.Context) error {
return c.JSON(200, ret)
}
// @Tags Feeds
// @Summary Add a single Feed
// @Produce json
// @Param DTO body AddFeedDto true "The Feed to save"
// @Success 201 {object} vex.Feed
// @Failure 400 {object} string
// @Failure 409 {object} string
// @Router /feeds [post]
// @Security JWT
func (h *Handler) AddFeed(c echo.Context) error {
user, err := GetCurrentUserId(c)
if err != nil {
+4
View File
@@ -39,6 +39,10 @@ func (cv *Validator) Validate(i interface{}) error {
}
// @title Swagger for VEX API
// @securityDefinitions.apikey JWT
// @in header
// @name Authorization
// @description Prefix the value with `Bearer: `
func main() {
con := fmt.Sprintf(
"postgresql://%v:%v@%v:%v/%v?sslmode=disable",
+28 -3
View File
@@ -9,6 +9,10 @@ import (
"github.com/zoriya/vex"
)
type JWTResponse struct {
Token string `json:"token" validate:"required"`
}
type LoginDto struct {
Email string `json:"email" validate:"required"`
Password string `json:"password" validate:"required"`
@@ -20,6 +24,13 @@ type RegisterDto struct {
Password string `json:"password" validate:"required"`
}
// @Tags Auth
// @Summary Log in
// @Produce json
// @Param DTO body LoginDto true "Login Form"
// @Success 200 {object} JWTResponse
// @Success 403 {object} string
// @Router /login [post]
func (h *Handler) Login(c echo.Context) error {
var req LoginDto
err := c.Bind(&req)
@@ -49,11 +60,19 @@ func (h *Handler) CreateToken(c echo.Context, user *vex.User) error {
if err != nil {
return err
}
return c.JSON(http.StatusOK, echo.Map{
"token": t,
return c.JSON(http.StatusOK, JWTResponse{
Token: t,
})
}
// @Tags Auth
// @Summary Create an account
// @Produce json
// @Param DTO body RegisterDto true "Signup Form"
// @Success 200 {object} JWTResponse
// @Success 403 {object} string
// @Success 409 {object} string
// @Router /register [post]
func (h *Handler) Register(c echo.Context) error {
var req RegisterDto
err := c.Bind(&req)
@@ -66,11 +85,17 @@ func (h *Handler) Register(c echo.Context) error {
user, err := h.users.Create(req.Name, req.Email, req.Password)
if err != nil {
return echo.NewHTTPError(409,"Email already taken")
return echo.NewHTTPError(409, "Email already taken")
}
return h.CreateToken(c, &user)
}
// @Tags Auth
// @Summary Get info about the authed user
// @Produce json
// @Success 200 {object} vex.User
// @Router /me [get]
// @Security JWT
func (h *Handler) GetMe(c echo.Context) error {
id, err := GetCurrentUserId(c)
if err != nil {