Skip to content
Merged

Dev #42

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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*.dll
*.so
*.dylib

*.sock
# Test binary, built with `go test -c`
*.test

Expand Down
25 changes: 13 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
FROM golang:1.24-alpine AS builder

# Build arguments
ARG VERSION=dev
ARG COMMIT_INFO=unknown
ARG BUILD_DATE=unknown
ARG BRANCH=unknown

# for sqlite
RUN apk update && apk add --no-cache gcc musl-dev
ENV CGO_ENABLED=1
Expand All @@ -19,6 +13,12 @@ RUN go mod download

COPY ./src .

# Build arguments
ARG VERSION=dev
ARG COMMIT_INFO=unknown
ARG BUILD_DATE=unknown
ARG BRANCH=unknown

# arg substitution, do not put it higher than this for caching
# https://stackoverflow.com/questions/44438637/arg-substitution-in-run-command-not-working-for-dockerfile
ENV VERSION=${VERSION}
Expand All @@ -29,12 +29,13 @@ ENV BRANCH=${BRANCH}
# build optimized binary without debugging symbols
RUN SOURCE_HASH=$(find . -type f -name "*.go" -print0 | sort -z | xargs -0 cat | sha256sum | cut -d ' ' -f1) && \
go build -ldflags "-s -w \
-X github.com/makeopensource/leviathan/common.Version=${VERSION} \
-X github.com/makeopensource/leviathan/common.CommitInfo=${COMMIT_INFO} \
-X github.com/makeopensource/leviathan/common.BuildDate=${BUILD_DATE} \
-X github.com/makeopensource/leviathan/common.Branch=${BRANCH} \
-X github.com/makeopensource/leviathan/common.SourceHash=${SOURCE_HASH}" \
-o leviathan
-X github.com/makeopensource/leviathan/internal/info.Version=${VERSION} \
-X github.com/makeopensource/leviathan/internal/info.CommitInfo=${COMMIT_INFO} \
-X github.com/makeopensource/leviathan/internal/info.BuildDate=${BUILD_DATE} \
-X github.com/makeopensource/leviathan/internal/info.Branch=${BRANCH} \
-X github.com/makeopensource/leviathan/internal/info.SourceHash=${SOURCE_HASH}" \
-o leviathan \
./cmd/server/main.go

FROM alpine:latest

Expand Down
73 changes: 0 additions & 73 deletions src/api/api.go

This file was deleted.

50 changes: 0 additions & 50 deletions src/api/v1/docker_impl.go

This file was deleted.

82 changes: 82 additions & 0 deletions src/cmd/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package cmd

import (
"connectrpc.com/connect"
"fmt"
dockerrpc "github.com/makeopensource/leviathan/generated/docker_rpc/v1/v1connect"
jobrpc "github.com/makeopensource/leviathan/generated/jobs/v1/v1connect"
labrpc "github.com/makeopensource/leviathan/generated/labs/v1/v1connect"
"github.com/makeopensource/leviathan/internal/config"
"github.com/makeopensource/leviathan/internal/docker"
fm "github.com/makeopensource/leviathan/internal/file_manager"
"github.com/makeopensource/leviathan/internal/jobs"
"github.com/makeopensource/leviathan/internal/labs"
"github.com/rs/zerolog/log"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"net/http"
)

// StartServerWithAddr starts a server using the port specified in the config
func StartServerWithAddr() {
srvAddr := fmt.Sprintf(":%s", config.ServerPort.GetStr())
StartServer(srvAddr)
}

// StartServer starts the grpc server at "addr:port"
//
// e.g. StartServer(":8080") or StartServer("127.0.0.1:8080")
func StartServer(srvAddr string) {
mux := setupEndpoints()

log.Info().Msg("Leviathan initialized successfully")
log.Info().Msgf("starting server on %s", srvAddr)
err := http.ListenAndServe(
srvAddr,
// Use h2c so we can serve HTTP/2 without TLS.
h2c.NewHandler(mux, &http2.Server{}),
)
if err != nil {
log.Fatal().Err(err).Msgf("Failed to start server on %s", srvAddr)
return
}
}

