From 73a654bfaaf24ee04894bc537f5556a8549ee995 Mon Sep 17 00:00:00 2001 From: Maxim Slipenko Date: Fri, 13 Dec 2024 15:01:49 +0300 Subject: [PATCH] wip --- .dockerignore | 3 +++ Dockerfile | 8 +++---- go.mod | 5 ++++ go.sum | 10 ++++++++ internal/controllers/taskcontroller/upload.go | 12 ---------- internal/services/reposervice/service.go | 24 +++++++++++++++++-- internal/services/taskservice/create.go | 12 +++++++--- internal/services/taskservice/service.go | 8 +++---- internal/services/taskservice/upload.go | 1 - 9 files changed, 57 insertions(+), 26 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..f1914ae --- /dev/null +++ b/.dockerignore @@ -0,0 +1,3 @@ +.git +app.db +uploads \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 545fbf9..b3fa0b2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,15 +1,15 @@ # Первый этап: сборка приложения FROM golang:latest AS builder -WORKDIR /rest-app +WORKDIR /app COPY go.mod go.sum ./ RUN go mod download COPY . . -RUN go build -o alt-storage +RUN go build -o aides-repo-api ./cmd/aides-repo-api/main.go # Второй этап: создание финального образа FROM registry.altlinux.org/alt/alt:sisyphus -COPY --from=builder /rest-app/alt-storage /bin/main -ENTRYPOINT ["/bin/main"] \ No newline at end of file +COPY --from=builder /app/aides-repo-api /bin/aides-repo-api +ENTRYPOINT ["/bin/aides-repo-api"] \ No newline at end of file diff --git a/go.mod b/go.mod index fcc8a8c..78aa012 100644 --- a/go.mod +++ b/go.mod @@ -10,9 +10,14 @@ require ( require ( github.com/ajg/form v1.5.1 // indirect + github.com/go-co-op/gocron/v2 v2.13.0 // indirect + github.com/google/uuid v1.6.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect + github.com/jonboulle/clockwork v0.4.0 // indirect github.com/mattn/go-sqlite3 v1.14.24 // indirect + github.com/robfig/cron/v3 v3.0.1 // indirect + golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect golang.org/x/text v0.21.0 // indirect gorm.io/driver/sqlite v1.5.7 // indirect gorm.io/gorm v1.25.12 // indirect diff --git a/go.sum b/go.sum index 5acd7b7..1c90d0d 100644 --- a/go.sum +++ b/go.sum @@ -6,12 +6,22 @@ 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= github.com/go-chi/render v1.0.3/go.mod h1:/gr3hVkmYR0YlEy3LxCuVRFzEu9Ruok+gFqbIofjao0= +github.com/go-co-op/gocron/v2 v2.13.0 h1:iGU/RoZvf4GF5hIZUkDSFvvajk9K3W4YgocarBol/ME= +github.com/go-co-op/gocron/v2 v2.13.0/go.mod h1:ZF70ZwEqz0OO4RBXE1sNxnANy/zvwLcattWEFsqpKig= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4= +github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc= github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM= github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= +github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= +github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= +golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 h1:yixxcjnhBmY0nkL253HFVIm0JsFHwrHdT3Yh6szTnfY= +golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8/go.mod h1:jj3sYF3dwk5D+ghuXyeI3r5MFf+NT2An6/9dOA95KSI= golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= gorm.io/driver/sqlite v1.5.7 h1:8NvsrhP0ifM7LX9G4zPB97NwovUakUxc+2V2uuf3Z1I= diff --git a/internal/controllers/taskcontroller/upload.go b/internal/controllers/taskcontroller/upload.go index 9700716..d60e507 100644 --- a/internal/controllers/taskcontroller/upload.go +++ b/internal/controllers/taskcontroller/upload.go @@ -12,7 +12,6 @@ import ( type TaskUploadResponse struct { TaskID string `json:"taskID"` - Repo string `json:"repo"` StatusText string `json:"status"` } @@ -39,15 +38,6 @@ func (c *TaskController) Upload(w http.ResponseWriter, r *http.Request) { return } - repo := r.FormValue("repo") - if repo == "" { - render.Render(w, r, &errors.ErrResponse{ - HTTPStatusCode: http.StatusBadRequest, - StatusText: "Missing required repo field", - }) - return - } - files := r.MultipartForm.File["files"] for _, fileHeader := range files { if fileHeader.Size > (1024 << 20) { // Limit each file size to 10MB @@ -61,7 +51,6 @@ func (c *TaskController) Upload(w http.ResponseWriter, r *http.Request) { err = c.taskService.Upload(&taskservice.TaskUploadInput{ TaskID: taskID, - Repo: repo, Files: files, }) if err != nil { @@ -76,7 +65,6 @@ func (c *TaskController) Upload(w http.ResponseWriter, r *http.Request) { response := TaskUploadResponse{ TaskID: taskID, - Repo: repo, StatusText: "Success!", } diff --git a/internal/services/reposervice/service.go b/internal/services/reposervice/service.go index bac9519..311bfce 100644 --- a/internal/services/reposervice/service.go +++ b/internal/services/reposervice/service.go @@ -101,8 +101,6 @@ func (s *Service) ForceUpdate() { } for _, el := range tasks { - fmt.Println(el.Files) - taskPath := path.Join( s.app.Config.UploadDir, "tasks", strconv.FormatUint(uint64(el.ID), 10), ) @@ -131,4 +129,26 @@ func (s *Service) ForceUpdate() { Update( "current_task_id", gorm.Expr("last_task_id"), ) + + os.MkdirAll(path.Join(s.app.Config.UploadDir, "repo"), os.ModePerm) + + aPath := path.Join(s.app.Config.UploadDir, "future_repo", "Sisyphus") + bPath := path.Join(s.app.Config.UploadDir, "repo", "Sisyphus") + cPath := path.Join(s.app.Config.UploadDir, "repo", ".Sisyphus") + + if _, err := os.Stat(bPath); err == nil { + fmt.Printf("Moving %s to %s\n", bPath, cPath) + if err := os.Rename(bPath, cPath); err != nil { + } + } else if !os.IsNotExist(err) { + + } + + if err := os.Rename(aPath, bPath); err != nil { + panic(err) + } + + if err := os.RemoveAll(cPath); err != nil { + + } } diff --git a/internal/services/taskservice/create.go b/internal/services/taskservice/create.go index d178359..aa81c21 100644 --- a/internal/services/taskservice/create.go +++ b/internal/services/taskservice/create.go @@ -1,17 +1,23 @@ package taskservice -import "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models" +import ( + "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models" +) func (s *Service) Create(repo string) (*models.Task, error) { taskRepo := models.GitRepo{ Name: repo, } - s.app.Db.FirstOrCreate(&taskRepo) + s.app.Db.Debug(). + Where(&taskRepo). + FirstOrCreate(&taskRepo) altRepo := models.ALTRepo{ Name: "Sisyphus", } - s.app.Db.FirstOrCreate(&altRepo) + s.app.Db. + Where(&altRepo). + FirstOrCreate(&altRepo) task := models.Task{ RepoID: taskRepo.ID, diff --git a/internal/services/taskservice/service.go b/internal/services/taskservice/service.go index c0e78f3..68d9511 100644 --- a/internal/services/taskservice/service.go +++ b/internal/services/taskservice/service.go @@ -26,11 +26,11 @@ func (s *Service) onTaskComplete(task *models.Task) error { grart := models.GitRepoAltRepoTask{ ALTRepoID: 1, - Repo: task.Repo, + RepoID: task.RepoID, } - s.app.Db.Debug().Where( - &grart, - ).FirstOrCreate(&grart) + s.app.Db.Debug(). + Where(&grart). + FirstOrCreate(&grart) grart.LastTaskID = task.ID diff --git a/internal/services/taskservice/upload.go b/internal/services/taskservice/upload.go index 0465c32..4334721 100644 --- a/internal/services/taskservice/upload.go +++ b/internal/services/taskservice/upload.go @@ -17,7 +17,6 @@ import ( type TaskUploadInput struct { TaskID string - Repo string Files []*multipart.FileHeader }