Skip to content

Commit 37f3d1f

Browse files
authored
Merge pull request #42 from makeopensource/dev
Dev
2 parents b77a326 + 3f2b831 commit 37f3d1f

59 files changed

Lines changed: 1074 additions & 873 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*.dll
55
*.so
66
*.dylib
7-
7+
*.sock
88
# Test binary, built with `go test -c`
99
*.test
1010

Dockerfile

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
FROM golang:1.24-alpine AS builder
22

3-
# Build arguments
4-
ARG VERSION=dev
5-
ARG COMMIT_INFO=unknown
6-
ARG BUILD_DATE=unknown
7-
ARG BRANCH=unknown
8-
93
# for sqlite
104
RUN apk update && apk add --no-cache gcc musl-dev
115
ENV CGO_ENABLED=1
@@ -19,6 +13,12 @@ RUN go mod download
1913

2014
COPY ./src .
2115

16+
# Build arguments
17+
ARG VERSION=dev
18+
ARG COMMIT_INFO=unknown
19+
ARG BUILD_DATE=unknown
20+
ARG BRANCH=unknown
21+
2222
# arg substitution, do not put it higher than this for caching
2323
# https://stackoverflow.com/questions/44438637/arg-substitution-in-run-command-not-working-for-dockerfile
2424
ENV VERSION=${VERSION}
@@ -29,12 +29,13 @@ ENV BRANCH=${BRANCH}
2929
# build optimized binary without debugging symbols
3030
RUN SOURCE_HASH=$(find . -type f -name "*.go" -print0 | sort -z | xargs -0 cat | sha256sum | cut -d ' ' -f1) && \
3131
go build -ldflags "-s -w \
32-
-X github.com/makeopensource/leviathan/common.Version=${VERSION} \
33-
-X github.com/makeopensource/leviathan/common.CommitInfo=${COMMIT_INFO} \
34-
-X github.com/makeopensource/leviathan/common.BuildDate=${BUILD_DATE} \
35-
-X github.com/makeopensource/leviathan/common.Branch=${BRANCH} \
36-
-X github.com/makeopensource/leviathan/common.SourceHash=${SOURCE_HASH}" \
37-
-o leviathan
32+
-X github.com/makeopensource/leviathan/internal/info.Version=${VERSION} \
33+
-X github.com/makeopensource/leviathan/internal/info.CommitInfo=${COMMIT_INFO} \
34+
-X github.com/makeopensource/leviathan/internal/info.BuildDate=${BUILD_DATE} \
35+
-X github.com/makeopensource/leviathan/internal/info.Branch=${BRANCH} \
36+
-X github.com/makeopensource/leviathan/internal/info.SourceHash=${SOURCE_HASH}" \
37+
-o leviathan \
38+
./cmd/server/main.go
3839

3940
FROM alpine:latest
4041

src/api/api.go

Lines changed: 0 additions & 73 deletions
This file was deleted.