func setupEndpoints() *http.ServeMux {
dk, job, lab := InitServices()

interceptor := connect.WithInterceptors()
if config.ApiKey.GetStr() != "" {
log.Info().Msg("ApiKey is set, endpoints now require authentication")
interceptor = connect.WithInterceptors(&authInterceptor{config.ApiKey.GetStr()})
}

v1Endpoints := []func() (string, http.Handler){
// jobs endpoints
func() (string, http.Handler) {
jobSrv := jobs.NewJobServer(job)
return jobrpc.NewJobServiceHandler(jobSrv, interceptor)
},
// docker endpoints
func() (string, http.Handler) {
dkSrv := &docker.Server{Service: dk}
return dockerrpc.NewDockerServiceHandler(dkSrv, interceptor)
},
func() (string, http.Handler) {
labSrv := labs.LabServer{Srv: lab}
return labrpc.NewLabServiceHandler(labSrv, interceptor)
},
func() (string, http.Handler) {
fileHandler := fm.NewFileManagerHandler("/files.v1")
return fileHandler.BasePath + "/", fileHandler
},
}

mux := http.NewServeMux()
for _, svc := range v1Endpoints {
path, handler := svc()
mux.Handle(path, handler)
}

return mux
}
2 changes: 1 addition & 1 deletion src/api/auth_middleware.go → src/cmd/auth_middleware.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package api
package cmd

import (
"connectrpc.com/connect"
Expand Down
12 changes: 12 additions & 0 deletions src/cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"github.com/makeopensource/leviathan/cmd"
"github.com/makeopensource/leviathan/internal/info"
)

func main() {
info.PrintInfo()
cmd.Setup()
cmd.StartServerWithAddr()
}
31 changes: 31 additions & 0 deletions src/cmd/setup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cmd

import (
"github.com/makeopensource/leviathan/internal/config"
"github.com/makeopensource/leviathan/internal/database"
"github.com/makeopensource/leviathan/internal/docker"
fu "github.com/makeopensource/leviathan/internal/file_manager"
"github.com/makeopensource/leviathan/internal/jobs"
"github.com/makeopensource/leviathan/internal/labs"
"github.com/makeopensource/leviathan/pkg/logger"
"github.com/rs/zerolog/log"
)

func Setup() {
log.Logger = logger.ConsoleLogger() // logs here are not saved to the log file
config.LoadConfig()
// once the log dir and level is set by config,
// we start a file logger along with the console logger
log.Logger = logger.FileConsoleLogger(config.LogDir.GetStr(), config.LogLevel.GetStr())
}

func InitServices() (*docker.DkService, *jobs.JobService, *labs.LabService) {
db, bc := database.NewDatabaseWithGorm()

dkService := docker.NewDockerServiceWithClients()
fileManService := fu.NewFileManagerService()
labService := labs.NewLabService(db, dkService, fileManService)
jobService := jobs.NewJobService(db, bc, dkService, labService, fileManService)

return dkService, jobService, labService
}
30 changes: 15 additions & 15 deletions src/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ go 1.24

require (
connectrpc.com/connect v1.18.1
github.com/docker/cli v28.0.4+incompatible
github.com/docker/docker v28.0.4+incompatible
github.com/docker/cli v28.2.1+incompatible
github.com/docker/docker v28.2.1+incompatible
github.com/google/uuid v1.6.0
github.com/joho/godotenv v1.5.1
github.com/opencontainers/image-spec v1.1.1
github.com/rs/zerolog v1.34.0
github.com/spf13/viper v1.20.1
github.com/stretchr/testify v1.10.0
golang.org/x/crypto v0.36.0
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394
golang.org/x/net v0.38.0
golang.org/x/crypto v0.38.0
golang.org/x/exp v0.0.0-20250506013437-ce4c2cf36ca6
golang.org/x/net v0.40.0
google.golang.org/protobuf v1.36.6
gopkg.in/natefinch/lumberjack.v2 v2.2.1
gorm.io/driver/postgres v1.5.11
gorm.io/driver/postgres v1.6.0
gorm.io/driver/sqlite v1.5.7
gorm.io/gorm v1.25.12
gorm.io/gorm v1.30.0
)

require (
Expand All @@ -31,32 +31,32 @@ require (
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/fsnotify/fsnotify v1.9.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgx/v5 v5.7.4 // indirect
github.com/jackc/pgx/v5 v5.7.5 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-sqlite3 v1.14.24 // indirect
github.com/mattn/go-sqlite3 v1.14.28 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/sagikazarmark/locafero v0.9.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.14.0 // indirect
github.com/spf13/cast v1.7.1 // indirect
github.com/spf13/cast v1.8.0 // indirect
github.com/spf13/pflag v1.0.6 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
Expand All @@ -66,9 +66,9 @@ require (
go.opentelemetry.io/otel/metric v1.35.0 // indirect
go.opentelemetry.io/otel/trace v1.35.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/sync v0.12.0 // indirect
golang.org/x/sys v0.31.0 // indirect
golang.org/x/text v0.23.0 // indirect
golang.org/x/sync v0.14.0 // indirect
golang.org/x/sys v0.33.0 // indirect
golang.org/x/text v0.25.0 // indirect
golang.org/x/time v0.11.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gotest.tools/v3 v3.5.1 // indirect
Expand Down
Loading
Loading