Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Dockerfile #6

Merged
merged 2 commits into from
Jul 5, 2024
Merged
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
40 changes: 40 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# stage for development, which contains tools for code generation and debugging.
FROM golang:1.22.2-bullseye as builder

RUN apt-get update \
&& apt-get install -y --no-install-recommends \
wget \
make \
unzip \
git \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

# install protoc
RUN wget https://github.com/protocolbuffers/protobuf/releases/download/v3.20.1/protoc-3.20.1-linux-x86_64.zip \
&& unzip -d /usr/local protoc-3.20.1-linux-x86_64.zip \
&& rm protoc-3.20.1-linux-x86_64.zip

# install protoc plugins
RUN go install google.golang.org/protobuf/cmd/[email protected]
RUN go install google.golang.org/grpc/cmd/[email protected]
RUN go install github.com/grpc-ecosystem/grpc-gateway/v2/[email protected]
RUN go install github.com/grpc-ecosystem/grpc-gateway/v2/[email protected]

WORKDIR /work

COPY go.mod go.sum ./
RUN go mod download && go mod verify

COPY . ./

RUN go build -o /openve ./go/cmd



# runner stage for server
FROM debian:bullseye-slim as runner

COPY --from=builder /openve /openve

CMD ["/openve"]
15 changes: 2 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Centralized and Consistent Data Validation Engine

This project is still under development and not ready for production use.

We only support limited CEL expression and gRPC API.
We only support limited CEL expression.

## Setup

Expand All @@ -16,30 +16,19 @@ If you want to specify the configuration below, you can create a `config.yaml` f
If you don't specify the configuration, the default values will be used.

```yaml
server:
grpc:
port: 9000
rest:
port: 8080
redis:
addr: "localhost:6379"
password: ""
db: 0
poolSize: 10
```

### Redis
### Run

```bash
docker compose up -d
```

### Server

```bash
go run cmd/main.go
```

## Example

### Register Validation Rules
Expand Down
17 changes: 14 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,19 @@ services:
volumes:
- redis-data:/data
networks:
- redis-net
- default
restart: unless-stopped
server:
build:
context: .
container_name: server
ports:
- "8080:8080"
- "8081:8081"
networks:
- default
depends_on:
- redis
apidocs:
image: swaggerapi/swagger-ui
container_name: apidocs
Expand All @@ -19,11 +30,11 @@ services:
- ./openapi:/openapi
restart: unless-stopped
ports:
- "8081:8080"
- "9000:8080"

volumes:
redis-data:

networks:
redis-net:
default:
driver: bridge
17 changes: 0 additions & 17 deletions go/cmd/gateway/main.go

This file was deleted.

21 changes: 16 additions & 5 deletions go/cmd/grpc/main.go → go/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"log/slog"

"github.com/go-redis/redis"
"github.com/shibukazu/open-ve/go/pkg/config"
Expand All @@ -11,7 +12,8 @@ import (
)

func main() {
var ctx = context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

cfg := config.NewConfig()

Expand All @@ -21,10 +23,19 @@ func main() {
DB: cfg.Redis.DB,
PoolSize: cfg.Redis.PoolSize,
})

dslReader := dsl.NewDSLReader(redis)
validator := validator.NewValidator(redis)
gw := server.NewGateway()
go func () {
slog.Info("🚀gateway is running")
gw.Run(ctx)
}()

grpc := server.NewGrpc(validator, dslReader)
go func () {
slog.Info("🚀grpc is running")
grpc.Run(ctx)
}()

srv := server.NewGrpc(validator, dslReader, &cfg.Server)
srv.Run(ctx)
}
<-ctx.Done()
}
23 changes: 1 addition & 22 deletions go/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,9 @@ import (
)

type Config struct {
Server ServerConfig `yaml:"server"`
Redis RedisConfig `yaml:"redis"`
}

type ServerConfig struct {
Grpc GrpcConfig `yaml:"grpc"`
Http HttpConfig `yaml:"http"`
}

type GrpcConfig struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
}

type HttpConfig struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
Expand Down Expand Up @@ -59,18 +48,8 @@ func NewConfig() *Config {

func defaultConfig () *Config {
return &Config{
Server: ServerConfig{
Grpc: GrpcConfig{
Host: "localhost",
Port: "9000",
},
Http: HttpConfig{
Host: "localhost",
Port: "8080",
},
},
Redis: RedisConfig{
Addr: "localhost:6379",
Addr: "redis:6379",
Password: "",
DB: 0,
PoolSize: 1000,
Expand Down
6 changes: 6 additions & 0 deletions go/pkg/server/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package server

const (
grpcEndpoint = "0.0.0.0:8081"
httpEndpoint = "0.0.0.0:8080"
)
14 changes: 4 additions & 10 deletions go/pkg/server/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,19 @@ import (
"net/http"

"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/shibukazu/open-ve/go/pkg/config"
pbDSL "github.com/shibukazu/open-ve/go/proto/dsl/v1"
pbValidate "github.com/shibukazu/open-ve/go/proto/validate/v1"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

type Gateway struct {
serverConfig *config.ServerConfig
}
type Gateway struct {}

func NewGateway(serverConfig *config.ServerConfig) *Gateway {
return &Gateway{
serverConfig: serverConfig,
}
func NewGateway() *Gateway {
return &Gateway{}
}

func (g *Gateway) Run(ctx context.Context) {
grpcEndpoint := g.serverConfig.Grpc.Host + ":" + g.serverConfig.Grpc.Port
grpcGateway := runtime.NewServeMux()
opts := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
Expand All @@ -36,7 +30,7 @@ func (g *Gateway) Run(ctx context.Context) {
panic(err)
}

if err := http.ListenAndServe(":"+g.serverConfig.Http.Port, grpcGateway); err != nil {
if err := http.ListenAndServe(httpEndpoint, grpcGateway); err != nil {
panic(err)
}
}
8 changes: 2 additions & 6 deletions go/pkg/server/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/morikuni/failure/v2"
"github.com/shibukazu/open-ve/go/pkg/appError"
"github.com/shibukazu/open-ve/go/pkg/config"
"github.com/shibukazu/open-ve/go/pkg/dsl"
svcDSL "github.com/shibukazu/open-ve/go/pkg/services/dsl/v1"
svcValidate "github.com/shibukazu/open-ve/go/pkg/services/validate/v1"
Expand All @@ -19,22 +18,20 @@ import (
)

type Grpc struct {
serverConfig *config.ServerConfig
dslReader *dsl.DSLReader
validator *validator.Validator
}

func NewGrpc(validator *validator.Validator, dslReader *dsl.DSLReader, serverConfig *config.ServerConfig) *Grpc {
func NewGrpc(validator *validator.Validator, dslReader *dsl.DSLReader) *Grpc {
return &Grpc{
validator: validator,
dslReader: dslReader,
serverConfig: serverConfig,
}
}

func (g *Grpc) Run(ctx context.Context) {

listen, err := net.Listen("tcp", ":" + g.serverConfig.Grpc.Port)
listen, err := net.Listen("tcp", grpcEndpoint)
if err != nil {
panic(failure.Translate(err, appError.ErrServerStartFailed))
}
Expand All @@ -49,7 +46,6 @@ func (g *Grpc) Run(ctx context.Context) {

reflection.Register(grpcServer)

// 以下でリッスンし続ける
if err := grpcServer.Serve(listen); err != nil {
panic(failure.Translate(err, appError.ErrServerStartFailed))
}
Expand Down
Loading