First local go service
This commit is contained in:
parent
1506a57231
commit
0d3fae2ca5
8
api/.idea/.gitignore
vendored
Normal file
8
api/.idea/.gitignore
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
9
api/.idea/api.iml
Normal file
9
api/.idea/api.iml
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="WEB_MODULE" version="4">
|
||||||
|
<component name="Go" enabled="true" />
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$" />
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
8
api/.idea/modules.xml
Normal file
8
api/.idea/modules.xml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/api.iml" filepath="$PROJECT_DIR$/.idea/api.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
6
api/.idea/vcs.xml
Normal file
6
api/.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
@ -5,4 +5,5 @@ go 1.17
|
|||||||
require (
|
require (
|
||||||
github.com/go-chi/chi v1.5.4 // indirect
|
github.com/go-chi/chi v1.5.4 // indirect
|
||||||
github.com/go-chi/render v1.0.1 // indirect
|
github.com/go-chi/render v1.0.1 // indirect
|
||||||
|
github.com/google/uuid v1.3.0 // indirect
|
||||||
)
|
)
|
||||||
|
@ -2,3 +2,5 @@ github.com/go-chi/chi v1.5.4 h1:QHdzF2szwjqVV4wmByUnTcsbIg7UGaQ0tPF2t5GcAIs=
|
|||||||
github.com/go-chi/chi v1.5.4/go.mod h1:uaf8YgoFazUOkPBG7fxPftUylNumIev9awIWOENIuEg=
|
github.com/go-chi/chi v1.5.4/go.mod h1:uaf8YgoFazUOkPBG7fxPftUylNumIev9awIWOENIuEg=
|
||||||
github.com/go-chi/render v1.0.1 h1:4/5tis2cKaNdnv9zFLfXzcquC9HbeZgCnxGnKrltBS8=
|
github.com/go-chi/render v1.0.1 h1:4/5tis2cKaNdnv9zFLfXzcquC9HbeZgCnxGnKrltBS8=
|
||||||
github.com/go-chi/render v1.0.1/go.mod h1:pq4Rr7HbnsdaeHagklXub+p6Wd16Af5l9koip1OvJns=
|
github.com/go-chi/render v1.0.1/go.mod h1:pq4Rr7HbnsdaeHagklXub+p6Wd16Af5l9koip1OvJns=
|
||||||
|
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||||
|
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
@ -27,3 +27,7 @@ func ErrInvalidRequest(err error) render.Renderer {
|
|||||||
ErrorText: err.Error(),
|
ErrorText: err.Error(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ErrNotFound() render.Renderer {
|
||||||
|
return &ErrResponse{HTTPStatusCode: 404, StatusText: "Resource not found."}
|
||||||
|
}
|
||||||
|
@ -1,42 +0,0 @@
|
|||||||
package download
|
|
||||||
|
|
||||||
import (
|
|
||||||
"downloader/internal/app/api/common/responses"
|
|
||||||
"errors"
|
|
||||||
"github.com/go-chi/chi"
|
|
||||||
"github.com/go-chi/render"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
type api struct{}
|
|
||||||
|
|
||||||
func New() *api {
|
|
||||||
return &api{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (api *api) SetupDownloadApi(router *chi.Mux) {
|
|
||||||
router.Route("/downloads", func(r chi.Router) {
|
|
||||||
r.Post("/", api.requestDownload)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
type requestDownloadRequest struct {
|
|
||||||
Provider string `json:"provider"`
|
|
||||||
Link string `json:"link"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (dr *requestDownloadRequest) Bind(r *http.Request) error {
|
|
||||||
if dr.Link == "" || dr.Provider == "" {
|
|
||||||
return errors.New("missing required download request field")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (api *api) requestDownload(w http.ResponseWriter, r *http.Request) {
|
|
||||||
data := &requestDownloadRequest{}
|
|
||||||
if err := render.Bind(r, data); err != nil {
|
|
||||||
_ = render.Render(w, r, responses.ErrInvalidRequest(err))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
22
api/internal/app/api/download/download_context.go
Normal file
22
api/internal/app/api/download/download_context.go
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package download
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"downloader/internal/app/api/common/responses"
|
||||||
|
"github.com/go-chi/chi"
|
||||||
|
"github.com/go-chi/render"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Context(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if downloadId := chi.URLParam(r, "downloadId"); downloadId != "" {
|
||||||
|
ctx := context.WithValue(r.Context(), "downloadId", downloadId)
|
||||||
|
next.ServeHTTP(w, r.WithContext(ctx))
|
||||||
|
} else {
|
||||||
|
_ = render.Render(w, r, responses.ErrNotFound())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
66
api/internal/app/api/download/request_download.go
Normal file
66
api/internal/app/api/download/request_download.go
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
package download
|
||||||
|
|
||||||
|
import (
|
||||||
|
"downloader/internal/app/api/common/responses"
|
||||||
|
"downloader/internal/core/entities"
|
||||||
|
"errors"
|
||||||
|
"github.com/go-chi/render"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type requestDownloadRequest struct {
|
||||||
|
Provider string `json:"provider"`
|
||||||
|
Link string `json:"link"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type requestDownloadResponse struct {
|
||||||
|
*entities.Download
|
||||||
|
}
|
||||||
|
|
||||||
|
func (_ requestDownloadResponse) Render(_ http.ResponseWriter, _ *http.Request) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dr *requestDownloadRequest) Bind(r *http.Request) error {
|
||||||
|
if dr.Link == "" || dr.Provider == "" {
|
||||||
|
return errors.New("missing required download request field")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *api) requestDownload(w http.ResponseWriter, r *http.Request) {
|
||||||
|
data := &requestDownloadRequest{}
|
||||||
|
if err := render.Bind(r, data); err != nil {
|
||||||
|
_ = render.Render(w, r, responses.ErrInvalidRequest(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
download, err := a.drService.Schedule(data.Provider, data.Link)
|
||||||
|
if err != nil {
|
||||||
|
_ = render.Render(w, r, responses.ErrInvalidRequest(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
render.Status(r, http.StatusAccepted)
|
||||||
|
_ = render.Render(w, r, newRequestDownloadResponse(download))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *api) getDownloadById(w http.ResponseWriter, r *http.Request) {
|
||||||
|
downloadId := r.Context().Value("downloadId").(string)
|
||||||
|
|
||||||
|
download, err := a.drService.Get(downloadId)
|
||||||
|
if err != nil {
|
||||||
|
_ = render.Render(w, r, responses.ErrNotFound())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := render.Render(w, r, newRequestDownloadResponse(download)); err != nil {
|
||||||
|
_ = render.Render(w, r, responses.ErrInvalidRequest(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func newRequestDownloadResponse(download *entities.Download) *requestDownloadResponse {
|
||||||
|
return &requestDownloadResponse{Download: download}
|
||||||
|
}
|
24
api/internal/app/api/download/routes.go
Normal file
24
api/internal/app/api/download/routes.go
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package download
|
||||||
|
|
||||||
|
import (
|
||||||
|
"downloader/internal/core/ports/download_request"
|
||||||
|
"github.com/go-chi/chi"
|
||||||
|
)
|
||||||
|
|
||||||
|
type api struct {
|
||||||
|
drService download_request.Service
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(service download_request.Service) *api {
|
||||||
|
return &api{drService: service}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *api) SetupDownloadApi(router *chi.Mux) {
|
||||||
|
router.Route("/downloads", func(r chi.Router) {
|
||||||
|
r.Post("/", a.requestDownload)
|
||||||
|
r.Route("/{downloadId}", func(r chi.Router) {
|
||||||
|
r.Use(Context)
|
||||||
|
r.Get("/", a.getDownloadById)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
@ -2,6 +2,8 @@ package router
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"downloader/internal/app/api/download"
|
"downloader/internal/app/api/download"
|
||||||
|
"downloader/internal/core/ports/download_request"
|
||||||
|
"downloader/pkg/common/uuid"
|
||||||
"github.com/go-chi/chi"
|
"github.com/go-chi/chi"
|
||||||
"github.com/go-chi/chi/middleware"
|
"github.com/go-chi/chi/middleware"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -23,7 +25,7 @@ func NewRouter() *router {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (router *router) Run() {
|
func (router *router) Run() {
|
||||||
http.ListenAndServe(":3333", router.internalRouter)
|
_ = http.ListenAndServe(":3333", router.internalRouter)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (router *router) RegisterApi() *chi.Mux {
|
func (router *router) RegisterApi() *chi.Mux {
|
||||||
@ -41,9 +43,17 @@ func (router *router) setupMiddleware() *router {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (router *router) setupRoutes() *router {
|
func (router *router) setupRoutes() *router {
|
||||||
downloadApi := download.New()
|
setupDownloadRoute(router)
|
||||||
|
|
||||||
downloadApi.SetupDownloadApi(router.internalRouter)
|
|
||||||
|
|
||||||
return router
|
return router
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setupDownloadRoute(router *router) {
|
||||||
|
drRepository := download_request.NewInMemoryRepository()
|
||||||
|
drBackgroundService := download_request.NewLocalBackgroundService(drRepository)
|
||||||
|
gen := uuid.New()
|
||||||
|
|
||||||
|
drService := download_request.NewLocalService(drRepository, gen, drBackgroundService)
|
||||||
|
downloadApi := download.New(drService)
|
||||||
|
downloadApi.SetupDownloadApi(router.internalRouter)
|
||||||
|
}
|
||||||
|
@ -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"`
|
|
||||||
}
|
|
28
api/internal/core/entities/download.go
Normal file
28
api/internal/core/entities/download.go
Normal 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
|
||||||
|
}
|
||||||
|
}
|
@ -3,11 +3,16 @@ package download_request
|
|||||||
import "downloader/internal/core/entities"
|
import "downloader/internal/core/entities"
|
||||||
|
|
||||||
type Service interface {
|
type Service interface {
|
||||||
Schedule(provider string, link string) (entities.Download, error)
|
Schedule(provider string, link string) (*entities.Download, error)
|
||||||
Get(id string) (entities.Download, error)
|
Get(id string) (*entities.Download, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Repository interface {
|
type Repository interface {
|
||||||
Create(download entities.Download) (entities.Download, error)
|
Create(download *entities.Download) (*entities.Download, error)
|
||||||
GetById(id string) (entities.Download, error)
|
GetById(id string) (*entities.Download, error)
|
||||||
|
Update(download *entities.Download) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type BackgroundService interface {
|
||||||
|
Run(download *entities.Download) error
|
||||||
}
|
}
|
||||||
|
110
api/internal/core/ports/download_request/in_memory.go
Normal file
110
api/internal/core/ports/download_request/in_memory.go
Normal 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
|
||||||
|
}
|
18
api/pkg/common/uuid/uuid.go
Normal file
18
api/pkg/common/uuid/uuid.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package uuid
|
||||||
|
|
||||||
|
import "github.com/google/uuid"
|
||||||
|
|
||||||
|
type Gen interface {
|
||||||
|
Create() string
|
||||||
|
}
|
||||||
|
|
||||||
|
type uuidGen struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func New() *uuidGen {
|
||||||
|
return &uuidGen{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u uuidGen) Create() string {
|
||||||
|
return uuid.New().String()
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user