Skip to content

Commit 83f684f

Browse files
authored
feat: implements Foundry API server (#156)
1 parent 2e5a103 commit 83f684f

40 files changed

+3389
-49
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ node_modules
1010
!/.vscode/launch.recommended.json
1111
!/.vscode/settings.recommended.json
1212
!/.vscode/tasks.recommended.json
13+
14+
.env

cli/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ go 1.23.0
44

55
require (
66
cuelang.org/go v0.12.0
7-
github.com/adrg/xdg v0.5.3
87
github.com/alecthomas/kong v0.9.0
98
github.com/aws/aws-sdk-go v1.55.5
109
github.com/aws/aws-sdk-go-v2 v1.32.6
@@ -44,6 +43,7 @@ require (
4443
github.com/Masterminds/squirrel v1.5.4 // indirect
4544
github.com/Microsoft/go-winio v0.6.2 // indirect
4645
github.com/ProtonMail/go-crypto v1.0.0 // indirect
46+
github.com/adrg/xdg v0.5.3 // indirect
4747
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230219212500-1f9a474cc2dc // indirect
4848
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
4949
github.com/aws/aws-sdk-go-v2/credentials v1.17.38 // indirect

foundry/api/.env.example

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Server configuration
2+
export HTTP_PORT=8080
3+
export SERVER_TIMEOUT=30s
4+
5+
# Database configuration
6+
export DB_HOST=localhost
7+
export DB_PORT=5432
8+
export DB_USER=postgres
9+
export DB_PASSWORD=postgres
10+
export DB_NAME=releases
11+
export DB_SSLMODE=disable
12+
13+
# Logging configuration
14+
export LOG_LEVEL=debug
15+
export LOG_FORMAT=text

foundry/api/Earthfile

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ src:
2020

2121
CACHE --persist --sharing shared /go
2222

23-
COPY --dir cmd .
23+
COPY --dir client cmd internal pkg .
2424
RUN go generate ./...
2525

2626
check:
@@ -37,34 +37,55 @@ build:
3737
ARG version="0.0.0"
3838

3939
ENV CGO_ENABLED=0
40-
RUN go build -ldflags="-extldflags=-static -X main.version=$version" -o bin/foundry cmd/main.go
40+
RUN go build -ldflags="-extldflags=-static -X main.version=$version" -o bin/foundry-api cmd/api/main.go
4141
RUN file bin/foundry
4242

43-
SAVE ARTIFACT bin/foundry foundry
43+
SAVE ARTIFACT bin/foundry-api foundry-api
4444

4545
test:
46-
FROM +build
46+
FROM earthly/dind:ubuntu-24.04-docker-27.3.1-1
4747

48-
RUN go test ./...
48+
COPY docker-compose.yml .
49+
50+
WITH DOCKER \
51+
--load foundry-api:latest=(+docker) \
52+
--load foundry-api-test:latest=(+docker-test) \
53+
--compose docker-compose.yml \
54+
--service api \
55+
--service postgres
56+
RUN docker compose up api-test
57+
END
58+
59+
docker-test:
60+
FROM +src
61+
62+
COPY --dir test .
63+
64+
ENTRYPOINT ["/usr/local/go/bin/go", "test", "-v", "./test/..."]
65+
SAVE IMAGE foundry-api-test:latest
4966

5067
docker:
5168
FROM debian:bookworm-slim
5269
WORKDIR /app
5370

54-
ARG container="foundry"
71+
ARG container="foundry-api"
5572
ARG tag="latest"
5673
ARG version="dev"
5774

5875
ARG TARGETOS
5976
ARG TARGETARCH
6077
ARG USERPLATFORM
6178

79+
RUN apt-get update && apt-get install -y curl postgresql-client
80+
6281
COPY \
6382
--platform=$USERPLATFORM \
64-
(+build/foundry \
83+
(+build/foundry-api \
6584
--GOOS=$TARGETOS \
6685
--GOARCH=$TARGETARCH \
67-
--version=$version) foundry
86+
--version=$version) foundry-api
87+
COPY --dir sql .
88+
COPY entrypoint.sh .
6889

69-
ENTRYPOINT ["/app/foundry"]
90+
ENTRYPOINT [ "/bin/bash", "/app/entrypoint.sh" ]
7091
SAVE IMAGE ${container}:${tag}

foundry/api/blueprint.cue

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@ project: {
77
version: string | *"dev" @forge(name="GIT_TAG")
88
}
99
}
10+
1011
github: {
1112
args: {
1213
version: string | *"dev" @forge(name="GIT_TAG")
1314
}
1415
}
16+
17+
test: privileged: true
1518
}
1619
deployment: {
1720
on: {
1821
merge: {}
1922
tag: {}
2023
}
24+
2125
bundle: {
2226
env: "shared-services"
2327
modules: main: {
@@ -29,26 +33,100 @@ project: {
2933
name: _ @forge(name="CONTAINER_IMAGE")
3034
tag: _ @forge(name="GIT_HASH_OR_TAG")
3135
}
36+
37+
env: {
38+
"HTTP_PORT": {
39+
value: "8080"
40+
}
41+
"GIN_MODE": {
42+
value: "debug"
43+
}
44+
"LOG_LEVEL": {
45+
value: "debug"
46+
}
47+
"LOG_FORMAT": {
48+
value: "json"
49+
}
50+
"DB_INIT": {
51+
value: "true"
52+
}
53+
"DB_SSLMODE": {
54+
value: "enable"
55+
}
56+
"DB_NAME": {
57+
value: "foundry"
58+
}
59+
"DB_HOST": {
60+
secret: {
61+
name: "db"
62+
key: "host"
63+
}
64+
}
65+
"DB_PORT": {
66+
secret: {
67+
name: "db"
68+
key: "port"
69+
}
70+
}
71+
"DB_USER": {
72+
secret: {
73+
name: "db"
74+
key: "username"
75+
}
76+
}
77+
"DB_PASSWORD": {
78+
secret: {
79+
name: "db"
80+
key: "password"
81+
}
82+
}
83+
"DB_SUPER_USER": {
84+
secret: {
85+
name: "db-root"
86+
key: "username"
87+
}
88+
}
89+
"DB_SUPER_PASSWORD": {
90+
secret: {
91+
name: "db-root"
92+
key: "password"
93+
}
94+
}
95+
}
96+
3297
port: 8080
98+
3399
probes: {
34-
liveness: path: "/"
35-
readiness: path: "/"
100+
liveness: path: "/healthz"
101+
readiness: path: "/healthz"
36102
}
37103
}
104+
38105
service: {
39106
targetPort: 8080
40107
port: 8080
41108
}
109+
110+
secrets: {
111+
db: {
112+
ref: "db/foundry"
113+
}
114+
"db-root": {
115+
ref: "db/root_account"
116+
}
117+
}
42118
}
43119
}
44120
}
45121
}
122+
46123
release: {
47124
docker: {
48125
on: {
49126
merge: {}
50127
tag: {}
51128
}
129+
52130
config: {
53131
tag: _ @forge(name="GIT_HASH_OR_TAG")
54132
}

foundry/api/client/alias.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package client
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
)
8+
9+
// CreateAlias creates a new alias for a release
10+
func (c *HTTPClient) CreateAlias(ctx context.Context, aliasName string, releaseID string) error {
11+
path := fmt.Sprintf("/release/alias/%s", aliasName)
12+
13+
payload := struct {
14+
ReleaseID string `json:"release_id"`
15+
}{
16+
ReleaseID: releaseID,
17+
}
18+
19+
return c.do(ctx, http.MethodPost, path, payload, nil)
20+
}
21+
22+
// DeleteAlias removes an alias
23+
func (c *HTTPClient) DeleteAlias(ctx context.Context, aliasName string) error {
24+
path := fmt.Sprintf("/release/alias/%s", aliasName)
25+
return c.do(ctx, http.MethodDelete, path, nil, nil)
26+
}
27+
28+
// ListAliases retrieves all aliases for a release
29+
func (c *HTTPClient) ListAliases(ctx context.Context, releaseID string) ([]ReleaseAlias, error) {
30+
path := fmt.Sprintf("/release/%s/aliases", releaseID)
31+
32+
var resp []ReleaseAlias
33+
err := c.do(ctx, http.MethodGet, path, nil, &resp)
34+
if err != nil {
35+
return nil, err
36+
}
37+
38+
return resp, nil
39+
}

0 commit comments

Comments
 (0)