Skip to content

Commit

Permalink
Add tool for grpc web testing (#30)
Browse files Browse the repository at this point in the history
* Add example

* add readme

* Add user v1 service reg and tests

* add user api v2 reg and tests

* fix docs
  • Loading branch information
crabio authored Sep 5, 2021
1 parent 0c2533f commit 507d727
Show file tree
Hide file tree
Showing 76 changed files with 6,112 additions and 105 deletions.
10 changes: 1 addition & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,4 @@ build:

run:
@echo "Run app"
go run .

build_docker:
@echo "Build docker image"
docker build . --file Dockerfile --tag archaeopteryx_boilerplate

run_docker:
@echo "Run app in docker"
docker run -p 8080:8080 -e LOG_LEVEL=trace archaeopteryx_boilerplate
go run .
78 changes: 3 additions & 75 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ This project is a prototype for microservice on Golang with all required depende
`archaeopteryx` helps you to create service with gRPC, gRPC proxy for REST API and some editional features from the box.
All features can be found in the list

You can find example of project in the `example` folder.

### Config

Service has configuration, bases on https://github.com/jinzhu/configor.
Expand Down Expand Up @@ -88,31 +90,6 @@ sudo mkdir /var/log/archaeopteryx
sudo chown $USER /var/log/archaeopteryx
```

## Build

### Docker

To build docker image use:
```sh
make build_docker
```

## Run

### Binary

To run server use:
```sh
make run
```

### Docker

To run server in docker use:
```sh
make run_docker
```

## Test

### Unit test
Expand All @@ -135,53 +112,4 @@ For testing gRPC API use [Kreya](https://kreya.app/)

Folder `kreya` contains Kreya project for working with the project.

### gRPC gateway

#### Hello world

To test REST gRPC gateway use command:
```sh
curl -X POST -k http://localhost:8090/v1/hello -d '{"name": " hello"}'
```

As a result you should get response: `{"message":" hello world"}`

#### User V1

##### Add User

To test REST gRPC gateway use command:
```sh
curl -X POST -k http://localhost:8090/v1/user -d '{"name":"bobby","password":"ilu"}'
```

As a result you should get response: `{"id":<random id>}`

##### Get User

To test REST gRPC gateway use command:
```sh
curl -X GET -k http://localhost:8090/v1/user?id=123
```

As a result you should get response: `{"name":"Bobby", "password":"qwerty"}`

#### User V2

##### Add User

To test REST gRPC gateway use command:
```sh
curl -X POST -k http://localhost:8090/v2/user -d '{"firstName":"bobby","lastName":"twist","password":"ilu"}'
```

As a result you should get response: `{"id":<random id>}`

##### Get User

To test REST gRPC gateway use command:
```sh
curl -X GET -k http://localhost:8090/v2/user?id=123
```

As a result you should get response: `{"firstName":"Bobby", "lastName":"Twist", "password":"qwerty"}`
Also `archaeopteryx` is compatible with [gRPC reflection](https://github.com/grpc/grpc/blob/master/doc/server-reflection.md)
2 changes: 2 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.vscode
coverage.txt
21 changes: 21 additions & 0 deletions example/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Build stage
FROM golang AS build-env
ADD . /src
ENV CGO_ENABLED=0
RUN apt-get update -y && \
# Build binary
cd /src && \
go build -o /app/main && \
# Get gRPC health probe
wget https://github.com/grpc-ecosystem/grpc-health-probe/releases/download/v0.4.5/grpc_health_probe-linux-amd64 -O /app/grpc_health_probe && \
chmod +x /app/grpc_health_probe

# Production stage
FROM alpine:3
COPY --from=build-env /app /

# Create folder for logs
RUN mkdir /var/log/archaeropteryx

HEALTHCHECK --interval=30s --timeout=5s --retries=3 CMD /grpc_health_probe -addr 0.0.0.0:8080 || exit 1
ENTRYPOINT ["/main"]
21 changes: 21 additions & 0 deletions example/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Ivan

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
66 changes: 66 additions & 0 deletions example/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
help:
@echo "Makefile for the archaeopteryx example."
@echo "Available targets:"
@echo " help - print help information"
@echo " install - install required dependecies for the project"
@echo " generate - generate proto files"
@echo " lint - run linter"
@echo " test - run unit tests"
@echo " build - build application binary"
@echo " run - run application"
@echo " build_docker - build application docker image"
@echo " run_docker - run application in docker"

install:
@echo "Install Buf generators"
go get \
google.golang.org/protobuf/cmd/protoc-gen-go \
google.golang.org/grpc/cmd/protoc-gen-go-grpc \
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway \
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2
go install \
google.golang.org/protobuf/cmd/protoc-gen-go \
google.golang.org/grpc/cmd/protoc-gen-go-grpc \
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway \
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2
curl -sSL \
"https://github.com/bufbuild/buf/releases/download/v0.48.2/buf-$(shell uname -s)-$(shell uname -m)" \
-o "$(shell go env GOPATH)/bin/buf" && \
chmod +x "$(shell go env GOPATH)/bin/buf"

echo "Install linter"
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell go env GOPATH)/bin v1.42.0

generate:
@echo "Generate proto"
buf mod update
buf generate

lint:
@echo "Run linter"
# Lint proto
buf lint
# Lint golang
golangci-lint run

test:
@echo "Run unit tests"
go test -v ./... -coverprofile coverage.txt -covermode atomic
@echo "Code coverage"
go tool cover -func coverage.txt

build:
@echo "Build app binary"
go build

run:
@echo "Run app"
go run .

build_docker:
@echo "Build docker image"
docker build . --file Dockerfile --tag archaeopteryx_example

run_docker:
@echo "Run app in docker"
docker run -p 8080:8080 -p 8090:8090 -e LOG_LEVEL=trace archaeopteryx_example
73 changes: 73 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# archaeopteryx_example

This project is a example of usage `archaeopteryx` for creating microservice on Golang with all required dependencies

## Install dependencies

```sh
make install
```

## Generate protobuf & docs

```sh
make generate
```

## Presequinces

`archaeopteryx` requires folder for log files.
To create it run:
```sh
sudo mkdir /var/log/archaeopteryx
sudo chown $USER /var/log/archaeopteryx
```

## Build

### Docker

To build docker image use:
```sh
make build_docker
```

## Run

### Binary

To run server use:
```sh
make run
```

### Docker

To run server in docker use:
```sh
make run_docker
```

## Test

### Unit test

For unit tests use:
```sh
make test
```

### Lint

For lint use:
```sh
make lint
```

### gRPC

For testing gRPC API use [Kreya](https://kreya.app/)

Folder `kreya` contains Kreya project for working with the project.

Also `archaeopteryx` is compatible with [gRPC reflection](https://github.com/grpc/grpc/blob/master/doc/server-reflection.md)
14 changes: 14 additions & 0 deletions example/buf.gen.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: v1beta1
plugins:
- name: go
out: proto/gen
opt: paths=source_relative
- name: go-grpc
out: proto/gen
opt: paths=source_relative
- name: grpc-gateway
out: proto/gen
opt: paths=source_relative
# Swagger docs
- name: openapiv2
out: docs/swagger
2 changes: 2 additions & 0 deletions example/buf.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Generated by buf. DO NOT EDIT.
version: v1
9 changes: 9 additions & 0 deletions example/buf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: v1beta1
name: buf.build/iakrevetkho/archaeopteryx_example
build:
roots:
- proto
lint:
ignore:
- google
- health
8 changes: 8 additions & 0 deletions example/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 0.0.1
log:
level: trace
filename: /var/log/archaeopteryx/archaeopteryx.log
maxSizeInMb: 20
maxAgeInDays: 30
maxBackups: 30
compress: true
8 changes: 8 additions & 0 deletions example/docs/embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package docs

import (
"embed"
)

//go:embed swagger/*
var Swagger embed.FS
46 changes: 46 additions & 0 deletions example/docs/swagger/google/api/annotations.swagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"swagger": "2.0",
"info": {
"title": "google/api/annotations.proto",
"version": "version not set"
},
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"paths": {},
"definitions": {
"protobufAny": {
"type": "object",
"properties": {
"typeUrl": {
"type": "string"
},
"value": {
"type": "string",
"format": "byte"
}
}
},
"rpcStatus": {
"type": "object",
"properties": {
"code": {
"type": "integer",
"format": "int32"
},
"message": {
"type": "string"
},
"details": {
"type": "array",
"items": {
"$ref": "#/definitions/protobufAny"
}
}
}
}
}
}
Loading

0 comments on commit 507d727

Please sign in to comment.