Skip to content

Commit cf9716b

Browse files
authored
Merge pull request #26 from rog-golang-buddies/base_infrastrastructure
Base infrastrastructure
2 parents 3b80382 + d100dbe commit cf9716b

24 files changed

+634
-34
lines changed

internal/app.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ import (
44
"context"
55
"fmt"
66
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/config"
7+
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/load"
78
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/logger"
9+
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/parse"
10+
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/process"
811
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/queue"
912
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/queue/handler"
1013
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/queue/publisher"
14+
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/recognize"
1115
)
1216

1317
func Start() int {
@@ -24,6 +28,12 @@ func Start() int {
2428
fmt.Println("error creating logger: ", err)
2529
return 1
2630
}
31+
32+
proc, err := createDefaultProcessor()
33+
if err != nil {
34+
log.Error("error while creating processor: ", err)
35+
return 1
36+
}
2737
//initialize publisher connection to the queue
2838
//this library assumes using one publisher and one consumer per application
2939
//https://github.com/wagslane/go-rabbitmq/issues/79
@@ -41,9 +51,9 @@ func Start() int {
4151
}
4252
defer queue.CloseConsumer(consumer, log)
4353

44-
handl := handler.NewApiSpecDocHandler(pub, conf.Queue, log)
54+
handl := handler.NewApiSpecDocHandler(pub, conf.Queue, proc, log)
4555
listener := queue.NewListener()
46-
err = listener.Start(consumer, &conf.Queue, handl)
56+
err = listener.Start(ctx, consumer, &conf.Queue, handl)
4757
if err != nil {
4858
log.Error("error while listening queue ", err)
4959
return 1
@@ -54,3 +64,12 @@ func Start() int {
5464
log.Info("application stopped gracefully (not)")
5565
return 0
5666
}
67+
68+
func createDefaultProcessor() (process.UrlProcessor, error) {
69+
recognizer := recognize.NewRecognizer()
70+
parsers := []parse.Parser{parse.NewJsonOpenApiParser(), parse.NewYamlOpenApiParser()}
71+
converter := parse.NewConverter(parsers)
72+
loader := load.NewContentLoader()
73+
74+
return process.NewProcessor(recognizer, converter, loader)
75+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package fileresource
2+
3+
// FileResource representation of file resource
4+
type FileResource struct {
5+
//File name if exists, else empty
6+
Name string
7+
8+
//Original link to file
9+
Link string
10+
11+
//File content
12+
Content []byte
13+
14+
//Type of the API specification file (json/yaml ...)
15+
Type AsdFileType
16+
}

internal/dto/fileresource/fileType.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package fileresource
2+
3+
type AsdFileType int
4+
5+
const (
6+
Undefined AsdFileType = iota
7+
YamlOpenApi
8+
JsonOpenAPI
9+
)

internal/dto/scrapingResult.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ import "github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/dto
55
type ScrapingResult struct {
66
IsNotifyUser bool
77

8-
ApiSpecDoc apiSpecDoc.ApiSpecDoc
8+
ApiSpecDoc *apiSpecDoc.ApiSpecDoc
99
}

internal/load/contentLoader.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package load
2+
3+
import (
4+
"context"
5+
"errors"
6+
7+
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/dto/fileresource"
8+
)
9+
10+
//ContentLoader loads content by url
11+
//go:generate mockgen -source=contentLoader.go -destination=./mocks/contentLoader.go -package=load
12+
type ContentLoader interface {
13+
Load(ctx context.Context, url string) (*fileresource.FileResource, error)
14+
}
15+
16+
type ContentLoaderImpl struct {
17+
}
18+
19+
// 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.
20+
func (cl *ContentLoaderImpl) Load(ctx context.Context, url string) (*fileresource.FileResource, error) {
21+
//load content by url
22+
return nil, errors.New("not implemented")
23+
}
24+
25+
func NewContentLoader() ContentLoader {
26+
return &ContentLoaderImpl{}
27+
}

internal/load/mocks/contentLoader.go

+51
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/parse/converter.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package parse
2+
3+
import (
4+
"errors"
5+
6+
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/dto/apiSpecDoc"
7+
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/dto/fileresource"
8+
)
9+
10+
// Converter converts file data to API specification document using specific file type
11+
//
12+
//go:generate mockgen -source=converter.go -destination=./mocks/converter.go -package=parse
13+
type Converter interface {
14+
Convert(file *fileresource.FileResource) (*apiSpecDoc.ApiSpecDoc, error)
15+
}
16+
17+
type ConverterImpl struct {
18+
//For instance, we may have a map to hold parsers for different types. And populate it in NewConverter
19+
parsers map[fileresource.AsdFileType]Parser
20+
}
21+
22+
// Convert gets bytes slice with json/yaml content and a filetype matching the type of the content and returns parsed ApiSpecDoc.
23+
func (c *ConverterImpl) Convert(file *fileresource.FileResource) (*apiSpecDoc.ApiSpecDoc, error) {
24+
//Just example
25+
parser, ok := c.parsers[file.Type]
26+
if !ok {
27+
return nil, errors.New("file type not supported")
28+
}
29+
apiSpec, err := parser.parse(file.Content)
30+
if err != nil {
31+
return nil, err
32+
}
33+
34+
return apiSpec, nil
35+
}
36+
37+
func NewConverter(parsers []Parser) Converter {
38+
parsersMap := make(map[fileresource.AsdFileType]Parser)
39+
for _, parser := range parsers {
40+
parsersMap[parser.getType()] = parser
41+
}
42+
return &ConverterImpl{
43+
parsers: parsersMap,
44+
}
45+
}

internal/parse/converter_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package parse
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestNewConverter(t *testing.T) {
9+
parsers := []Parser{NewYamlOpenApiParser(), NewJsonOpenApiParser()}
10+
converter := NewConverter(parsers)
11+
assert.NotNil(t, converter)
12+
}

internal/parse/jsonOpenApiParser.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package parse
2+
3+
import (
4+
"errors"
5+
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/dto/apiSpecDoc"
6+
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/dto/fileresource"
7+
)
8+
9+
//JsonOpenApiParser implementation for parsing json open API files
10+
type JsonOpenApiParser struct {
11+
}
12+
13+
func (joap *JsonOpenApiParser) parse(content []byte) (*apiSpecDoc.ApiSpecDoc, error) {
14+
return nil, errors.New("not implemented")
15+
}
16+
17+
func (joap *JsonOpenApiParser) getType() fileresource.AsdFileType {
18+
return fileresource.JsonOpenAPI
19+
}
20+
21+
func NewJsonOpenApiParser() Parser {
22+
return &JsonOpenApiParser{}
23+
}

internal/parse/mocks/converter.go

+51
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/parse/parser.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package parse
2+
3+
import (
4+
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/dto/apiSpecDoc"
5+
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/dto/fileresource"
6+
)
7+
8+
//Parser is common interface with functionality
9+
//to parse content of the specific API specification document
10+
//and to construct ApiSpecDoc object from it
11+
type Parser interface {
12+
// parses the bytes slice to a ApiSecDoc
13+
parse(content []byte) (*apiSpecDoc.ApiSpecDoc, error)
14+
15+
// returns the type (json or yaml) of the parser
16+
getType() fileresource.AsdFileType
17+
}

internal/parse/yamlOpenApiParser.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package parse
2+
3+
import (
4+
"errors"
5+
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/dto/apiSpecDoc"
6+
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/dto/fileresource"
7+
)
8+
9+
//YamlOpenApiParser implementation for parsing yml open API files
10+
type YamlOpenApiParser struct {
11+
}
12+
13+
func (yoap *YamlOpenApiParser) parse(content []byte) (*apiSpecDoc.ApiSpecDoc, error) {
14+
return nil, errors.New("not implemented")
15+
}
16+
17+
func (yoap *YamlOpenApiParser) getType() fileresource.AsdFileType {
18+
return fileresource.JsonOpenAPI
19+
}
20+
21+
func NewYamlOpenApiParser() Parser {
22+
return &YamlOpenApiParser{}
23+
}

internal/process/mocks/processor.go

+51
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)