Initial commit

This commit is contained in:
Melon 2024-03-04 13:11:06 +00:00
commit 8bbaee1d4a
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
19 changed files with 571 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.idea/

80
cmd/gomvn/main.go Normal file
View File

@ -0,0 +1,80 @@
package main
import (
"errors"
"flag"
"github.com/1f349/gomvn"
"github.com/1f349/gomvn/routes"
exitReload "github.com/MrMelon54/exit-reload"
"gopkg.in/yaml.v3"
"log"
"net/http"
"os"
"path/filepath"
"time"
)
type startupConfig struct {
Name string `yaml:"name"`
Listen string `yaml:"listen"`
Repository []string `yaml:"repository"`
}
func main() {
configPath := flag.String("conf", "", "/path/to/config.yml : path to the config file")
flag.Parse()
log.Println("[GoMVN] Starting...")
if *configPath == "" {
log.Fatal("[GoMVN] Error: config flag is missing")
return
}
openConf, err := os.Open(*configPath)
if err != nil {
if os.IsNotExist(err) {
log.Fatal("[Violet] Error: missing config file")
} else {
log.Fatal("[Violet] Error: open config file: ", err)
}
return
}
var config startupConfig
err = yaml.NewDecoder(openConf).Decode(&config)
if err != nil {
log.Fatal("[GoMVN] Error: invalid config file: ", err)
}
// working directory is the parent of the config file
wd := filepath.Dir(*configPath)
db, err := gomvn.InitDB(filepath.Join(wd, "gomvn.sqlite3.db"))
if err != nil {
log.Fatal("[GoMVN] Error: invalid database: ", err)
return
}
srv := &http.Server{
Addr: config.Listen,
Handler: routes.Router(db, config.Name, config.Repository),
ReadTimeout: time.Minute,
ReadHeaderTimeout: time.Minute,
WriteTimeout: time.Minute,
IdleTimeout: time.Minute,
MaxHeaderBytes: 5000,
}
go func() {
err = srv.ListenAndServe()
if err != nil && !errors.Is(http.ErrServerClosed, err) {
log.Println("Serve HTTP Error:", err)
}
}()
exitReload.ExitReload("GoMVN", func() {}, func() {
err := srv.Close()
if err != nil {
log.Println(err)
}
})
}

5
config.example.yml Normal file
View File

@ -0,0 +1,5 @@
name: Maven Repository
listen: 0.0.0.0:8080
repository:
- release
- snapshot

31
database/db.go Normal file
View File

@ -0,0 +1,31 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.25.0
package database
import (
"context"
"database/sql"
)
type DBTX interface {
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
PrepareContext(context.Context, string) (*sql.Stmt, error)
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
}
func New(db DBTX) *Queries {
return &Queries{db: db}
}
type Queries struct {
db DBTX
}
func (q *Queries) WithTx(tx *sql.Tx) *Queries {
return &Queries{
db: tx,
}
}

View File

@ -0,0 +1,3 @@
DROP TABLE artifacts;
DROP TABLE paths;
DROP TABLE users;

View File

@ -0,0 +1,28 @@
CREATE TABLE artifacts
(
mvn_group TEXT NOT NULL,
artifact TEXT NOT NULL,
version TEXT NOT NULL,
modified TEXT 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,
name TEXT NOT NULL,
admin TINYINT,
token_hash TEXT NOT NULL,
created_at DATETIME,
updated_at DATETIME
);

33
database/models.go Normal file
View File

@ -0,0 +1,33 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.25.0
package database
import (
"database/sql"
)
type Artifact struct {
MvnGroup string `json:"mvn_group"`
Artifact string `json:"artifact"`
Version string `json:"version"`
Modified string `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"`
Name string `json:"name"`
Admin sql.NullInt64 `json:"admin"`
TokenHash string `json:"token_hash"`
CreatedAt sql.NullTime `json:"created_at"`
UpdatedAt sql.NullTime `json:"updated_at"`
}

17
database/queries/user.sql Normal file
View File

@ -0,0 +1,17 @@
-- name: CountUsers :one
SELECT count(*)
FROM users;
-- name: IsAdmin :one
SELECT 1
FROM users
WHERE admin = 1
AND token_hash = ?;
-- name: GetAllUsers :many
SELECT id, name, admin, created_at, updated_at
FROM users;
-- name: CreateUser :execlastid
INSERT INTO users (name, admin, token_hash, created_at, updated_at)
VALUES (?, ?, ?, ?, ?);

106
database/user.sql.go Normal file
View File

