refactor: initial go service

This commit is contained in:
Ran
2026-01-20 00:04:23 +07:00
parent 95aa4151cd
commit d40cc581d2
55 changed files with 8390 additions and 307 deletions
+101
View File
@@ -0,0 +1,101 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.30.0
// source: containers.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const getContainerByContainerID = `-- name: GetContainerByContainerID :one
SELECT id, user_id, container_id, container_name, image, status, namespace, auto_start, host_path, container_path, created_at, updated_at, last_started_at, last_stopped_at FROM containers WHERE container_id = $1
`
func (q *Queries) GetContainerByContainerID(ctx context.Context, containerID string) (Container, error) {
row := q.db.QueryRow(ctx, getContainerByContainerID, containerID)
var i Container
err := row.Scan(
&i.ID,
&i.UserID,
&i.ContainerID,
&i.ContainerName,
&i.Image,
&i.Status,
&i.Namespace,
&i.AutoStart,
&i.HostPath,
&i.ContainerPath,
&i.CreatedAt,
&i.UpdatedAt,
&i.LastStartedAt,
&i.LastStoppedAt,
)
return i, err
}
const upsertContainer = `-- name: UpsertContainer :exec
INSERT INTO containers (
user_id, container_id, container_name, image, status, namespace, auto_start,
host_path, container_path, last_started_at, last_stopped_at
)
VALUES (
$1,
$2,
$3,
$4,
$5,
$6,
$7,
$8,
$9,
$10,
$11
)
ON CONFLICT (container_id) DO UPDATE SET
user_id = EXCLUDED.user_id,
container_name = EXCLUDED.container_name,
image = EXCLUDED.image,
status = EXCLUDED.status,
namespace = EXCLUDED.namespace,
auto_start = EXCLUDED.auto_start,
host_path = EXCLUDED.host_path,
container_path = EXCLUDED.container_path,
last_started_at = EXCLUDED.last_started_at,
last_stopped_at = EXCLUDED.last_stopped_at,
updated_at = now()
`
type UpsertContainerParams struct {
UserID pgtype.UUID `json:"user_id"`
ContainerID string `json:"container_id"`
ContainerName string `json:"container_name"`
Image string `json:"image"`
Status string `json:"status"`
Namespace string `json:"namespace"`
AutoStart bool `json:"auto_start"`
HostPath pgtype.Text `json:"host_path"`
ContainerPath string `json:"container_path"`
LastStartedAt pgtype.Timestamptz `json:"last_started_at"`
LastStoppedAt pgtype.Timestamptz `json:"last_stopped_at"`
}
func (q *Queries) UpsertContainer(ctx context.Context, arg UpsertContainerParams) error {
_, err := q.db.Exec(ctx, upsertContainer,
arg.UserID,
arg.ContainerID,
arg.ContainerName,
arg.Image,
arg.Status,
arg.Namespace,
arg.AutoStart,
arg.HostPath,
arg.ContainerPath,
arg.LastStartedAt,
arg.LastStoppedAt,
)
return err
}
+32
View File
@@ -0,0 +1,32 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.30.0
package sqlc
import (
"context"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)
type DBTX interface {
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
QueryRow(context.Context, string, ...interface{}) pgx.Row
}
func New(db DBTX) *Queries {
return &Queries{db: db}
}
type Queries struct {
db DBTX
}
func (q *Queries) WithTx(tx pgx.Tx) *Queries {
return &Queries{
db: tx,
}
}
+67
View File
@@ -0,0 +1,67 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.30.0
// source: events.sql
package sqlc
import (
"context"
)
const insertLifecycleEvent = `-- name: InsertLifecycleEvent :exec
INSERT INTO lifecycle_events (id, container_id, event_type, payload)
VALUES (
$1,
$2,
$3,
$4
)
`
type InsertLifecycleEventParams struct {
ID string `json:"id"`
ContainerID string `json:"container_id"`
EventType string `json:"event_type"`
Payload []byte `json:"payload"`
}
func (q *Queries) InsertLifecycleEvent(ctx context.Context, arg InsertLifecycleEventParams) error {
_, err := q.db.Exec(ctx, insertLifecycleEvent,
arg.ID,
arg.ContainerID,
arg.EventType,
arg.Payload,
)
return err
}
const listLifecycleEventsByContainerID = `-- name: ListLifecycleEventsByContainerID :many
SELECT id, container_id, event_type, payload, created_at FROM lifecycle_events WHERE container_id = $1 ORDER BY created_at ASC
`
func (q *Queries) ListLifecycleEventsByContainerID(ctx context.Context, containerID string) ([]LifecycleEvent, error) {
rows, err := q.db.Query(ctx, listLifecycleEventsByContainerID, containerID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []LifecycleEvent
for rows.Next() {
var i LifecycleEvent
if err := rows.Scan(
&i.ID,
&i.ContainerID,
&i.EventType,
&i.Payload,
&i.CreatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
+66
View File
@@ -0,0 +1,66 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.30.0
package sqlc
import (
"github.com/jackc/pgx/v5/pgtype"
)
type Container struct {
ID pgtype.UUID `json:"id"`
UserID pgtype.UUID `json:"user_id"`
ContainerID string `json:"container_id"`
ContainerName string `json:"container_name"`
Image string `json:"image"`
Status string `json:"status"`
Namespace string `json:"namespace"`
AutoStart bool `json:"auto_start"`
HostPath pgtype.Text `json:"host_path"`
ContainerPath string `json:"container_path"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
LastStartedAt pgtype.Timestamptz `json:"last_started_at"`
LastStoppedAt pgtype.Timestamptz `json:"last_stopped_at"`
}
type ContainerVersion struct {
ID string `json:"id"`
ContainerID string `json:"container_id"`
SnapshotID string `json:"snapshot_id"`
Version int32 `json:"version"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
}
type LifecycleEvent struct {
ID string `json:"id"`
ContainerID string `json:"container_id"`
EventType string `json:"event_type"`
Payload []byte `json:"payload"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
}
type Snapshot struct {
ID string `json:"id"`
ContainerID string `json:"container_id"`
ParentSnapshotID pgtype.Text `json:"parent_snapshot_id"`
Snapshotter string `json:"snapshotter"`
Digest pgtype.Text `json:"digest"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
}
type User struct {
ID pgtype.UUID `json:"id"`
Username string `json:"username"`
Email pgtype.Text `json:"email"`
PasswordHash string `json:"password_hash"`
Role interface{} `json:"role"`
DisplayName pgtype.Text `json:"display_name"`
AvatarUrl pgtype.Text `json:"avatar_url"`
IsActive bool `json:"is_active"`
DataRoot pgtype.Text `json:"data_root"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
LastLoginAt pgtype.Timestamptz `json:"last_login_at"`
}
+74
View File
@@ -0,0 +1,74 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.30.0
// source: snapshots.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const insertSnapshot = `-- name: InsertSnapshot :exec
INSERT INTO snapshots (id, container_id, parent_snapshot_id, snapshotter, digest)
VALUES (
$1,
$2,
$3,
$4,
$5
)
ON CONFLICT (id) DO NOTHING
`
type InsertSnapshotParams struct {
ID string `json:"id"`
ContainerID string `json:"container_id"`
ParentSnapshotID pgtype.Text `json:"parent_snapshot_id"`
Snapshotter string `json:"snapshotter"`
Digest pgtype.Text `json:"digest"`
}
func (q *Queries) InsertSnapshot(ctx context.Context, arg InsertSnapshotParams) error {
_, err := q.db.Exec(ctx, insertSnapshot,
arg.ID,
arg.ContainerID,
arg.ParentSnapshotID,
arg.Snapshotter,
arg.Digest,
)
return err
}
const listSnapshotsByContainerID = `-- name: ListSnapshotsByContainerID :many
SELECT id, container_id, parent_snapshot_id, snapshotter, digest, created_at FROM snapshots WHERE container_id = $1 ORDER BY created_at ASC
`
func (q *Queries) ListSnapshotsByContainerID(ctx context.Context, containerID string) ([]Snapshot, error) {
rows, err := q.db.Query(ctx, listSnapshotsByContainerID, containerID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Snapshot
for rows.Next() {
var i Snapshot
if err := rows.Scan(
&i.ID,
&i.ContainerID,
&i.ParentSnapshotID,
&i.Snapshotter,
&i.Digest,
&i.CreatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
+155
View File
@@ -0,0 +1,155 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.30.0
// source: users.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const createUser = `-- name: CreateUser :one
INSERT INTO users (username, email, password_hash, role, display_name, avatar_url, is_active, data_root)
VALUES (
$1,
$2,
$3,
$4,
$5,
$6,
$7,
$8
)
RETURNING id, username, email, password_hash, role, display_name, avatar_url, is_active, data_root, created_at, updated_at, last_login_at
`
type CreateUserParams struct {
Username string `json:"username"`
Email pgtype.Text `json:"email"`
PasswordHash string `json:"password_hash"`
Role interface{} `json:"role"`
DisplayName pgtype.Text `json:"display_name"`
AvatarUrl pgtype.Text `json:"avatar_url"`
IsActive bool `json:"is_active"`
DataRoot pgtype.Text `json:"data_root"`
}
func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, error) {
row := q.db.QueryRow(ctx, createUser,
arg.Username,
arg.Email,
arg.PasswordHash,
arg.Role,
arg.DisplayName,
arg.AvatarUrl,
arg.IsActive,
arg.DataRoot,
)
var i User
err := row.Scan(
&i.ID,
&i.Username,
&i.Email,
&i.PasswordHash,
&i.Role,
&i.DisplayName,
&i.AvatarUrl,
&i.IsActive,
&i.DataRoot,
&i.CreatedAt,
&i.UpdatedAt,
&i.LastLoginAt,
)
return i, err
}
const getUserByUsername = `-- name: GetUserByUsername :one
SELECT id, username, email, password_hash, role, display_name, avatar_url, is_active, data_root, created_at, updated_at, last_login_at FROM users WHERE username = $1
`
func (q *Queries) GetUserByUsername(ctx context.Context, username string) (User, error) {
row := q.db.QueryRow(ctx, getUserByUsername, username)
var i User
err := row.Scan(
&i.ID,
&i.Username,
&i.Email,
&i.PasswordHash,
&i.Role,
&i.DisplayName,
&i.AvatarUrl,
&i.IsActive,
&i.DataRoot,
&i.CreatedAt,
&i.UpdatedAt,
&i.LastLoginAt,
)
return i, err
}
const upsertUserByUsername = `-- name: UpsertUserByUsername :one
INSERT INTO users (username, email, password_hash, role, display_name, avatar_url, is_active, data_root)
VALUES (
$1,
$2,
$3,
$4,
$5,
$6,
$7,
$8
)
ON CONFLICT (username) DO UPDATE SET
email = EXCLUDED.email,
password_hash = EXCLUDED.password_hash,
role = EXCLUDED.role,
display_name = EXCLUDED.display_name,
avatar_url = EXCLUDED.avatar_url,
is_active = EXCLUDED.is_active,
data_root = EXCLUDED.data_root,
updated_at = now()
RETURNING id, username, email, password_hash, role, display_name, avatar_url, is_active, data_root, created_at, updated_at, last_login_at
`
type UpsertUserByUsernameParams struct {
Username string `json:"username"`
Email pgtype.Text `json:"email"`
PasswordHash string `json:"password_hash"`
Role interface{} `json:"role"`
DisplayName pgtype.Text `json:"display_name"`
AvatarUrl pgtype.Text `json:"avatar_url"`
IsActive bool `json:"is_active"`
DataRoot pgtype.Text `json:"data_root"`
}
func (q *Queries) UpsertUserByUsername(ctx context.Context, arg UpsertUserByUsernameParams) (User, error) {
row := q.db.QueryRow(ctx, upsertUserByUsername,
arg.Username,
arg.Email,
arg.PasswordHash,
arg.Role,
arg.DisplayName,
arg.AvatarUrl,
arg.IsActive,
arg.DataRoot,
)
var i User
err := row.Scan(
&i.ID,
&i.Username,
&i.Email,
&i.PasswordHash,
&i.Role,
&i.DisplayName,
&i.AvatarUrl,
&i.IsActive,
&i.DataRoot,
&i.CreatedAt,
&i.UpdatedAt,
&i.LastLoginAt,
)
return i, err
}
+103
View File
@@ -0,0 +1,103 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.30.0
// source: versions.sql
package sqlc
import (
"context"
)
const getVersionSnapshotID = `-- name: GetVersionSnapshotID :one
SELECT snapshot_id FROM container_versions WHERE container_id = $1 AND version = $2
`
type GetVersionSnapshotIDParams struct {
ContainerID string `json:"container_id"`
Version int32 `json:"version"`
}
func (q *Queries) GetVersionSnapshotID(ctx context.Context, arg GetVersionSnapshotIDParams) (string, error) {
row := q.db.QueryRow(ctx, getVersionSnapshotID, arg.ContainerID, arg.Version)
var snapshot_id string
err := row.Scan(&snapshot_id)
return snapshot_id, err
}
const insertVersion = `-- name: InsertVersion :one
INSERT INTO container_versions (id, container_id, snapshot_id, version)
VALUES (
$1,
$2,
$3,
$4
)
RETURNING id, container_id, snapshot_id, version, created_at
`
type InsertVersionParams struct {
ID string `json:"id"`
ContainerID string `json:"container_id"`
SnapshotID string `json:"snapshot_id"`
Version int32 `json:"version"`
}
func (q *Queries) InsertVersion(ctx context.Context, arg InsertVersionParams) (ContainerVersion, error) {
row := q.db.QueryRow(ctx, insertVersion,
arg.ID,
arg.ContainerID,
arg.SnapshotID,
arg.Version,
)
var i ContainerVersion
err := row.Scan(
&i.ID,
&i.ContainerID,
&i.SnapshotID,
&i.Version,
&i.CreatedAt,
)
return i, err
}
const listVersionsByContainerID = `-- name: ListVersionsByContainerID :many
SELECT id, container_id, snapshot_id, version, created_at FROM container_versions WHERE container_id = $1 ORDER BY version ASC
`
func (q *Queries) ListVersionsByContainerID(ctx context.Context, containerID string) ([]ContainerVersion, error) {
rows, err := q.db.Query(ctx, listVersionsByContainerID, containerID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []ContainerVersion
for rows.Next() {
var i ContainerVersion
if err := rows.Scan(
&i.ID,
&i.ContainerID,
&i.SnapshotID,
&i.Version,
&i.CreatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const nextVersion = `-- name: NextVersion :one
SELECT COALESCE(MAX(version), 0) + 1 FROM container_versions WHERE container_id = $1
`
func (q *Queries) NextVersion(ctx context.Context, containerID string) (int32, error) {
row := q.db.QueryRow(ctx, nextVersion, containerID)
var column_1 int32
err := row.Scan(&column_1)
return column_1, err
}