Skip to content

Commit

Permalink
Merge pull request #26 from rog-golang-buddies/base_infrastrastructure
Browse files Browse the repository at this point in the history
Base infrastrastructure
  • Loading branch information
ldmi3i authored Aug 13, 2022
2 parents 3b80382 + d100dbe commit cf9716b
Show file tree
Hide file tree
Showing 24 changed files with 634 additions and 34 deletions.
23 changes: 21 additions & 2 deletions internal/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import (
"context"
"fmt"
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/config"
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/load"
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/logger"
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/parse"
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/process"
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/queue"
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/queue/handler"
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/queue/publisher"
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/recognize"
)

func Start() int {
Expand All @@ -24,6 +28,12 @@ func Start() int {
fmt.Println("error creating logger: ", err)
return 1
}

proc, err := createDefaultProcessor()
if err != nil {
log.Error("error while creating processor: ", err)
return 1
}
//initialize publisher connection to the queue
//this library assumes using one publisher and one consumer per application
//https://github.com/wagslane/go-rabbitmq/issues/79
Expand All @@ -41,9 +51,9 @@ func Start() int {
}
defer queue.CloseConsumer(consumer, log)

handl := handler.NewApiSpecDocHandler(pub, conf.Queue, log)
handl := handler.NewApiSpecDocHandler(pub, conf.Queue, proc, log)
listener := queue.NewListener()
err = listener.Start(consumer, &conf.Queue, handl)
err = listener.Start(ctx, consumer, &conf.Queue, handl)
if err != nil {
log.Error("error while listening queue ", err)
return 1
Expand All @@ -54,3 +64,12 @@ func Start() int {
log.Info("application stopped gracefully (not)")
return 0
}

func createDefaultProcessor() (process.UrlProcessor, error) {
recognizer := recognize.NewRecognizer()
parsers := []parse.Parser{parse.NewJsonOpenApiParser(), parse.NewYamlOpenApiParser()}
converter := parse.NewConverter(parsers)
loader := load.NewContentLoader()

return process.NewProcessor(recognizer, converter, loader)
}
16 changes: 16 additions & 0 deletions internal/dto/fileresource/fileResource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package fileresource

// FileResource representation of file resource
type FileResource struct {
//File name if exists, else empty
Name string

//Original link to file
Link string

//File content
Content []byte

//Type of the API specification file (json/yaml ...)
Type AsdFileType
}
9 changes: 9 additions & 0 deletions internal/dto/fileresource/fileType.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package fileresource

type AsdFileType int

const (
Undefined AsdFileType = iota
YamlOpenApi
JsonOpenAPI
)
2 changes: 1 addition & 1 deletion internal/dto/scrapingResult.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import "github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/dto
type ScrapingResult struct {
IsNotifyUser bool

ApiSpecDoc apiSpecDoc.ApiSpecDoc
ApiSpecDoc *apiSpecDoc.ApiSpecDoc
}
27 changes: 27 additions & 0 deletions internal/load/contentLoader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package load

import (
"context"
"errors"

"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/dto/fileresource"
)

//ContentLoader loads content by url
//go:generate mockgen -source=contentLoader.go -destination=./mocks/contentLoader.go -package=load
type ContentLoader interface {
Load(ctx context.Context, url string) (*fileresource.FileResource, error)
}

type ContentLoaderImpl struct {
}

// Gets context and an url of a OpenApi file (Swagger file) string as parameter and returns a FileResource containing the link, optionally name and main content of the file.
func (cl *ContentLoaderImpl) Load(ctx context.Context, url string) (*fileresource.FileResource, error) {
//load content by url
return nil, errors.New("not implemented")
}

func NewContentLoader() ContentLoader {
return &ContentLoaderImpl{}
}
51 changes: 51 additions & 0 deletions internal/load/mocks/contentLoader.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions internal/parse/converter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package parse

import (
"errors"

"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/dto/apiSpecDoc"
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/dto/fileresource"
)

// Converter converts file data to API specification document using specific file type
//
//go:generate mockgen -source=converter.go -destination=./mocks/converter.go -package=parse
type Converter interface {
Convert(file *fileresource.FileResource) (*apiSpecDoc.ApiSpecDoc, error)
}

type ConverterImpl struct {
//For instance, we may have a map to hold parsers for different types. And populate it in NewConverter
parsers map[fileresource.AsdFileType]Parser
}

// Convert gets bytes slice with json/yaml content and a filetype matching the type of the content and returns parsed ApiSpecDoc.
func (c *ConverterImpl) Convert(file *fileresource.FileResource) (*apiSpecDoc.ApiSpecDoc, error) {
//Just example
parser, ok := c.parsers[file.Type]
if !ok {
return nil, errors.New("file type not supported")
}
apiSpec, err := parser.parse(file.Content)
if err != nil {
return nil, err
}

return apiSpec, nil
}

func NewConverter(parsers []Parser) Converter {
parsersMap := make(map[fileresource.AsdFileType]Parser)
for _, parser := range parsers {
parsersMap[parser.getType()] = parser
}
return &ConverterImpl{
parsers: parsersMap,
}
}
12 changes: 12 additions & 0 deletions internal/parse/converter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package parse

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestNewConverter(t *testing.T) {
parsers := []Parser{NewYamlOpenApiParser(), NewJsonOpenApiParser()}
converter := NewConverter(parsers)
assert.NotNil(t, converter)
}
23 changes: 23 additions & 0 deletions internal/parse/jsonOpenApiParser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package parse

import (
"errors"
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/dto/apiSpecDoc"
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/dto/fileresource"
)

//JsonOpenApiParser implementation for parsing json open API files
type JsonOpenApiParser struct {
}

func (joap *JsonOpenApiParser) parse(content []byte) (*apiSpecDoc.ApiSpecDoc, error) {
return nil, errors.New("not implemented")
}

func (joap *JsonOpenApiParser) getType() fileresource.AsdFileType {
return fileresource.JsonOpenAPI
}

func NewJsonOpenApiParser() Parser {
return &JsonOpenApiParser{}
}
51 changes: 51 additions & 0 deletions internal/parse/mocks/converter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions internal/parse/parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package parse

import (
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/dto/apiSpecDoc"
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/dto/fileresource"
)

//Parser is common interface with functionality
//to parse content of the specific API specification document
//and to construct ApiSpecDoc object from it
type Parser interface {
// parses the bytes slice to a ApiSecDoc
parse(content []byte) (*apiSpecDoc.ApiSpecDoc, error)

// returns the type (json or yaml) of the parser
getType() fileresource.AsdFileType
}
23 changes: 23 additions & 0 deletions internal/parse/yamlOpenApiParser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package parse

import (
"errors"
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/dto/apiSpecDoc"
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/dto/fileresource"
)

//YamlOpenApiParser implementation for parsing yml open API files
type YamlOpenApiParser struct {
}

func (yoap *YamlOpenApiParser) parse(content []byte) (*apiSpecDoc.ApiSpecDoc, error) {
return nil, errors.New("not implemented")
}

func (yoap *YamlOpenApiParser) getType() fileresource.AsdFileType {
return fileresource.JsonOpenAPI
}

func NewYamlOpenApiParser() Parser {
return &YamlOpenApiParser{}
}
51 changes: 51 additions & 0 deletions internal/process/mocks/processor.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cf9716b

Please sign in to comment.