feat: add database #7

Merged
maks1ms merged 7 commits from feat/add-database into main 2024-12-14 08:35:46 +00:00
9 changed files with 57 additions and 26 deletions
Showing only changes of commit 73a654bfaa - Show all commits

3
.dockerignore Normal file
View file

@ -0,0 +1,3 @@
.git
app.db
uploads

View file

@ -1,15 +1,15 @@
# Первый этап: сборка приложения # Первый этап: сборка приложения
FROM golang:latest AS builder FROM golang:latest AS builder
WORKDIR /rest-app WORKDIR /app
COPY go.mod go.sum ./ COPY go.mod go.sum ./
RUN go mod download RUN go mod download
COPY . . 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 FROM registry.altlinux.org/alt/alt:sisyphus
COPY --from=builder /rest-app/alt-storage /bin/main COPY --from=builder /app/aides-repo-api /bin/aides-repo-api
ENTRYPOINT ["/bin/main"] ENTRYPOINT ["/bin/aides-repo-api"]

5
go.mod
View file

@ -10,9 +10,14 @@ require (
require ( require (
github.com/ajg/form v1.5.1 // indirect 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/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // 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/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 golang.org/x/text v0.21.0 // indirect
gorm.io/driver/sqlite v1.5.7 // indirect gorm.io/driver/sqlite v1.5.7 // indirect
gorm.io/gorm v1.25.12 // indirect gorm.io/gorm v1.25.12 // indirect

10
go.sum
View file

@ -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/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=
github.com/go-chi/render v1.0.3/go.mod h1:/gr3hVkmYR0YlEy3LxCuVRFzEu9Ruok+gFqbIofjao0= 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 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= 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 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= 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 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= 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 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
gorm.io/driver/sqlite v1.5.7 h1:8NvsrhP0ifM7LX9G4zPB97NwovUakUxc+2V2uuf3Z1I= gorm.io/driver/sqlite v1.5.7 h1:8NvsrhP0ifM7LX9G4zPB97NwovUakUxc+2V2uuf3Z1I=

View file

@ -12,7 +12,6 @@ import (
type TaskUploadResponse struct { type TaskUploadResponse struct {
TaskID string `json:"taskID"` TaskID string `json:"taskID"`
Repo string `json:"repo"`
StatusText string `json:"status"` StatusText string `json:"status"`
} }
@ -39,15 +38,6 @@ func (c *TaskController) Upload(w http.ResponseWriter, r *http.Request) {
return 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"] files := r.MultipartForm.File["files"]
for _, fileHeader := range files { for _, fileHeader := range files {
if fileHeader.Size > (1024 << 20) { // Limit each file size to 10MB 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{ err = c.taskService.Upload(&taskservice.TaskUploadInput{
TaskID: taskID, TaskID: taskID,
Repo: repo,
Files: files, Files: files,
}) })
if err != nil { if err != nil {
@ -76,7 +65,6 @@ func (c *TaskController) Upload(w http.ResponseWriter, r *http.Request) {
response := TaskUploadResponse{ response := TaskUploadResponse{
TaskID: taskID, TaskID: taskID,
Repo: repo,
StatusText: "Success!", StatusText: "Success!",
} }

View file

@ -101,8 +101,6 @@ func (s *Service) ForceUpdate() {
} }
for _, el := range tasks { for _, el := range tasks {
fmt.Println(el.Files)
taskPath := path.Join( taskPath := path.Join(
s.app.Config.UploadDir, "tasks", strconv.FormatUint(uint64(el.ID), 10), s.app.Config.UploadDir, "tasks", strconv.FormatUint(uint64(el.ID), 10),
) )
@ -131,4 +129,26 @@ func (s *Service) ForceUpdate() {
Update( Update(
"current_task_id", gorm.Expr("last_task_id"), "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 {
}
} }

View file

@ -1,17 +1,23 @@
package taskservice 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) { func (s *Service) Create(repo string) (*models.Task, error) {
taskRepo := models.GitRepo{ taskRepo := models.GitRepo{
Name: repo, Name: repo,
} }
s.app.Db.FirstOrCreate(&taskRepo) s.app.Db.Debug().
Where(&taskRepo).
FirstOrCreate(&taskRepo)
altRepo := models.ALTRepo{ altRepo := models.ALTRepo{
Name: "Sisyphus", Name: "Sisyphus",
} }
s.app.Db.FirstOrCreate(&altRepo) s.app.Db.
Where(&altRepo).
FirstOrCreate(&altRepo)
task := models.Task{ task := models.Task{
RepoID: taskRepo.ID, RepoID: taskRepo.ID,

View file

@ -26,11 +26,11 @@ func (s *Service) onTaskComplete(task *models.Task) error {
grart := models.GitRepoAltRepoTask{ grart := models.GitRepoAltRepoTask{
ALTRepoID: 1, ALTRepoID: 1,
Repo: task.Repo, RepoID: task.RepoID,
} }
s.app.Db.Debug().Where( s.app.Db.Debug().
&grart, Where(&grart).
).FirstOrCreate(&grart) FirstOrCreate(&grart)
grart.LastTaskID = task.ID grart.LastTaskID = task.ID

View file

@ -17,7 +17,6 @@ import (
type TaskUploadInput struct { type TaskUploadInput struct {
TaskID string TaskID string
Repo string
Files []*multipart.FileHeader Files []*multipart.FileHeader
} }