Skip to content

Commit

Permalink
Merge pull request #2 from victor-skurikhin/issue-2
Browse files Browse the repository at this point in the history
MVP
  • Loading branch information
victor-skurikhin authored Aug 15, 2024
2 parents 16a8847 + 71e0edc commit 01f8391
Show file tree
Hide file tree
Showing 29 changed files with 2,472 additions and 387 deletions.
105 changes: 105 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
include Makefile.env

## run: Compile and run server
run: go-compile start

## start: Start in development mode. Auto-starts when code changes.
start: start-etcd-proxy

## stop: Stop development mode. GO_ETCD_PROXY
stop: stop-etcd-proxy

start-etcd-proxy: stop-etcd-proxy
@echo " > $(PROJECTNAME) is available at $(HTTP_ADDRESS) and gRPC at $(GRPC_ADDRESS)"
@-cd ./$(DIR_ETCD_PROXY) && (./etcd-proxy -h $(HTTP_ADDRESS) -g $(GRPC_ADDRESS) & echo $$! > $(PID_GO_ETCD_PROXY))
@cat $(PID_GO_ETCD_PROXY) | sed "/^/s/^/ \> PID: /"

stop-etcd-proxy:
@echo " > stop by $(PID_GO_ETCD_PROXY)"
@-touch $(PID_GO_ETCD_PROXY)
@-kill `cat $(PID_GO_ETCD_PROXY)` 2> /dev/null || true
@-rm $(PID_GO_ETCD_PROXY)

restart-etcd-proxy: stop-etcd-proxy start-etcd-proxy

## build: Build and the binary compile server
build: go-build-etcd-proxy

## clean: Clean build files. Runs `go clean` internally.
clean:
@(MAKEFILE) go-clean

go-compile: go-build-etcd-proxy

go-build-etcd-proxy:
@echo " > Building GO_ETCD_PROXY binary..."
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) cd ./$(DIR_ETCD_PROXY) && go build -o ./etcd-proxy $(GOFILES)

go-generate:
@echo " > Generating dependency files..."
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go generate $(generate)

go-get:
@echo " > Checking if there is any missing dependencies..."
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go get $(get)

.PHONY: go-update-deps
go-update-deps:
@echo ">> updating Go dependencies"
@for m in $$(go list -mod=readonly -m -f '{{ if and (not .Indirect) (not .Main)}}{{.Path}}{{end}}' all); do \
go get $$m; \
done
go mod tidy
ifneq (,$(wildcard vendor))
go mod vendor
endif

go-install:
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go install $(GOFILES)

go-swag:
swag init -g $(MAIN_GO) --output $(DOCS_DIR)
sed -i 's/"localhost:8080",/env.GetConfig().Address(),/' $(DOCS_GO)
goimports -w $(DOCS_GO)

go-clean:
@echo " > Cleaning build cache"
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go clean

cert:
@cd cert; openssl req -x509 -newkey rsa:1024 -days 365 -nodes -keyout ca-key.pem -out ca-cert.pem -subj "/C=RU/ST=Moscow/L=Moscow/O=Tech School/OU=Education/CN=localhost/[email protected]"
@echo "CA's self-signed certificate"
@cd cert; openssl x509 -in ca-cert.pem -noout -text
@cd cert; openssl req -newkey rsa:1024 -nodes -keyout server-key.pem -out server-req.pem -subj "/C=RU/ST=Moscow/L=Moscow/O=Tech School/OU=Education/CN=localhost/[email protected]"
@cd cert; openssl x509 -req -in server-req.pem -days 60 -CA ca-cert.pem -CAkey ca-key.pem -CAcreateserial -out server-cert.pem -extfile server-ext.cnf
@echo "Server's signed certificate"
@cd cert; openssl x509 -in server-cert.pem -noout -text

##################
# Implicit targets
##################

# This rulle is used to generate the message source files based
# on the *.proto files.
%.pb.go: %.proto
@protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative ./$<

####################################
# Major source code-generate targets
####################################
generate: $(PROTO_PB_GO)
@echo " > Done generating source files based on *.proto and Mock files."

test:
@echo " > Test Iteration ..."
go vet -vettool=$(which statictest) ./...
cd cmd/etcd-proxy

.PHONY: cert help
all: help
help: Makefile
@echo
@echo " Choose a command run in "$(PROJECTNAME)":"
@echo
@sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /'
@echo
39 changes: 39 additions & 0 deletions Makefile.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

PROJECTNAME=$(shell basename "$(PWD)")

# Go related variables.
GOBASE=$(shell pwd)
GOPATH="$(GOBASE)/vendor:$(GOBASE)"
DIR_ETCD_PROXY=cmd/etcd-proxy
GOBIN=$(GOBASE)/$(CMD_FAVORITES)
GOFILES=$(wildcard *.go)

# Redirect error output to a file, so we can show it in development mode.
STDERR=/tmp/.$(PROJECTNAME)-stderr.txt

# PID file will keep the process id of the etcd-proxy
PID_GO_ETCD_PROXY=/tmp/.$(PROJECTNAME)-etcd-proxy.pid

RANDOM=$(shell date +%s)
RND1=$(shell echo "("$RANDOM" % 1024) + 63490" | bc)
RND2=$(shell echo "("$RND1" + 1" | bc)
GRPC_ADDRESS=localhost:$(RND2)
HTTP_ADDRESS=localhost:$(RND1)
TEMP_FILE=$(shell mktemp)
DOCS_DIR=./docs
DOCS_GO=$(DOCS_DIR)/docs.go
MAIN_GO=./$(CMD_FAVORITES)/main.go

# Make is verbose in Linux. Make it silent.
MAKEFLAGS += --silent

