Skip to content

Commit

Permalink
✨ Add demo server (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
tosone authored Oct 8, 2023
1 parent 3026984 commit 36ac77a
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ linters-settings:
multi-func: true
cyclop:
max-complexity: 50
package-average: 30
package-average: 50
skip-tests: true
gci:
sections:
Expand Down
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,29 @@

![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/go-sigma/sigma/test.yml?style=for-the-badge) ![Codecov](https://img.shields.io/codecov/c/github/go-sigma/sigma?style=for-the-badge) ![GitHub repo size](https://img.shields.io/github/repo-size/go-sigma/sigma?style=for-the-badge)

Yet another OCI artifact manager. [Harbor](https://goharbor.io/) is a great product, but it's not easy to use, it is so complex. So I want to make a simple artifact manager, it never depends on [distribution](https://github.com/distribution/distribution) like [harbor](https://goharbor.io/), no [distribution](https://github.com/distribution/distribution).
Yet another OCI artifact manager. [Harbor](https://goharbor.io/) is a great product, but it's not easy to use, it is so complex. So I want to make a simple artifact manager, it never depends on [distribution](https://github.com/distribution/distribution) like [harbor](https://goharbor.io/).

## Introduction
## Demo Server

<https://user-images.githubusercontent.com/5346506/229798487-798225b1-e2bf-40a2-b5ab-588003c02f7b.mp4>
Runs on aws ec2 (2C2G, Disk 20G), Linux distribution is Debian 12.1, Docker version 24.0.6.

``` sh
# Install Docker from get.docker.com
./scripts/samples/init.sh

# If your docker running with rootless mode,
# make sure add net.ipv4.ip_unprivileged_port_start=0 to /etc/sysctl.conf and run sudo sysctl --system.
docker run --name sigma -v /home/admin/config:/etc/sigma \
-v /var/run/docker.sock:/var/run/docker.sock -p 443:3000 \
-d ghcr.io/go-sigma/sigma:nightly-alpine

# Add sample data
./scripts/samples/samples.sh
```

Visit: <https://sigma.tosone.cn>, username/password: sigma/sigma.

I will periodically reboot the container, and since the container doesn't have any disk mount, every reboot will clear all the data.

## Architecture

Expand Down
4 changes: 2 additions & 2 deletions build/Dockerfile.debian
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ARG GOLANG_VERSION=1.21.2
ARG GOLANG_VERSION=1.21.2-bookworm
ARG NODE_VERSION=18-alpine3.18
ARG ALPINE_VERSION=3.18
ARG DEBIAN_VERSION=bullseye-slim
ARG DEBIAN_VERSION=bookworm-slim

FROM node:${NODE_VERSION} as web-builder

Expand Down
11 changes: 11 additions & 0 deletions pkg/cmds/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"

Expand Down Expand Up @@ -66,6 +67,16 @@ func Serve(serverConfig ServerConfig) error {
// Interface("resp-header", c.Response().Header()).
// Int("status", c.Response().Status).
Msg("Request debugger")
reqPath := c.Request().URL.Path
if strings.HasPrefix(reqPath, "/assets/") {
if strings.HasSuffix(c.Request().URL.Path, ".js") ||
strings.HasSuffix(c.Request().URL.Path, ".css") ||
strings.HasSuffix(c.Request().URL.Path, ".svg") ||
strings.HasSuffix(c.Request().URL.Path, ".png") ||
strings.HasSuffix(c.Request().URL.Path, ".ttf") {
c.Response().Header().Add("Cache-Control", "max-age=3600")
}
}
n := next(c)
return n
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/configs/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ func defaultSettings() {
viper.SetDefault("server.endpoint", "http://127.0.0.1:3000")
viper.SetDefault("server.internalEndpoint", "http://127.0.0.1:3000")

configuration.HTTP.Endpoint = "http://127.0.0.1:3000"
// configuration.HTTP.Endpoint = "http://127.0.0.1:3000"
// configuration.HTTP.InternalEndpoint = "http://127.0.0.1:3000"
}
3 changes: 1 addition & 2 deletions pkg/dal/dao/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package dao

import (
"context"
"time"

"github.com/go-sigma/sigma/pkg/dal/models"
"github.com/go-sigma/sigma/pkg/dal/query"
Expand Down Expand Up @@ -69,7 +68,7 @@ func (s *auditService) Create(ctx context.Context, audit *models.Audit) error {
func (s *auditService) HotNamespace(ctx context.Context, userID int64, top int) ([]*models.Namespace, error) {
type result struct {
NamespaceID int64
CreatedAt time.Time
CreatedAt string
Count int64
}
var rs []result
Expand Down
4 changes: 2 additions & 2 deletions pkg/dal/migrations/postgresql/0001_initialize.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,8 @@ CREATE TABLE IF NOT EXISTS "builders" (
"repository_id" bigint NOT NULL,
"source" builder_source NOT NULL,
-- source SelfCodeRepository
"scm_credential_type" varchar(16) NOT NULL,
"scm_repository" varchar(256) NOT NULL,
"scm_credential_type" varchar(16),
"scm_repository" varchar(256),
"scm_ssh_key" bytea,
"scm_token" varchar(256),
"scm_username" varchar(30),
Expand Down
6 changes: 3 additions & 3 deletions pkg/dal/migrations/sqlite3/0001_initialize.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,8 @@ CREATE TABLE IF NOT EXISTS `builders` (
`repository_id` integer NOT NULL,
`source` text CHECK (`source` IN ('SelfCodeRepository', 'CodeRepository', 'Dockerfile')) NOT NULL,
-- source SelfCodeRepository
`scm_credential_type` varchar(16) NOT NULL,
`scm_repository` varchar(256) NOT NULL,
`scm_credential_type` varchar(16),
`scm_repository` varchar(256),
`scm_ssh_key` BLOB,
`scm_token` varchar(256),
`scm_username` varchar(30),
Expand Down Expand Up @@ -409,7 +409,7 @@ CREATE TABLE IF NOT EXISTS `work_queues` (
`payload` BLOB NOT NULL,
`times` integer NOT NULL DEFAULT 0,
`version` varchar(36) NOT NULL,
`status` text CHECK (`status` IN ('Success', 'Failed', 'Pending', 'Scheduling', 'Building')) NOT NULL DEFAULT 'Pending',
`status` text CHECK (`status` IN ('Success', 'Failed', 'Pending', 'Doing')) NOT NULL DEFAULT 'Pending',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`deleted_at` integer NOT NULL DEFAULT 0
Expand Down
3 changes: 1 addition & 2 deletions pkg/modules/workq/database/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package database
import (
"context"
"errors"
"strings"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -69,7 +68,7 @@ func (h *consumerHandler) consume(topic string) error {
workQueueService := dao.NewWorkQueueServiceFactory().New()
// daoCtx := log.Logger.WithContext(context.Background())
daoCtx := context.Background()
wq, err := workQueueService.Get(daoCtx, strings.ToLower(topic))
wq, err := workQueueService.Get(daoCtx, topic)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
log.Trace().Err(err).Msgf("None task in topic(%s)", topic)
Expand Down
3 changes: 2 additions & 1 deletion pkg/storage/cos/cos.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"path"
"reflect"
"strconv"
"strings"

gonanoid "github.com/matoous/go-nanoid"
"github.com/tencentyun/cos-go-sdk-v5"
Expand Down Expand Up @@ -58,7 +59,7 @@ func (f factory) New(config configs.Configuration) (storage.StorageDriver, error
return &tencentcos{
client: c,
domain: u.Host,
rootDirectory: config.Storage.RootDirectory,
rootDirectory: strings.TrimPrefix(config.Storage.RootDirectory, "/"),
}, nil
}

Expand Down
22 changes: 17 additions & 5 deletions pkg/storage/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ func (f factory) New(_ configs.Configuration) (storage.StorageDriver, error) {
return nil, err
}
}
if !utils.IsExist(path.Join(driver.rootDirectory, consts.DirCache)) {
err := os.MkdirAll(path.Join(driver.rootDirectory, consts.DirCache), 0755)
if err != nil {
return nil, err
}
}
return driver, nil
}

Expand Down Expand Up @@ -169,7 +175,7 @@ func (f *fs) AbortUpload(ctx context.Context, _ string, uploadID string) error {
}

// Upload upload a file to the given path.
func (f *fs) Upload(ctx context.Context, path string, body io.Reader) error {
func (f *fs) Upload(ctx context.Context, p string, body io.Reader) error {
if body == nil {
return fmt.Errorf("body is nil")
}
Expand Down Expand Up @@ -205,14 +211,20 @@ func (f *fs) Upload(ctx context.Context, path string, body io.Reader) error {
}
}()

path = storage.SanitizePath(f.rootDirectory, path)
if utils.IsExist(path) {
err := os.RemoveAll(path)
p = storage.SanitizePath(f.rootDirectory, p)
if utils.IsExist(p) {
err := os.RemoveAll(p)
if err != nil {
return err
}
}
if !utils.IsDir(path.Dir(p)) {
err := os.MkdirAll(path.Dir(p), 0755)
if err != nil {
return err
}
}
fp, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0644)
fp, err := os.Create(p)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/storage/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (f factory) New(_ configs.Configuration) (storage.StorageDriver, error) {
S3: s3.New(sess),
uploader: s3manager.NewUploader(sess),
bucket: bucket,
rootDirectory: viper.GetString("storage.rootDirectory"),
rootDirectory: strings.TrimPrefix(viper.GetString("storage.rootDirectory"), "/"),
}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,5 @@ func SanitizePath(rootDirectory, p string) string {
if rootDirectory == "" || rootDirectory == "." || rootDirectory == "./" || rootDirectory == "/" {
return p
}
return strings.TrimPrefix(strings.TrimPrefix(path.Join(rootDirectory, p), "."), "/")
return path.Join(strings.TrimPrefix(rootDirectory, "./"), p)
}
2 changes: 1 addition & 1 deletion pkg/storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func TestSanitizePath(t *testing.T) {
rootDirectory: "/test",
p: "test",
},
want: "test/test",
want: "/test/test",
},
{
name: "test-6",
Expand Down
5 changes: 5 additions & 0 deletions scripts/samples/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

sudo curl -sSL https://get.docker.io/ | sh

sudo apt update && sudo apt upgrade -y
8 changes: 8 additions & 0 deletions scripts/samples/samples.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh

docker login sigma.tosone.cn -u sigma -p sigma

docker pull redis:7

docker tag redis:7 sigma.tosone.cn/library/redis:7
docker push sigma.tosone.cn/library/redis:7

0 comments on commit 36ac77a

Please sign in to comment.