Skip to content
This repository was archived by the owner on Jul 9, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ WORKDIR /build
COPY go.mod go.sum ./
RUN go mod download
COPY api/ ./api/
COPY common/ ./common/
RUN CGO_ENABLED=0 go build -o /build/bin/api ./api/cmd/api

FROM scratch
Expand Down
29 changes: 23 additions & 6 deletions api/cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,44 @@ import (

"github.com/caarlos0/env/v11"
"github.com/fundament-oss/dcim/api/pkg/dcim"
"github.com/fundament-oss/fundament/common/psqldb"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
)

type config struct {
DatabaseURL string `env:"DATABASE_URL,required"`
ListenAddr string `env:"LISTEN_ADDR" envDefault:":8080"`
LogLevel string `env:"LOG_LEVEL" envDefault:"info"`
Database psqldb.Config
ListenAddr string `env:"LISTEN_ADDR" envDefault:":8080"`
LogLevel string `env:"LOG_LEVEL" envDefault:"info"`
}

func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}

func run() error {
cfg, err := env.ParseAs[config]()
if err != nil {
fmt.Fprintf(os.Stderr, "config: %v\n", err)
os.Exit(1)
return fmt.Errorf("config: %w", err)
}

level := slog.LevelInfo
_ = level.UnmarshalText([]byte(cfg.LogLevel))
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: level}))
slog.SetDefault(logger)

server := dcim.New(logger)
ctx := context.Background()

database, err := psqldb.New(ctx, logger, cfg.Database)
if err != nil {
return fmt.Errorf("database: %w", err)
}
defer database.Close()

server := dcim.New(logger, database)

mux := http.NewServeMux()
mux.HandleFunc("GET /livez", func(w http.ResponseWriter, _ *http.Request) {
Expand Down Expand Up @@ -67,4 +82,6 @@ func main() {
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_ = srv.Shutdown(shutdownCtx)

return nil
}
1 change: 1 addition & 0 deletions api/hotreload/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY api/ ./api/
COPY common/ ./common/
CMD ["air", "-c", "api/hotreload/.air.toml"]
32 changes: 32 additions & 0 deletions api/pkg/db/gen/db.sqlc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions api/pkg/db/gen/models.sqlc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

138 changes: 138 additions & 0 deletions api/pkg/db/gen/site.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/pkg/db/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//go:generate sqlc generate
package db
26 changes: 26 additions & 0 deletions api/pkg/db/site.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- name: SiteList :many
SELECT id, name, address, created
FROM core.sites
WHERE deleted IS NULL
ORDER BY created;

-- name: SiteGetByID :one
SELECT id, name, address, created
FROM core.sites
WHERE id = $1 AND deleted IS NULL;

-- name: SiteCreate :one
INSERT INTO core.sites (name, address)
VALUES ($1, $2)
RETURNING id;

-- name: SiteUpdate :execrows
UPDATE core.sites
SET name = COALESCE(sqlc.narg('name'), name),
address = COALESCE(sqlc.narg('address'), address)
WHERE id = $1 AND deleted IS NULL;

-- name: SiteDelete :execrows
UPDATE core.sites
SET deleted = now()
WHERE id = $1 AND deleted IS NULL;
19 changes: 19 additions & 0 deletions api/pkg/db/sqlc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: "2"
sql:
- engine: "postgresql"
queries: "."
schema: "../../../db/migrations/001_init.up.sql"
gen:
go:
package: "db"
out: "gen"
sql_package: "pgx/v5"
query_parameter_limit: 0
omit_unused_structs: true
output_db_file_name: "db.sqlc.go"
output_models_file_name: "models.sqlc.go"
overrides:
- db_type: "uuid"
go_type:
import: "github.com/google/uuid"
type: "UUID"
10 changes: 8 additions & 2 deletions api/pkg/dcim/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,25 @@ import (
"connectrpc.com/connect"
"connectrpc.com/grpcreflect"
"connectrpc.com/validate"
db "github.com/fundament-oss/dcim/api/pkg/db/gen"
"github.com/fundament-oss/dcim/api/pkg/proto/gen/v1/dcimv1connect"
"github.com/fundament-oss/fundament/common/connectrecovery"
"github.com/fundament-oss/fundament/common/psqldb"
"github.com/svrana/go-connect-middleware/interceptors/logging"
)

type Server struct {
logger *slog.Logger
db *psqldb.DB
queries *db.Queries
handler http.Handler
}

func New(logger *slog.Logger) *Server {
func New(logger *slog.Logger, database *psqldb.DB) *Server {
s := &Server{
logger: logger,
logger: logger,
db: database,
queries: db.New(database.Pool),
}

mux := http.NewServeMux()
Expand Down
20 changes: 0 additions & 20 deletions api/pkg/dcim/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,6 @@ import (
dcimv1 "github.com/fundament-oss/dcim/api/pkg/proto/gen/v1"
)

func (s *Server) ListSites(ctx context.Context, req *connect.Request[dcimv1.ListSitesRequest]) (*connect.Response[dcimv1.ListSitesResponse], error) {
return nil, connect.NewError(connect.CodeUnimplemented, nil)
}

func (s *Server) GetSite(ctx context.Context, req *connect.Request[dcimv1.GetSiteRequest]) (*connect.Response[dcimv1.GetSiteResponse], error) {
return nil, connect.NewError(connect.CodeUnimplemented, nil)
}

func (s *Server) CreateSite(ctx context.Context, req *connect.Request[dcimv1.CreateSiteRequest]) (*connect.Response[dcimv1.CreateSiteResponse], error) {
return nil, connect.NewError(connect.CodeUnimplemented, nil)
}

func (s *Server) UpdateSite(ctx context.Context, req *connect.Request[dcimv1.UpdateSiteRequest]) (*connect.Response[dcimv1.UpdateSiteResponse], error) {
return nil, connect.NewError(connect.CodeUnimplemented, nil)
}

func (s *Server) DeleteSite(ctx context.Context, req *connect.Request[dcimv1.DeleteSiteRequest]) (*connect.Response[dcimv1.DeleteSiteResponse], error) {
return nil, connect.NewError(connect.CodeUnimplemented, nil)
}

func (s *Server) ListRooms(ctx context.Context, req *connect.Request[dcimv1.ListRoomsRequest]) (*connect.Response[dcimv1.ListRoomsResponse], error) {
return nil, connect.NewError(connect.CodeUnimplemented, nil)
}
Expand Down
35 changes: 35 additions & 0 deletions api/pkg/dcim/site_convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package dcim

import (
db "github.com/fundament-oss/dcim/api/pkg/db/gen"
dcimv1 "github.com/fundament-oss/dcim/api/pkg/proto/gen/v1"
"google.golang.org/protobuf/types/known/timestamppb"
)

func siteFromRow(row *db.SiteGetByIDRow) *dcimv1.Site {
site := dcimv1.Site_builder{
Id: row.ID.String(),
Name: row.Name,
Created: timestamppb.New(row.Created.Time),
}.Build()

if row.Address.Valid {
site.SetAddress(row.Address.String)
}

return site
}

func siteFromListRow(row *db.SiteListRow) *dcimv1.Site {
site := dcimv1.Site_builder{
Id: row.ID.String(),
Name: row.Name,
Created: timestamppb.New(row.Created.Time),
}.Build()

if row.Address.Valid {
site.SetAddress(row.Address.String)
}

return site
}
Loading
Loading