Initial commit
This commit is contained in:
parent
e9585bef50
commit
dc17e0453b
5 changed files with 23 additions and 14 deletions
1
go.mod
1
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
|
||||
)
|
||||
|
|
2
go.sum
2
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=
|
||||
|
|
15
main.go
15
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)
|
||||
|
||||
}
|
||||
|
|
|
@ -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"`
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue