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

refactor: Modualize the samples so that they can be gonew #130

Merged
merged 24 commits into from
Oct 14, 2023
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
25 changes: 6 additions & 19 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ jobs:
runs-on: ubuntu-latest
steps:

- name: Collect Workflow Telemetry
uses: runforesight/foresight-workflow-kit-action@v1
if: success() || failure()

- name: Set up Go 1.21
uses: actions/[email protected]
with:
Expand All @@ -25,18 +21,9 @@ jobs:
run: echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
id: setup_path

- name: Build
run: make ci

- name: Compute code coverage
run: go test -v -json -coverprofile=coverage.out ./...> ./test-report.json || true

- name: Analyze Test and/or Coverage Results
uses: runforesight/foresight-test-kit-action@v1
if: success() || failure()
with:
test_format: JSON
test_framework: GOLANG
test_path: "./test-report.json"
coverage_format: GOLANG
coverage_path: ./coverage.out
- name: Run CI for each Makefile
run: |
for makefile in $(find . -type f -name Makefile); do
make -C $(dirname $makefile) test
done

183 changes: 0 additions & 183 deletions Makefile

This file was deleted.

19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,24 @@ This repository contains examples of microservices implemented using
the directory name. The [cellar](https://github.com/goadesign/examples/tree/master/cellar) example
provides a complete implementation of a simple microservice.

The samples in each directory serve as templates, and you can clone them using the `gonew` command, as explained in [this blog post](https://go.dev/blog/gonew).

```shell
$ go install golang.org/x/tools/cmd/gonew@latest
$ gonew goa.design/examples/basic@latest github.com/<your_repo>/basic
$ cd basic
```

A [fully instrumented example](https://github.com/goadesign/clue/tree/main/example/weather) of a
system consisting of multiple Goa microservices is included in the
[Clue](https://github.com/goadesign/clue) repo.
system consisting of multiple Goa microservices is included in the [Clue](https://github.com/goadesign/clue) repo.

To get started with the Goa Clue example, you can use the gonew command to clone it into your own repository:

```shell
$ gonew github.com/goadesign/clue/example/weather github.com/<your_repo>/weather
$ cd weather
```
Please follow the README in the Clue repository for more details on running and testing the Goa Clue example.

As you study each example consider contributing back by providing better or more complete docs,
adding clarifying comments to code or fixing any error you may run across!
101 changes: 101 additions & 0 deletions basic/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#! /usr/bin/make
#
# Makefile for Goa examples
#
# Targets:
# - "depend" retrieves the Go packages needed to run the linter and tests
# - "gen" invokes the "goa" tool to generate the examples source code
# - "build" compiles the example microservices and client CLIs
# - "clean" deletes the output of "build"
# - "lint" runs the linter and checks the code format using goimports
# - "test" runs the tests
#
# Meta targets:
# - "all" is the default target, it runs all the targets in the order above.
#
GO_FILES=$(shell find . -type f -name '*.go')
MODULE=$(shell go list -m)
APP=calc

# Only list test and build dependencies
# Standard dependencies are installed via go get
DEPEND=\
github.com/hashicorp/go-getter \
github.com/cheggaaa/pb \
github.com/golang/protobuf/protoc-gen-go \
github.com/golang/protobuf/proto \
goa.design/goa/... \
golang.org/x/lint/golint \
golang.org/x/tools/cmd/goimports \
honnef.co/go/tools/cmd/staticcheck

.phony: all depend lint test build clean

all: gen lint test
@echo DONE!

# Install protoc
GOOS=$(shell go env GOOS)
PROTOC_VERSION=3.6.1
ifeq ($(GOOS),linux)
PROTOC=protoc-$(PROTOC_VERSION)-linux-x86_64
PROTOC_EXEC=$(PROTOC)/bin/protoc
GOBIN=$(GOPATH)/bin
else
ifeq ($(GOOS),darwin)
PROTOC=protoc-$(PROTOC_VERSION)-osx-x86_64
PROTOC_EXEC=$(PROTOC)/bin/protoc
GOBIN=$(GOPATH)/bin
else
ifeq ($(GOOS),windows)
PROTOC=protoc-$(PROTOC_VERSION)-win32
PROTOC_EXEC="$(PROTOC)\bin\protoc.exe"
GOBIN="$(GOPATH)\bin"
endif
endif
endif
depend:
@echo INSTALLING DEPENDENCIES...
@go get -v $(DEPEND)
@go install github.com/hashicorp/go-getter/cmd/go-getter && \
go-getter https://github.com/google/protobuf/releases/download/v$(PROTOC_VERSION)/$(PROTOC).zip $(PROTOC) && \
cp $(PROTOC_EXEC) $(GOBIN) && \
rm -r $(PROTOC)
@go install github.com/golang/protobuf/protoc-gen-go
@go get -t -v ./...

lint:
@echo LINTING CODE...
@if [ "`goimports -l $(GO_FILES) | grep -v .pb.go | tee /dev/stderr`" ]; then \
echo "^ - Repo contains improperly formatted go files" && echo && exit 1; \
fi
@if [ "`staticcheck -checks all,-ST1000,-ST1001 ./... | grep -v ".pb.go" | tee /dev/stderr`" ]; then \
echo "^ - staticcheck errors!" && echo && exit 1; \
fi

.PHONY: gen
gen:
@# NOTE: not all command line tools are generated
@echo GENERATING CODE...
goa gen "$(MODULE)/design" && \
goa example "$(MODULE)/design"

build:
@go build "./cmd/$(APP)" && go build "./cmd/$(APP)-cli"

clean:
@rm -rf "./cmd/$(APP)" "./cmd/$(APP)-cli"

test:
@echo TESTING...
@go test ./... > /dev/null

check-freshness:
@if [ "`git diff | wc -l`" -gt "0" ]; then \
echo "[ERROR] generated code not in-sync with design:"; \
echo; \
git status -s; \
git --no-pager diff; \
echo; \
exit 1; \
fi
9 changes: 6 additions & 3 deletions basic/cmd/calc/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func handleHTTPServer(ctx context.Context, u *url.URL, calcEndpoints *calc.Endpo
)
{
eh := errorHandler(logger)
calcServer = calcsvr.New(calcEndpoints, mux, dec, enc, eh, nil, http.Dir("../../gen/http"))
calcServer = calcsvr.New(calcEndpoints, mux, dec, enc, eh, nil, nil)
if debug {
servers := goahttp.Servers{
calcServer,
Expand All @@ -74,7 +74,7 @@ func handleHTTPServer(ctx context.Context, u *url.URL, calcEndpoints *calc.Endpo

// Start HTTP server using default configuration, change the code to
// configure the server as required by your service.
srv := &http.Server{Addr: u.Host, Handler: handler}
srv := &http.Server{Addr: u.Host, Handler: handler, ReadHeaderTimeout: time.Second * 60}
for _, m := range calcServer.Mounts {
logger.Printf("HTTP %q mounted on %s %s", m.Method, m.Verb, m.Pattern)
}
Expand All @@ -96,7 +96,10 @@ func handleHTTPServer(ctx context.Context, u *url.URL, calcEndpoints *calc.Endpo
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

_ = srv.Shutdown(ctx)
err := srv.Shutdown(ctx)
if err != nil {
logger.Printf("failed to shutdown: %v", err)
}
}()
}

Expand Down
Loading
Loading