Skip to content

Commit

Permalink
Merge branch 'polish'
Browse files Browse the repository at this point in the history
  • Loading branch information
w1ck3dg0ph3r committed Sep 21, 2024
2 parents 81a67d4 + 35f09d6 commit 12cf308
Show file tree
Hide file tree
Showing 21 changed files with 106 additions and 68 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.git
/.cache
/.github
/goce
Expand Down
22 changes: 22 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
linters:
enable:
- gosec
- bodyclose
- err113
- exhaustive
- gofumpt
- gci
- nilnil
- wastedassign

linters-settings:
gci:
sections:
- standard # Standard section: captures all standard packages.
- default # Default section: contains all imports that could not be matched to another section type.
- localmodule # Local module section: contains all local packages. This section is not present unless explicitly enabled.
- dot # Dot section: contains all dot imports. This section is not present unless explicitly enabled.
gosec:
excludes:
- G204 # Subprocess launched with a potential tainted input or cmd arguments
- G115 # Potential integer overflow when converting between integer types
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ RUN go mod download
COPY . .
COPY --from=ui-builder /src/ui/dist ./ui/dist
RUN \
go build -o /bin/goce -ldflags "-s -w -X main.version=${version}" . &&\
go build -o /bin/godl -ldflags "-s -w" ./tools/godl
go build -o /bin/goce -ldflags "-s -w -X main.version=${version}" ./cmd/goce &&\
go build -o /bin/godl -ldflags "-s -w" ./cmd/tools/godl

FROM alpine:3.20
RUN apk add ca-certificates tzdata curl tar git
Expand Down
11 changes: 6 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=$(shell git describe --tags 2>/dev/null || git rev-parse --short=7 HEAD 2>/dev/null || echo -n tip)
VERSION=$(shell git describe --tags 2>/dev/null || git rev-parse --short=7 HEAD 2>/dev/null || echo -n unknown)
DOCKER_IMAGE=w1ck3dg0ph3r/goce
DOCKER_TAG=$(DOCKER_IMAGE):latest

Expand All @@ -19,7 +19,7 @@ build-ui:
pnpm vite build --mode=localhost

build-api:
go build -ldflags $(GO_LDFLAGS) .
go build -ldflags $(GO_LDFLAGS) ./cmd/goce

test: test-api test-ui

Expand All @@ -35,7 +35,7 @@ test-ui:
lint: lint-api lint-ui

lint-api: install-golangcilint
golangci-lint run
golangci-lint run -v

lint-ui:
@cd ui &&\
Expand All @@ -44,12 +44,13 @@ lint-ui:
image:
docker build \
--build-arg version=$(VERSION) \
--build-arg ui_mode=localhost \
-t $(DOCKER_TAG) .

image-local:
image-production:
docker build \
--build-arg version=$(VERSION) \
--build-arg ui_mode=localhost \
--build-arg ui_mode=production \
-t $(DOCKER_TAG) .

