Rename ServerConfig->RouterConfig, smaller build fixes

master
René Jochum 2 years ago
parent 2237cb4785
commit 7fef5964da

6
.gitignore vendored

@ -1,5 +1,5 @@
.task/ .DS_STORE
!.gitkeep .task/
.env !.gitkeep

@ -19,7 +19,7 @@ It looks for services that host "proto/routerclientpb/routerclientpb.RouterClien
Have a look at [internalService](https://github.com/go-micro/router/blob/master/cmd/microrouterd/main.go#L35) or the author's FOSS project [microlobby](https://github.com/pcdummy/microlobby). Have a look at [internalService](https://github.com/go-micro/router/blob/master/cmd/microrouterd/main.go#L35) or the author's FOSS project [microlobby](https://github.com/pcdummy/microlobby).
Here some code from the microlobby project Here's some code from the microlobby project
```go ```go
import ( import (
"github.com/go-micro/router" "github.com/go-micro/router"
@ -82,20 +82,22 @@ func main() {
} }
``` ```
## Build podman/docker image ## Developers corner
### Prerequesits ### Build podman/docker image
#### Prerequesits
- podman - podman
- [Task](https://taskfile.dev/#/installation) - [Task](https://taskfile.dev/#/installation)
### Build #### Build
```bash ```bash
task task
``` ```
### Remove everything except the resulting podman images created by task #### Remove everything except the resulting podman images created by task
```bash ```bash
task rm task rm

@ -29,9 +29,9 @@ tasks:
deps: deps:
- volume - volume
cmds: cmds:
- podman build -v "{{.VOLUME_PATH}}:/go:rw" -t docker.io/pcdummy/go-micro-router-builder:latest -f ./docker/builder/Dockerfile . - podman build -v "{{.VOLUME_PATH}}:/go:rw" -t docker.io/pcdummy/go-micro-router-builder:latest -f ./docker/builder/Dockerfile ./docker/builder/
sources: sources:
- ./docker/builder/Dockerfile - ./docker/builder/**/*
vars: vars:
VOLUME_PATH: VOLUME_PATH:
sh: podman volume inspect micro_router_go --format "{{"{{"}}.Mountpoint{{"}}"}}" sh: podman volume inspect micro_router_go --format "{{"{{"}}.Mountpoint{{"}}"}}"
@ -53,16 +53,13 @@ tasks:
cmds: cmds:
- task: builder - task: builder
vars: vars:
CLI_ARGS: /bin/sh -c 'cd ./internal/proto/routerclientpb; protoc --proto_path=/go/bin:. --micro_out=paths=source_relative:. --go_out=paths=source_relative:. routerclientpb.proto' CLI_ARGS: /scripts/protoc_gen.sh
- task: builder
vars:
CLI_ARGS: /bin/sh -c 'cd ./internal/proto/routerserverpb; protoc --proto_path=/go/bin:. --micro_out=paths=source_relative:. --go_out=paths=source_relative:. routerserverpb.proto'
build:podman: build:podman:
deps: deps:
- protoc - protoc
cmds: cmds:
- podman build -v "$PWD:/code:rw" -v "{{.VOLUME_PATH}}:/go:rw" --build-arg CACHEBUST={{.DATE}} --build-arg VERSION={{.VERSION}} -t docker.io/pcdummy/go-micro-router:latest -f ./docker/go-micro-router/Dockerfile . - podman build -v "$PWD:/code:rw" -v "{{.VOLUME_PATH}}:/go:rw" --build-arg CACHEBUST={{.DATE}} --build-arg VERSION={{.VERSION}} -t docker.io/pcdummy/go-micro-router:latest -f ./docker/router/Dockerfile .
vars: vars:
DATE: DATE:
sh: date +%s sh: date +%s
@ -103,7 +100,7 @@ tasks:
cmds: cmds:
- task: builder - task: builder
vars: vars:
CLI_ARGS: go get -u ./... CLI_ARGS: /scripts/upgrade_deps.sh
rm: rm:
desc: Remove all persistent data desc: Remove all persistent data

@ -33,7 +33,7 @@ func internalService(engine *gin.Engine) {
routerserverpb.RegisterRouterServerServiceHandler(srv.Server(), routerHandler) routerserverpb.RegisterRouterServerServiceHandler(srv.Server(), routerHandler)
r := router.NewHandler( r := router.NewHandler(
config.GetServerConfig().RouterURI, config.GetRouterConfig().RouterURI,
router.NewRoute( router.NewRoute(
router.Method(router.MethodGet), router.Method(router.MethodGet),
router.Path("/routes"), router.Path("/routes"),
@ -68,7 +68,7 @@ func main() {
logger.Fatal(err) logger.Fatal(err)
} }
if config.GetServerConfig().Env == config.EnvProd { if config.GetRouterConfig().Env == config.EnvProd {
gin.SetMode(gin.ReleaseMode) gin.SetMode(gin.ReleaseMode)
} }
@ -76,7 +76,7 @@ func main() {
opts := []micro.Option{ opts := []micro.Option{
micro.Name(config.Name), micro.Name(config.Name),
micro.Version(config.Version), micro.Version(config.Version),
micro.Address(config.GetServerConfig().Address), micro.Address(config.GetRouterConfig().Address),
micro.Action(func(c *cli.Context) error { micro.Action(func(c *cli.Context) error {
r.Use(gin.Logger(), gin.Recovery()) r.Use(gin.Logger(), gin.Recovery())

@ -7,12 +7,14 @@ RUN apt-get update --allow-releaseinfo-change && \
cd /tmp && wget https://github.com/protocolbuffers/protobuf/releases/download/v21.5/protoc-21.5-linux-x86_64.zip && \ cd /tmp && wget https://github.com/protocolbuffers/protobuf/releases/download/v21.5/protoc-21.5-linux-x86_64.zip && \
unzip protoc-21.5-linux-x86_64.zip -d /usr/local/ && chmod +x /usr/local/bin/protoc unzip protoc-21.5-linux-x86_64.zip -d /usr/local/ && chmod +x /usr/local/bin/protoc
VOLUME [ "/code", "/go" ]
# RUN go mod download # RUN go mod download
ENV GOPATH="/go" ENV GOPATH="/go"
VOLUME [ "/code", "/go" ]
# Install protoc-gen-go + protoc-gen-micro # Install protoc-gen-go + protoc-gen-micro
RUN cd /tmp; go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28; go install github.com/go-micro/generator/cmd/protoc-gen-micro@v1.0.0 RUN cd /tmp; go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28; go install github.com/go-micro/generator/cmd/protoc-gen-micro@v1.0.0
WORKDIR /code WORKDIR /code
COPY ./scripts /scripts

@ -0,0 +1,2 @@
#!/bin/sh
find . -name '*.proto' -print0 | xargs -0 -I {} /bin/sh -c 'cd $(dirname $1); PATH=$PATH:/go/bin protoc --micro_out=paths=source_relative:. --go_out=paths=source_relative:. $(basename $1)' '_' '{}'

@ -0,0 +1,10 @@
#!/bin/bash
set -ex
go mod tidy -go=1.19
for i in $(find . -name 'main.go'); do
pushd $(dirname $i)
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go get -installsuffix cgo -ldflags="-w -s" -u ./...
popd
done

@ -15,10 +15,10 @@ const (
) )
type Config struct { type Config struct {
Server ServerConfig Router RouterConfig
} }
type ServerConfig struct { type RouterConfig struct {
Env string Env string
Address string Address string
RouterURI string RouterURI string
@ -29,6 +29,6 @@ func GetConfig() Config {
return *_cfg return *_cfg
} }
func GetServerConfig() ServerConfig { func GetRouterConfig() RouterConfig {
return _cfg.Server return _cfg.Router
} }

@ -18,7 +18,7 @@ import (
// internal instance of Config // internal instance of Config
var _cfg *Config = &Config{ var _cfg *Config = &Config{
Server: ServerConfig{ Router: RouterConfig{
Env: EnvProd, Env: EnvProd,
Address: ":8080", Address: ":8080",
RouterURI: "router", RouterURI: "router",

@ -86,7 +86,7 @@ func (h *Handler) Start() error {
} }
} }
time.Sleep(time.Duration(config.GetServerConfig().RefreshSeconds) * time.Second) time.Sleep(time.Duration(config.GetRouterConfig().RefreshSeconds) * time.Second)
} }
}() }()

Loading…
Cancel
Save