From dc17e0453b8947231a3fee04ef39be03363833fc Mon Sep 17 00:00:00 2001 From: mrrchristmas Date: Fri, 6 Dec 2024 18:13:29 +0300 Subject: [PATCH] Initial commit --- go.mod | 1 + go.sum | 2 ++ main.go | 15 +++++++-------- models/models.go | 18 ++++++++++++------ router/router.go | 1 + 5 files changed, 23 insertions(+), 14 deletions(-) diff --git a/go.mod b/go.mod index b88f40a..e31edba 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.23.3 require ( github.com/ajg/form v1.5.1 // indirect + github.com/caarlos0/env/v11 v11.2.2 // indirect github.com/go-chi/chi/v5 v5.1.0 // indirect github.com/go-chi/render v1.0.3 // indirect ) diff --git a/go.sum b/go.sum index 0b7bdf6..55110ff 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/ajg/form v1.5.1 h1:t9c7v8JUKu/XxOGBU0yjNpaMloxGEJhUkqFRq0ibGeU= github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY= +github.com/caarlos0/env/v11 v11.2.2 h1:95fApNrUyueipoZN/EhA8mMxiNxrBwDa+oAZrMWl3Kg= +github.com/caarlos0/env/v11 v11.2.2/go.mod h1:JBfcdeQiBoI3Zh1QRAWfe+tpiNTmDtcCj/hHHHMx0vc= github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/go-chi/render v1.0.3 h1:AsXqd2a1/INaIfUSKq3G5uA8weYx20FOsM7uSoCyyt4= diff --git a/main.go b/main.go index a132d6a..3ca8355 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "github.com/caarlos0/env/v11" "log" "net/http" "rest_api/models" @@ -8,16 +9,14 @@ import ( ) func main() { - config := models.Config{ - Token: "Alt", - UploadDir: "./uploads", // Папка для сохранения файлов - MaxSizeUpload: 100 * 1024 * 1024, // Max размер файла - TaskDir: "extra", + var config models.Config + if err := env.Parse(&config); err != nil { + log.Fatalf("ошибка при парсинге переменных %v", err) } - + // Конфигурация сервера router := router.NewRouter(config).SetupRoutes() - log.Println("Сервер запущен на :8080") - http.ListenAndServe(":8080", router) + log.Printf("Сервер запущен на порту: %s", config.Port) + http.ListenAndServe(":"+config.Port, router) } diff --git a/models/models.go b/models/models.go index d2f19db..cd7de19 100644 --- a/models/models.go +++ b/models/models.go @@ -1,12 +1,13 @@ package models type Config struct { - Token string - Repo string - UploadDir string - TaskDir string - SymLink string - MaxSizeUpload int64 + Token string `env:"TOKEN"` + Repo string `env:"REPO"` + UploadDir string `env:"UPLOAD_DIR" envDefault:"./uploads"` + TaskDir string `env:"TASK_DIR" envDefault:"./tasks"` + SymLink string `env:"SYM_LINK"` + Port string `env:"PORT" envDefault:"8080"` + MaxSizeUpload int64 `env:"MAX_SIZE_UPLOAD" envDefault:"104857600"` //100 MB } type FileUpload struct { @@ -19,3 +20,8 @@ type ErrResponse struct { Message string `json:"message"` Code int `json:"code"` } + +type Task struct { + TaskID string `json:"task_id"` + Link string `json:"link"` +} diff --git a/router/router.go b/router/router.go index d86e52a..9068e86 100644 --- a/router/router.go +++ b/router/router.go @@ -53,6 +53,7 @@ func (r *Router) SetupRoutes() *chi.Mux { // Метод отвечает за загрузку файлов func (r *Router) uploadHandler(w http.ResponseWriter, req *http.Request) { + // Проверка авторизации if req.Header.Get("Authorization") != "Bearer "+r.Config.Token { render.JSON(w, req, models.ErrResponse{Message: "Не авторизованный", Code: http.StatusUnauthorized}) return