Add base
This commit is contained in:
commit
1506a57231
8
.idea/.gitignore
vendored
Normal file
8
.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
.idea/downloader.iml
Normal file
9
.idea/downloader.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
.idea/modules.xml
Normal file
8
.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/downloader.iml" filepath="$PROJECT_DIR$/.idea/downloader.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
9
api/cmd/api/main.go
Normal file
9
api/cmd/api/main.go
Normal file
@ -0,0 +1,9 @@
|
||||
package main
|
||||
|
||||
import router "downloader/internal/app/router"
|
||||
|
||||
func main() {
|
||||
router.
|
||||
NewRouter().
|
||||
Run()
|
||||
}
|
8
api/go.mod
Normal file
8
api/go.mod
Normal file
@ -0,0 +1,8 @@
|
||||
module downloader
|
||||
|
||||
go 1.17
|
||||
|
||||
require (
|
||||
github.com/go-chi/chi v1.5.4 // indirect
|
||||
github.com/go-chi/render v1.0.1 // indirect
|
||||
)
|
4
api/go.sum
Normal file
4
api/go.sum
Normal file
@ -0,0 +1,4 @@
|
||||
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/render v1.0.1 h1:4/5tis2cKaNdnv9zFLfXzcquC9HbeZgCnxGnKrltBS8=
|
||||
github.com/go-chi/render v1.0.1/go.mod h1:pq4Rr7HbnsdaeHagklXub+p6Wd16Af5l9koip1OvJns=
|
29
api/internal/app/api/common/responses/responses.go
Normal file
29
api/internal/app/api/common/responses/responses.go
Normal file
@ -0,0 +1,29 @@
|
||||
package responses
|
||||
|
||||
import (
|
||||
"github.com/go-chi/render"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type ErrResponse struct {
|
||||
Err error `json:"-"` // low-level runtime error
|
||||
HTTPStatusCode int `json:"-"` // http response status code
|
||||
|
||||
StatusText string `json:"status"` // user-level status message
|
||||
AppCode int64 `json:"code,omitempty"` // application-specific error code
|
||||
ErrorText string `json:"error,omitempty"` // application-level error message, for debugging
|
||||
}
|
||||
|
||||
func (e ErrResponse) Render(w http.ResponseWriter, r *http.Request) error {
|
||||
render.Status(r, e.HTTPStatusCode)
|
||||
return nil
|
||||
}
|
||||
|
||||
func ErrInvalidRequest(err error) render.Renderer {
|
||||
return &ErrResponse{
|
||||
Err: err,
|
||||
HTTPStatusCode: 400,
|
||||
StatusText: "Invalid request.",
|
||||
ErrorText: err.Error(),
|
||||
}
|
||||
}
|
42
api/internal/app/api/download/download.go
Normal file
42
api/internal/app/api/download/download.go
Normal file
@ -0,0 +1,42 @@
|
||||
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
|
||||
}
|
||||
}
|
49
api/internal/app/router/router.go
Normal file
49
api/internal/app/router/router.go
Normal file
@ -0,0 +1,49 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"downloader/internal/app/api/download"
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/chi/middleware"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type router struct {
|
||||
internalRouter *chi.Mux
|
||||
}
|
||||
|
||||
func NewRouter() *router {
|
||||
router := &router{internalRouter: chi.NewRouter()}
|
||||
|
||||
router.
|
||||
setupMiddleware().
|
||||
setupRoutes()
|
||||
|
||||
return router
|
||||
}
|
||||
|
||||
func (router *router) Run() {
|
||||
http.ListenAndServe(":3333", router.internalRouter)
|
||||
}
|
||||
|
||||
func (router *router) RegisterApi() *chi.Mux {
|
||||
return router.internalRouter
|
||||
}
|
||||
|
||||
func (router *router) setupMiddleware() *router {
|
||||
router.internalRouter.Use(middleware.Logger)
|
||||
router.internalRouter.Use(middleware.RequestID)
|
||||
router.internalRouter.Use(middleware.RealIP)
|
||||
router.internalRouter.Use(middleware.Recoverer)
|
||||
router.internalRouter.Use(middleware.Timeout(time.Second * 60))
|
||||
|
||||
return router
|
||||
}
|
||||
|
||||
func (router *router) setupRoutes() *router {
|
||||
downloadApi := download.New()
|
||||
|
||||
downloadApi.SetupDownloadApi(router.internalRouter)
|
||||
|
||||
return router
|
||||
}
|
8
api/internal/core/entities/Download.go
Normal file
8
api/internal/core/entities/Download.go
Normal file
@ -0,0 +1,8 @@
|
||||
package entities
|
||||
|
||||
type Download struct {
|
||||
ID string `json:"id"`
|
||||
Status string `json:"status"`
|
||||
Link string `json:"link"`
|
||||
Data string `json:"data"`
|
||||
}
|
13
api/internal/core/ports/download_request/download_request.go
Normal file
13
api/internal/core/ports/download_request/download_request.go
Normal file
@ -0,0 +1,13 @@
|
||||
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)
|
||||
}
|
||||
|
||||
type Repository interface {
|
||||
Create(download entities.Download) (entities.Download, error)
|
||||
GetById(id string) (entities.Download, error)
|
||||
}
|
Loading…
Reference in New Issue
Block a user