@ -0,0 +1,106 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.25.0
// source: user.sql
package database
import (
"context"
"database/sql"
)
const countUsers = `-- name: CountUsers :one
SELECT count(*)
FROM users
`
func (q *Queries) CountUsers(ctx context.Context) (int64, error) {
row := q.db.QueryRowContext(ctx, countUsers)
var count int64
err := row.Scan(&count)
return count, err
}
const createUser = `-- name: CreateUser :execlastid
INSERT INTO users (name, admin, token_hash, created_at, updated_at)
VALUES (?, ?, ?, ?, ?)
`
type CreateUserParams struct {
Name string `json:"name"`
Admin sql.NullInt64 `json:"admin"`
TokenHash string `json:"token_hash"`
CreatedAt sql.NullTime `json:"created_at"`
UpdatedAt sql.NullTime `json:"updated_at"`
}
func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (int64, error) {
result, err := q.db.ExecContext(ctx, createUser,
arg.Name,
arg.Admin,
arg.TokenHash,
arg.CreatedAt,
arg.UpdatedAt,
)
if err != nil {
return 0, err
}
return result.LastInsertId()
}
const getAllUsers = `-- name: GetAllUsers :many
SELECT id, name, admin, created_at, updated_at
FROM users
`
type GetAllUsersRow struct {
ID int64 `json:"id"`
Name string `json:"name"`
Admin sql.NullInt64 `json:"admin"`
CreatedAt sql.NullTime `json:"created_at"`
UpdatedAt sql.NullTime `json:"updated_at"`
}
func (q *Queries) GetAllUsers(ctx context.Context) ([]GetAllUsersRow, error) {
rows, err := q.db.QueryContext(ctx, getAllUsers)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetAllUsersRow
for rows.Next() {
var i GetAllUsersRow
if err := rows.Scan(
&i.ID,
&i.Name,
&i.Admin,
&i.CreatedAt,
&i.UpdatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const isAdmin = `-- name: IsAdmin :one
SELECT 1
FROM users
WHERE admin = 1
AND token_hash = ?
`
func (q *Queries) IsAdmin(ctx context.Context, tokenHash string) (int64, error) {
row := q.db.QueryRowContext(ctx, isAdmin, tokenHash)
var column_1 int64
err := row.Scan(&column_1)
return column_1, err
}

17
go.mod Normal file
View File

@ -0,0 +1,17 @@
module github.com/1f349/gomvn
go 1.22
require (
github.com/MrMelon54/exit-reload v0.0.1
github.com/golang-migrate/migrate/v4 v4.17.0
github.com/julienschmidt/httprouter v1.3.0
gopkg.in/yaml.v3 v3.0.1
)
require (
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/mattn/go-sqlite3 v1.14.22 // indirect
go.uber.org/atomic v1.11.0 // indirect
)

27
go.sum Normal file
View File

@ -0,0 +1,27 @@
github.com/MrMelon54/exit-reload v0.0.1 h1:sxHa59tNEQMcikwuX2+93lw6Vi1+R7oCRF8a0C3alXc=
github.com/MrMelon54/exit-reload v0.0.1/go.mod h1:PLiSfmUzwdpTTQP3BBfUPhkqPwaIZjx0DuXBnM76Bug=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang-migrate/migrate/v4 v4.17.0 h1:rd40H3QXU0AA4IoLllFcEAEo9dYKRHYND2gB4p7xcaU=
github.com/golang-migrate/migrate/v4 v4.17.0/go.mod h1:+Cp2mtLP4/aXDTKb9wmXYitdrNx2HGs45rbWAo6OsKM=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

BIN
identifier.sqlite Normal file

Binary file not shown.

38
initdb.go Normal file
View File

@ -0,0 +1,38 @@
package gomvn
import (
"database/sql"
"embed"
"errors"
"github.com/1f349/gomvn/database"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/sqlite3"
"github.com/golang-migrate/migrate/v4/source/iofs"
)
//go:embed database/migrations/*.sql
var migrations embed.FS
func InitDB(p string) (*database.Queries, error) {
migDrv, err := iofs.New(migrations, "database/migrations")
if err != nil {
return nil, err
}
dbOpen, err := sql.Open("sqlite3", p)
if err != nil {
return nil, err
}
dbDrv, err := sqlite3.WithInstance(dbOpen, &sqlite3.Config{})
if err != nil {
return nil, err
}
mig, err := migrate.NewWithInstance("iofs", migDrv, "sqlite3", dbDrv)
if err != nil {
return nil, err
}
err = mig.Up()
if err != nil && !errors.Is(err, migrate.ErrNoChange) {
return nil, err
}
return database.New(dbOpen), nil
}

47
paths/paths.go Normal file
View File

@ -0,0 +1,47 @@
package paths
import (
"fmt"
"net/http"
"strings"
)
type Paths struct {
Repository []string
}
func (p Paths) NormalizePath(path string) string {
if path[0] == '/' {
path = path[1:]
}
if strings.Contains(path, "..") || strings.Contains(path, "~") {
return ""
}
if strings.Count(path, "/") <= 1 {
return path
}
for _, repo := range p.Repository {
if strings.HasPrefix(path, repo) {
return path
}
}
return ""
}
func (p Paths) ParsePath(req *http.Request) (string, error) {
path := p.NormalizePath(req.URL.Path)
if strings.Count(path, "/") < 3 {
return "", fmt.Errorf("path should be repository/group/artifact")
}
return path, nil
}
func (p Paths) ParsePathParts(req *http.Request) (string, string, string, error) {
path, err := p.ParsePath(req)
if err != nil {
return "", "", "", err
}
parts := strings.Split(path, "/")
last := len(parts) - 1
return parts[0], strings.Join(parts[1:last-1], "/"), parts[last], nil
}

25
paths/repo.go Normal file
View File

@ -0,0 +1,25 @@
package paths
import (
"os"
"path/filepath"
"strings"
)
func GetRepositories(basePath string, repository []string) map[string][]*entity.Artifact {
result := map[string][]*database.Artifact{}
for _, repo := range repository {
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)
artifact := entity.NewArtifact(path, info.ModTime())
result[repo] = append(result[repo], artifact)
}
return nil
})
}
return result
}

