From 63aa76b63a6cbb76af84e35c00bec29797c84e54 Mon Sep 17 00:00:00 2001 From: MrMelon54 Date: Fri, 15 Mar 2024 15:13:09 +0000 Subject: [PATCH] More changes --- cmd/gomvn/main.go | 2 +- database/extend-artifact.go | 7 +++++ .../migrations/20240304102707_init.up.sql | 19 ++++++++++++ database/models.go | 20 ++++++++++++- database/types/artifact.go | 10 ------- paths/repo.go | 30 +++++++++++-------- routes/router.go | 4 --- 7 files changed, 63 insertions(+), 29 deletions(-) create mode 100644 database/extend-artifact.go delete mode 100644 database/types/artifact.go diff --git a/cmd/gomvn/main.go b/cmd/gomvn/main.go index 8cd7c45..01275d8 100644 --- a/cmd/gomvn/main.go +++ b/cmd/gomvn/main.go @@ -61,7 +61,7 @@ func main() { srv := &http.Server{ Addr: config.Listen, - Handler: routes.Router(db, config.Name, repoBasePath, config.Repository), + Handler: routes.Router(db, config.Name, repoDir, config.Repository), ReadTimeout: time.Minute, ReadHeaderTimeout: time.Minute, WriteTimeout: time.Minute, diff --git a/database/extend-artifact.go b/database/extend-artifact.go new file mode 100644 index 0000000..fb26171 --- /dev/null +++ b/database/extend-artifact.go @@ -0,0 +1,7 @@ +package database + +import "strings" + +func (a Artifact) GetPath() string { + return strings.Replace(a.MvnGroup, ".", "/", -1) + "/" + a.Artifact + "/" + a.Version +} diff --git a/database/migrations/20240304102707_init.up.sql b/database/migrations/20240304102707_init.up.sql index df146b3..2f98be4 100644 --- a/database/migrations/20240304102707_init.up.sql +++ b/database/migrations/20240304102707_init.up.sql @@ -1,3 +1,22 @@ +CREATE TABLE artifacts +( + mvn_group TEXT NOT NULL, + artifact TEXT NOT NULL, + version TEXT NOT NULL, + modified DATETIME NOT NULL +); + +CREATE TABLE paths +( + user_id INTEGER UNIQUE, + path TEXT PRIMARY KEY, + deploy TINYINT, + created_at DATETIME, + updated_at DATETIME, + + FOREIGN KEY (user_id) REFERENCES users (id) +); + CREATE TABLE users ( id INTEGER UNIQUE PRIMARY KEY AUTOINCREMENT, diff --git a/database/models.go b/database/models.go index 3376fc8..28a8db7 100644 --- a/database/models.go +++ b/database/models.go @@ -4,7 +4,25 @@ package database -import () +import ( + "database/sql" + "time" +) + +type Artifact struct { + MvnGroup string `json:"mvn_group"` + Artifact string `json:"artifact"` + Version string `json:"version"` + Modified time.Time `json:"modified"` +} + +type Path struct { + UserID sql.NullInt64 `json:"user_id"` + Path string `json:"path"` + Deploy sql.NullInt64 `json:"deploy"` + CreatedAt sql.NullTime `json:"created_at"` + UpdatedAt sql.NullTime `json:"updated_at"` +} type User struct { ID int64 `json:"id"` diff --git a/database/types/artifact.go b/database/types/artifact.go deleted file mode 100644 index 5f037a1..0000000 --- a/database/types/artifact.go +++ /dev/null @@ -1,10 +0,0 @@ -package types - -import "time" - -type Artifact struct { - MvnGroup string `json:"mvn_group"` - Artifact string `json:"artifact"` - Version string `json:"version"` - Modified time.Time `json:"modified"` -} diff --git a/paths/repo.go b/paths/repo.go index e69b743..aa65351 100644 --- a/paths/repo.go +++ b/paths/repo.go @@ -1,30 +1,23 @@ package paths import ( - "github.com/1f349/gomvn/database/types" + "github.com/1f349/gomvn/database" "os" "path/filepath" "strings" + "time" ) -func GetRepositories(basePath string, repository []string) map[string][]*types.Artifact { - result := map[string][]*types.Artifact{} +func GetRepositories(basePath string, repository []string) map[string][]*database.Artifact { + result := map[string][]*database.Artifact{} for _, repo := range repository { - result[repo] = []*types.Artifact{} + result[repo] = []*database.Artifact{} repoPath := filepath.Join(basePath, repo) _ = filepath.Walk(repoPath, func(path string, info os.FileInfo, err error) error { if strings.HasSuffix(path, ".pom") { path = strings.Replace(path, "\\", "/", -1) path = strings.Replace(path, repoPath+"/", "", 1) - - parts := strings.Split(path, "/") - last := len(parts) - 1 - artifact := &types.Artifact{ - MvnGroup: strings.Join(parts[0:last-2], "."), - Artifact: parts[last-2], - Version: parts[last-1], - Modified: info.ModTime(), - } + artifact := newArtifact(path, info.ModTime()) result[repo] = append(result[repo], artifact) } return nil @@ -32,3 +25,14 @@ func GetRepositories(basePath string, repository []string) map[string][]*types.A } return result } + +func newArtifact(p string, mod time.Time) *database.Artifact { + parts := strings.Split(p, "/") + last := len(parts) - 1 + return &database.Artifact{ + MvnGroup: strings.Join(parts[0:last-2], "."), + Artifact: parts[last-2], + Version: parts[last-1], + Modified: mod, + } +} diff --git a/routes/router.go b/routes/router.go index ec7ba9f..bcde217 100644 --- a/routes/router.go +++ b/routes/router.go @@ -114,7 +114,3 @@ func (r *routeCtx) handlePut(rw http.ResponseWriter, req *http.Request, params h return } } - -func (r *routeCtx) handleGet(rw http.ResponseWriter, req *http.Request, params httprouter.Params) { - -}