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

@@ -27,3 +27,7 @@ func ErrInvalidRequest(err error) render.Renderer {
ErrorText: err.Error(),
}
}
func ErrNotFound() render.Renderer {
return &ErrResponse{HTTPStatusCode: 404, StatusText: "Resource not found."}
}

View File

@@ -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
}
}

View 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
}
})
}

View 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}
}

View 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)
})
})
}