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 (
|
require (
|
||||||
github.com/ajg/form v1.5.1 // indirect
|
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/chi/v5 v5.1.0 // indirect
|
||||||
github.com/go-chi/render v1.0.3 // 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 h1:t9c7v8JUKu/XxOGBU0yjNpaMloxGEJhUkqFRq0ibGeU=
|
||||||
github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY=
|
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 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
|
||||||
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
|
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
|
||||||
github.com/go-chi/render v1.0.3 h1:AsXqd2a1/INaIfUSKq3G5uA8weYx20FOsM7uSoCyyt4=
|
github.com/go-chi/render v1.0.3 h1:AsXqd2a1/INaIfUSKq3G5uA8weYx20FOsM7uSoCyyt4=
|
||||||
|
|
15
main.go
15
main.go
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/caarlos0/env/v11"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"rest_api/models"
|
"rest_api/models"
|
||||||
|
@ -8,16 +9,14 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
config := models.Config{
|
var config models.Config
|
||||||
Token: "Alt",
|
if err := env.Parse(&config); err != nil {
|
||||||
UploadDir: "./uploads", // Папка для сохранения файлов
|
log.Fatalf("ошибка при парсинге переменных %v", err)
|
||||||
MaxSizeUpload: 100 * 1024 * 1024, // Max размер файла
|
|
||||||
TaskDir: "extra",
|
|
||||||
}
|
}
|
||||||
|
// Конфигурация сервера
|
||||||
router := router.NewRouter(config).SetupRoutes()
|
router := router.NewRouter(config).SetupRoutes()
|
||||||
|
|
||||||
log.Println("Сервер запущен на :8080")
|
log.Printf("Сервер запущен на порту: %s", config.Port)
|
||||||
http.ListenAndServe(":8080", router)
|
http.ListenAndServe(":"+config.Port, router)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Token string
|
Token string `env:"TOKEN"`
|
||||||
Repo string
|
Repo string `env:"REPO"`
|
||||||
UploadDir string
|
UploadDir string `env:"UPLOAD_DIR" envDefault:"./uploads"`
|
||||||
TaskDir string
|
TaskDir string `env:"TASK_DIR" envDefault:"./tasks"`
|
||||||
SymLink string
|
SymLink string `env:"SYM_LINK"`
|
||||||
MaxSizeUpload int64
|
Port string `env:"PORT" envDefault:"8080"`
|
||||||
|
MaxSizeUpload int64 `env:"MAX_SIZE_UPLOAD" envDefault:"104857600"` //100 MB
|
||||||
}
|
}
|
||||||
|
|
||||||
type FileUpload struct {
|
type FileUpload struct {
|
||||||
|
@ -19,3 +20,8 @@ type ErrResponse struct {
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
Code int `json:"code"`
|
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) {
|
func (r *Router) uploadHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
|
// Проверка авторизации
|
||||||
if req.Header.Get("Authorization") != "Bearer "+r.Config.Token {
|
if req.Header.Get("Authorization") != "Bearer "+r.Config.Token {
|
||||||
render.JSON(w, req, models.ErrResponse{Message: "Не авторизованный", Code: http.StatusUnauthorized})
|
render.JSON(w, req, models.ErrResponse{Message: "Не авторизованный", Code: http.StatusUnauthorized})
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in a new issue