22
routes/index.go.html Normal file
View File

@ -0,0 +1,22 @@
<html>
<head>
<title>GoMVN - {{.Name}}</title>
<style>
.dir {
font-weight: bold;
font-family: monospace;
}
</style>
</head>
<body>
<h1>{{.Name}}</h1>
{{range $k, $v := .Repositories}}
<ul>
{{range $v}}
<li><a href="{{ $k }}/{{ .GetPath }}" class="dir">{{ .Group }}:{{ .Artifact }}:{{ .Version }}</a>, last
modified {{ .Modified }}</li>
{{end}}
</ul>
{{end}}
</body>
</html>

65
routes/router.go Normal file
View File

@ -0,0 +1,65 @@
package routes
import (
_ "embed"
"github.com/1f349/gomvn/database"
"github.com/1f349/gomvn/paths"
"github.com/julienschmidt/httprouter"
"html/template"
"io"
"net/http"
"os"
)
type routeCtx struct {
db *database.Queries
pathUtils paths.Paths
name string
basePath string
repository []string
}
func Router(db *database.Queries, name, basePath string, repository []string) http.Handler {
pUtils := paths.Paths{Repository: repository}
base := routeCtx{db, pUtils, name, basePath, repository}
r := httprouter.New()
r.PUT("/*", base.handlePut)
r.GET("/", base.handleIndex)
r.GET("/*", base.handleGet)
return r
}
//go:embed index.go.html
var indexHtml string
var indexTemplate = template.Must(template.New("index").Parse(indexHtml))
func (r *routeCtx) handleIndex(rw http.ResponseWriter, req *http.Request, params httprouter.Params) {
_ = indexTemplate.Execute(rw, map[string]any{
"Name": r.name,
"Repositories": paths.GetRepositories(r.basePath, r.repository),
})
}
func (r *routeCtx) handlePut(rw http.ResponseWriter, req *http.Request, params httprouter.Params) {
p, err := r.pathUtils.ParsePath(req)
if err != nil {
http.Error(rw, "404 Not Found", http.StatusNotFound)
return
}
create, err := os.Create(p)
if err != nil {
http.Error(rw, "500 Failed to open file", http.StatusInternalServerError)
return
}
_, err = io.Copy(create, req.Body)
if err != nil {
http.Error(rw, "500 Failed to write file", http.StatusInternalServerError)
return
}
}
func (r *routeCtx) handleGet(rw http.ResponseWriter, req *http.Request, params httprouter.Params) {
}

13
routes/utils.go Normal file
View File

@ -0,0 +1,13 @@
package routes
import (
"github.com/julienschmidt/httprouter"
"strconv"
)
func getQueryUserId(params httprouter.Params) (int64, bool) {
if val, err := strconv.ParseInt(params.ByName("id"), 10, 64); err == nil {
return val, true
}
return 0, false
}

13
sqlc.yaml Normal file
View File

@ -0,0 +1,13 @@
version: "2"
sql:
- engine: "sqlite"
queries: "database/queries"
schema: "database/migrations"
gen:
go:
package: "database"
out: "database"
emit_json_tags: true
overrides:
- column: "builds.meta"
go_type: "*github.com/mrmelon54/mc-upload-api/database/types.BuildMeta"