# Define where the *.proto files are located.
PROTO_DIR = ./proto

# Find all the proto files.
# Extend this for subfolders.
PROTO_FILES = $(wildcard $(PROTO_DIR)/*.proto)

# Convert the names of the proto files to the name of the
# generated header files.
PROTO_PB_GO := $(PROTO_FILES:%.proto=%.pb.go)
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# encoding: utf-8
etcdclient:
enabled: true
cache:
enabled: true
expire_ms: 1000
gc_interval_sec: 10
etcd:
addresses:
- localhost:2379
enabled: true
grpc:
address: localhost
enabled: true
Expand Down
33 changes: 19 additions & 14 deletions cmd/etcd-client/main.go → cmd/etcd-proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package main
import (
"context"
"crypto/tls"
"fmt"
"log"
"log/slog"
"net"
Expand All @@ -26,15 +25,19 @@ import (
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/requestid"
"github.com/victor-skurikhin/etcd-client/v1/internal/alog"
"github.com/victor-skurikhin/etcd-client/v1/internal/controllers"
"github.com/victor-skurikhin/etcd-client/v1/internal/controllers/dto"
"github.com/victor-skurikhin/etcd-client/v1/internal/env"
"github.com/victor-skurikhin/etcd-client/v1/internal/services"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/reflection"

pb "github.com/victor-skurikhin/etcd-client/v1/proto"
_ "google.golang.org/grpc/encoding/gzip"
)

const MSG = "etcd-client"
const MSG = "etcd-proxy"

var (
buildVersion = "N/A"
Expand All @@ -58,7 +61,7 @@ func run(ctx context.Context) {

func serve(ctx context.Context, cfg env.Config) {

sLog = alog.GetLogger()
sLog = cfg.Logger()
listen, err := net.Listen("tcp", cfg.GRPCAddress())

if err != nil {
Expand All @@ -68,8 +71,8 @@ func serve(ctx context.Context, cfg env.Config) {
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)

httpServer := makeHTTP(cfg)
grpcServer := makeGRPC(cfg)
httpServer := makeHTTP(ctx, cfg)
grpcServer := makeGRPC(ctx, cfg)

go func() {
<-sigint
Expand Down Expand Up @@ -114,7 +117,7 @@ func serve(ctx context.Context, cfg env.Config) {
sLog.Info(MSG+"shutdown app", "msg", "Корректное завершение работы сервера")
}

func makeHTTP(prop env.Config) *fiber.App {
func makeHTTP(ctx context.Context, cfg env.Config) *fiber.App {

logHandler := logger.New(logger.Config{
Format: "${pid} | ${time} | ${status} | ${locals:requestid} | ${latency} | ${ip} | ${method} | ${path} | ${error}\n",
Expand All @@ -125,33 +128,33 @@ func makeHTTP(prop env.Config) *fiber.App {
})
slogLogger := slog.New(slog.NewJSONHandler(os.Stdout, nil))

app := fiber.New()
app := fiber.New(fiber.Config{DisableHeaderNormalizing: true})
micro := fiber.New()
app.Mount("/api", micro)
app.Use(requestid.New())

micro.Use(requestid.New())

if prop.SlogJSON() {
if cfg.SlogJSON() {
app.Use(alog.New(slogLogger))
micro.Use(alog.New(slogLogger))
} else {
app.Use(logHandler)
micro.Use(logHandler)
}
ctrl := controllers.GetEtcdProxyController(ctx, cfg)
micro.Delete("/delete/:name", ctrl.Delete)
micro.Get("/get/:name", ctrl.Get)
micro.Put("/put/:name", ctrl.Put)
micro.All("*", func(c *fiber.Ctx) error {
path := c.Path()
return c.
Status(fiber.StatusNotFound).
JSON(fiber.Map{
"status": "fail",
"message": fmt.Sprintf("Path: %v does not exists on this server", path),
})
JSON(dto.StatusMessagePathDoesNotExists(path))
})
return app
}

func makeGRPC(cfg env.Config) *grpc.Server {
func makeGRPC(ctx context.Context, cfg env.Config) *grpc.Server {

var opts []grpc.ServerOption

Expand All @@ -164,7 +167,9 @@ func makeGRPC(cfg env.Config) *grpc.Server {
grpc.Creds(insecure.NewCredentials()),
}
}
srv := services.GetEtcdProxyService(ctx, cfg)
grpcServer := grpc.NewServer(opts...)
pb.RegisterEtcdClientServiceServer(grpcServer, srv)
reflection.Register(grpcServer)

return grpcServer
Expand Down
File renamed without changes.
42 changes: 24 additions & 18 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,54 +10,60 @@ require (
github.com/jackc/pgx/v5 v5.6.0
github.com/samber/slog-fiber v1.16.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.15.0
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.9.0
github.com/valyala/bytebufferpool v1.0.0
github.com/vskurikhin/gofavorites v0.0.0-20240807140301-c343728abe54
go.etcd.io/etcd/client/v3 v3.5.15
go.opentelemetry.io/otel/trace v1.28.0
google.golang.org/grpc v1.65.0
)

require (
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/coreos/go-semver v0.3.1 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/gabriel-vasile/mimetype v1.4.5 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.22.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/pelletier/go-toml v1.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/valyala/fasthttp v1.55.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
go.mongodb.org/mongo-driver v1.16.0 // indirect
go.etcd.io/etcd/api/v3 v3.5.15 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.15 // indirect
go.opentelemetry.io/otel v1.28.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.23.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240805194559-2c9e96a0b5d4 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.17.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240808171019-573a1156607a // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240808171019-573a1156607a // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 01f8391

Please sign in to comment.