refactor #6

Merged
maks1ms merged 3 commits from refactor into main 2024-12-12 13:27:39 +00:00
4 changed files with 34 additions and 20 deletions
Showing only changes of commit 72131ed7ce - Show all commits

View file

@ -4,19 +4,15 @@ import (
"log" "log"
"net/http" "net/http"
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models" "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/config"
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/router" "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/router"
"github.com/caarlos0/env/v11"
) )
func main() { func main() {
var config models.Config config := config.New()
if err := env.Parse(&config); err != nil {
log.Fatalf("ошибка при парсинге переменных %v", err)
}
// Конфигурация сервера // Конфигурация сервера
router := router.NewRouter(config).SetupRoutes() router := router.NewRouter(*config).SetupRoutes()
log.Printf("Сервер запущен на порту: %s", config.Port) log.Printf("Сервер запущен на порту: %s", config.Port)
http.ListenAndServe(":"+config.Port, router) http.ListenAndServe(":"+config.Port, router)

27
internal/config/config.go Normal file
View file

@ -0,0 +1,27 @@
package config
import (
"log"
"github.com/caarlos0/env/v11"
)
type Config struct {
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
}
func New() *Config {
config := new(Config)
if err := env.Parse(config); err != nil {
log.Fatalf("ошибка при парсинге переменных %v", err)
}
return config
}

View file

@ -1,15 +1,5 @@
package models package models
type Config struct {
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 { type FileUpload struct {
TaskID string TaskID string

View file

@ -9,6 +9,7 @@ import (
"path" "path"
"strconv" "strconv"
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/config"
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models" "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models"
"github.com/go-chi/render" "github.com/go-chi/render"
@ -32,11 +33,11 @@ func createSymlink(target, link string) error {
} }
type Router struct { type Router struct {
Config models.Config Config config.Config
} }
// Создает Роутер // Создает Роутер
func NewRouter(cfg models.Config) *Router { func NewRouter(cfg config.Config) *Router {
return &Router{Config: cfg} return &Router{Config: cfg}
} }