install-golangcilint:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ make
./goce
```

### In the docker container
### Inside the docker container

Build the docker image:

```bash
make image-local
make image
```

Prepare volume mounts for compilation cache, shared code storage and Go toolchains, module and build caches:
Expand Down
32 changes: 17 additions & 15 deletions api.go → api/api.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package api

import (
"fmt"
Expand All @@ -8,15 +8,17 @@ import (
"mvdan.cc/gofumpt/format"

"github.com/w1ck3dg0ph3r/goce/compilers"
"github.com/w1ck3dg0ph3r/goce/config"
"github.com/w1ck3dg0ph3r/goce/parsers"
"github.com/w1ck3dg0ph3r/goce/store"
)

type API struct {
Config *Config
Config *config.Config

CompilersSvc *compilers.CompilersSvc
CompilationCache *CompilationCache
SharedCodeStore *SharedCodeStore
Compilers *compilers.Service
CompilationCache *store.CompilationCache
SharedCodeStore *store.SharedCode
}

func (api *API) GetCompilers(ctx *fiber.Ctx) error {
Expand All @@ -25,7 +27,7 @@ func (api *API) GetCompilers(ctx *fiber.Ctx) error {
compilers.CompilerInfo
}
type Response []CompilerInfo
list := api.CompilersSvc.List()
list := api.Compilers.List()
res := make(Response, 0, len(list))
for i := range list {
res = append(res, CompilerInfo{
Expand Down Expand Up @@ -88,20 +90,20 @@ func (api *API) Compile(ctx *fiber.Ctx) error {
}
code := []byte(req.Code)

compiler := api.CompilersSvc.Default()
compiler := api.Compilers.Default()
if req.Name != "" {
compiler = api.CompilersSvc.Get(req.Name)
compiler = api.Compilers.Get(req.Name)
}
if compiler == nil {
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("compiler not found: %s", req.Name))
}

cacheKey := CompilationCacheKey{
cacheKey := store.CompilationCacheKey{
CompilerName: compInfo.Name(),
CompilerOptions: req.Options,
Code: code,
}
var cacheValue CompilationCacheValue
var cacheValue store.CompilationCacheValue
if api.CompilationCache != nil {
if found, err := api.CompilationCache.Get(cacheKey, &cacheValue); found {
return ctx.JSON(Response(cacheValue))
Expand Down Expand Up @@ -153,8 +155,8 @@ func (api *API) ShareCode(ctx *fiber.Ctx) error {
type Response struct {
ID string `json:"id"`
}
id := NewSharedCodeKey()
val := SharedCodeValue{
id := store.NewSharedCodeKey()
val := store.SharedCodeValue{
Code: ctx.Body(),
}
if err := api.SharedCodeStore.Set(id, val, api.Config.SharedCodeTTL); err != nil {
Expand All @@ -166,11 +168,11 @@ func (api *API) ShareCode(ctx *fiber.Ctx) error {
}

func (api *API) GetSharedCode(ctx *fiber.Ctx) error {
id, err := ParseSharedCodeKey(ctx.Params("id"))
id, err := store.ParseSharedCodeKey(ctx.Params("id"))
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
var val SharedCodeValue
var val store.SharedCodeValue
if found, err := api.SharedCodeStore.Get(id, &val); !found {
return fiber.NewError(fiber.StatusNotFound, "shared code not found")
} else if err != nil {
Expand All @@ -187,7 +189,7 @@ func (api *API) gofumptVersionForCompiler(name string) string {
if name == "" {
return defaultVeriosn
}
compiler := api.CompilersSvc.Get(name)
compiler := api.Compilers.Get(name)
if compiler == nil {
return defaultVeriosn
}
Expand Down
28 changes: 14 additions & 14 deletions main.go → cmd/goce/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"embed"
"errors"
"fmt"
"net/http"
Expand All @@ -17,17 +16,18 @@ import (
"github.com/gofiber/fiber/v2/middleware/filesystem"
"github.com/gofiber/fiber/v2/middleware/logger"

"github.com/w1ck3dg0ph3r/goce/cache"
"github.com/w1ck3dg0ph3r/goce/api"
"github.com/w1ck3dg0ph3r/goce/compilers"
"github.com/w1ck3dg0ph3r/goce/config"
"github.com/w1ck3dg0ph3r/goce/pkg/cache"
"github.com/w1ck3dg0ph3r/goce/store"
"github.com/w1ck3dg0ph3r/goce/ui"
)

//go:embed ui/dist
var distFS embed.FS

var version string

func main() {
cfg, err := ReadConfig()
cfg, err := config.Read()
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
Expand Down Expand Up @@ -63,25 +63,25 @@ func main() {
fmt.Printf("can't create data directory: %v", err.Error())
}

var compilationCache *cache.Cache[CompilationCacheKey, CompilationCacheValue]
var compilationCache *cache.Cache[store.CompilationCacheKey, store.CompilationCacheValue]
if cfg.Cache.Enabled {
compilationCache, err = NewCompilationCache("data/cache.db")
compilationCache, err = store.NewCompilationCache("data/cache.db")
if err != nil {
fmt.Printf("compilation cache: %v", err.Error())
os.Exit(1)
}
}

sharedCodeStore, err := NewSharedStore("data/shared.db")
sharedCodeStore, err := store.NewSharedCode("data/shared.db")
if err != nil {
fmt.Printf("shared code store: %v", err.Error())
os.Exit(1)
}

api := &API{
api := &api.API{
Config: cfg,

CompilersSvc: compilersSvc,
Compilers: compilersSvc,
CompilationCache: compilationCache,
SharedCodeStore: sharedCodeStore,
}
Expand Down Expand Up @@ -119,10 +119,10 @@ func main() {

func serveUI() fiber.Handler {
return filesystem.New(filesystem.Config{
Root: http.FS(distFS),
PathPrefix: "ui/dist",
Root: http.FS(ui.DistFS),
PathPrefix: "dist",
Index: "index.html",
NotFoundFile: "ui/dist/index.html",
NotFoundFile: "dist/index.html",
MaxAge: 60,
})
}
Expand Down
File renamed without changes.
30 changes: 17 additions & 13 deletions compilers/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ type Config struct {
AdditionalArchitectures bool // Add supported cross-compilation architectures
}

var ErrNoCompilers = errors.New("no compilers found")
var (
ErrNoCompilers = errors.New("no compilers found")
ErrInvalidName = errors.New("invalid compiler name")
ErrInvalidPath = errors.New("invalid compiler path")
)

// New creates and initializes [CompilersSvc].
func New(cfg *Config) (*CompilersSvc, error) {
svc := &CompilersSvc{
// New creates and initializes [Service].
func New(cfg *Config) (*Service, error) {
svc := &Service{
cfg: cfg,
}
if err := svc.refreshAvailable(); err != nil {
Expand All @@ -38,7 +42,7 @@ func New(cfg *Config) (*CompilersSvc, error) {
return svc, nil
}

type CompilersSvc struct {
type Service struct {
cfg *Config

availableMu sync.RWMutex
Expand Down Expand Up @@ -85,7 +89,7 @@ type Result struct {
}

// List returns available compilers.
func (svc *CompilersSvc) List() []CompilerInfo {
func (svc *Service) List() []CompilerInfo {
_ = svc.refreshAvailable()
svc.availableMu.RLock()
defer svc.availableMu.RUnlock()
Expand All @@ -97,7 +101,7 @@ func (svc *CompilersSvc) List() []CompilerInfo {
}

// Get returns compiler with a given name.
func (svc *CompilersSvc) Get(name string) Compiler {
func (svc *Service) Get(name string) Compiler {
_ = svc.refreshAvailable()
svc.availableMu.RLock()
defer svc.availableMu.RUnlock()
Expand All @@ -108,7 +112,7 @@ func (svc *CompilersSvc) Get(name string) Compiler {
}

// Default returns default compiler.
func (svc *CompilersSvc) Default() Compiler {
func (svc *Service) Default() Compiler {
_ = svc.refreshAvailable()
svc.availableMu.RLock()
defer svc.availableMu.RUnlock()
Expand All @@ -129,7 +133,7 @@ func ParseInfo(name string) (CompilerInfo, error) {
var ci CompilerInfo
match := reCompilerName.FindStringSubmatch(name)
if match == nil {
return ci, fmt.Errorf("invalid compiler name: %s", name)
return ci, fmt.Errorf("%w: %s", ErrInvalidName, name)
}
ci = CompilerInfo{
Version: match[reCompilerName_Version],
Expand All @@ -151,7 +155,7 @@ type compilerDesc struct {
version *semver.Version
}

func (svc *CompilersSvc) refreshAvailable() error {
func (svc *Service) refreshAvailable() error {
now := time.Now()
needRefresh := false
svc.availableMu.RLock()
Expand All @@ -176,7 +180,7 @@ func (svc *CompilersSvc) refreshAvailable() error {
return nil
}

func (svc *CompilersSvc) listAvailable() (availableCompilers, error) {
func (svc *Service) listAvailable() (availableCompilers, error) {
ac := availableCompilers{
compilerByName: map[string]*compilerDesc{},
}
Expand Down Expand Up @@ -248,10 +252,10 @@ func (ac *availableCompilers) searchSDKPath() {
func (ac *availableCompilers) addLocal(path string) error {
fs, err := os.Stat(path)
if err != nil {
return fmt.Errorf("register compiler: %w", err)
return fmt.Errorf("%w: %w", ErrInvalidPath, err)
}
if fs.Mode().Perm()&0o111 == 0 {
return fmt.Errorf("register compiler: not executable: %s", path)
return fmt.Errorf("%w: not executable: %s", ErrInvalidPath, path)
}

comp := &localCompiler{GoPath: path}
Expand Down
4 changes: 2 additions & 2 deletions compilers/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ func (c *localCompiler) Info() (CompilerInfo, error) {
cmd := exec.Command(c.GoPath, "version")
out, err := cmd.Output()
if err != nil {
return CompilerInfo{}, fmt.Errorf("cant run go version: %w", err)
return CompilerInfo{}, fmt.Errorf("%w: go version: %w", ErrInvalidPath, err)
}
out = bytes.TrimPrefix(out, []byte("go version "))
out = bytes.TrimSpace(out)
match := reGoVersion.FindSubmatch(out)
if match == nil {
return CompilerInfo{}, fmt.Errorf("cant parse go version: %q", string(out))
return CompilerInfo{}, fmt.Errorf("%w: go version: %q", ErrInvalidPath, string(out))
}
c.info = CompilerInfo{
Version: string(match[reGoVersion_Version]),
Expand Down
6 changes: 3 additions & 3 deletions config.go → config/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package config

import (
"os"
Expand Down Expand Up @@ -36,8 +36,8 @@ type Config struct {
}
}

// ReadConfig reads configuration options from available sources.
func ReadConfig() (*Config, error) {
// Read reads configuration options from available sources.
func Read() (*Config, error) {
viper.MustBindEnv("Listen", "GOCE_LISTEN")
viper.MustBindEnv("CompilationCacheTTL", "GOCE_COMPILATION_CACHE_TTL")
viper.MustBindEnv("SharedCodeTTL", "GOCE_SHARED_CODE_TTL")
Expand Down
File renamed without changes.
Loading

0 comments on commit 12cf308

Please sign in to comment.