src/api/v1/docker_impl.go

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/cmd/api.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package cmd
2+
3+
import (
4+
"connectrpc.com/connect"
5+
"fmt"
6+
dockerrpc "github.com/makeopensource/leviathan/generated/docker_rpc/v1/v1connect"
7+
jobrpc "github.com/makeopensource/leviathan/generated/jobs/v1/v1connect"
8+
labrpc "github.com/makeopensource/leviathan/generated/labs/v1/v1connect"
9+
"github.com/makeopensource/leviathan/internal/config"
10+
"github.com/makeopensource/leviathan/internal/docker"
11+
fm "github.com/makeopensource/leviathan/internal/file_manager"
12+
"github.com/makeopensource/leviathan/internal/jobs"
13+
"github.com/makeopensource/leviathan/internal/labs"
14+
"github.com/rs/zerolog/log"
15+
"golang.org/x/net/http2"
16+
"golang.org/x/net/http2/h2c"
17+
"net/http"
18+
)
19+
20+
// StartServerWithAddr starts a server using the port specified in the config
21+
func StartServerWithAddr() {
22+
srvAddr := fmt.Sprintf(":%s", config.ServerPort.GetStr())
23+
StartServer(srvAddr)
24+
}
25+
26+
// StartServer starts the grpc server at "addr:port"
27+
//
28+
// e.g. StartServer(":8080") or StartServer("127.0.0.1:8080")
29+
func StartServer(srvAddr string) {
30+
mux := setupEndpoints()
31+
32+
log.Info().Msg("Leviathan initialized successfully")
33+
log.Info().Msgf("starting server on %s", srvAddr)
34+
err := http.ListenAndServe(
35+
srvAddr,
36+
// Use h2c so we can serve HTTP/2 without TLS.
37+
h2c.NewHandler(mux, &http2.Server{}),
38+
)
39+
if err != nil {
40+
log.Fatal().Err(err).Msgf("Failed to start server on %s", srvAddr)
41+
return
42+
}
43+
}
44+
45+
func setupEndpoints() *http.ServeMux {
46+
dk, job, lab := InitServices()
47+
48+
interceptor := connect.WithInterceptors()
49+
if config.ApiKey.GetStr() != "" {
50+
log.Info().Msg("ApiKey is set, endpoints now require authentication")
51+
interceptor = connect.WithInterceptors(&authInterceptor{config.ApiKey.GetStr()})
52+
}
53+
54+
v1Endpoints := []func() (string, http.Handler){
55+
// jobs endpoints
56+
func() (string, http.Handler) {
57+
jobSrv := jobs.NewJobServer(job)
58+
return jobrpc.NewJobServiceHandler(jobSrv, interceptor)
59+
},
60+
// docker endpoints
61+
func() (string, http.Handler) {
62+
dkSrv := &docker.Server{Service: dk}
63+
return dockerrpc.NewDockerServiceHandler(dkSrv, interceptor)
64+
},
65+
func() (string, http.Handler) {
66+
labSrv := labs.LabServer{Srv: lab}
67+
return labrpc.NewLabServiceHandler(labSrv, interceptor)
68+
},
69+
func() (string, http.Handler) {
70+
fileHandler := fm.NewFileManagerHandler("/files.v1")
71+
return fileHandler.BasePath + "/", fileHandler
72+
},
73+
}
74+
75+
mux := http.NewServeMux()
76+
for _, svc := range v1Endpoints {
77+
path, handler := svc()
78+
mux.Handle(path, handler)
79+
}
80+
81+
return mux
82+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package api
1+
package cmd
22

33
import (
44
"connectrpc.com/connect"

src/cmd/server/main.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
import (
4+
"github.com/makeopensource/leviathan/cmd"
5+
"github.com/makeopensource/leviathan/internal/info"
6+
)
7+
8+
func main() {
9+
info.PrintInfo()
10+
cmd.Setup()
11+
cmd.StartServerWithAddr()
12+
}

src/cmd/setup.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package cmd
2+
3+
import (
4+
"github.com/makeopensource/leviathan/internal/config"
5+
"github.com/makeopensource/leviathan/internal/database"
6+
"github.com/makeopensource/leviathan/internal/docker"
7+
fu "github.com/makeopensource/leviathan/internal/file_manager"
8+
"github.com/makeopensource/leviathan/internal/jobs"
9+
"github.com/makeopensource/leviathan/internal/labs"
10+
"github.com/makeopensource/leviathan/pkg/logger"
11+
"github.com/rs/zerolog/log"
12+
)
13+
14+
func Setup() {
15+
log.Logger = logger.ConsoleLogger() // logs here are not saved to the log file
16+
config.LoadConfig()
17+
// once the log dir and level is set by config,
18+
// we start a file logger along with the console logger
19+
log.Logger = logger.FileConsoleLogger(config.LogDir.GetStr(), config.LogLevel.GetStr())
20+
}
21+
22+
func InitServices() (*docker.DkService, *jobs.JobService, *labs.LabService) {
23+
db, bc := database.NewDatabaseWithGorm()
24+
25+
dkService := docker.NewDockerServiceWithClients()
26+
fileManService := fu.NewFileManagerService()
27+
labService := labs.NewLabService(db, dkService, fileManService)
28+
jobService := jobs.NewJobService(db, bc, dkService, labService, fileManService)
29+
30+
return dkService, jobService, labService
31+
}

src/go.mod

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@ go 1.24
44

55
require (
66
connectrpc.com/connect v1.18.1
7-
github.com/docker/cli v28.0.4+incompatible
8-
github.com/docker/docker v28.0.4+incompatible
7+
github.com/docker/cli v28.2.1+incompatible
8+
github.com/docker/docker v28.2.1+incompatible
99
github.com/google/uuid v1.6.0
1010
github.com/joho/godotenv v1.5.1
1111
github.com/opencontainers/image-spec v1.1.1
1212
github.com/rs/zerolog v1.34.0
1313
github.com/spf13/viper v1.20.1
1414
github.com/stretchr/testify v1.10.0
15-
golang.org/x/crypto v0.36.0
16-
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394
17-
golang.org/x/net v0.38.0
15+
golang.org/x/crypto v0.38.0
16+
golang.org/x/exp v0.0.0-20250506013437-ce4c2cf36ca6
17+
golang.org/x/net v0.40.0
1818
google.golang.org/protobuf v1.36.6
1919
gopkg.in/natefinch/lumberjack.v2 v2.2.1
20-
gorm.io/driver/postgres v1.5.11
20+
gorm.io/driver/postgres v1.6.0
2121
gorm.io/driver/sqlite v1.5.7
22-
gorm.io/gorm v1.25.12
22+
gorm.io/gorm v1.30.0
2323
)
2424

2525
require (
@@ -31,32 +31,32 @@ require (
3131
github.com/docker/go-connections v0.5.0 // indirect
3232
github.com/docker/go-units v0.5.0 // indirect
3333
github.com/felixge/httpsnoop v1.0.4 // indirect
34-
github.com/fsnotify/fsnotify v1.8.0 // indirect
34+
github.com/fsnotify/fsnotify v1.9.0 // indirect
3535
github.com/go-logr/logr v1.4.2 // indirect
3636
github.com/go-logr/stdr v1.2.2 // indirect
3737
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
3838
github.com/gogo/protobuf v1.3.2 // indirect
3939
github.com/jackc/pgpassfile v1.0.0 // indirect
4040
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
41-
github.com/jackc/pgx/v5 v5.7.4 // indirect
41+
github.com/jackc/pgx/v5 v5.7.5 // indirect
4242
github.com/jackc/puddle/v2 v2.2.2 // indirect
4343
github.com/jinzhu/inflection v1.0.0 // indirect
4444
github.com/jinzhu/now v1.1.5 // indirect
4545
github.com/mattn/go-colorable v0.1.14 // indirect
4646
github.com/mattn/go-isatty v0.0.20 // indirect
47-
github.com/mattn/go-sqlite3 v1.14.24 // indirect
47+
github.com/mattn/go-sqlite3 v1.14.28 // indirect
4848
github.com/moby/docker-image-spec v1.3.1 // indirect
4949
github.com/moby/term v0.5.0 // indirect
5050
github.com/morikuni/aec v1.0.0 // indirect
5151
github.com/opencontainers/go-digest v1.0.0 // indirect
52-
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
52+
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
5353
github.com/pkg/errors v0.9.1 // indirect
5454
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
5555
github.com/sagikazarmark/locafero v0.9.0 // indirect
5656
github.com/sirupsen/logrus v1.9.3 // indirect
5757
github.com/sourcegraph/conc v0.3.0 // indirect
5858
github.com/spf13/afero v1.14.0 // indirect
59-
github.com/spf13/cast v1.7.1 // indirect
59+
github.com/spf13/cast v1.8.0 // indirect
6060
github.com/spf13/pflag v1.0.6 // indirect
6161
github.com/subosito/gotenv v1.6.0 // indirect
6262
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
@@ -66,9 +66,9 @@ require (
6666
go.opentelemetry.io/otel/metric v1.35.0 // indirect
6767
go.opentelemetry.io/otel/trace v1.35.0 // indirect
6868
go.uber.org/multierr v1.11.0 // indirect
69-
golang.org/x/sync v0.12.0 // indirect
70-
golang.org/x/sys v0.31.0 // indirect
71-
golang.org/x/text v0.23.0 // indirect
69+
golang.org/x/sync v0.14.0 // indirect
70+
golang.org/x/sys v0.33.0 // indirect
71+
golang.org/x/text v0.25.0 // indirect
7272
golang.org/x/time v0.11.0 // indirect
7373
gopkg.in/yaml.v3 v3.0.1 // indirect
7474
gotest.tools/v3 v3.5.1 // indirect

0 commit comments

Comments
 (0)