Compare commits

..

No commits in common. "3cf6afb39c5e01317e89fb8888871cb4f5433daf" and "56199a795a2b7a8c5e37a904aa133829b9026c47" have entirely different histories.

16 changed files with 450 additions and 4307 deletions

2
.gitignore vendored
View File

@ -3,7 +3,6 @@ dev.session.sql
__debug_bin __debug_bin
server server
.vscode .vscode
openapi.json
# Binaries for programs and plugins # Binaries for programs and plugins
*.exe *.exe
@ -13,7 +12,6 @@ openapi.json
*.dylib *.dylib
collector collector
newsbot.db newsbot.db
tmp/
# Test binary, built with `go test -c` # Test binary, built with `go test -c`
*.test *.test

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +0,0 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/deepmap/oapi-codegen/HEAD/configuration-schema.json
package: api
output: api.gen.go
generate:
models: true
client: true

View File

@ -32,11 +32,10 @@ const docTemplate = `{
"summary": "Lists the top 25 records ordering from newest to oldest.", "summary": "Lists the top 25 records ordering from newest to oldest.",
"parameters": [ "parameters": [
{ {
"type": "integer", "type": "string",
"description": "page number", "description": "page number",
"name": "page", "name": "page",
"in": "query", "in": "query"
"required": true
} }
], ],
"responses": { "responses": {
@ -61,7 +60,7 @@ const docTemplate = `{
} }
} }
}, },
"/v1/articles/by/source/{id}": { "/v1/articles/by/sourceId": {
"get": { "get": {
"security": [ "security": [
{ {
@ -80,7 +79,7 @@ const docTemplate = `{
"type": "string", "type": "string",
"description": "source id", "description": "source id",
"name": "id", "name": "id",
"in": "path", "in": "query",
"required": true "required": true
}, },
{ {
@ -112,7 +111,7 @@ const docTemplate = `{
} }
} }
}, },
"/v1/articles/{id}": { "/v1/articles/{ID}": {
"get": { "get": {
"security": [ "security": [
{ {
@ -130,7 +129,7 @@ const docTemplate = `{
{ {
"type": "string", "type": "string",
"description": "int", "description": "int",
"name": "id", "name": "ID",
"in": "path", "in": "path",
"required": true "required": true
} }
@ -157,7 +156,7 @@ const docTemplate = `{
} }
} }
}, },
"/v1/articles/{id}/details": { "/v1/articles/{ID}/details": {
"get": { "get": {
"security": [ "security": [
{ {
@ -175,7 +174,7 @@ const docTemplate = `{
{ {
"type": "string", "type": "string",
"description": "int", "description": "int",
"name": "id", "name": "ID",
"in": "path", "in": "path",
"required": true "required": true
} }
@ -346,50 +345,7 @@ const docTemplate = `{
} }
} }
}, },
"/v1/discord/webhooks/{id}": { "/v1/discord/webhooks/{ID}": {
"get": {
"security": [
{
"Bearer": []
}
],
"produces": [
"application/json"
],
"tags": [
"DiscordWebhook"
],
"summary": "Returns the top 100 entries from the queue to be processed.",
"parameters": [
{
"type": "integer",
"description": "id",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/domain.DiscordWebhookResponse"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/domain.DiscordWebhookResponse"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/domain.DiscordWebhookResponse"
}
}
}
},
"delete": { "delete": {
"security": [ "security": [
{ {
@ -431,7 +387,7 @@ const docTemplate = `{
} }
} }
}, },
"/v1/discord/webhooks/{id}/disable": { "/v1/discord/webhooks/{ID}/disable": {
"post": { "post": {
"security": [ "security": [
{ {
@ -473,7 +429,7 @@ const docTemplate = `{
} }
} }
}, },
"/v1/discord/webhooks/{id}/enable": { "/v1/discord/webhooks/{ID}/enable": {
"post": { "post": {
"security": [ "security": [
{ {
@ -515,6 +471,51 @@ const docTemplate = `{
} }
} }
}, },
"/v1/discord/webhooks/{id}": {
"get": {
"security": [
{
"Bearer": []
}
],
"produces": [
"application/json"
],
"tags": [
"DiscordWebhook"
],
"summary": "Returns the top 100 entries from the queue to be processed.",
"parameters": [
{
"type": "integer",
"description": "id",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/domain.DiscordWebhookResponse"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/domain.DiscordWebhookResponse"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/domain.DiscordWebhookResponse"
}
}
}
}
},
"/v1/sources": { "/v1/sources": {
"get": { "get": {
"security": [ "security": [
@ -1511,16 +1512,24 @@ const docTemplate = `{
} }
} }
} }
},
"securityDefinitions": {
"Bearer": {
"description": "Type \"Bearer\" followed by a space and JWT token.",
"type": "apiKey",
"name": "Authorization",
"in": "header"
}
} }
}` }`
// SwaggerInfo holds exported Swagger Info so clients can modify it // SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = &swag.Spec{ var SwaggerInfo = &swag.Spec{
Version: "", Version: "0.1",
Host: "", Host: "",
BasePath: "", BasePath: "/api",
Schemes: []string{}, Schemes: []string{},
Title: "", Title: "NewsBot collector",
Description: "", Description: "",
InfoInstanceName: "swagger", InfoInstanceName: "swagger",
SwaggerTemplate: docTemplate, SwaggerTemplate: docTemplate,

View File

@ -1,8 +1,11 @@
{ {
"swagger": "2.0", "swagger": "2.0",
"info": { "info": {
"contact": {} "title": "NewsBot collector",
"contact": {},
"version": "0.1"
}, },
"basePath": "/api",
"paths": { "paths": {
"/v1/articles": { "/v1/articles": {
"get": { "get": {
@ -20,11 +23,10 @@
"summary": "Lists the top 25 records ordering from newest to oldest.", "summary": "Lists the top 25 records ordering from newest to oldest.",
"parameters": [ "parameters": [
{ {
"type": "integer", "type": "string",
"description": "page number", "description": "page number",
"name": "page", "name": "page",
"in": "query", "in": "query"
"required": true
} }
], ],
"responses": { "responses": {
@ -49,7 +51,7 @@
} }
} }
}, },
"/v1/articles/by/source/{id}": { "/v1/articles/by/sourceId": {
"get": { "get": {
"security": [ "security": [
{ {
@ -68,7 +70,7 @@
"type": "string", "type": "string",
"description": "source id", "description": "source id",
"name": "id", "name": "id",
"in": "path", "in": "query",
"required": true "required": true
}, },
{ {
@ -100,7 +102,7 @@
} }
} }
}, },
"/v1/articles/{id}": { "/v1/articles/{ID}": {
"get": { "get": {
"security": [ "security": [
{ {
@ -118,7 +120,7 @@
{ {
"type": "string", "type": "string",
"description": "int", "description": "int",
"name": "id", "name": "ID",
"in": "path", "in": "path",
"required": true "required": true
} }
@ -145,7 +147,7 @@
} }
} }
}, },
"/v1/articles/{id}/details": { "/v1/articles/{ID}/details": {
"get": { "get": {
"security": [ "security": [
{ {
@ -163,7 +165,7 @@
{ {
"type": "string", "type": "string",
"description": "int", "description": "int",
"name": "id", "name": "ID",
"in": "path", "in": "path",
"required": true "required": true
} }
@ -334,50 +336,7 @@
} }
} }
}, },
"/v1/discord/webhooks/{id}": { "/v1/discord/webhooks/{ID}": {
"get": {
"security": [
{
"Bearer": []
}
],
"produces": [
"application/json"
],
"tags": [
"DiscordWebhook"
],
"summary": "Returns the top 100 entries from the queue to be processed.",
"parameters": [
{
"type": "integer",
"description": "id",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/domain.DiscordWebhookResponse"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/domain.DiscordWebhookResponse"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/domain.DiscordWebhookResponse"
}
}
}
},
"delete": { "delete": {
"security": [ "security": [
{ {
@ -419,7 +378,7 @@
} }
} }
}, },
"/v1/discord/webhooks/{id}/disable": { "/v1/discord/webhooks/{ID}/disable": {
"post": { "post": {
"security": [ "security": [
{ {
@ -461,7 +420,7 @@
} }
} }
}, },
"/v1/discord/webhooks/{id}/enable": { "/v1/discord/webhooks/{ID}/enable": {
"post": { "post": {
"security": [ "security": [
{ {
@ -503,6 +462,51 @@
} }
} }
}, },
"/v1/discord/webhooks/{id}": {
"get": {
"security": [
{
"Bearer": []
}
],
"produces": [
"application/json"
],
"tags": [
"DiscordWebhook"
],
"summary": "Returns the top 100 entries from the queue to be processed.",
"parameters": [
{
"type": "integer",
"description": "id",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/domain.DiscordWebhookResponse"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/domain.DiscordWebhookResponse"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/domain.DiscordWebhookResponse"
}
}
}
}
},
"/v1/sources": { "/v1/sources": {
"get": { "get": {
"security": [ "security": [
@ -1499,5 +1503,13 @@
} }
} }
} }
},
"securityDefinitions": {
"Bearer": {
"description": "Type \"Bearer\" followed by a space and JWT token.",
"type": "apiKey",
"name": "Authorization",
"in": "header"
}
} }
} }

View File

@ -1,3 +1,4 @@
basePath: /api
definitions: definitions:
domain.ArticleAndSourceModel: domain.ArticleAndSourceModel:
properties: properties:
@ -153,6 +154,8 @@ definitions:
type: object type: object
info: info:
contact: {} contact: {}
title: NewsBot collector
version: "0.1"
paths: paths:
/v1/articles: /v1/articles:
get: get:
@ -160,8 +163,7 @@ paths:
- description: page number - description: page number
in: query in: query
name: page name: page
required: true type: string
type: integer
produces: produces:
- application/json - application/json
responses: responses:
@ -182,12 +184,12 @@ paths:
summary: Lists the top 25 records ordering from newest to oldest. summary: Lists the top 25 records ordering from newest to oldest.
tags: tags:
- Articles - Articles
/v1/articles/{id}: /v1/articles/{ID}:
get: get:
parameters: parameters:
- description: int - description: int
in: path in: path
name: id name: ID
required: true required: true
type: string type: string
produces: produces:
@ -210,12 +212,12 @@ paths:
summary: Returns an article based on defined ID. summary: Returns an article based on defined ID.
tags: tags:
- Articles - Articles
/v1/articles/{id}/details: /v1/articles/{ID}/details:
get: get:
parameters: parameters:
- description: int - description: int
in: path in: path
name: id name: ID
required: true required: true
type: string type: string
produces: produces:
@ -238,11 +240,11 @@ paths:
summary: Returns an article and source based on defined ID. summary: Returns an article and source based on defined ID.
tags: tags:
- Articles - Articles
/v1/articles/by/source/{id}: /v1/articles/by/sourceId:
get: get:
parameters: parameters:
- description: source id - description: source id
in: path in: query
name: id name: id
required: true required: true
type: string type: string
@ -293,7 +295,7 @@ paths:
summary: Returns the top 100 summary: Returns the top 100
tags: tags:
- DiscordWebhook - DiscordWebhook
/v1/discord/webhooks/{id}: /v1/discord/webhooks/{ID}:
delete: delete:
parameters: parameters:
- description: id - description: id
@ -319,34 +321,7 @@ paths:
summary: Deletes a record by ID. summary: Deletes a record by ID.
tags: tags:
- DiscordWebhook - DiscordWebhook
get: /v1/discord/webhooks/{ID}/disable:
parameters:
- description: id
in: path
name: id
required: true
type: integer
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/domain.DiscordWebhookResponse'
"400":
description: Bad Request
schema:
$ref: '#/definitions/domain.DiscordWebhookResponse'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/domain.DiscordWebhookResponse'
security:
- Bearer: []
summary: Returns the top 100 entries from the queue to be processed.
tags:
- DiscordWebhook
/v1/discord/webhooks/{id}/disable:
post: post:
parameters: parameters:
- description: id - description: id
@ -372,7 +347,7 @@ paths:
summary: Disables a Webhook from being used. summary: Disables a Webhook from being used.
tags: tags:
- DiscordWebhook - DiscordWebhook
/v1/discord/webhooks/{id}/enable: /v1/discord/webhooks/{ID}/enable:
post: post:
parameters: parameters:
- description: id - description: id
@ -398,6 +373,34 @@ paths:
summary: Enables a source to continue processing. summary: Enables a source to continue processing.
tags: tags:
- DiscordWebhook - DiscordWebhook
/v1/discord/webhooks/{id}:
get:
parameters:
- description: id
in: path
name: id
required: true
type: integer
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/domain.DiscordWebhookResponse'
"400":
description: Bad Request
schema:
$ref: '#/definitions/domain.DiscordWebhookResponse'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/domain.DiscordWebhookResponse'
security:
- Bearer: []
summary: Returns the top 100 entries from the queue to be processed.
tags:
- DiscordWebhook
/v1/discord/webhooks/by/serverAndChannel: /v1/discord/webhooks/by/serverAndChannel:
get: get:
parameters: parameters:
@ -942,4 +945,10 @@ paths:
summary: Adds a new scope to a user account summary: Adds a new scope to a user account
tags: tags:
- Users - Users
securityDefinitions:
Bearer:
description: Type "Bearer" followed by a space and JWT token.
in: header
name: Authorization
type: apiKey
swagger: "2.0" swagger: "2.0"

View File

@ -12,7 +12,7 @@ import (
// ListArticles // ListArticles
// @Summary Lists the top 25 records ordering from newest to oldest. // @Summary Lists the top 25 records ordering from newest to oldest.
// @Produce application/json // @Produce application/json
// @Param page query int true "page number" test // @Param page query string false "page number"
// @Tags Articles // @Tags Articles
// @Router /v1/articles [get] // @Router /v1/articles [get]
// @Success 200 {object} domain.ArticleResponse // @Success 200 {object} domain.ArticleResponse
@ -49,10 +49,10 @@ func (s *Handler) listArticles(c echo.Context) error {
// GetArticle // GetArticle
// @Summary Returns an article based on defined ID. // @Summary Returns an article based on defined ID.
// @Param id path string true "int" // @Param ID path string true "int"
// @Produce application/json // @Produce application/json
// @Tags Articles // @Tags Articles
// @Router /v1/articles/{id} [get] // @Router /v1/articles/{ID} [get]
// @Success 200 {object} domain.ArticleResponse "OK" // @Success 200 {object} domain.ArticleResponse "OK"
// @Failure 400 {object} domain.BaseResponse // @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse // @Failure 500 {object} domain.BaseResponse
@ -91,10 +91,10 @@ func (s *Handler) getArticle(c echo.Context) error {
// GetArticleDetails // GetArticleDetails
// @Summary Returns an article and source based on defined ID. // @Summary Returns an article and source based on defined ID.
// @Param id path string true "int" // @Param ID path string true "int"
// @Produce application/json // @Produce application/json
// @Tags Articles // @Tags Articles
// @Router /v1/articles/{id}/details [get] // @Router /v1/articles/{ID}/details [get]
// @Success 200 {object} domain.ArticleDetailedResponse "OK" // @Success 200 {object} domain.ArticleDetailedResponse "OK"
// @Failure 400 {object} domain.BaseResponse // @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse // @Failure 500 {object} domain.BaseResponse
@ -136,11 +136,11 @@ func (s *Handler) getArticleDetails(c echo.Context) error {
// ListArticlesBySourceID // ListArticlesBySourceID
// @Summary Finds the articles based on the SourceID provided. Returns the top 25. // @Summary Finds the articles based on the SourceID provided. Returns the top 25.
// @Param id path string true "source id" // @Param id query string true "source id"
// @Param page query int false "Page to query" // @Param page query int false "Page to query"
// @Produce application/json // @Produce application/json
// @Tags Articles // @Tags Articles
// @Router /v1/articles/by/source/{id} [get] // @Router /v1/articles/by/sourceId [get]
// @Success 200 {object} domain.ArticleResponse "OK" // @Success 200 {object} domain.ArticleResponse "OK"
// @Failure 400 {object} domain.BaseResponse // @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse // @Failure 500 {object} domain.BaseResponse

View File

@ -211,7 +211,7 @@ func (s *Handler) NewDiscordWebHook(c echo.Context) error {
// @Summary Disables a Webhook from being used. // @Summary Disables a Webhook from being used.
// @Param id path int true "id" // @Param id path int true "id"
// @Tags DiscordWebhook // @Tags DiscordWebhook
// @Router /v1/discord/webhooks/{id}/disable [post] // @Router /v1/discord/webhooks/{ID}/disable [post]
// @Success 200 {object} domain.DiscordWebhookResponse "OK" // @Success 200 {object} domain.DiscordWebhookResponse "OK"
// @Failure 400 {object} domain.DiscordWebhookResponse // @Failure 400 {object} domain.DiscordWebhookResponse
// @Failure 500 {object} domain.DiscordWebhookResponse // @Failure 500 {object} domain.DiscordWebhookResponse
@ -279,7 +279,7 @@ func (s *Handler) disableDiscordWebHook(c echo.Context) error {
// @Summary Enables a source to continue processing. // @Summary Enables a source to continue processing.
// @Param id path int true "id" // @Param id path int true "id"
// @Tags DiscordWebhook // @Tags DiscordWebhook
// @Router /v1/discord/webhooks/{id}/enable [post] // @Router /v1/discord/webhooks/{ID}/enable [post]
// @Success 200 {object} domain.DiscordWebhookResponse "OK" // @Success 200 {object} domain.DiscordWebhookResponse "OK"
// @Failure 400 {object} domain.DiscordWebhookResponse // @Failure 400 {object} domain.DiscordWebhookResponse
// @Failure 500 {object} domain.DiscordWebhookResponse // @Failure 500 {object} domain.DiscordWebhookResponse
@ -345,7 +345,7 @@ func (s *Handler) enableDiscordWebHook(c echo.Context) error {
// @Summary Deletes a record by ID. // @Summary Deletes a record by ID.
// @Param id path string true "id" // @Param id path string true "id"
// @Tags DiscordWebhook // @Tags DiscordWebhook
// @Router /v1/discord/webhooks/{id} [delete] // @Router /v1/discord/webhooks/{ID} [delete]
// @Success 200 {object} domain.DiscordWebhookResponse "OK" // @Success 200 {object} domain.DiscordWebhookResponse "OK"
// @Failure 400 {object} domain.DiscordWebhookResponse // @Failure 400 {object} domain.DiscordWebhookResponse
// @Failure 500 {object} domain.DiscordWebhookResponse // @Failure 500 {object} domain.DiscordWebhookResponse

View File

@ -8,7 +8,6 @@ import (
"git.jamestombleson.com/jtom38/newsbot-api/domain" "git.jamestombleson.com/jtom38/newsbot-api/domain"
"git.jamestombleson.com/jtom38/newsbot-api/internal/dtoconv" "git.jamestombleson.com/jtom38/newsbot-api/internal/dtoconv"
"git.jamestombleson.com/jtom38/newsbot-api/internal/entity"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
) )
@ -420,8 +419,7 @@ func (s *Handler) newRssSource(c echo.Context) error {
} }
param := domain.NewSourceParamRequest{} param := domain.NewSourceParamRequest{}
err = c.Bind(&param) err = (&echo.DefaultBinder{}).BindBody(c, &param)
//err = (&echo.DefaultBinder{}).BindBody(c, &param)
if err != nil { if err != nil {
p.BaseResponse.Message = err.Error() p.BaseResponse.Message = err.Error()
return c.JSON(http.StatusBadRequest, p) return c.JSON(http.StatusBadRequest, p)
@ -433,15 +431,8 @@ func (s *Handler) newRssSource(c echo.Context) error {
} }
// Check if the record already exists // Check if the record already exists
blankRecord := entity.SourceEntity{}
item, err := s.repo.Sources.GetBySourceAndName(c.Request().Context(), domain.SourceCollectorRss, param.Name) item, err := s.repo.Sources.GetBySourceAndName(c.Request().Context(), domain.SourceCollectorRss, param.Name)
if err != nil { if err == nil {
p.IsError = true
return c.JSON(http.StatusInternalServerError, p)
}
// No record was found, but no error returned
if item != blankRecord {
var dto []domain.SourceDto var dto []domain.SourceDto
dto = append(dto, dtoconv.SourceToDto(item)) dto = append(dto, dtoconv.SourceToDto(item))
p.Payload = dto p.Payload = dto
@ -449,14 +440,6 @@ func (s *Handler) newRssSource(c echo.Context) error {
return c.JSON(http.StatusOK, p) return c.JSON(http.StatusOK, p)
} }
//if err == nil {
// var dto []domain.SourceDto
// dto = append(dto, dtoconv.SourceToDto(item))
// p.Payload = dto
// p.BaseResponse.IsError = false
// return c.JSON(http.StatusOK, p)
//}
tags := fmt.Sprintf("rss, %v, %s", param.Name, param.Tags) tags := fmt.Sprintf("rss, %v, %s", param.Name, param.Tags)
rows, err := s.repo.Sources.Create(c.Request().Context(), domain.SourceCollectorRss, param.Name, param.Url, tags, true) rows, err := s.repo.Sources.Create(c.Request().Context(), domain.SourceCollectorRss, param.Name, param.Url, tags, true)
if err != nil { if err != nil {

View File

@ -121,7 +121,7 @@ func (r sourceRepository) GetBySourceAndName(ctx context.Context, source, name s
b.Select("*") b.Select("*")
b.From("Sources").Where( b.From("Sources").Where(
b.Equal("Source", source), b.Equal("Source", source),
b.Equal("DisplayName", name), b.Equal("Name", name),
) )
b.Limit(1) b.Limit(1)
query, args := b.Build() query, args := b.Build()

View File

@ -19,13 +19,8 @@ migrate-dev-down: ## revert sql migrations to dev db
goose -dir "./internal/database/migrations" sqlite3 ./cmd/newsbot.db down goose -dir "./internal/database/migrations" sqlite3 ./cmd/newsbot.db down
swag: ## Generates the swagger documentation with the swag tool swag: ## Generates the swagger documentation with the swag tool
~/go/bin/swag f -g cmd/server.go ~/go/bin/swag f
~/go/bin/swag init -g cmd/server.go ~/go/bin/swag init -g cmd/server.go
go run tools/swaggertoopenapi/main.go
oapi-codegen -config api/client.yaml docs/openapi.json
mv api.gen.go api/
install-tools: ## Installs the required tools for this project gensql: ## Generates SQL code with sqlc
go install github.com/swaggo/swag/cmd/swag@v1.8.1 sqlc generate
go install github.com/pressly/goose/v3/cmd/goose@latest
go install github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen@v2.3.0

View File

@ -1,17 +0,0 @@
package main
import (
"fmt"
"os"
"git.jamestombleson.com/jtom38/newsbot-api/tools/swaggertoopenapi/src"
)
func main() {
err := src.ConvertToOpenApi("docs/swagger.json", "docs/openapi.json", true)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}

View File

@ -1,70 +0,0 @@
package src
import (
"bytes"
"fmt"
"io"
"log"
"net/http"
"os"
)
const (
ApplicationJson = "application/json"
)
func ConvertToOpenApi(filePath, outputPath string, force bool) error {
// check if the file already exists
exists, err := os.Stat(outputPath)
if exists != nil {
// if force was given, attempt to remove it
if force {
err = os.Remove(outputPath)
if err != nil {
return err
}
} else {
return fmt.Errorf("'%s' already exists and force was not approved", outputPath)
}
}
log.Printf("Reading '%s'", filePath)
content, err := os.ReadFile(filePath)
if err != nil {
return err
}
client := http.Client{}
req, err := http.NewRequest(http.MethodPost, "https://converter.swagger.io/api/convert", bytes.NewReader(content))
if err != nil {
return err
}
req.Header.Add("Accept", ApplicationJson)
req.Header.Add("Content-Type", ApplicationJson)
log.Println("Converting to OpenAPI spec")
resp, err := client.Do(req)
if err != nil {
return err
}
log.Println("Reading the response")
respContent, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
log.Println("Writing converted data to openapi.json")
writer, err := os.Create(outputPath)
if err != nil {
return err
}
defer writer.Close()
_, err = writer.Write(respContent)
if err != nil {
return err
}
return nil
}

View File

@ -1,14 +0,0 @@
package src_test
import (
"testing"
"git.jamestombleson.com/jtom38/newsbot-api/tools/swaggerToOpenapi/src"
)
func TestConvertOnline(t *testing.T) {
err := src.ConvertToOpenApi("../../docs/swagger.json", "../../docs/openapi.json", true)
if err != nil {
t.FailNow()
}
}