First local go service

This commit is contained in:
2021-12-21 02:18:11 +01:00
parent 1506a57231
commit 0d3fae2ca5
17 changed files with 329 additions and 58 deletions

View File

@@ -1,8 +0,0 @@
package entities
type Download struct {
ID string `json:"id"`
Status string `json:"status"`
Link string `json:"link"`
Data string `json:"data"`
}

View File

@@ -0,0 +1,28 @@
package entities
import (
"downloader/pkg/common/uuid"
"errors"
)
type Download struct {
ID string `json:"id"`
Status string `json:"status"`
Link string `json:"link"`
Data string `json:"data"`
}
func NewDownload(link string, data string) func(uuidGen uuid.Gen) (*Download, error) {
return func(uuidGen uuid.Gen) (*Download, error) {
if link == "" || data == "" {
return nil, errors.New("A field was not valid")
}
return &Download{
ID: uuidGen.Create(),
Status: "scheduled",
Link: link,
Data: data,
}, nil
}
}

View File

@@ -3,11 +3,16 @@ package download_request
import "downloader/internal/core/entities"
type Service interface {
Schedule(provider string, link string) (entities.Download, error)
Get(id string) (entities.Download, error)
Schedule(provider string, link string) (*entities.Download, error)
Get(id string) (*entities.Download, error)
}
type Repository interface {
Create(download entities.Download) (entities.Download, error)
GetById(id string) (entities.Download, error)
Create(download *entities.Download) (*entities.Download, error)
GetById(id string) (*entities.Download, error)
Update(download *entities.Download) error
}
type BackgroundService interface {
Run(download *entities.Download) error
}

View File

@@ -0,0 +1,110 @@
package download_request
import (
"downloader/internal/core/entities"
"downloader/pkg/common/uuid"
"errors"
"fmt"
"time"
)
// Repository
type inMemoryRepository struct {
collection map[string]*entities.Download
}
func NewInMemoryRepository() Repository {
return &inMemoryRepository{collection: make(map[string]*entities.Download)}
}
func (i *inMemoryRepository) Create(download *entities.Download) (*entities.Download, error) {
if doc := i.collection[download.ID]; doc != nil {
return nil, errors.New("download request already exists")
}
i.collection[download.ID] = download
return download, nil
}
func (i *inMemoryRepository) Update(download *entities.Download) error {
if doc := i.collection[download.ID]; doc == nil {
return errors.New("download request doesn't exist exists")
}
i.collection[download.ID] = download
return nil
}
func (i *inMemoryRepository) GetById(id string) (*entities.Download, error) {
if download := i.collection[id]; download != nil {
return download, nil
} else {
return nil, errors.New("download was not found in the database")
}
}
// Service
type localService struct {
repository Repository
uuidGen uuid.Gen
BackgroundService BackgroundService
}
func NewLocalService(repository Repository, uuidGen uuid.Gen, backgroundService BackgroundService) Service {
return &localService{
repository: repository,
uuidGen: uuidGen,
BackgroundService: backgroundService,
}
}
func (l *localService) Schedule(provider string, link string) (*entities.Download, error) {
download, err := entities.NewDownload(link, provider)(l.uuidGen)
if err != nil {
return nil, err
}
persistedDownloadRequest, uploadErr := l.repository.Create(download)
if uploadErr != nil {
return nil, uploadErr
}
err = l.BackgroundService.Run(persistedDownloadRequest)
if err != nil {
return nil, err
}
return persistedDownloadRequest, nil
}
func (l *localService) Get(id string) (*entities.Download, error) {
return l.repository.GetById(id)
}
// Background service
type localBackgroundService struct {
repository Repository
}
func NewLocalBackgroundService(repository Repository) BackgroundService {
return &localBackgroundService{repository: repository}
}
func (l localBackgroundService) Run(download *entities.Download) error {
go func() {
time.Sleep(time.Second * 5)
download.Status = "done"
err := l.repository.Update(download)
if err != nil {
fmt.Printf("Download request: %s failed\n", download.ID)
panic(err)
} else {
fmt.Printf("Download request: %s done\n", download.ID)
}
}()
return nil
}