commit c33ea5500e99b644d843951545853cc8eb0b7909 Author: bagas Date: Wed Dec 31 11:44:15 2025 +0700 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..451fb35 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.env +.idea \ No newline at end of file diff --git a/db/sqlc/migrations/001_create_identifiers.down.sql b/db/sqlc/migrations/001_create_identifiers.down.sql new file mode 100644 index 0000000..f83303a --- /dev/null +++ b/db/sqlc/migrations/001_create_identifiers.down.sql @@ -0,0 +1,5 @@ +DROP INDEX IF EXISTS idx_identifiers_created_at; +DROP INDEX IF EXISTS idx_identifiers_slug; + +DROP TABLE IF EXISTS identifiers; + diff --git a/db/sqlc/migrations/001_create_identifiers.up.sql b/db/sqlc/migrations/001_create_identifiers.up.sql new file mode 100644 index 0000000..5692521 --- /dev/null +++ b/db/sqlc/migrations/001_create_identifiers.up.sql @@ -0,0 +1,12 @@ +CREATE TABLE identifiers ( + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), + slug VARCHAR(255) UNIQUE NOT NULL, + created_at TIMESTAMP NOT NULL DEFAULT NOW(), + updated_at TIMESTAMP NOT NULL DEFAULT NOW() +); + +CREATE INDEX idx_identifiers_created_at + ON identifiers(created_at); + +CREATE INDEX idx_identifiers_slug + ON identifiers(slug); diff --git a/db/sqlc/queries/query.sql b/db/sqlc/queries/query.sql new file mode 100644 index 0000000..80246be --- /dev/null +++ b/db/sqlc/queries/query.sql @@ -0,0 +1,34 @@ +-- name: CreateIdentifier :one +INSERT INTO identifiers (slug) +VALUES ($1) +RETURNING id, slug, created_at, updated_at; + +-- name: GetIdentifierById :one +SELECT id, slug, created_at, updated_at +FROM identifiers +WHERE id = $1; + +-- name: GetIdentifierBySlug :one +SELECT id, slug, created_at, updated_at +FROM identifiers +WHERE slug = $1; + +-- name: ListIdentifiers :many +SELECT id, slug, created_at, updated_at +FROM identifiers +ORDER BY created_at DESC +LIMIT $1 OFFSET $2; + +-- name: DeleteIdentifier :exec +DELETE FROM identifiers +WHERE id = $1; + +-- name: UpdateIdentifierSlug :one +UPDATE identifiers +SET slug = $2, updated_at = NOW() +WHERE id = $1 +RETURNING id, slug, created_at, updated_at; + +-- name: CountIdentifiers :one +SELECT COUNT(*) FROM identifiers; + diff --git a/db/sqlc/repository/db.go b/db/sqlc/repository/db.go new file mode 100644 index 0000000..a0b40e7 --- /dev/null +++ b/db/sqlc/repository/db.go @@ -0,0 +1,32 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.27.0 + +package repository + +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, + } +} diff --git a/db/sqlc/repository/models.go b/db/sqlc/repository/models.go new file mode 100644 index 0000000..6a124ed --- /dev/null +++ b/db/sqlc/repository/models.go @@ -0,0 +1,17 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.27.0 + +package repository + +import ( + "github.com/google/uuid" + "github.com/jackc/pgx/v5/pgtype" +) + +type Identifier struct { + ID uuid.UUID + Slug string + CreatedAt pgtype.Timestamp + UpdatedAt pgtype.Timestamp +} diff --git a/db/sqlc/repository/query.sql.go b/db/sqlc/repository/query.sql.go new file mode 100644 index 0000000..e9baf2e --- /dev/null +++ b/db/sqlc/repository/query.sql.go @@ -0,0 +1,148 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.27.0 +// source: query.sql + +package repository + +import ( + "context" + + "github.com/google/uuid" +) + +const countIdentifiers = `-- name: CountIdentifiers :one +SELECT COUNT(*) FROM identifiers +` + +func (q *Queries) CountIdentifiers(ctx context.Context) (int64, error) { + row := q.db.QueryRow(ctx, countIdentifiers) + var count int64 + err := row.Scan(&count) + return count, err +} + +const createIdentifier = `-- name: CreateIdentifier :one +INSERT INTO identifiers (slug) +VALUES ($1) +RETURNING id, slug, created_at, updated_at +` + +func (q *Queries) CreateIdentifier(ctx context.Context, slug string) (Identifier, error) { + row := q.db.QueryRow(ctx, createIdentifier, slug) + var i Identifier + err := row.Scan( + &i.ID, + &i.Slug, + &i.CreatedAt, + &i.UpdatedAt, + ) + return i, err +} + +const deleteIdentifier = `-- name: DeleteIdentifier :exec +DELETE FROM identifiers +WHERE id = $1 +` + +func (q *Queries) DeleteIdentifier(ctx context.Context, id uuid.UUID) error { + _, err := q.db.Exec(ctx, deleteIdentifier, id) + return err +} + +const getIdentifierById = `-- name: GetIdentifierById :one +SELECT id, slug, created_at, updated_at +FROM identifiers +WHERE id = $1 +` + +func (q *Queries) GetIdentifierById(ctx context.Context, id uuid.UUID) (Identifier, error) { + row := q.db.QueryRow(ctx, getIdentifierById, id) + var i Identifier + err := row.Scan( + &i.ID, + &i.Slug, + &i.CreatedAt, + &i.UpdatedAt, + ) + return i, err +} + +const getIdentifierBySlug = `-- name: GetIdentifierBySlug :one +SELECT id, slug, created_at, updated_at +FROM identifiers +WHERE slug = $1 +` + +func (q *Queries) GetIdentifierBySlug(ctx context.Context, slug string) (Identifier, error) { + row := q.db.QueryRow(ctx, getIdentifierBySlug, slug) + var i Identifier + err := row.Scan( + &i.ID, + &i.Slug, + &i.CreatedAt, + &i.UpdatedAt, + ) + return i, err +} + +const listIdentifiers = `-- name: ListIdentifiers :many +SELECT id, slug, created_at, updated_at +FROM identifiers +ORDER BY created_at DESC +LIMIT $1 OFFSET $2 +` + +type ListIdentifiersParams struct { + Limit int32 + Offset int32 +} + +func (q *Queries) ListIdentifiers(ctx context.Context, arg ListIdentifiersParams) ([]Identifier, error) { + rows, err := q.db.Query(ctx, listIdentifiers, arg.Limit, arg.Offset) + if err != nil { + return nil, err + } + defer rows.Close() + var items []Identifier + for rows.Next() { + var i Identifier + if err := rows.Scan( + &i.ID, + &i.Slug, + &i.CreatedAt, + &i.UpdatedAt, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const updateIdentifierSlug = `-- name: UpdateIdentifierSlug :one +UPDATE identifiers +SET slug = $2, updated_at = NOW() +WHERE id = $1 +RETURNING id, slug, created_at, updated_at +` + +type UpdateIdentifierSlugParams struct { + ID uuid.UUID + Slug string +} + +func (q *Queries) UpdateIdentifierSlug(ctx context.Context, arg UpdateIdentifierSlugParams) (Identifier, error) { + row := q.db.QueryRow(ctx, updateIdentifierSlug, arg.ID, arg.Slug) + var i Identifier + err := row.Scan( + &i.ID, + &i.Slug, + &i.CreatedAt, + &i.UpdatedAt, + ) + return i, err +} diff --git a/db/sqlc/sqlc.yaml b/db/sqlc/sqlc.yaml new file mode 100644 index 0000000..7997984 --- /dev/null +++ b/db/sqlc/sqlc.yaml @@ -0,0 +1,26 @@ +version: "2" +sql: + - engine: "postgresql" + queries: "queries" + schema: "migrations" + gen: + go: + package: "repository" + out: "repository" + sql_package: "pgx/v5" + overrides: + - db_type: "uuid" + go_type: + import: "github.com/google/uuid" + type: "UUID" + - db_type: "uuid" + go_type: + import: "github.com/google/uuid" + type: "UUID" + pointer: true + nullable: true + - db_type: "timestamptz" + go_type: + type: "*time.Time" + pointer: true + nullable: true \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..342a837 --- /dev/null +++ b/go.mod @@ -0,0 +1,21 @@ +module git.fossy.my.id/bagas/tunnel-please-controller + +go 1.25.5 + +require ( + git.fossy.my.id/bagas/tunnel-please-grpc v1.0.0 + github.com/google/uuid v1.6.0 + github.com/jackc/pgx/v5 v5.8.0 + github.com/joho/godotenv v1.5.1 + google.golang.org/grpc v1.78.0 + google.golang.org/protobuf v1.36.11 +) + +require ( + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect + golang.org/x/net v0.47.0 // indirect + golang.org/x/sys v0.38.0 // indirect + golang.org/x/text v0.31.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20251029180050-ab9386a59fda // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..644d8e0 --- /dev/null +++ b/go.sum @@ -0,0 +1,64 @@ +git.fossy.my.id/bagas/tunnel-please-grpc v1.0.0 h1:G9U7YmfMBeTK8ASbqdBCmkrRsCNU3e6RUzGaTmg1NB0= +git.fossy.my.id/bagas/tunnel-please-grpc v1.0.0/go.mod h1:fG+VkArdkceGB0bNA7IFQus9GetLAwdF5Oi4jdMlXtY= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +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/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI= +github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= +github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= +github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= +github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= +github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= +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/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.8.0 h1:TYPDoleBBme0xGSAX3/+NujXXtpZn9HBONkQC7IEZSo= +github.com/jackc/pgx/v5 v5.8.0/go.mod h1:QVeDInX2m9VyzvNeiCJVjCkNFqzsNb43204HshNSZKw= +github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= +github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +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/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64= +go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y= +go.opentelemetry.io/otel v1.38.0 h1:RkfdswUDRimDg0m2Az18RKOsnI8UDzppJAtj01/Ymk8= +go.opentelemetry.io/otel v1.38.0/go.mod h1:zcmtmQ1+YmQM9wrNsTGV/q/uyusom3P8RxwExxkZhjM= +go.opentelemetry.io/otel/metric v1.38.0 h1:Kl6lzIYGAh5M159u9NgiRkmoMKjvbsKtYRwgfrA6WpA= +go.opentelemetry.io/otel/metric v1.38.0/go.mod h1:kB5n/QoRM8YwmUahxvI3bO34eVtQf2i4utNVLr9gEmI= +go.opentelemetry.io/otel/sdk v1.38.0 h1:l48sr5YbNf2hpCUj/FoGhW9yDkl+Ma+LrVl8qaM5b+E= +go.opentelemetry.io/otel/sdk v1.38.0/go.mod h1:ghmNdGlVemJI3+ZB5iDEuk4bWA3GkTpW+DOoZMYBVVg= +go.opentelemetry.io/otel/sdk/metric v1.38.0 h1:aSH66iL0aZqo//xXzQLYozmWrXxyFkBJ6qT5wthqPoM= +go.opentelemetry.io/otel/sdk/metric v1.38.0/go.mod h1:dg9PBnW9XdQ1Hd6ZnRz689CbtrUp0wMMs9iPcgT9EZA= +go.opentelemetry.io/otel/trace v1.38.0 h1:Fxk5bKrDZJUH+AMyyIXGcFAPah0oRcT+LuNtJrmcNLE= +go.opentelemetry.io/otel/trace v1.38.0/go.mod h1:j1P9ivuFsTceSWe1oY+EeW3sc+Pp42sO++GHkg4wwhs= +golang.org/x/net v0.47.0 h1:Mx+4dIFzqraBXUugkia1OOvlD6LemFo1ALMHjrXDOhY= +golang.org/x/net v0.47.0/go.mod h1:/jNxtkgq5yWUGYkaZGqo27cfGZ1c5Nen03aYrrKpVRU= +golang.org/x/sync v0.18.0 h1:kr88TuHDroi+UVf+0hZnirlk8o8T+4MrK6mr60WkH/I= +golang.org/x/sync v0.18.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI= +golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc= +golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= +golang.org/x/text v0.31.0 h1:aC8ghyu4JhP8VojJ2lEHBnochRno1sgL6nEi9WGFGMM= +golang.org/x/text v0.31.0/go.mod h1:tKRAlv61yKIjGGHX/4tP1LTbc13YSec1pxVEWXzfoeM= +gonum.org/v1/gonum v0.16.0 h1:5+ul4Swaf3ESvrOnidPp4GZbzf0mxVQpDCYUQE7OJfk= +gonum.org/v1/gonum v0.16.0/go.mod h1:fef3am4MQ93R2HHpKnLk4/Tbh/s0+wqD5nfa6Pnwy4E= +google.golang.org/genproto/googleapis/rpc v0.0.0-20251029180050-ab9386a59fda h1:i/Q+bfisr7gq6feoJnS/DlpdwEL4ihp41fvRiM3Ork0= +google.golang.org/genproto/googleapis/rpc v0.0.0-20251029180050-ab9386a59fda/go.mod h1:7i2o+ce6H/6BluujYR+kqX3GKH+dChPTQU19wjRPiGk= +google.golang.org/grpc v1.78.0 h1:K1XZG/yGDJnzMdd/uZHAkVqJE+xIDOcmdSFZkBUicNc= +google.golang.org/grpc v1.78.0/go.mod h1:I47qjTo4OKbMkjA/aOOwxDIiPSBofUtQUI5EfpWvW7U= +google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE= +google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..6b7af01 --- /dev/null +++ b/main.go @@ -0,0 +1,44 @@ +package main + +import ( + "context" + "log" + "os" + + "git.fossy.my.id/bagas/tunnel-please-controller/db/sqlc/repository" + "git.fossy.my.id/bagas/tunnel-please-controller/server" + "github.com/jackc/pgx/v5" + "github.com/joho/godotenv" +) + +func main() { + if _, err := os.Stat(".env"); err == nil { + if err := godotenv.Load(".env"); err != nil { + log.Printf("Warning: Failed to load .env file: %s", err) + } + } + + ctx := context.Background() + + connect, err := pgx.Connect(ctx, os.Getenv("DATABASE_URL")) + if err != nil { + panic(err) + return + } + defer func(connect *pgx.Conn, ctx context.Context) { + err := connect.Close(ctx) + if err != nil { + panic(err) + } + }(connect, ctx) + + repo := repository.New(connect) + s := server.New(repo) + + log.Printf("Listening on :8080\n") + err = s.ListenAndServe(":8080") + if err != nil { + panic(err) + return + } +} diff --git a/server/server.go b/server/server.go new file mode 100644 index 0000000..2668ab0 --- /dev/null +++ b/server/server.go @@ -0,0 +1,79 @@ +package server + +import ( + "context" + "log" + mathrand "math/rand" + "net" + "strings" + "time" + + "git.fossy.my.id/bagas/tunnel-please-controller/db/sqlc/repository" + identifier "git.fossy.my.id/bagas/tunnel-please-grpc/gen" + "github.com/google/uuid" + "google.golang.org/grpc" + "google.golang.org/grpc/reflection" + "google.golang.org/protobuf/types/known/emptypb" +) + +type Server struct { + Database *repository.Queries + identifier.UnimplementedIdentityServer +} + +func (s *Server) Get(ctx context.Context, request *identifier.IdentifierRequest) (*identifier.IdentifierResponse, error) { + parse, err := uuid.Parse(request.GetId()) + if err != nil { + return nil, err + } + data, err := s.Database.GetIdentifierById(ctx, parse) + if err != nil { + return nil, err + } + return &identifier.IdentifierResponse{ + Id: data.ID.String(), + Slug: data.Slug, + }, nil +} + +func (s *Server) Create(ctx context.Context, request *emptypb.Empty) (*identifier.IdentifierResponse, error) { + createIdentifier, err := s.Database.CreateIdentifier(ctx, GenerateRandomString(32)) + if err != nil { + return nil, err + } + return &identifier.IdentifierResponse{ + Id: createIdentifier.ID.String(), + Slug: createIdentifier.Slug, + }, nil +} + +func GenerateRandomString(length int) string { + const charset = "abcdefghijklmnopqrstuvwxyz" + seededRand := mathrand.New(mathrand.NewSource(time.Now().UnixNano() + int64(mathrand.Intn(9999)))) + var result strings.Builder + for i := 0; i < length; i++ { + randomIndex := seededRand.Intn(len(charset)) + result.WriteString(string(charset[randomIndex])) + } + return result.String() +} + +func New(database *repository.Queries) *Server { + return &Server{Database: database} +} + +func (s *Server) ListenAndServe(Addr string) error { + listener, err := net.Listen("tcp", Addr) + if err != nil { + return err + } + + grpcServer := grpc.NewServer() + reflection.Register(grpcServer) + + identifier.RegisterIdentityServer(grpcServer, s) + if err := grpcServer.Serve(listener); err != nil { + log.Fatalf("failed to serve: %v", err) + } + return nil +}