First local go service
This commit is contained in:
@@ -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."}
|
||||
}
|
||||
|
@@ -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)
|
||||
})
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user