Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
94bd1025a6 | ||
|
7be4a67673 | ||
|
3e6ac73cfe | ||
|
aef6878ee0 | ||
|
81aa8e0231 | ||
|
c28f625cd4 | ||
|
5b161b88f7 | ||
|
cca8826a1f |
@@ -1,24 +0,0 @@
|
|||||||
---
|
|
||||||
name: Bug report
|
|
||||||
about: For reporting bugs in go-micro
|
|
||||||
title: "[BUG]"
|
|
||||||
labels: ''
|
|
||||||
assignees: ''
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
**Describe the bug**
|
|
||||||
|
|
||||||
1. What are you trying to do?
|
|
||||||
2. What did you expect to happen?
|
|
||||||
3. What happens instead?
|
|
||||||
|
|
||||||
**How to reproduce the bug:**
|
|
||||||
|
|
||||||
If possible, please include a minimal code snippet here.
|
|
||||||
|
|
||||||
**Environment:**
|
|
||||||
Go Version: please paste `go version` output here
|
|
||||||
```
|
|
||||||
please paste `go env` output here
|
|
||||||
```
|
|
@@ -1,17 +0,0 @@
|
|||||||
---
|
|
||||||
name: Feature request / Enhancement
|
|
||||||
about: If you have a need not served by go-micro
|
|
||||||
title: "[FEATURE]"
|
|
||||||
labels: ''
|
|
||||||
assignees: ''
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
**Is your feature request related to a problem? Please describe.**
|
|
||||||
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
|
|
||||||
|
|
||||||
**Describe the solution you'd like**
|
|
||||||
A clear and concise description of what you want to happen.
|
|
||||||
|
|
||||||
**Additional context**
|
|
||||||
Add any other context or screenshots about the feature request here.
|
|
@@ -1,14 +0,0 @@
|
|||||||
---
|
|
||||||
name: Question
|
|
||||||
about: Ask a question about go-micro
|
|
||||||
title: ''
|
|
||||||
labels: ''
|
|
||||||
assignees: ''
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
Before asking, please check if your question has already been answered:
|
|
||||||
|
|
||||||
1. Check the documentation - https://micro.mu/docs/
|
|
||||||
2. Check the examples and plugins - https://github.com/micro/examples & https://github.com/micro/go-plugins
|
|
||||||
3. Search existing issues
|
|
@@ -1,9 +0,0 @@
|
|||||||
## Pull Request template
|
|
||||||
Please, go through these steps before clicking submit on this PR.
|
|
||||||
|
|
||||||
1. Give a descriptive title to your PR.
|
|
||||||
2. Provide a description of your changes.
|
|
||||||
3. Make sure you have some relevant tests.
|
|
||||||
4. Put `closes #XXXX` in your comment to auto-close the issue that your PR fixes (if applicable).
|
|
||||||
|
|
||||||
**PLEASE REMOVE THIS TEMPLATE BEFORE SUBMITTING**
|
|
@@ -1,26 +0,0 @@
|
|||||||
# To get started with Dependabot version updates, you'll need to specify which
|
|
||||||
# package ecosystems to update and where the package manifests are located.
|
|
||||||
# Please see the documentation for all configuration options:
|
|
||||||
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
|
|
||||||
|
|
||||||
version: 2
|
|
||||||
updates:
|
|
||||||
|
|
||||||
# Maintain dependencies for GitHub Actions
|
|
||||||
- package-ecosystem: "github-actions"
|
|
||||||
directory: "/"
|
|
||||||
schedule:
|
|
||||||
interval: "daily"
|
|
||||||
commit-message:
|
|
||||||
prefix: "chore"
|
|
||||||
include: "scope"
|
|
||||||
|
|
||||||
# Maintain dependencies for Golang
|
|
||||||
- package-ecosystem: "gomod"
|
|
||||||
directory: "/"
|
|
||||||
schedule:
|
|
||||||
interval: "daily"
|
|
||||||
commit-message:
|
|
||||||
prefix: "chore"
|
|
||||||
include: "scope"
|
|
||||||
|
|
@@ -1,24 +0,0 @@
|
|||||||
name: "autoapprove"
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request_target:
|
|
||||||
types: [assigned, opened, synchronize, reopened]
|
|
||||||
workflow_run:
|
|
||||||
workflows: ["prbuild"]
|
|
||||||
types:
|
|
||||||
- completed
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
pull-requests: write
|
|
||||||
contents: write
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
autoapprove:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: approve
|
|
||||||
uses: hmarr/auto-approve-action@v3
|
|
||||||
if: github.actor == 'vtolstov' || github.actor == 'dependabot[bot]'
|
|
||||||
id: approve
|
|
||||||
with:
|
|
||||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
|
@@ -1,21 +0,0 @@
|
|||||||
name: "automerge"
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request_target:
|
|
||||||
types: [assigned, opened, synchronize, reopened]
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
pull-requests: write
|
|
||||||
contents: write
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
automerge:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
if: github.actor == 'vtolstov'
|
|
||||||
steps:
|
|
||||||
- name: merge
|
|
||||||
id: merge
|
|
||||||
run: gh pr merge --auto --merge "$PR_URL"
|
|
||||||
env:
|
|
||||||
PR_URL: ${{github.event.pull_request.html_url}}
|
|
||||||
GITHUB_TOKEN: ${{secrets.TOKEN}}
|
|
@@ -1,47 +0,0 @@
|
|||||||
name: build
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
- v3
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
name: test
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: setup
|
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: 1.17
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: cache
|
|
||||||
uses: actions/cache@v3
|
|
||||||
with:
|
|
||||||
path: ~/go/pkg/mod
|
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
|
||||||
restore-keys: ${{ runner.os }}-go-
|
|
||||||
- name: deps
|
|
||||||
run: go get -v -t -d ./...
|
|
||||||
- name: test
|
|
||||||
env:
|
|
||||||
INTEGRATION_TESTS: yes
|
|
||||||
run: go test -mod readonly -v ./...
|
|
||||||
lint:
|
|
||||||
name: lint
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: lint
|
|
||||||
uses: golangci/golangci-lint-action@v3.4.0
|
|
||||||
continue-on-error: true
|
|
||||||
with:
|
|
||||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
|
||||||
version: v1.30
|
|
||||||
# Optional: working directory, useful for monorepos
|
|
||||||
# working-directory: somedir
|
|
||||||
# Optional: golangci-lint command line arguments.
|
|
||||||
# args: --issues-exit-code=0
|
|
||||||
# Optional: show only new issues if it's a pull request. The default value is `false`.
|
|
||||||
# only-new-issues: true
|
|
@@ -1,39 +0,0 @@
|
|||||||
name: "codecov"
|
|
||||||
|
|
||||||
on:
|
|
||||||
workflow_run:
|
|
||||||
workflows: ["build"]
|
|
||||||
types:
|
|
||||||
- completed
|
|
||||||
push:
|
|
||||||
branches: [ v3 ]
|
|
||||||
pull_request:
|
|
||||||
branches: [ v3 ]
|
|
||||||
schedule:
|
|
||||||
- cron: '34 1 * * 0'
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
codecov:
|
|
||||||
name: codecov
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
permissions:
|
|
||||||
actions: read
|
|
||||||
contents: read
|
|
||||||
security-events: write
|
|
||||||
|
|
||||||
strategy:
|
|
||||||
fail-fast: false
|
|
||||||
matrix:
|
|
||||||
language: [ 'go' ]
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: setup
|
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: 1.17
|
|
||||||
- name: Run coverage
|
|
||||||
run: go test -v -race -coverprofile=coverage.out -covermode=atomic ./...
|
|
||||||
- name: codecov
|
|
||||||
uses: codecov/codecov-action@v3.1.1
|
|
@@ -1,78 +0,0 @@
|
|||||||
# For most projects, this workflow file will not need changing; you simply need
|
|
||||||
# to commit it to your repository.
|
|
||||||
#
|
|
||||||
# You may wish to alter this file to override the set of languages analyzed,
|
|
||||||
# or to provide custom queries or build logic.
|
|
||||||
#
|
|
||||||
# ******** NOTE ********
|
|
||||||
# We have attempted to detect the languages in your repository. Please check
|
|
||||||
# the `language` matrix defined below to confirm you have the correct set of
|
|
||||||
# supported CodeQL languages.
|
|
||||||
#
|
|
||||||
name: "codeql"
|
|
||||||
|
|
||||||
on:
|
|
||||||
workflow_run:
|
|
||||||
workflows: ["prbuild"]
|
|
||||||
types:
|
|
||||||
- completed
|
|
||||||
push:
|
|
||||||
branches: [ master, v3 ]
|
|
||||||
pull_request:
|
|
||||||
# The branches below must be a subset of the branches above
|
|
||||||
branches: [ master, v3 ]
|
|
||||||
schedule:
|
|
||||||
- cron: '34 1 * * 0'
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
analyze:
|
|
||||||
name: analyze
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
permissions:
|
|
||||||
actions: read
|
|
||||||
contents: read
|
|
||||||
security-events: write
|
|
||||||
|
|
||||||
strategy:
|
|
||||||
fail-fast: false
|
|
||||||
matrix:
|
|
||||||
language: [ 'go' ]
|
|
||||||
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
|
|
||||||
# Learn more:
|
|
||||||
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: setup
|
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: 1.17
|
|
||||||
# Initializes the CodeQL tools for scanning.
|
|
||||||
- name: init
|
|
||||||
uses: github/codeql-action/init@v2
|
|
||||||
with:
|
|
||||||
languages: ${{ matrix.language }}
|
|
||||||
# If you wish to specify custom queries, you can do so here or in a config file.
|
|
||||||
# By default, queries listed here will override any specified in a config file.
|
|
||||||
# Prefix the list here with "+" to use these queries and those in the config file.
|
|
||||||
# queries: ./path/to/local/query, your-org/your-repo/queries@main
|
|
||||||
|
|
||||||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
|
||||||
# If this step fails, then you should remove it and run the build manually (see below)
|
|
||||||
- name: autobuild
|
|
||||||
uses: github/codeql-action/autobuild@v2
|
|
||||||
|
|
||||||
# ℹ️ Command-line programs to run using the OS shell.
|
|
||||||
# 📚 https://git.io/JvXDl
|
|
||||||
|
|
||||||
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
|
|
||||||
# and modify them (or add more) to build your code if your project
|
|
||||||
# uses a compiled language
|
|
||||||
|
|
||||||
#- run: |
|
|
||||||
# make bootstrap
|
|
||||||
# make release
|
|
||||||
|
|
||||||
- name: analyze
|
|
||||||
uses: github/codeql-action/analyze@v2
|
|
@@ -1,27 +0,0 @@
|
|||||||
name: "dependabot-automerge"
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request_target:
|
|
||||||
types: [assigned, opened, synchronize, reopened]
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
pull-requests: write
|
|
||||||
contents: write
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
automerge:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
if: github.actor == 'dependabot[bot]'
|
|
||||||
steps:
|
|
||||||
- name: metadata
|
|
||||||
id: metadata
|
|
||||||
uses: dependabot/fetch-metadata@v1.3.6
|
|
||||||
with:
|
|
||||||
github-token: "${{ secrets.TOKEN }}"
|
|
||||||
- name: merge
|
|
||||||
id: merge
|
|
||||||
if: ${{contains(steps.metadata.outputs.dependency-names, 'go.unistack.org')}}
|
|
||||||
run: gh pr merge --auto --merge "$PR_URL"
|
|
||||||
env:
|
|
||||||
PR_URL: ${{github.event.pull_request.html_url}}
|
|
||||||
GITHUB_TOKEN: ${{secrets.TOKEN}}
|
|
@@ -1,40 +0,0 @@
|
|||||||
name: prbuild
|
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
- v3
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
name: test
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: setup
|
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: 1.17
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: cache
|
|
||||||
uses: actions/cache@v3
|
|
||||||
with:
|
|
||||||
path: ~/go/pkg/mod
|
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
|
||||||
restore-keys: ${{ runner.os }}-go-
|
|
||||||
- name: deps
|
|
||||||
run: go get -v -t -d ./...
|
|
||||||
- name: test
|
|
||||||
env:
|
|
||||||
INTEGRATION_TESTS: yes
|
|
||||||
run: go test -mod readonly -v ./...
|
|
||||||
lint:
|
|
||||||
name: lint
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: lint
|
|
||||||
uses: golangci/golangci-lint-action@v3.4.0
|
|
||||||
continue-on-error: true
|
|
||||||
with:
|
|
||||||
version: v1.30
|
|
3
.github/FUNDING.yml
vendored
Normal file
3
.github/FUNDING.yml
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# These are supported funding model platforms
|
||||||
|
|
||||||
|
github: micro
|
8
.github/ISSUE_TEMPLATE/bug_report.md
vendored
8
.github/ISSUE_TEMPLATE/bug_report.md
vendored
@@ -1,6 +1,6 @@
|
|||||||
---
|
---
|
||||||
name: Bug report
|
name: Bug report
|
||||||
about: For reporting bugs in micro
|
about: For reporting bugs in go-micro
|
||||||
title: "[BUG]"
|
title: "[BUG]"
|
||||||
labels: ''
|
labels: ''
|
||||||
assignees: ''
|
assignees: ''
|
||||||
@@ -16,3 +16,9 @@ assignees: ''
|
|||||||
**How to reproduce the bug:**
|
**How to reproduce the bug:**
|
||||||
|
|
||||||
If possible, please include a minimal code snippet here.
|
If possible, please include a minimal code snippet here.
|
||||||
|
|
||||||
|
**Environment:**
|
||||||
|
Go Version: please paste `go version` output here
|
||||||
|
```
|
||||||
|
please paste `go env` output here
|
||||||
|
```
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
---
|
---
|
||||||
name: Feature request / Enhancement
|
name: Feature request / Enhancement
|
||||||
about: If you have a need not served by micro
|
about: If you have a need not served by go-micro
|
||||||
title: "[FEATURE]"
|
title: "[FEATURE]"
|
||||||
labels: ''
|
labels: ''
|
||||||
assignees: ''
|
assignees: ''
|
||||||
@@ -14,4 +14,4 @@ A clear and concise description of what the problem is. Ex. I'm always frustrate
|
|||||||
A clear and concise description of what you want to happen.
|
A clear and concise description of what you want to happen.
|
||||||
|
|
||||||
**Additional context**
|
**Additional context**
|
||||||
Add any other context or screenshots about the feature request here.
|
Add any other context or screenshots about the feature request here.
|
||||||
|
10
.github/ISSUE_TEMPLATE/question.md
vendored
10
.github/ISSUE_TEMPLATE/question.md
vendored
@@ -1,8 +1,14 @@
|
|||||||
---
|
---
|
||||||
name: Question
|
name: Question
|
||||||
about: Ask a question about micro
|
about: Ask a question about go-micro
|
||||||
title: ''
|
title: ''
|
||||||
labels: ''
|
labels: ''
|
||||||
assignees: ''
|
assignees: ''
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
Before asking, please check if your question has already been answered:
|
||||||
|
|
||||||
|
1. Check the documentation - https://micro.mu/docs/
|
||||||
|
2. Check the examples and plugins - https://github.com/micro/examples & https://github.com/micro/go-plugins
|
||||||
|
3. Search existing issues
|
||||||
|
9
.github/PULL_REQUEST_TEMPLATE.md
vendored
9
.github/PULL_REQUEST_TEMPLATE.md
vendored
@@ -1,9 +1,10 @@
|
|||||||
## Pull Request template
|
## Pull Request template
|
||||||
Please, go through these steps before clicking submit on this PR.
|
Please, go through these steps before clicking submit on this PR.
|
||||||
|
|
||||||
1. Give a descriptive title to your PR.
|
1. Make sure this PR targets the `develop` branch. We follow the git-flow branching model.
|
||||||
2. Provide a description of your changes.
|
2. Give a descriptive title to your PR.
|
||||||
3. Make sure you have some relevant tests.
|
3. Provide a description of your changes.
|
||||||
4. Put `closes #XXXX` in your comment to auto-close the issue that your PR fixes (if applicable).
|
4. Make sure you have some relevant tests.
|
||||||
|
5. Put `closes #XXXX` in your comment to auto-close the issue that your PR fixes (if applicable).
|
||||||
|
|
||||||
**PLEASE REMOVE THIS TEMPLATE BEFORE SUBMITTING**
|
**PLEASE REMOVE THIS TEMPLATE BEFORE SUBMITTING**
|
||||||
|
28
.github/autoapprove.yml
vendored
28
.github/autoapprove.yml
vendored
@@ -1,28 +0,0 @@
|
|||||||
name: "autoapprove"
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request_target:
|
|
||||||
types: [assigned, opened, synchronize, reopened]
|
|
||||||
workflow_run:
|
|
||||||
workflows: ["prbuild"]
|
|
||||||
types:
|
|
||||||
- completed
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
pull-requests: write
|
|
||||||
contents: write
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
autoapprove:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: approve
|
|
||||||
run: [ "curl -o tea https://dl.gitea.com/tea/main/tea-main-linux-amd64",
|
|
||||||
"chmod +x ./tea",
|
|
||||||
"./tea login add --name unistack --token ${{ secrets.GITHUB_TOKEN }} --url https://git.unistack.org",
|
|
||||||
"./tea pr --repo ${{ github.event.repository.name }}"
|
|
||||||
]
|
|
||||||
if: github.actor == 'vtolstov'
|
|
||||||
id: approve
|
|
||||||
with:
|
|
||||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
|
15
.github/generate.sh
vendored
Executable file
15
.github/generate.sh
vendored
Executable file
@@ -0,0 +1,15 @@
|
|||||||
|
#!/bin/bash -e
|
||||||
|
|
||||||
|
find . -type f -name '*.pb.*.go' -o -name '*.pb.go' -a ! -name 'message.pb.go' -delete
|
||||||
|
PROTOS=$(find . -type f -name '*.proto' | grep -v proto/google/api)
|
||||||
|
|
||||||
|
mkdir -p proto/google/api
|
||||||
|
curl -s -o proto/google/api/annotations.proto -L https://raw.githubusercontent.com/googleapis/googleapis/master/google/api/annotations.proto
|
||||||
|
curl -s -o proto/google/api/http.proto -L https://raw.githubusercontent.com/googleapis/googleapis/master/google/api/http.proto
|
||||||
|
|
||||||
|
for PROTO in $PROTOS; do
|
||||||
|
echo $PROTO
|
||||||
|
protoc -I./proto -I. -I$(dirname $PROTO) --go_out=plugins=grpc,paths=source_relative:. --micro_out=paths=source_relative:. $PROTO
|
||||||
|
done
|
||||||
|
|
||||||
|
rm -r proto
|
22
.github/workflows/docker.yml
vendored
Normal file
22
.github/workflows/docker.yml
vendored
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
name: Docker
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
tags:
|
||||||
|
- v2.*
|
||||||
|
- v3.*
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
name: Check out repository
|
||||||
|
- uses: elgohr/Publish-Docker-Github-Action@2.12
|
||||||
|
name: Build and Push Docker Image
|
||||||
|
with:
|
||||||
|
name: micro/go-micro
|
||||||
|
username: ${{ secrets.DOCKER_USERNAME }}
|
||||||
|
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||||
|
tag_names: true
|
53
.github/workflows/job_coverage.yml
vendored
53
.github/workflows/job_coverage.yml
vendored
@@ -1,53 +0,0 @@
|
|||||||
name: coverage
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches: [ main, v3, v4 ]
|
|
||||||
paths-ignore:
|
|
||||||
- '.github/**'
|
|
||||||
- '.gitea/**'
|
|
||||||
pull_request:
|
|
||||||
branches: [ main, v3, v4 ]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
|
|
||||||
build:
|
|
||||||
if: github.server_url != 'https://github.com'
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: checkout code
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
filter: 'blob:none'
|
|
||||||
|
|
||||||
- name: setup go
|
|
||||||
uses: actions/setup-go@v5
|
|
||||||
with:
|
|
||||||
cache-dependency-path: "**/*.sum"
|
|
||||||
go-version: 'stable'
|
|
||||||
|
|
||||||
- name: test coverage
|
|
||||||
run: |
|
|
||||||
go test -v -cover ./... -covermode=count -coverprofile coverage.out -coverpkg ./...
|
|
||||||
go tool cover -func coverage.out -o coverage.out
|
|
||||||
|
|
||||||
- name: coverage badge
|
|
||||||
uses: tj-actions/coverage-badge-go@v2
|
|
||||||
with:
|
|
||||||
green: 80
|
|
||||||
filename: coverage.out
|
|
||||||
|
|
||||||
- uses: stefanzweifel/git-auto-commit-action@v4
|
|
||||||
name: autocommit
|
|
||||||
with:
|
|
||||||
commit_message: Apply Code Coverage Badge
|
|
||||||
skip_fetch: false
|
|
||||||
skip_checkout: false
|
|
||||||
file_pattern: ./README.md
|
|
||||||
|
|
||||||
- name: push
|
|
||||||
if: steps.auto-commit-action.outputs.changes_detected == 'true'
|
|
||||||
uses: ad-m/github-push-action@master
|
|
||||||
with:
|
|
||||||
github_token: ${{ github.token }}
|
|
||||||
branch: ${{ github.ref }}
|
|
29
.github/workflows/job_lint.yml
vendored
29
.github/workflows/job_lint.yml
vendored
@@ -1,29 +0,0 @@
|
|||||||
name: lint
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
types: [opened, reopened, synchronize]
|
|
||||||
branches: [ master, v3, v4 ]
|
|
||||||
paths-ignore:
|
|
||||||
- '.github/**'
|
|
||||||
- '.gitea/**'
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
lint:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: checkout code
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
filter: 'blob:none'
|
|
||||||
- name: setup go
|
|
||||||
uses: actions/setup-go@v5
|
|
||||||
with:
|
|
||||||
cache-dependency-path: "**/*.sum"
|
|
||||||
go-version: 'stable'
|
|
||||||
- name: setup deps
|
|
||||||
run: go get -v ./...
|
|
||||||
- name: run lint
|
|
||||||
uses: golangci/golangci-lint-action@v6
|
|
||||||
with:
|
|
||||||
version: 'latest'
|
|
94
.github/workflows/job_sync.yml
vendored
94
.github/workflows/job_sync.yml
vendored
@@ -1,94 +0,0 @@
|
|||||||
name: sync
|
|
||||||
|
|
||||||
on:
|
|
||||||
schedule:
|
|
||||||
- cron: '*/5 * * * *'
|
|
||||||
# Allows you to run this workflow manually from the Actions tab
|
|
||||||
workflow_dispatch:
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
sync:
|
|
||||||
if: github.server_url != 'https://github.com'
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: init
|
|
||||||
run: |
|
|
||||||
git config --global user.email "vtolstov <vtolstov@users.noreply.github.com>"
|
|
||||||
git config --global user.name "github-actions[bot]"
|
|
||||||
echo "machine git.unistack.org login vtolstov password ${{ secrets.TOKEN_GITEA }}" >> /root/.netrc
|
|
||||||
echo "machine github.com login vtolstov password ${{ secrets.TOKEN_GITHUB }}" >> /root/.netrc
|
|
||||||
|
|
||||||
- name: check master
|
|
||||||
id: check_master
|
|
||||||
run: |
|
|
||||||
src_hash=$(git ls-remote https://github.com/${GITHUB_REPOSITORY} refs/heads/master | cut -f1)
|
|
||||||
dst_hash=$(git ls-remote ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} refs/heads/master | cut -f1)
|
|
||||||
echo "src_hash=$src_hash"
|
|
||||||
echo "dst_hash=$dst_hash"
|
|
||||||
if [ "$src_hash" != "$dst_hash" ]; then
|
|
||||||
echo "sync_needed=true" >> $GITHUB_OUTPUT
|
|
||||||
else
|
|
||||||
echo "sync_needed=false" >> $GITHUB_OUTPUT
|
|
||||||
fi
|
|
||||||
|
|
||||||
- name: sync master
|
|
||||||
if: steps.check_master.outputs.sync_needed == 'true'
|
|
||||||
run: |
|
|
||||||
git clone --filter=blob:none --filter=tree:0 --branch master --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo
|
|
||||||
cd repo
|
|
||||||
git remote add --no-tags --fetch --track master upstream https://github.com/${GITHUB_REPOSITORY}
|
|
||||||
git pull --rebase upstream master
|
|
||||||
git push upstream master --progress
|
|
||||||
git push origin master --progress
|
|
||||||
cd ../
|
|
||||||
rm -rf repo
|
|
||||||
|
|
||||||
- name: check v3
|
|
||||||
id: check_v3
|
|
||||||
run: |
|
|
||||||
src_hash=$(git ls-remote https://github.com/${GITHUB_REPOSITORY} refs/heads/v3 | cut -f1)
|
|
||||||
dst_hash=$(git ls-remote ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} refs/heads/v3 | cut -f1)
|
|
||||||
echo "src_hash=$src_hash"
|
|
||||||
echo "dst_hash=$dst_hash"
|
|
||||||
if [ "$src_hash" != "$dst_hash" ]; then
|
|
||||||
echo "sync_needed=true" >> $GITHUB_OUTPUT
|
|
||||||
else
|
|
||||||
echo "sync_needed=false" >> $GITHUB_OUTPUT
|
|
||||||
fi
|
|
||||||
|
|
||||||
- name: sync v3
|
|
||||||
if: steps.check_v3.outputs.sync_needed == 'true'
|
|
||||||
run: |
|
|
||||||
git clone --filter=blob:none --filter=tree:0 --branch v3 --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo
|
|
||||||
cd repo
|
|
||||||
git remote add --no-tags --fetch --track v3 upstream https://github.com/${GITHUB_REPOSITORY}
|
|
||||||
git pull --rebase upstream v3
|
|
||||||
git push upstream v3 --progress
|
|
||||||
git push origin v3 --progress
|
|
||||||
cd ../
|
|
||||||
rm -rf repo
|
|
||||||
|
|
||||||
- name: check v4
|
|
||||||
id: check_v4
|
|
||||||
run: |
|
|
||||||
src_hash=$(git ls-remote https://github.com/${GITHUB_REPOSITORY} refs/heads/v4 | cut -f1)
|
|
||||||
dst_hash=$(git ls-remote ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} refs/heads/v4 | cut -f1)
|
|
||||||
echo "src_hash=$src_hash"
|
|
||||||
echo "dst_hash=$dst_hash"
|
|
||||||
if [ "$src_hash" != "$dst_hash" ]; then
|
|
||||||
echo "sync_needed=true" >> $GITHUB_OUTPUT
|
|
||||||
else
|
|
||||||
echo "sync_needed=false" >> $GITHUB_OUTPUT
|
|
||||||
fi
|
|
||||||
|
|
||||||
- name: sync v4
|
|
||||||
if: steps.check_v4.outputs.sync_needed == 'true'
|
|
||||||
run: |
|
|
||||||
git clone --filter=blob:none --filter=tree:0 --branch v4 --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo
|
|
||||||
cd repo
|
|
||||||
git remote add --no-tags --fetch --track v4 upstream https://github.com/${GITHUB_REPOSITORY}
|
|
||||||
git pull --rebase upstream v4
|
|
||||||
git push upstream v4 --progress
|
|
||||||
git push origin v4 --progress
|
|
||||||
cd ../
|
|
||||||
rm -rf repo
|
|
31
.github/workflows/job_test.yml
vendored
31
.github/workflows/job_test.yml
vendored
@@ -1,31 +0,0 @@
|
|||||||
name: test
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
types: [opened, reopened, synchronize]
|
|
||||||
branches: [ master, v3, v4 ]
|
|
||||||
push:
|
|
||||||
branches: [ master, v3, v4 ]
|
|
||||||
paths-ignore:
|
|
||||||
- '.github/**'
|
|
||||||
- '.gitea/**'
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: checkout code
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
filter: 'blob:none'
|
|
||||||
- name: setup go
|
|
||||||
uses: actions/setup-go@v5
|
|
||||||
with:
|
|
||||||
cache-dependency-path: "**/*.sum"
|
|
||||||
go-version: 'stable'
|
|
||||||
- name: setup deps
|
|
||||||
run: go get -v ./...
|
|
||||||
- name: run test
|
|
||||||
env:
|
|
||||||
INTEGRATION_TESTS: yes
|
|
||||||
run: go test -mod readonly -v ./...
|
|
50
.github/workflows/job_tests.yml
vendored
50
.github/workflows/job_tests.yml
vendored
@@ -1,50 +0,0 @@
|
|||||||
name: test
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
types: [opened, reopened, synchronize]
|
|
||||||
branches: [ master, v3, v4 ]
|
|
||||||
push:
|
|
||||||
branches: [ master, v3, v4 ]
|
|
||||||
paths-ignore:
|
|
||||||
- '.github/**'
|
|
||||||
- '.gitea/**'
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: checkout code
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
filter: 'blob:none'
|
|
||||||
- name: checkout tests
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
ref: master
|
|
||||||
filter: 'blob:none'
|
|
||||||
repository: unistack-org/micro-tests
|
|
||||||
path: micro-tests
|
|
||||||
- name: setup go
|
|
||||||
uses: actions/setup-go@v5
|
|
||||||
with:
|
|
||||||
cache-dependency-path: "**/*.sum"
|
|
||||||
go-version: 'stable'
|
|
||||||
- name: setup go work
|
|
||||||
env:
|
|
||||||
GOWORK: ${{ github.workspace }}/go.work
|
|
||||||
run: |
|
|
||||||
go work init
|
|
||||||
go work use .
|
|
||||||
go work use micro-tests
|
|
||||||
- name: setup deps
|
|
||||||
env:
|
|
||||||
GOWORK: ${{ github.workspace }}/go.work
|
|
||||||
run: go get -v ./...
|
|
||||||
- name: run tests
|
|
||||||
env:
|
|
||||||
INTEGRATION_TESTS: yes
|
|
||||||
GOWORK: ${{ github.workspace }}/go.work
|
|
||||||
run: |
|
|
||||||
cd micro-tests
|
|
||||||
go test -mod readonly -v ./... || true
|
|
34
.github/workflows/micro-examples.yml
vendored
Normal file
34
.github/workflows/micro-examples.yml
vendored
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
name: Build all github.com/micro/examples
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- 'release-**'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
|
||||||
|
build:
|
||||||
|
name: Build repos
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
|
||||||
|
- name: Set up Go 1.13
|
||||||
|
uses: actions/setup-go@v1
|
||||||
|
with:
|
||||||
|
go-version: 1.13
|
||||||
|
id: go
|
||||||
|
|
||||||
|
- name: Check out this code
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
with:
|
||||||
|
path: 'go-micro'
|
||||||
|
|
||||||
|
- name: Check out code examples
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
with:
|
||||||
|
repository: 'micro/examples'
|
||||||
|
path: 'examples'
|
||||||
|
|
||||||
|
- name: Build all
|
||||||
|
run: $GITHUB_WORKSPACE/go-micro/.github/workflows/scripts/build-all-examples.sh $GITHUB_SHA
|
||||||
|
working-directory: examples
|
||||||
|
|
34
.github/workflows/micro-main.yml
vendored
Normal file
34
.github/workflows/micro-main.yml
vendored
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
name: Build and test micro
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- 'release-**'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
|
||||||
|
build:
|
||||||
|
name: Build and test micro
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
|
||||||
|
- name: Set up Go 1.13
|
||||||
|
uses: actions/setup-go@v1
|
||||||
|
with:
|
||||||
|
go-version: 1.13
|
||||||
|
id: go
|
||||||
|
|
||||||
|
- name: Check out this code
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
with:
|
||||||
|
path: 'go-micro'
|
||||||
|
|
||||||
|
- name: Check out micro
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
with:
|
||||||
|
repository: 'micro/micro'
|
||||||
|
path: 'micro'
|
||||||
|
|
||||||
|
- name: Build all
|
||||||
|
run: $GITHUB_WORKSPACE/go-micro/.github/workflows/scripts/build-micro.sh $GITHUB_SHA
|
||||||
|
working-directory: micro
|
||||||
|
|
28
.github/workflows/pr.yml
vendored
Normal file
28
.github/workflows/pr.yml
vendored
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
name: PR Sanity Check
|
||||||
|
on: pull_request
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
|
||||||
|
prtest:
|
||||||
|
name: PR sanity check
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
|
||||||
|
- name: Set up Go 1.13
|
||||||
|
uses: actions/setup-go@v1
|
||||||
|
with:
|
||||||
|
go-version: 1.13
|
||||||
|
id: go
|
||||||
|
|
||||||
|
- name: Check out code into the Go module directory
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
|
- name: Get dependencies
|
||||||
|
run: |
|
||||||
|
go get -v -t -d ./...
|
||||||
|
|
||||||
|
- name: Run tests
|
||||||
|
id: tests
|
||||||
|
env:
|
||||||
|
IN_TRAVIS_CI: yes
|
||||||
|
run: go test -v ./...
|
41
.github/workflows/scripts/build-all-examples.sh
vendored
Executable file
41
.github/workflows/scripts/build-all-examples.sh
vendored
Executable file
@@ -0,0 +1,41 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# set -x
|
||||||
|
|
||||||
|
function build_binary {
|
||||||
|
echo building $1
|
||||||
|
pushd $1
|
||||||
|
go build -o _main
|
||||||
|
local ret=$?
|
||||||
|
if [ $ret -gt 0 ]; then
|
||||||
|
failed=1
|
||||||
|
failed_arr+=($1)
|
||||||
|
fi
|
||||||
|
popd
|
||||||
|
}
|
||||||
|
|
||||||
|
function is_main {
|
||||||
|
grep "package main" -l -dskip $1/*.go > /dev/null 2>&1
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function check_dir {
|
||||||
|
is_main $1
|
||||||
|
local ret=$?
|
||||||
|
if [ $ret == 0 ]; then
|
||||||
|
build_binary $1 $2
|
||||||
|
fi
|
||||||
|
for filename in $1/*; do
|
||||||
|
if [ -d $filename ]; then
|
||||||
|
check_dir $filename $2
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
}
|
||||||
|
failed_arr=()
|
||||||
|
failed=0
|
||||||
|
go mod edit -replace github.com/micro/go-micro/v2=github.com/micro/go-micro/v2@$1
|
||||||
|
check_dir . $1
|
||||||
|
if [ $failed -gt 0 ]; then
|
||||||
|
echo Some builds failed
|
||||||
|
printf '%s\n' "${failed_arr[@]}"
|
||||||
|
fi
|
||||||
|
exit $failed
|
14
.github/workflows/scripts/build-micro.sh
vendored
Executable file
14
.github/workflows/scripts/build-micro.sh
vendored
Executable file
@@ -0,0 +1,14 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# set -x
|
||||||
|
|
||||||
|
failed=0
|
||||||
|
go mod edit -replace github.com/micro/go-micro/v2=github.com/micro/go-micro/v2@$1
|
||||||
|
# basic test, build the binary
|
||||||
|
go build
|
||||||
|
failed=$?
|
||||||
|
if [ $failed -gt 0 ]; then
|
||||||
|
exit $failed
|
||||||
|
fi
|
||||||
|
# unit tests
|
||||||
|
IN_TRAVIS_CI=yes go test -v ./...
|
||||||
|
# TODO integration tests
|
51
.github/workflows/tests.yml
vendored
Normal file
51
.github/workflows/tests.yml
vendored
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
name: Run tests
|
||||||
|
on: [push]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
|
||||||
|
test:
|
||||||
|
name: Test repo
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
|
||||||
|
- name: Set up Go 1.13
|
||||||
|
uses: actions/setup-go@v1
|
||||||
|
with:
|
||||||
|
go-version: 1.13
|
||||||
|
id: go
|
||||||
|
|
||||||
|
- name: Check out code into the Go module directory
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
|
- name: Get dependencies
|
||||||
|
run: |
|
||||||
|
go get -v -t -d ./...
|
||||||
|
|
||||||
|
- name: Run tests
|
||||||
|
id: tests
|
||||||
|
env:
|
||||||
|
IN_TRAVIS_CI: yes
|
||||||
|
run: go test -v ./...
|
||||||
|
|
||||||
|
- name: Notify of test failure
|
||||||
|
if: failure()
|
||||||
|
uses: rtCamp/action-slack-notify@v2.0.0
|
||||||
|
env:
|
||||||
|
SLACK_CHANNEL: build
|
||||||
|
SLACK_COLOR: '#BF280A'
|
||||||
|
SLACK_ICON: https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png
|
||||||
|
SLACK_TITLE: Tests Failed
|
||||||
|
SLACK_USERNAME: GitHub Actions
|
||||||
|
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_URL }}
|
||||||
|
|
||||||
|
- name: Notify of test success
|
||||||
|
if: success()
|
||||||
|
uses: rtCamp/action-slack-notify@v2.0.0
|
||||||
|
env:
|
||||||
|
SLACK_CHANNEL: build
|
||||||
|
SLACK_COLOR: '#1FAD2B'
|
||||||
|
SLACK_ICON: https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png
|
||||||
|
SLACK_TITLE: Tests Passed
|
||||||
|
SLACK_USERNAME: GitHub Actions
|
||||||
|
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_URL }}
|
||||||
|
|
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,8 +1,6 @@
|
|||||||
# Develop tools
|
# Develop tools
|
||||||
/.vscode/
|
/.vscode/
|
||||||
/.idea/
|
/.idea/
|
||||||
.idea
|
|
||||||
.vscode
|
|
||||||
|
|
||||||
# Binaries for programs and plugins
|
# Binaries for programs and plugins
|
||||||
*.exe
|
*.exe
|
||||||
@@ -15,7 +13,6 @@
|
|||||||
_obj
|
_obj
|
||||||
_test
|
_test
|
||||||
_build
|
_build
|
||||||
.DS_Store
|
|
||||||
|
|
||||||
# Architecture specific extensions/prefixes
|
# Architecture specific extensions/prefixes
|
||||||
*.[568vq]
|
*.[568vq]
|
||||||
|
@@ -1,5 +1,26 @@
|
|||||||
run:
|
run:
|
||||||
concurrency: 8
|
deadline: 10m
|
||||||
timeout: 5m
|
linters:
|
||||||
issues-exit-code: 1
|
disable-all: false
|
||||||
tests: true
|
enable-all: false
|
||||||
|
enable:
|
||||||
|
- megacheck
|
||||||
|
- staticcheck
|
||||||
|
- deadcode
|
||||||
|
- varcheck
|
||||||
|
- gosimple
|
||||||
|
- unused
|
||||||
|
- prealloc
|
||||||
|
- scopelint
|
||||||
|
- gocritic
|
||||||
|
- goimports
|
||||||
|
- unconvert
|
||||||
|
- govet
|
||||||
|
- nakedret
|
||||||
|
- structcheck
|
||||||
|
- gosec
|
||||||
|
disable:
|
||||||
|
- maligned
|
||||||
|
- interfacer
|
||||||
|
- typecheck
|
||||||
|
- dupl
|
||||||
|
13
Dockerfile
Normal file
13
Dockerfile
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
FROM golang:1.13-alpine
|
||||||
|
|
||||||
|
RUN mkdir /user && \
|
||||||
|
echo 'nobody:x:65534:65534:nobody:/:' > /user/passwd && \
|
||||||
|
echo 'nobody:x:65534:' > /user/group
|
||||||
|
|
||||||
|
ENV GO111MODULE=on
|
||||||
|
RUN apk --no-cache add make git gcc libtool musl-dev ca-certificates dumb-init && \
|
||||||
|
rm -rf /var/cache/apk/* /tmp/*
|
||||||
|
|
||||||
|
WORKDIR /
|
||||||
|
COPY ./go.mod ./go.sum ./
|
||||||
|
RUN go mod download && rm go.mod go.sum
|
3
LICENSE
3
LICENSE
@@ -176,8 +176,7 @@
|
|||||||
|
|
||||||
END OF TERMS AND CONDITIONS
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
Copyright 2015-2020 Asim Aslam.
|
Copyright 2015 Asim Aslam.
|
||||||
Copyright 2019-2020 Unistack LLC.
|
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
you may not use this file except in compliance with the License.
|
you may not use this file except in compliance with the License.
|
||||||
|
66
README.md
66
README.md
@@ -1,43 +1,73 @@
|
|||||||
# Micro
|
# Go Micro [](https://opensource.org/licenses/Apache-2.0) [](https://pkg.go.dev/github.com/micro/go-micro?tab=doc) [](https://travis-ci.org/micro/go-micro) [](https://goreportcard.com/report/github.com/micro/go-micro)
|
||||||

|
|
||||||
[](https://opensource.org/licenses/Apache-2.0)
|
|
||||||
[](https://pkg.go.dev/go.unistack.org/micro/v4?tab=overview)
|
|
||||||
[](https://git.unistack.org/unistack-org/micro/actions?query=workflow%3Abuild+branch%3Av4+event%3Apush)
|
|
||||||
[](https://goreportcard.com/report/go.unistack.org/micro/v4)
|
|
||||||
|
|
||||||
Micro is a standard library for microservices.
|
Go Micro is a framework for distributed systems development.
|
||||||
|
|
||||||
## Overview
|
## Overview
|
||||||
|
|
||||||
Micro provides the core requirements for distributed systems development including RPC and Event driven communication.
|
Go Micro provides the core requirements for distributed systems development including RPC and Event driven communication.
|
||||||
|
The **Micro** philosophy is sane defaults with a pluggable architecture. We provide defaults to get you started quickly
|
||||||
|
but everything can be easily swapped out.
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
Micro abstracts away the details of distributed systems. Here are the main features.
|
Go Micro abstracts away the details of distributed systems. Here are the main features.
|
||||||
|
|
||||||
|
- **Authentication** - Auth is built in as a first class citizen. Authentication and authorization enable secure
|
||||||
|
zero trust networking by providing every service an identity and certificates. This additionally includes rule
|
||||||
|
based access control.
|
||||||
|
|
||||||
- **Dynamic Config** - Load and hot reload dynamic config from anywhere. The config interface provides a way to load application
|
- **Dynamic Config** - Load and hot reload dynamic config from anywhere. The config interface provides a way to load application
|
||||||
level config from any source such as env vars, cmdline, file, consul, vault... You can merge the sources and even define fallbacks.
|
level config from any source such as env vars, file, etcd. You can merge the sources and even define fallbacks.
|
||||||
|
|
||||||
- **Data Storage** - A simple data store interface to read, write and delete records. It includes support for memory, file and
|
- **Data Storage** - A simple data store interface to read, write and delete records. It includes support for memory, file and
|
||||||
s3. State and persistence becomes a core requirement beyond prototyping and Micro looks to build that into the framework.
|
CockroachDB by default. State and persistence becomes a core requirement beyond prototyping and Micro looks to build that into the framework.
|
||||||
|
|
||||||
- **Service Discovery** - Automatic service registration and name resolution. Service discovery is at the core of micro service
|
- **Service Discovery** - Automatic service registration and name resolution. Service discovery is at the core of micro service
|
||||||
development. When service A needs to speak to service B it needs the location of that service.
|
development. When service A needs to speak to service B it needs the location of that service. The default discovery mechanism is
|
||||||
|
multicast DNS (mdns), a zeroconf system.
|
||||||
|
|
||||||
|
- **Load Balancing** - Client side load balancing built on service discovery. Once we have the addresses of any number of instances
|
||||||
|
of a service we now need a way to decide which node to route to. We use random hashed load balancing to provide even distribution
|
||||||
|
across the services and retry a different node if there's a problem.
|
||||||
|
|
||||||
- **Message Encoding** - Dynamic message encoding based on content-type. The client and server will use codecs along with content-type
|
- **Message Encoding** - Dynamic message encoding based on content-type. The client and server will use codecs along with content-type
|
||||||
to seamlessly encode and decode Go types for you. Any variety of messages could be encoded and sent from different clients. The client
|
to seamlessly encode and decode Go types for you. Any variety of messages could be encoded and sent from different clients. The client
|
||||||
and server handle this by default.
|
and server handle this by default. This includes protobuf and json by default.
|
||||||
|
|
||||||
- **Async Messaging** - Pub/Sub is built in as a first class citizen for asynchronous communication and event driven architectures.
|
- **gRPC Transport** - gRPC based request/response with support for bidirectional streaming. We provide an abstraction for synchronous communication. A request made to a service will be automatically resolved, load balanced, dialled and streamed.
|
||||||
Event notifications are a core pattern in micro service development.
|
|
||||||
|
- **Async Messaging** - PubSub is built in as a first class citizen for asynchronous communication and event driven architectures.
|
||||||
|
Event notifications are a core pattern in micro service development. The default messaging system is a HTTP event message broker.
|
||||||
|
|
||||||
- **Synchronization** - Distributed systems are often built in an eventually consistent manner. Support for distributed locking and
|
- **Synchronization** - Distributed systems are often built in an eventually consistent manner. Support for distributed locking and
|
||||||
leadership are built in as a Sync interface. When using an eventually consistent database or scheduling use the Sync interface.
|
leadership are built in as a Sync interface. When using an eventually consistent database or scheduling use the Sync interface.
|
||||||
|
|
||||||
- **Pluggable Interfaces** - Micro makes use of Go interfaces for each system abstraction. Because of this these interfaces
|
- **Pluggable Interfaces** - Go Micro makes use of Go interfaces for each distributed system abstraction. Because of this these interfaces
|
||||||
are pluggable and allows Micro to be runtime agnostic.
|
are pluggable and allows Go Micro to be runtime agnostic. You can plugin any underlying technology. Find plugins in
|
||||||
|
[github.com/micro/go-plugins](https://github.com/micro/go-plugins).
|
||||||
|
|
||||||
|
## Getting Started
|
||||||
|
|
||||||
|
To make use of Go Micro
|
||||||
|
|
||||||
|
```golang
|
||||||
|
import "github.com/micro/go-micro/v2"
|
||||||
|
|
||||||
|
// create a new service
|
||||||
|
service := micro.NewService(
|
||||||
|
micro.Name("helloworld"),
|
||||||
|
)
|
||||||
|
|
||||||
|
// initialise flags
|
||||||
|
service.Init()
|
||||||
|
|
||||||
|
// start the service
|
||||||
|
service.Run()
|
||||||
|
```
|
||||||
|
|
||||||
|
See the [docs](https://dev.m3o.com) for detailed information on the architecture, installation and use of go-micro.
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
Micro is Apache 2.0 licensed.
|
Go Micro is Apache 2.0 licensed.
|
||||||
|
|
||||||
|
1
_config.yml
Normal file
1
_config.yml
Normal file
@@ -0,0 +1 @@
|
|||||||
|
theme: jekyll-theme-architect
|
2
agent/agent.go
Normal file
2
agent/agent.go
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
// Package agent provides an interface for building robots
|
||||||
|
package agent
|
54
agent/command/command.go
Normal file
54
agent/command/command.go
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
// Package command is an interface for defining bot commands
|
||||||
|
package command
|
||||||
|
|
||||||
|
var (
|
||||||
|
// Commmands keyed by golang/regexp patterns
|
||||||
|
// regexp.Match(key, input) is used to match
|
||||||
|
Commands = map[string]Command{}
|
||||||
|
)
|
||||||
|
|
||||||
|
// Command is the interface for specific named
|
||||||
|
// commands executed via plugins or the bot.
|
||||||
|
type Command interface {
|
||||||
|
// Executes the command with args passed in
|
||||||
|
Exec(args ...string) ([]byte, error)
|
||||||
|
// Usage of the command
|
||||||
|
Usage() string
|
||||||
|
// Description of the command
|
||||||
|
Description() string
|
||||||
|
// Name of the command
|
||||||
|
String() string
|
||||||
|
}
|
||||||
|
|
||||||
|
type cmd struct {
|
||||||
|
name string
|
||||||
|
usage string
|
||||||
|
description string
|
||||||
|
exec func(args ...string) ([]byte, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cmd) Description() string {
|
||||||
|
return c.description
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cmd) Exec(args ...string) ([]byte, error) {
|
||||||
|
return c.exec(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cmd) Usage() string {
|
||||||
|
return c.usage
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cmd) String() string {
|
||||||
|
return c.name
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewCommand helps quickly create a new command
|
||||||
|
func NewCommand(name, usage, description string, exec func(args ...string) ([]byte, error)) Command {
|
||||||
|
return &cmd{
|
||||||
|
name: name,
|
||||||
|
usage: usage,
|
||||||
|
description: description,
|
||||||
|
exec: exec,
|
||||||
|
}
|
||||||
|
}
|
65
agent/command/command_test.go
Normal file
65
agent/command/command_test.go
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
package command
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCommand(t *testing.T) {
|
||||||
|
c := &cmd{
|
||||||
|
name: "test",
|
||||||
|
usage: "test usage",
|
||||||
|
description: "test description",
|
||||||
|
exec: func(args ...string) ([]byte, error) {
|
||||||
|
return []byte("test"), nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.String() != c.name {
|
||||||
|
t.Fatalf("expected name %s got %s", c.name, c.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.Usage() != c.usage {
|
||||||
|
t.Fatalf("expected usage %s got %s", c.usage, c.Usage())
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.Description() != c.description {
|
||||||
|
t.Fatalf("expected description %s got %s", c.description, c.Description())
|
||||||
|
}
|
||||||
|
|
||||||
|
if r, err := c.Exec(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
} else if string(r) != "test" {
|
||||||
|
t.Fatalf("expected exec result test got %s", string(r))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewCommand(t *testing.T) {
|
||||||
|
c := &cmd{
|
||||||
|
name: "test",
|
||||||
|
usage: "test usage",
|
||||||
|
description: "test description",
|
||||||
|
exec: func(args ...string) ([]byte, error) {
|
||||||
|
return []byte("test"), nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
nc := NewCommand(c.name, c.usage, c.description, c.exec)
|
||||||
|
|
||||||
|
if nc.String() != c.name {
|
||||||
|
t.Fatalf("expected name %s got %s", c.name, nc.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
if nc.Usage() != c.usage {
|
||||||
|
t.Fatalf("expected usage %s got %s", c.usage, nc.Usage())
|
||||||
|
}
|
||||||
|
|
||||||
|
if nc.Description() != c.description {
|
||||||
|
t.Fatalf("expected description %s got %s", c.description, nc.Description())
|
||||||
|
}
|
||||||
|
|
||||||
|
if r, err := nc.Exec(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
} else if string(r) != "test" {
|
||||||
|
t.Fatalf("expected exec result test got %s", string(r))
|
||||||
|
}
|
||||||
|
}
|
22
agent/input/discord/README.md
Normal file
22
agent/input/discord/README.md
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
# Discord input for micro-bot
|
||||||
|
[Discord](https://discordapp.com) support for micro bot based on [discordgo](github.com/bwmarrin/discordgo).
|
||||||
|
|
||||||
|
This was originally written by Aleksandr Tihomirov (@zet4) and can be found at https://github.com/zet4/micro-misc/.
|
||||||
|
|
||||||
|
## Options
|
||||||
|
### discord_token
|
||||||
|
|
||||||
|
You have to supply an application token via `--discord_token`.
|
||||||
|
|
||||||
|
Head over to Discord's [developer introduction](https://discordapp.com/developers/docs/intro)
|
||||||
|
to learn how to create applications and how the API works.
|
||||||
|
|
||||||
|
### discord_prefix
|
||||||
|
|
||||||
|
Set a command prefix with `--discord_prefix`. The default prefix is `Micro `.
|
||||||
|
You can mention the bot or use the prefix to run a command.
|
||||||
|
|
||||||
|
### discord_whitelist
|
||||||
|
|
||||||
|
Pass a list of comma-separated user IDs with `--discord_whitelist`. Only allow
|
||||||
|
these users to use the bot.
|
116
agent/input/discord/conn.go
Normal file
116
agent/input/discord/conn.go
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
package discord
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
"github.com/micro/go-micro/v2/agent/input"
|
||||||
|
"github.com/micro/go-micro/v2/logger"
|
||||||
|
)
|
||||||
|
|
||||||
|
type discordConn struct {
|
||||||
|
master *discordInput
|
||||||
|
exit chan struct{}
|
||||||
|
recv chan *discordgo.Message
|
||||||
|
|
||||||
|
sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func newConn(master *discordInput) *discordConn {
|
||||||
|
conn := &discordConn{
|
||||||
|
master: master,
|
||||||
|
exit: make(chan struct{}),
|
||||||
|
recv: make(chan *discordgo.Message),
|
||||||
|
}
|
||||||
|
|
||||||
|
conn.master.session.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
|
if m.Author.ID == master.botID {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
whitelisted := false
|
||||||
|
for _, ID := range conn.master.whitelist {
|
||||||
|
if m.Author.ID == ID {
|
||||||
|
whitelisted = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(master.whitelist) > 0 && !whitelisted {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var valid bool
|
||||||
|
m.Message.Content, valid = conn.master.prefixfn(m.Message.Content)
|
||||||
|
if !valid {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
conn.recv <- m.Message
|
||||||
|
})
|
||||||
|
|
||||||
|
return conn
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dc *discordConn) Recv(event *input.Event) error {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-dc.exit:
|
||||||
|
return errors.New("connection closed")
|
||||||
|
case msg := <-dc.recv:
|
||||||
|
|
||||||
|
event.From = msg.ChannelID + ":" + msg.Author.ID
|
||||||
|
event.To = dc.master.botID
|
||||||
|
event.Type = input.TextEvent
|
||||||
|
event.Data = []byte(msg.Content)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ChunkString(s string, chunkSize int) []string {
|
||||||
|
var chunks []string
|
||||||
|
runes := []rune(s)
|
||||||
|
|
||||||
|
if len(runes) == 0 {
|
||||||
|
return []string{s}
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < len(runes); i += chunkSize {
|
||||||
|
nn := i + chunkSize
|
||||||
|
if nn > len(runes) {
|
||||||
|
nn = len(runes)
|
||||||
|
}
|
||||||
|
chunks = append(chunks, string(runes[i:nn]))
|
||||||
|
}
|
||||||
|
return chunks
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dc *discordConn) Send(e *input.Event) error {
|
||||||
|
fields := strings.Split(e.To, ":")
|
||||||
|
for _, chunk := range ChunkString(string(e.Data), 2000) {
|
||||||
|
_, err := dc.master.session.ChannelMessageSend(fields[0], chunk)
|
||||||
|
if err != nil {
|
||||||
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
||||||
|
logger.Error("[bot][loop][send]", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dc *discordConn) Close() error {
|
||||||
|
if err := dc.master.session.Close(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-dc.exit:
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
close(dc.exit)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
153
agent/input/discord/discord.go
Normal file
153
agent/input/discord/discord.go
Normal file
@@ -0,0 +1,153 @@
|
|||||||
|
package discord
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
"github.com/micro/cli/v2"
|
||||||
|
"github.com/micro/go-micro/v2/agent/input"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
input.Inputs["discord"] = newInput()
|
||||||
|
}
|
||||||
|
|
||||||
|
func newInput() *discordInput {
|
||||||
|
return &discordInput{}
|
||||||
|
}
|
||||||
|
|
||||||
|
type discordInput struct {
|
||||||
|
token string
|
||||||
|
whitelist []string
|
||||||
|
prefix string
|
||||||
|
prefixfn func(string) (string, bool)
|
||||||
|
botID string
|
||||||
|
|
||||||
|
session *discordgo.Session
|
||||||
|
|
||||||
|
sync.Mutex
|
||||||
|
running bool
|
||||||
|
exit chan struct{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *discordInput) Flags() []cli.Flag {
|
||||||
|
return []cli.Flag{
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "discord_token",
|
||||||
|
EnvVars: []string{"MICRO_DISCORD_TOKEN"},
|
||||||
|
Usage: "Discord token (prefix with Bot if it's for bot account)",
|
||||||
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "discord_whitelist",
|
||||||
|
EnvVars: []string{"MICRO_DISCORD_WHITELIST"},
|
||||||
|
Usage: "Discord Whitelist (seperated by ,)",
|
||||||
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "discord_prefix",
|
||||||
|
Usage: "Discord Prefix",
|
||||||
|
EnvVars: []string{"MICRO_DISCORD_PREFIX"},
|
||||||
|
Value: "Micro ",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *discordInput) Init(ctx *cli.Context) error {
|
||||||
|
token := ctx.String("discord_token")
|
||||||
|
whitelist := ctx.String("discord_whitelist")
|
||||||
|
prefix := ctx.String("discord_prefix")
|
||||||
|
|
||||||
|
if len(token) == 0 {
|
||||||
|
return errors.New("require token")
|
||||||
|
}
|
||||||
|
|
||||||
|
d.token = token
|
||||||
|
d.prefix = prefix
|
||||||
|
|
||||||
|
if len(whitelist) > 0 {
|
||||||
|
d.whitelist = strings.Split(whitelist, ",")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *discordInput) Start() error {
|
||||||
|
if len(d.token) == 0 {
|
||||||
|
return errors.New("missing discord configuration")
|
||||||
|
}
|
||||||
|
|
||||||
|
d.Lock()
|
||||||
|
defer d.Unlock()
|
||||||
|
|
||||||
|
if d.running {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
d.session, err = discordgo.New("Bot " + d.token)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
u, err := d.session.User("@me")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
d.botID = u.ID
|
||||||
|
d.prefixfn = CheckPrefixFactory(fmt.Sprintf("<@%s> ", d.botID), fmt.Sprintf("<@!%s> ", d.botID), d.prefix)
|
||||||
|
|
||||||
|
d.exit = make(chan struct{})
|
||||||
|
d.running = true
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *discordInput) Stream() (input.Conn, error) {
|
||||||
|
d.Lock()
|
||||||
|
defer d.Unlock()
|
||||||
|
if !d.running {
|
||||||
|
return nil, errors.New("not running")
|
||||||
|
}
|
||||||
|
|
||||||
|
//Fire-n-forget close just in case...
|
||||||
|
d.session.Close()
|
||||||
|
|
||||||
|
conn := newConn(d)
|
||||||
|
if err := d.session.Open(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return conn, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *discordInput) Stop() error {
|
||||||
|
d.Lock()
|
||||||
|
defer d.Unlock()
|
||||||
|
|
||||||
|
if !d.running {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
close(d.exit)
|
||||||
|
d.running = false
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *discordInput) String() string {
|
||||||
|
return "discord"
|
||||||
|
}
|
||||||
|
|
||||||
|
// CheckPrefixFactory Creates a prefix checking function and stuff.
|
||||||
|
func CheckPrefixFactory(prefixes ...string) func(string) (string, bool) {
|
||||||
|
return func(content string) (string, bool) {
|
||||||
|
for _, prefix := range prefixes {
|
||||||
|
if strings.HasPrefix(content, prefix) {
|
||||||
|
return strings.TrimPrefix(content, prefix), true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
}
|
55
agent/input/input.go
Normal file
55
agent/input/input.go
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
// Package input is an interface for bot inputs
|
||||||
|
package input
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/micro/cli/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type EventType string
|
||||||
|
|
||||||
|
const (
|
||||||
|
TextEvent EventType = "text"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// Inputs keyed by name
|
||||||
|
// Example slack or hipchat
|
||||||
|
Inputs = map[string]Input{}
|
||||||
|
)
|
||||||
|
|
||||||
|
// Event is the unit sent and received
|
||||||
|
type Event struct {
|
||||||
|
Type EventType
|
||||||
|
From string
|
||||||
|
To string
|
||||||
|
Data []byte
|
||||||
|
Meta map[string]interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Input is an interface for sources which
|
||||||
|
// provide a way to communicate with the bot.
|
||||||
|
// Slack, HipChat, XMPP, etc.
|
||||||
|
type Input interface {
|
||||||
|
// Provide cli flags
|
||||||
|
Flags() []cli.Flag
|
||||||
|
// Initialise input using cli context
|
||||||
|
Init(*cli.Context) error
|
||||||
|
// Stream events from the input
|
||||||
|
Stream() (Conn, error)
|
||||||
|
// Start the input
|
||||||
|
Start() error
|
||||||
|
// Stop the input
|
||||||
|
Stop() error
|
||||||
|
// name of the input
|
||||||
|
String() string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Conn interface provides a way to
|
||||||
|
// send and receive events. Send and
|
||||||
|
// Recv both block until succeeding
|
||||||
|
// or failing.
|
||||||
|
type Conn interface {
|
||||||
|
Close() error
|
||||||
|
Recv(*Event) error
|
||||||
|
Send(*Event) error
|
||||||
|
}
|
160
agent/input/slack/conn.go
Normal file
160
agent/input/slack/conn.go
Normal file
@@ -0,0 +1,160 @@
|
|||||||
|
package slack
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/micro/go-micro/v2/agent/input"
|
||||||
|
"github.com/nlopes/slack"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Satisfies the input.Conn interface
|
||||||
|
type slackConn struct {
|
||||||
|
auth *slack.AuthTestResponse
|
||||||
|
rtm *slack.RTM
|
||||||
|
exit chan bool
|
||||||
|
|
||||||
|
sync.Mutex
|
||||||
|
names map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *slackConn) run() {
|
||||||
|
// func retrieves user names and maps to IDs
|
||||||
|
setNames := func() {
|
||||||
|
names := make(map[string]string)
|
||||||
|
users, err := s.rtm.Client.GetUsers()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, user := range users {
|
||||||
|
names[user.ID] = user.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
s.Lock()
|
||||||
|
s.names = names
|
||||||
|
s.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
setNames()
|
||||||
|
|
||||||
|
t := time.NewTicker(time.Minute)
|
||||||
|
defer t.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-s.exit:
|
||||||
|
return
|
||||||
|
case <-t.C:
|
||||||
|
setNames()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *slackConn) getName(id string) string {
|
||||||
|
s.Lock()
|
||||||
|
name := s.names[id]
|
||||||
|
s.Unlock()
|
||||||
|
return name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *slackConn) Close() error {
|
||||||
|
select {
|
||||||
|
case <-s.exit:
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
close(s.exit)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *slackConn) Recv(event *input.Event) error {
|
||||||
|
if event == nil {
|
||||||
|
return errors.New("event cannot be nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-s.exit:
|
||||||
|
return errors.New("connection closed")
|
||||||
|
case e := <-s.rtm.IncomingEvents:
|
||||||
|
switch ev := e.Data.(type) {
|
||||||
|
case *slack.MessageEvent:
|
||||||
|
// only accept type message
|
||||||
|
if ev.Type != "message" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// only accept DMs or messages to me
|
||||||
|
switch {
|
||||||
|
case strings.HasPrefix(ev.Channel, "D"):
|
||||||
|
case strings.HasPrefix(ev.Text, s.auth.User):
|
||||||
|
case strings.HasPrefix(ev.Text, fmt.Sprintf("<@%s>", s.auth.UserID)):
|
||||||
|
default:
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Strip username from text
|
||||||
|
switch {
|
||||||
|
case strings.HasPrefix(ev.Text, s.auth.User):
|
||||||
|
args := strings.Split(ev.Text, " ")[1:]
|
||||||
|
ev.Text = strings.Join(args, " ")
|
||||||
|
event.To = s.auth.User
|
||||||
|
case strings.HasPrefix(ev.Text, fmt.Sprintf("<@%s>", s.auth.UserID)):
|
||||||
|
args := strings.Split(ev.Text, " ")[1:]
|
||||||
|
ev.Text = strings.Join(args, " ")
|
||||||
|
event.To = s.auth.UserID
|
||||||
|
}
|
||||||
|
|
||||||
|
if event.Meta == nil {
|
||||||
|
event.Meta = make(map[string]interface{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// fill in the blanks
|
||||||
|
event.From = ev.Channel + ":" + ev.User
|
||||||
|
event.Type = input.TextEvent
|
||||||
|
event.Data = []byte(ev.Text)
|
||||||
|
event.Meta["reply"] = ev
|
||||||
|
return nil
|
||||||
|
case *slack.InvalidAuthEvent:
|
||||||
|
return errors.New("invalid credentials")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *slackConn) Send(event *input.Event) error {
|
||||||
|
var channel, message, name string
|
||||||
|
|
||||||
|
if len(event.To) == 0 {
|
||||||
|
return errors.New("require Event.To")
|
||||||
|
}
|
||||||
|
|
||||||
|
parts := strings.Split(event.To, ":")
|
||||||
|
|
||||||
|
if len(parts) == 2 {
|
||||||
|
channel = parts[0]
|
||||||
|
name = s.getName(parts[1])
|
||||||
|
// try using reply meta
|
||||||
|
} else if ev, ok := event.Meta["reply"]; ok {
|
||||||
|
channel = ev.(*slack.MessageEvent).Channel
|
||||||
|
name = s.getName(ev.(*slack.MessageEvent).User)
|
||||||
|
}
|
||||||
|
|
||||||
|
// don't know where to send the message
|
||||||
|
if len(channel) == 0 {
|
||||||
|
return errors.New("could not determine who message is to")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(name) == 0 || strings.HasPrefix(channel, "D") {
|
||||||
|
message = string(event.Data)
|
||||||
|
} else {
|
||||||
|
message = fmt.Sprintf("@%s: %s", name, string(event.Data))
|
||||||
|
}
|
||||||
|
|
||||||
|
s.rtm.SendMessage(s.rtm.NewOutgoingMessage(message, channel))
|
||||||
|
return nil
|
||||||
|
}
|
147
agent/input/slack/slack.go
Normal file
147
agent/input/slack/slack.go
Normal file
@@ -0,0 +1,147 @@
|
|||||||
|
package slack
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/micro/cli/v2"
|
||||||
|
"github.com/micro/go-micro/v2/agent/input"
|
||||||
|
"github.com/nlopes/slack"
|
||||||
|
)
|
||||||
|
|
||||||
|
type slackInput struct {
|
||||||
|
debug bool
|
||||||
|
token string
|
||||||
|
|
||||||
|
sync.Mutex
|
||||||
|
running bool
|
||||||
|
exit chan bool
|
||||||
|
|
||||||
|
api *slack.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
input.Inputs["slack"] = NewInput()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *slackInput) Flags() []cli.Flag {
|
||||||
|
return []cli.Flag{
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "slack_debug",
|
||||||
|
Usage: "Slack debug output",
|
||||||
|
EnvVars: []string{"MICRO_SLACK_DEBUG"},
|
||||||
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "slack_token",
|
||||||
|
Usage: "Slack token",
|
||||||
|
EnvVars: []string{"MICRO_SLACK_TOKEN"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *slackInput) Init(ctx *cli.Context) error {
|
||||||
|
debug := ctx.Bool("slack_debug")
|
||||||
|
token := ctx.String("slack_token")
|
||||||
|
|
||||||
|
if len(token) == 0 {
|
||||||
|
return errors.New("missing slack token")
|
||||||
|
}
|
||||||
|
|
||||||
|
p.debug = debug
|
||||||
|
p.token = token
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *slackInput) Stream() (input.Conn, error) {
|
||||||
|
p.Lock()
|
||||||
|
defer p.Unlock()
|
||||||
|
|
||||||
|
if !p.running {
|
||||||
|
return nil, errors.New("not running")
|
||||||
|
}
|
||||||
|
|
||||||
|
// test auth
|
||||||
|
auth, err := p.api.AuthTest()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
rtm := p.api.NewRTM()
|
||||||
|
exit := make(chan bool)
|
||||||
|
|
||||||
|
go rtm.ManageConnection()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
select {
|
||||||
|
case <-p.exit:
|
||||||
|
select {
|
||||||
|
case <-exit:
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
close(exit)
|
||||||
|
}
|
||||||
|
case <-exit:
|
||||||
|
}
|
||||||
|
|
||||||
|
rtm.Disconnect()
|
||||||
|
}()
|
||||||
|
|
||||||
|
conn := &slackConn{
|
||||||
|
auth: auth,
|
||||||
|
rtm: rtm,
|
||||||
|
exit: exit,
|
||||||
|
names: make(map[string]string),
|
||||||
|
}
|
||||||
|
|
||||||
|
go conn.run()
|
||||||
|
|
||||||
|
return conn, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *slackInput) Start() error {
|
||||||
|
if len(p.token) == 0 {
|
||||||
|
return errors.New("missing slack token")
|
||||||
|
}
|
||||||
|
|
||||||
|
p.Lock()
|
||||||
|
defer p.Unlock()
|
||||||
|
|
||||||
|
if p.running {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
api := slack.New(p.token, slack.OptionDebug(p.debug))
|
||||||
|
|
||||||
|
// test auth
|
||||||
|
_, err := api.AuthTest()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
p.api = api
|
||||||
|
p.exit = make(chan bool)
|
||||||
|
p.running = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *slackInput) Stop() error {
|
||||||
|
p.Lock()
|
||||||
|
defer p.Unlock()
|
||||||
|
|
||||||
|
if !p.running {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
close(p.exit)
|
||||||
|
p.running = false
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *slackInput) String() string {
|
||||||
|
return "slack"
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewInput() input.Input {
|
||||||
|
return &slackInput{}
|
||||||
|
}
|
18
agent/input/telegram/README.md
Normal file
18
agent/input/telegram/README.md
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
# Telegram Messenger input for micro bot
|
||||||
|
[Telegram](https://telegram.org) support for micro bot based on [telegram-bot-api](https://github.com/go-telegram-bot-api/telegram-bot-api).
|
||||||
|
|
||||||
|
## Options
|
||||||
|
### --telegram_token (required)
|
||||||
|
|
||||||
|
Sets bot's token for interacting with API.
|
||||||
|
|
||||||
|
Head over to Telegram's [API documentation](https://core.telegram.org/bots/api)
|
||||||
|
to learn how to create bots and how the API works.
|
||||||
|
|
||||||
|
### --telegram_debug
|
||||||
|
|
||||||
|
Sets the debug flag to make the bot's output verbose.
|
||||||
|
|
||||||
|
### --telegram_whitelist
|
||||||
|
|
||||||
|
Sets a list of comma-separated nicknames (without @ symbol in the beginning) for interacting with bot. Only these users can use the bot.
|
115
agent/input/telegram/conn.go
Normal file
115
agent/input/telegram/conn.go
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
package telegram
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/forestgiant/sliceutil"
|
||||||
|
"github.com/micro/go-micro/v2/agent/input"
|
||||||
|
"github.com/micro/go-micro/v2/logger"
|
||||||
|
tgbotapi "gopkg.in/telegram-bot-api.v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
type telegramConn struct {
|
||||||
|
input *telegramInput
|
||||||
|
|
||||||
|
recv <-chan tgbotapi.Update
|
||||||
|
exit chan bool
|
||||||
|
|
||||||
|
syncCond *sync.Cond
|
||||||
|
mutex sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func newConn(input *telegramInput) (*telegramConn, error) {
|
||||||
|
conn := &telegramConn{
|
||||||
|
input: input,
|
||||||
|
}
|
||||||
|
|
||||||
|
conn.syncCond = sync.NewCond(&conn.mutex)
|
||||||
|
|
||||||
|
go conn.run()
|
||||||
|
|
||||||
|
return conn, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tc *telegramConn) run() {
|
||||||
|
u := tgbotapi.NewUpdate(0)
|
||||||
|
u.Timeout = 60
|
||||||
|
updates, err := tc.input.api.GetUpdatesChan(u)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
tc.recv = updates
|
||||||
|
tc.syncCond.Signal()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-tc.exit:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tc *telegramConn) Close() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tc *telegramConn) Recv(event *input.Event) error {
|
||||||
|
if event == nil {
|
||||||
|
return errors.New("event cannot be nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
if tc.recv == nil {
|
||||||
|
tc.mutex.Lock()
|
||||||
|
tc.syncCond.Wait()
|
||||||
|
}
|
||||||
|
|
||||||
|
update := <-tc.recv
|
||||||
|
|
||||||
|
if update.Message == nil || (len(tc.input.whitelist) > 0 && !sliceutil.Contains(tc.input.whitelist, update.Message.From.UserName)) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if event.Meta == nil {
|
||||||
|
event.Meta = make(map[string]interface{})
|
||||||
|
}
|
||||||
|
|
||||||
|
event.Type = input.TextEvent
|
||||||
|
event.From = update.Message.From.UserName
|
||||||
|
event.To = tc.input.api.Self.UserName
|
||||||
|
event.Data = []byte(update.Message.Text)
|
||||||
|
event.Meta["chatId"] = update.Message.Chat.ID
|
||||||
|
event.Meta["chatType"] = update.Message.Chat.Type
|
||||||
|
event.Meta["messageId"] = update.Message.MessageID
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tc *telegramConn) Send(event *input.Event) error {
|
||||||
|
messageText := strings.TrimSpace(string(event.Data))
|
||||||
|
|
||||||
|
chatId := event.Meta["chatId"].(int64)
|
||||||
|
chatType := ChatType(event.Meta["chatType"].(string))
|
||||||
|
|
||||||
|
msgConfig := tgbotapi.NewMessage(chatId, messageText)
|
||||||
|
msgConfig.ParseMode = tgbotapi.ModeHTML
|
||||||
|
|
||||||
|
if sliceutil.Contains([]ChatType{Group, Supergroup}, chatType) {
|
||||||
|
msgConfig.ReplyToMessageID = event.Meta["messageId"].(int)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := tc.input.api.Send(msgConfig)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
// probably it could be because of nested HTML tags -- telegram doesn't allow nested tags
|
||||||
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
||||||
|
logger.Error("[telegram][Send] error:", err)
|
||||||
|
}
|
||||||
|
msgConfig.Text = "This bot couldn't send the response (Internal error)"
|
||||||
|
tc.input.api.Send(msgConfig)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
101
agent/input/telegram/telegram.go
Normal file
101
agent/input/telegram/telegram.go
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
package telegram
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/micro/cli/v2"
|
||||||
|
"github.com/micro/go-micro/v2/agent/input"
|
||||||
|
tgbotapi "gopkg.in/telegram-bot-api.v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
type telegramInput struct {
|
||||||
|
sync.Mutex
|
||||||
|
|
||||||
|
debug bool
|
||||||
|
token string
|
||||||
|
whitelist []string
|
||||||
|
|
||||||
|
api *tgbotapi.BotAPI
|
||||||
|
}
|
||||||
|
|
||||||
|
type ChatType string
|
||||||
|
|
||||||
|
const (
|
||||||
|
Private ChatType = "private"
|
||||||
|
Group ChatType = "group"
|
||||||
|
Supergroup ChatType = "supergroup"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
input.Inputs["telegram"] = &telegramInput{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ti *telegramInput) Flags() []cli.Flag {
|
||||||
|
return []cli.Flag{
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "telegram_debug",
|
||||||
|
EnvVars: []string{"MICRO_TELEGRAM_DEBUG"},
|
||||||
|
Usage: "Telegram debug output",
|
||||||
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "telegram_token",
|
||||||
|
EnvVars: []string{"MICRO_TELEGRAM_TOKEN"},
|
||||||
|
Usage: "Telegram token",
|
||||||
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "telegram_whitelist",
|
||||||
|
EnvVars: []string{"MICRO_TELEGRAM_WHITELIST"},
|
||||||
|
Usage: "Telegram bot's users (comma-separated values)",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ti *telegramInput) Init(ctx *cli.Context) error {
|
||||||
|
ti.debug = ctx.Bool("telegram_debug")
|
||||||
|
ti.token = ctx.String("telegram_token")
|
||||||
|
|
||||||
|
whitelist := ctx.String("telegram_whitelist")
|
||||||
|
|
||||||
|
if whitelist != "" {
|
||||||
|
ti.whitelist = strings.Split(whitelist, ",")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(ti.token) == 0 {
|
||||||
|
return errors.New("missing telegram token")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ti *telegramInput) Stream() (input.Conn, error) {
|
||||||
|
ti.Lock()
|
||||||
|
defer ti.Unlock()
|
||||||
|
|
||||||
|
return newConn(ti)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ti *telegramInput) Start() error {
|
||||||
|
ti.Lock()
|
||||||
|
defer ti.Unlock()
|
||||||
|
|
||||||
|
api, err := tgbotapi.NewBotAPI(ti.token)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
ti.api = api
|
||||||
|
|
||||||
|
api.Debug = ti.debug
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ti *telegramInput) Stop() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *telegramInput) String() string {
|
||||||
|
return "telegram"
|
||||||
|
}
|
333
agent/proto/bot.pb.go
Normal file
333
agent/proto/bot.pb.go
Normal file
@@ -0,0 +1,333 @@
|
|||||||
|
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||||
|
// source: agent/proto/bot.proto
|
||||||
|
|
||||||
|
package go_micro_bot
|
||||||
|
|
||||||
|
import (
|
||||||
|
context "context"
|
||||||
|
fmt "fmt"
|
||||||
|
proto "github.com/golang/protobuf/proto"
|
||||||
|
grpc "google.golang.org/grpc"
|
||||||
|
codes "google.golang.org/grpc/codes"
|
||||||
|
status "google.golang.org/grpc/status"
|
||||||
|
math "math"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Reference imports to suppress errors if they are not otherwise used.
|
||||||
|
var _ = proto.Marshal
|
||||||
|
var _ = fmt.Errorf
|
||||||
|
var _ = math.Inf
|
||||||
|
|
||||||
|
// This is a compile-time assertion to ensure that this generated file
|
||||||
|
// is compatible with the proto package it is being compiled against.
|
||||||
|
// A compilation error at this line likely means your copy of the
|
||||||
|
// proto package needs to be updated.
|
||||||
|
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||||
|
|
||||||
|
type HelpRequest struct {
|
||||||
|
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||||
|
XXX_unrecognized []byte `json:"-"`
|
||||||
|
XXX_sizecache int32 `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *HelpRequest) Reset() { *m = HelpRequest{} }
|
||||||
|
func (m *HelpRequest) String() string { return proto.CompactTextString(m) }
|
||||||
|
func (*HelpRequest) ProtoMessage() {}
|
||||||
|
func (*HelpRequest) Descriptor() ([]byte, []int) {
|
||||||
|
return fileDescriptor_79b974b8c77805fa, []int{0}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *HelpRequest) XXX_Unmarshal(b []byte) error {
|
||||||
|
return xxx_messageInfo_HelpRequest.Unmarshal(m, b)
|
||||||
|
}
|
||||||
|
func (m *HelpRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||||
|
return xxx_messageInfo_HelpRequest.Marshal(b, m, deterministic)
|
||||||
|
}
|
||||||
|
func (m *HelpRequest) XXX_Merge(src proto.Message) {
|
||||||
|
xxx_messageInfo_HelpRequest.Merge(m, src)
|
||||||
|
}
|
||||||
|
func (m *HelpRequest) XXX_Size() int {
|
||||||
|
return xxx_messageInfo_HelpRequest.Size(m)
|
||||||
|
}
|
||||||
|
func (m *HelpRequest) XXX_DiscardUnknown() {
|
||||||
|
xxx_messageInfo_HelpRequest.DiscardUnknown(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
var xxx_messageInfo_HelpRequest proto.InternalMessageInfo
|
||||||
|
|
||||||
|
type HelpResponse struct {
|
||||||
|
Usage string `protobuf:"bytes,1,opt,name=usage,proto3" json:"usage,omitempty"`
|
||||||
|
Description string `protobuf:"bytes,2,opt,name=description,proto3" json:"description,omitempty"`
|
||||||
|
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||||
|
XXX_unrecognized []byte `json:"-"`
|
||||||
|
XXX_sizecache int32 `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *HelpResponse) Reset() { *m = HelpResponse{} }
|
||||||
|
func (m *HelpResponse) String() string { return proto.CompactTextString(m) }
|
||||||
|
func (*HelpResponse) ProtoMessage() {}
|
||||||
|
func (*HelpResponse) Descriptor() ([]byte, []int) {
|
||||||
|
return fileDescriptor_79b974b8c77805fa, []int{1}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *HelpResponse) XXX_Unmarshal(b []byte) error {
|
||||||
|
return xxx_messageInfo_HelpResponse.Unmarshal(m, b)
|
||||||
|
}
|
||||||
|
func (m *HelpResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||||
|
return xxx_messageInfo_HelpResponse.Marshal(b, m, deterministic)
|
||||||
|
}
|
||||||
|
func (m *HelpResponse) XXX_Merge(src proto.Message) {
|
||||||
|
xxx_messageInfo_HelpResponse.Merge(m, src)
|
||||||
|
}
|
||||||
|
func (m *HelpResponse) XXX_Size() int {
|
||||||
|
return xxx_messageInfo_HelpResponse.Size(m)
|
||||||
|
}
|
||||||
|
func (m *HelpResponse) XXX_DiscardUnknown() {
|
||||||
|
xxx_messageInfo_HelpResponse.DiscardUnknown(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
var xxx_messageInfo_HelpResponse proto.InternalMessageInfo
|
||||||
|
|
||||||
|
func (m *HelpResponse) GetUsage() string {
|
||||||
|
if m != nil {
|
||||||
|
return m.Usage
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *HelpResponse) GetDescription() string {
|
||||||
|
if m != nil {
|
||||||
|
return m.Description
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
type ExecRequest struct {
|
||||||
|
Args []string `protobuf:"bytes,1,rep,name=args,proto3" json:"args,omitempty"`
|
||||||
|
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||||
|
XXX_unrecognized []byte `json:"-"`
|
||||||
|
XXX_sizecache int32 `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *ExecRequest) Reset() { *m = ExecRequest{} }
|
||||||
|
func (m *ExecRequest) String() string { return proto.CompactTextString(m) }
|
||||||
|
func (*ExecRequest) ProtoMessage() {}
|
||||||
|
func (*ExecRequest) Descriptor() ([]byte, []int) {
|
||||||
|
return fileDescriptor_79b974b8c77805fa, []int{2}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *ExecRequest) XXX_Unmarshal(b []byte) error {
|
||||||
|
return xxx_messageInfo_ExecRequest.Unmarshal(m, b)
|
||||||
|
}
|
||||||
|
func (m *ExecRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||||
|
return xxx_messageInfo_ExecRequest.Marshal(b, m, deterministic)
|
||||||
|
}
|
||||||
|
func (m *ExecRequest) XXX_Merge(src proto.Message) {
|
||||||
|
xxx_messageInfo_ExecRequest.Merge(m, src)
|
||||||
|
}
|
||||||
|
func (m *ExecRequest) XXX_Size() int {
|
||||||
|
return xxx_messageInfo_ExecRequest.Size(m)
|
||||||
|
}
|
||||||
|
func (m *ExecRequest) XXX_DiscardUnknown() {
|
||||||
|
xxx_messageInfo_ExecRequest.DiscardUnknown(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
var xxx_messageInfo_ExecRequest proto.InternalMessageInfo
|
||||||
|
|
||||||
|
func (m *ExecRequest) GetArgs() []string {
|
||||||
|
if m != nil {
|
||||||
|
return m.Args
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type ExecResponse struct {
|
||||||
|
Result []byte `protobuf:"bytes,1,opt,name=result,proto3" json:"result,omitempty"`
|
||||||
|
Error string `protobuf:"bytes,2,opt,name=error,proto3" json:"error,omitempty"`
|
||||||
|
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||||
|
XXX_unrecognized []byte `json:"-"`
|
||||||
|
XXX_sizecache int32 `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *ExecResponse) Reset() { *m = ExecResponse{} }
|
||||||
|
func (m *ExecResponse) String() string { return proto.CompactTextString(m) }
|
||||||
|
func (*ExecResponse) ProtoMessage() {}
|
||||||
|
func (*ExecResponse) Descriptor() ([]byte, []int) {
|
||||||
|
return fileDescriptor_79b974b8c77805fa, []int{3}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *ExecResponse) XXX_Unmarshal(b []byte) error {
|
||||||
|
return xxx_messageInfo_ExecResponse.Unmarshal(m, b)
|
||||||
|
}
|
||||||
|
func (m *ExecResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||||
|
return xxx_messageInfo_ExecResponse.Marshal(b, m, deterministic)
|
||||||
|
}
|
||||||
|
func (m *ExecResponse) XXX_Merge(src proto.Message) {
|
||||||
|
xxx_messageInfo_ExecResponse.Merge(m, src)
|
||||||
|
}
|
||||||
|
func (m *ExecResponse) XXX_Size() int {
|
||||||
|
return xxx_messageInfo_ExecResponse.Size(m)
|
||||||
|
}
|
||||||
|
func (m *ExecResponse) XXX_DiscardUnknown() {
|
||||||
|
xxx_messageInfo_ExecResponse.DiscardUnknown(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
var xxx_messageInfo_ExecResponse proto.InternalMessageInfo
|
||||||
|
|
||||||
|
func (m *ExecResponse) GetResult() []byte {
|
||||||
|
if m != nil {
|
||||||
|
return m.Result
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *ExecResponse) GetError() string {
|
||||||
|
if m != nil {
|
||||||
|
return m.Error
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
proto.RegisterType((*HelpRequest)(nil), "go.micro.bot.HelpRequest")
|
||||||
|
proto.RegisterType((*HelpResponse)(nil), "go.micro.bot.HelpResponse")
|
||||||
|
proto.RegisterType((*ExecRequest)(nil), "go.micro.bot.ExecRequest")
|
||||||
|
proto.RegisterType((*ExecResponse)(nil), "go.micro.bot.ExecResponse")
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() { proto.RegisterFile("agent/proto/bot.proto", fileDescriptor_79b974b8c77805fa) }
|
||||||
|
|
||||||
|
var fileDescriptor_79b974b8c77805fa = []byte{
|
||||||
|
// 234 bytes of a gzipped FileDescriptorProto
|
||||||
|
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x90, 0x3f, 0x4f, 0xc3, 0x30,
|
||||||
|
0x10, 0xc5, 0x1b, 0x28, 0x45, 0xbd, 0x84, 0xc5, 0x02, 0x14, 0x3a, 0x05, 0x4f, 0x9d, 0x5c, 0x09,
|
||||||
|
0x56, 0x24, 0x06, 0x04, 0x62, 0xce, 0x37, 0x48, 0xd2, 0x53, 0x14, 0xa9, 0xf1, 0x99, 0xb3, 0x23,
|
||||||
|
0xf1, 0x1d, 0xf8, 0xd2, 0xc8, 0x7f, 0x06, 0xab, 0xea, 0x76, 0xcf, 0x67, 0xbd, 0xf7, 0x7b, 0x07,
|
||||||
|
0x0f, 0xdd, 0x88, 0xda, 0x1d, 0x0c, 0x93, 0xa3, 0x43, 0x4f, 0x4e, 0x85, 0x49, 0x54, 0x23, 0xa9,
|
||||||
|
0x79, 0x1a, 0x98, 0x54, 0x4f, 0x4e, 0xde, 0x41, 0xf9, 0x8d, 0x27, 0xd3, 0xe2, 0xcf, 0x82, 0xd6,
|
||||||
|
0xc9, 0x2f, 0xa8, 0xa2, 0xb4, 0x86, 0xb4, 0x45, 0x71, 0x0f, 0x37, 0x8b, 0xed, 0x46, 0xac, 0x8b,
|
||||||
|
0xa6, 0xd8, 0x6f, 0xdb, 0x28, 0x44, 0x03, 0xe5, 0x11, 0xed, 0xc0, 0x93, 0x71, 0x13, 0xe9, 0xfa,
|
||||||
|
0x2a, 0xec, 0xf2, 0x27, 0xf9, 0x0c, 0xe5, 0xe7, 0x2f, 0x0e, 0xc9, 0x56, 0x08, 0x58, 0x77, 0x3c,
|
||||||
|
0xda, 0xba, 0x68, 0xae, 0xf7, 0xdb, 0x36, 0xcc, 0xf2, 0x0d, 0xaa, 0xf8, 0x25, 0x45, 0x3d, 0xc2,
|
||||||
|
0x86, 0xd1, 0x2e, 0x27, 0x17, 0xb2, 0xaa, 0x36, 0x29, 0x8f, 0x80, 0xcc, 0xc4, 0x29, 0x26, 0x8a,
|
||||||
|
0x97, 0xbf, 0x02, 0x6e, 0x3f, 0x68, 0x9e, 0x3b, 0x7d, 0x14, 0xef, 0xb0, 0xf6, 0xd0, 0xe2, 0x49,
|
||||||
|
0xe5, 0xd5, 0x54, 0xd6, 0x6b, 0xb7, 0xbb, 0xb4, 0x8a, 0xc1, 0x72, 0xe5, 0x0d, 0x3c, 0xca, 0xb9,
|
||||||
|
0x41, 0xd6, 0xe0, 0xdc, 0x20, 0x27, 0x97, 0xab, 0x7e, 0x13, 0x4e, 0xfb, 0xfa, 0x1f, 0x00, 0x00,
|
||||||
|
0xff, 0xff, 0xe8, 0x08, 0x5e, 0xad, 0x73, 0x01, 0x00, 0x00,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reference imports to suppress errors if they are not otherwise used.
|
||||||
|
var _ context.Context
|
||||||
|
var _ grpc.ClientConn
|
||||||
|
|
||||||
|
// This is a compile-time assertion to ensure that this generated file
|
||||||
|
// is compatible with the grpc package it is being compiled against.
|
||||||
|
const _ = grpc.SupportPackageIsVersion4
|
||||||
|
|
||||||
|
// CommandClient is the client API for Command service.
|
||||||
|
//
|
||||||
|
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
|
||||||
|
type CommandClient interface {
|
||||||
|
Help(ctx context.Context, in *HelpRequest, opts ...grpc.CallOption) (*HelpResponse, error)
|
||||||
|
Exec(ctx context.Context, in *ExecRequest, opts ...grpc.CallOption) (*ExecResponse, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type commandClient struct {
|
||||||
|
cc *grpc.ClientConn
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCommandClient(cc *grpc.ClientConn) CommandClient {
|
||||||
|
return &commandClient{cc}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *commandClient) Help(ctx context.Context, in *HelpRequest, opts ...grpc.CallOption) (*HelpResponse, error) {
|
||||||
|
out := new(HelpResponse)
|
||||||
|
err := c.cc.Invoke(ctx, "/go.micro.bot.Command/Help", in, out, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *commandClient) Exec(ctx context.Context, in *ExecRequest, opts ...grpc.CallOption) (*ExecResponse, error) {
|
||||||
|
out := new(ExecResponse)
|
||||||
|
err := c.cc.Invoke(ctx, "/go.micro.bot.Command/Exec", in, out, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CommandServer is the server API for Command service.
|
||||||
|
type CommandServer interface {
|
||||||
|
Help(context.Context, *HelpRequest) (*HelpResponse, error)
|
||||||
|
Exec(context.Context, *ExecRequest) (*ExecResponse, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnimplementedCommandServer can be embedded to have forward compatible implementations.
|
||||||
|
type UnimplementedCommandServer struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*UnimplementedCommandServer) Help(ctx context.Context, req *HelpRequest) (*HelpResponse, error) {
|
||||||
|
return nil, status.Errorf(codes.Unimplemented, "method Help not implemented")
|
||||||
|
}
|
||||||
|
func (*UnimplementedCommandServer) Exec(ctx context.Context, req *ExecRequest) (*ExecResponse, error) {
|
||||||
|
return nil, status.Errorf(codes.Unimplemented, "method Exec not implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterCommandServer(s *grpc.Server, srv CommandServer) {
|
||||||
|
s.RegisterService(&_Command_serviceDesc, srv)
|
||||||
|
}
|
||||||
|
|
||||||
|
func _Command_Help_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||||
|
in := new(HelpRequest)
|
||||||
|
if err := dec(in); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if interceptor == nil {
|
||||||
|
return srv.(CommandServer).Help(ctx, in)
|
||||||
|
}
|
||||||
|
info := &grpc.UnaryServerInfo{
|
||||||
|
Server: srv,
|
||||||
|
FullMethod: "/go.micro.bot.Command/Help",
|
||||||
|
}
|
||||||
|
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||||
|
return srv.(CommandServer).Help(ctx, req.(*HelpRequest))
|
||||||
|
}
|
||||||
|
return interceptor(ctx, in, info, handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
func _Command_Exec_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||||
|
in := new(ExecRequest)
|
||||||
|
if err := dec(in); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if interceptor == nil {
|
||||||
|
return srv.(CommandServer).Exec(ctx, in)
|
||||||
|
}
|
||||||
|
info := &grpc.UnaryServerInfo{
|
||||||
|
Server: srv,
|
||||||
|
FullMethod: "/go.micro.bot.Command/Exec",
|
||||||
|
}
|
||||||
|
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||||
|
return srv.(CommandServer).Exec(ctx, req.(*ExecRequest))
|
||||||
|
}
|
||||||
|
return interceptor(ctx, in, info, handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
var _Command_serviceDesc = grpc.ServiceDesc{
|
||||||
|
ServiceName: "go.micro.bot.Command",
|
||||||
|
HandlerType: (*CommandServer)(nil),
|
||||||
|
Methods: []grpc.MethodDesc{
|
||||||
|
{
|
||||||
|
MethodName: "Help",
|
||||||
|
Handler: _Command_Help_Handler,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
MethodName: "Exec",
|
||||||
|
Handler: _Command_Exec_Handler,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Streams: []grpc.StreamDesc{},
|
||||||
|
Metadata: "agent/proto/bot.proto",
|
||||||
|
}
|
110
agent/proto/bot.pb.micro.go
Normal file
110
agent/proto/bot.pb.micro.go
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
// Code generated by protoc-gen-micro. DO NOT EDIT.
|
||||||
|
// source: agent/proto/bot.proto
|
||||||
|
|
||||||
|
package go_micro_bot
|
||||||
|
|
||||||
|
import (
|
||||||
|
fmt "fmt"
|
||||||
|
proto "github.com/golang/protobuf/proto"
|
||||||
|
math "math"
|
||||||
|
)
|
||||||
|
|
||||||
|
import (
|
||||||
|
context "context"
|
||||||
|
api "github.com/micro/go-micro/v2/api"
|
||||||
|
client "github.com/micro/go-micro/v2/client"
|
||||||
|
server "github.com/micro/go-micro/v2/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Reference imports to suppress errors if they are not otherwise used.
|
||||||
|
var _ = proto.Marshal
|
||||||
|
var _ = fmt.Errorf
|
||||||
|
var _ = math.Inf
|
||||||
|
|
||||||
|
// This is a compile-time assertion to ensure that this generated file
|
||||||
|
// is compatible with the proto package it is being compiled against.
|
||||||
|
// A compilation error at this line likely means your copy of the
|
||||||
|
// proto package needs to be updated.
|
||||||
|
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||||
|
|
||||||
|
// Reference imports to suppress errors if they are not otherwise used.
|
||||||
|
var _ api.Endpoint
|
||||||
|
var _ context.Context
|
||||||
|
var _ client.Option
|
||||||
|
var _ server.Option
|
||||||
|
|
||||||
|
// Api Endpoints for Command service
|
||||||
|
|
||||||
|
func NewCommandEndpoints() []*api.Endpoint {
|
||||||
|
return []*api.Endpoint{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Client API for Command service
|
||||||
|
|
||||||
|
type CommandService interface {
|
||||||
|
Help(ctx context.Context, in *HelpRequest, opts ...client.CallOption) (*HelpResponse, error)
|
||||||
|
Exec(ctx context.Context, in *ExecRequest, opts ...client.CallOption) (*ExecResponse, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type commandService struct {
|
||||||
|
c client.Client
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCommandService(name string, c client.Client) CommandService {
|
||||||
|
return &commandService{
|
||||||
|
c: c,
|
||||||
|
name: name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *commandService) Help(ctx context.Context, in *HelpRequest, opts ...client.CallOption) (*HelpResponse, error) {
|
||||||
|
req := c.c.NewRequest(c.name, "Command.Help", in)
|
||||||
|
out := new(HelpResponse)
|
||||||
|
err := c.c.Call(ctx, req, out, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *commandService) Exec(ctx context.Context, in *ExecRequest, opts ...client.CallOption) (*ExecResponse, error) {
|
||||||
|
req := c.c.NewRequest(c.name, "Command.Exec", in)
|
||||||
|
out := new(ExecResponse)
|
||||||
|
err := c.c.Call(ctx, req, out, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Server API for Command service
|
||||||
|
|
||||||
|
type CommandHandler interface {
|
||||||
|
Help(context.Context, *HelpRequest, *HelpResponse) error
|
||||||
|
Exec(context.Context, *ExecRequest, *ExecResponse) error
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterCommandHandler(s server.Server, hdlr CommandHandler, opts ...server.HandlerOption) error {
|
||||||
|
type command interface {
|
||||||
|
Help(ctx context.Context, in *HelpRequest, out *HelpResponse) error
|
||||||
|
Exec(ctx context.Context, in *ExecRequest, out *ExecResponse) error
|
||||||
|
}
|
||||||
|
type Command struct {
|
||||||
|
command
|
||||||
|
}
|
||||||
|
h := &commandHandler{hdlr}
|
||||||
|
return s.Handle(s.NewHandler(&Command{h}, opts...))
|
||||||
|
}
|
||||||
|
|
||||||
|
type commandHandler struct {
|
||||||
|
CommandHandler
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *commandHandler) Help(ctx context.Context, in *HelpRequest, out *HelpResponse) error {
|
||||||
|
return h.CommandHandler.Help(ctx, in, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *commandHandler) Exec(ctx context.Context, in *ExecRequest, out *ExecResponse) error {
|
||||||
|
return h.CommandHandler.Exec(ctx, in, out)
|
||||||
|
}
|
25
agent/proto/bot.proto
Normal file
25
agent/proto/bot.proto
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
package go.micro.bot;
|
||||||
|
|
||||||
|
service Command {
|
||||||
|
rpc Help(HelpRequest) returns (HelpResponse) {};
|
||||||
|
rpc Exec(ExecRequest) returns (ExecResponse) {};
|
||||||
|
}
|
||||||
|
|
||||||
|
message HelpRequest {
|
||||||
|
}
|
||||||
|
|
||||||
|
message HelpResponse {
|
||||||
|
string usage = 1;
|
||||||
|
string description = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
message ExecRequest {
|
||||||
|
repeated string args = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message ExecResponse {
|
||||||
|
bytes result = 1;
|
||||||
|
string error = 2;
|
||||||
|
}
|
187
api/api.go
Normal file
187
api/api.go
Normal file
@@ -0,0 +1,187 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/micro/go-micro/v2/registry"
|
||||||
|
"github.com/micro/go-micro/v2/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Api interface {
|
||||||
|
// Initialise options
|
||||||
|
Init(...Option) error
|
||||||
|
// Get the options
|
||||||
|
Options() Options
|
||||||
|
// Register a http handler
|
||||||
|
Register(*Endpoint) error
|
||||||
|
// Register a route
|
||||||
|
Deregister(*Endpoint) error
|
||||||
|
// Implemenation of api
|
||||||
|
String() string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Options struct{}
|
||||||
|
|
||||||
|
type Option func(*Options) error
|
||||||
|
|
||||||
|
// Endpoint is a mapping between an RPC method and HTTP endpoint
|
||||||
|
type Endpoint struct {
|
||||||
|
// RPC Method e.g. Greeter.Hello
|
||||||
|
Name string
|
||||||
|
// Description e.g what's this endpoint for
|
||||||
|
Description string
|
||||||
|
// API Handler e.g rpc, proxy
|
||||||
|
Handler string
|
||||||
|
// HTTP Host e.g example.com
|
||||||
|
Host []string
|
||||||
|
// HTTP Methods e.g GET, POST
|
||||||
|
Method []string
|
||||||
|
// HTTP Path e.g /greeter. Expect POSIX regex
|
||||||
|
Path []string
|
||||||
|
// Body destination
|
||||||
|
// "*" or "" - top level message value
|
||||||
|
// "string" - inner message value
|
||||||
|
Body string
|
||||||
|
// Stream flag
|
||||||
|
Stream bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// Service represents an API service
|
||||||
|
type Service struct {
|
||||||
|
// Name of service
|
||||||
|
Name string
|
||||||
|
// The endpoint for this service
|
||||||
|
Endpoint *Endpoint
|
||||||
|
// Versions of this service
|
||||||
|
Services []*registry.Service
|
||||||
|
}
|
||||||
|
|
||||||
|
func strip(s string) string {
|
||||||
|
return strings.TrimSpace(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func slice(s string) []string {
|
||||||
|
var sl []string
|
||||||
|
|
||||||
|
for _, p := range strings.Split(s, ",") {
|
||||||
|
if str := strip(p); len(str) > 0 {
|
||||||
|
sl = append(sl, strip(p))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sl
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encode encodes an endpoint to endpoint metadata
|
||||||
|
func Encode(e *Endpoint) map[string]string {
|
||||||
|
if e == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// endpoint map
|
||||||
|
ep := make(map[string]string)
|
||||||
|
|
||||||
|
// set vals only if they exist
|
||||||
|
set := func(k, v string) {
|
||||||
|
if len(v) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ep[k] = v
|
||||||
|
}
|
||||||
|
|
||||||
|
set("endpoint", e.Name)
|
||||||
|
set("description", e.Description)
|
||||||
|
set("handler", e.Handler)
|
||||||
|
set("method", strings.Join(e.Method, ","))
|
||||||
|
set("path", strings.Join(e.Path, ","))
|
||||||
|
set("host", strings.Join(e.Host, ","))
|
||||||
|
|
||||||
|
return ep
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decode decodes endpoint metadata into an endpoint
|
||||||
|
func Decode(e map[string]string) *Endpoint {
|
||||||
|
if e == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Endpoint{
|
||||||
|
Name: e["endpoint"],
|
||||||
|
Description: e["description"],
|
||||||
|
Method: slice(e["method"]),
|
||||||
|
Path: slice(e["path"]),
|
||||||
|
Host: slice(e["host"]),
|
||||||
|
Handler: e["handler"],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate validates an endpoint to guarantee it won't blow up when being served
|
||||||
|
func Validate(e *Endpoint) error {
|
||||||
|
if e == nil {
|
||||||
|
return errors.New("endpoint is nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(e.Name) == 0 {
|
||||||
|
return errors.New("name required")
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, p := range e.Path {
|
||||||
|
ps := p[0]
|
||||||
|
pe := p[len(p)-1]
|
||||||
|
|
||||||
|
if ps == '^' && pe == '$' {
|
||||||
|
_, err := regexp.CompilePOSIX(p)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else if ps == '^' && pe != '$' {
|
||||||
|
return errors.New("invalid path")
|
||||||
|
} else if ps != '^' && pe == '$' {
|
||||||
|
return errors.New("invalid path")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(e.Handler) == 0 {
|
||||||
|
return errors.New("invalid handler")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Design ideas
|
||||||
|
|
||||||
|
// Gateway is an api gateway interface
|
||||||
|
type Gateway interface {
|
||||||
|
// Register a http handler
|
||||||
|
Handle(pattern string, http.Handler)
|
||||||
|
// Register a route
|
||||||
|
RegisterRoute(r Route)
|
||||||
|
// Init initialises the command line.
|
||||||
|
// It also parses further options.
|
||||||
|
Init(...Option) error
|
||||||
|
// Run the gateway
|
||||||
|
Run() error
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGateway returns a new api gateway
|
||||||
|
func NewGateway() Gateway {
|
||||||
|
return newGateway()
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
// WithEndpoint returns a server.HandlerOption with endpoint metadata set
|
||||||
|
//
|
||||||
|
// Usage:
|
||||||
|
//
|
||||||
|
// proto.RegisterHandler(service.Server(), new(Handler), api.WithEndpoint(
|
||||||
|
// &api.Endpoint{
|
||||||
|
// Name: "Greeter.Hello",
|
||||||
|
// Path: []string{"/greeter"},
|
||||||
|
// },
|
||||||
|
// ))
|
||||||
|
func WithEndpoint(e *Endpoint) server.HandlerOption {
|
||||||
|
return server.EndpointMetadata(e.Name, Encode(e))
|
||||||
|
}
|
152
api/api_test.go
Normal file
152
api/api_test.go
Normal file
@@ -0,0 +1,152 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestEncoding(t *testing.T) {
|
||||||
|
testData := []*Endpoint{
|
||||||
|
nil,
|
||||||
|
{
|
||||||
|
Name: "Foo.Bar",
|
||||||
|
Description: "A test endpoint",
|
||||||
|
Handler: "meta",
|
||||||
|
Host: []string{"foo.com"},
|
||||||
|
Method: []string{"GET"},
|
||||||
|
Path: []string{"/test"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
compare := func(expect, got []string) bool {
|
||||||
|
// no data to compare, return true
|
||||||
|
if len(expect) == 0 && len(got) == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
// no data expected but got some return false
|
||||||
|
if len(expect) == 0 && len(got) > 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// compare expected with what we got
|
||||||
|
for _, e := range expect {
|
||||||
|
var seen bool
|
||||||
|
for _, g := range got {
|
||||||
|
if e == g {
|
||||||
|
seen = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !seen {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// we're done, return true
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, d := range testData {
|
||||||
|
// encode
|
||||||
|
e := Encode(d)
|
||||||
|
// decode
|
||||||
|
de := Decode(e)
|
||||||
|
|
||||||
|
// nil endpoint returns nil
|
||||||
|
if d == nil {
|
||||||
|
if e != nil {
|
||||||
|
t.Fatalf("expected nil got %v", e)
|
||||||
|
}
|
||||||
|
if de != nil {
|
||||||
|
t.Fatalf("expected nil got %v", de)
|
||||||
|
}
|
||||||
|
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// check encoded map
|
||||||
|
name := e["endpoint"]
|
||||||
|
desc := e["description"]
|
||||||
|
method := strings.Split(e["method"], ",")
|
||||||
|
path := strings.Split(e["path"], ",")
|
||||||
|
host := strings.Split(e["host"], ",")
|
||||||
|
handler := e["handler"]
|
||||||
|
|
||||||
|
if name != d.Name {
|
||||||
|
t.Fatalf("expected %v got %v", d.Name, name)
|
||||||
|
}
|
||||||
|
if desc != d.Description {
|
||||||
|
t.Fatalf("expected %v got %v", d.Description, desc)
|
||||||
|
}
|
||||||
|
if handler != d.Handler {
|
||||||
|
t.Fatalf("expected %v got %v", d.Handler, handler)
|
||||||
|
}
|
||||||
|
if ok := compare(d.Method, method); !ok {
|
||||||
|
t.Fatalf("expected %v got %v", d.Method, method)
|
||||||
|
}
|
||||||
|
if ok := compare(d.Path, path); !ok {
|
||||||
|
t.Fatalf("expected %v got %v", d.Path, path)
|
||||||
|
}
|
||||||
|
if ok := compare(d.Host, host); !ok {
|
||||||
|
t.Fatalf("expected %v got %v", d.Host, host)
|
||||||
|
}
|
||||||
|
|
||||||
|
if de.Name != d.Name {
|
||||||
|
t.Fatalf("expected %v got %v", d.Name, de.Name)
|
||||||
|
}
|
||||||
|
if de.Description != d.Description {
|
||||||
|
t.Fatalf("expected %v got %v", d.Description, de.Description)
|
||||||
|
}
|
||||||
|
if de.Handler != d.Handler {
|
||||||
|
t.Fatalf("expected %v got %v", d.Handler, de.Handler)
|
||||||
|
}
|
||||||
|
if ok := compare(d.Method, de.Method); !ok {
|
||||||
|
t.Fatalf("expected %v got %v", d.Method, de.Method)
|
||||||
|
}
|
||||||
|
if ok := compare(d.Path, de.Path); !ok {
|
||||||
|
t.Fatalf("expected %v got %v", d.Path, de.Path)
|
||||||
|
}
|
||||||
|
if ok := compare(d.Host, de.Host); !ok {
|
||||||
|
t.Fatalf("expected %v got %v", d.Host, de.Host)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValidate(t *testing.T) {
|
||||||
|
epPcre := &Endpoint{
|
||||||
|
Name: "Foo.Bar",
|
||||||
|
Description: "A test endpoint",
|
||||||
|
Handler: "meta",
|
||||||
|
Host: []string{"foo.com"},
|
||||||
|
Method: []string{"GET"},
|
||||||
|
Path: []string{"^/test/?$"},
|
||||||
|
}
|
||||||
|
if err := Validate(epPcre); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
epGpath := &Endpoint{
|
||||||
|
Name: "Foo.Bar",
|
||||||
|
Description: "A test endpoint",
|
||||||
|
Handler: "meta",
|
||||||
|
Host: []string{"foo.com"},
|
||||||
|
Method: []string{"GET"},
|
||||||
|
Path: []string{"/test/{id}"},
|
||||||
|
}
|
||||||
|
if err := Validate(epGpath); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
epPcreInvalid := &Endpoint{
|
||||||
|
Name: "Foo.Bar",
|
||||||
|
Description: "A test endpoint",
|
||||||
|
Handler: "meta",
|
||||||
|
Host: []string{"foo.com"},
|
||||||
|
Method: []string{"GET"},
|
||||||
|
Path: []string{"/test/?$"},
|
||||||
|
}
|
||||||
|
if err := Validate(epPcreInvalid); err == nil {
|
||||||
|
t.Fatalf("invalid pcre %v", epPcreInvalid.Path[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
123
api/handler/api/api.go
Normal file
123
api/handler/api/api.go
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
// Package api provides an http-rpc handler which provides the entire http request over rpc
|
||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
goapi "github.com/micro/go-micro/v2/api"
|
||||||
|
"github.com/micro/go-micro/v2/api/handler"
|
||||||
|
api "github.com/micro/go-micro/v2/api/proto"
|
||||||
|
"github.com/micro/go-micro/v2/client"
|
||||||
|
"github.com/micro/go-micro/v2/client/selector"
|
||||||
|
"github.com/micro/go-micro/v2/errors"
|
||||||
|
"github.com/micro/go-micro/v2/util/ctx"
|
||||||
|
)
|
||||||
|
|
||||||
|
type apiHandler struct {
|
||||||
|
opts handler.Options
|
||||||
|
s *goapi.Service
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
Handler = "api"
|
||||||
|
)
|
||||||
|
|
||||||
|
// API handler is the default handler which takes api.Request and returns api.Response
|
||||||
|
func (a *apiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
bsize := handler.DefaultMaxRecvSize
|
||||||
|
if a.opts.MaxRecvSize > 0 {
|
||||||
|
bsize = a.opts.MaxRecvSize
|
||||||
|
}
|
||||||
|
|
||||||
|
r.Body = http.MaxBytesReader(w, r.Body, bsize)
|
||||||
|
request, err := requestToProto(r)
|
||||||
|
if err != nil {
|
||||||
|
er := errors.InternalServerError("go.micro.api", err.Error())
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(500)
|
||||||
|
w.Write([]byte(er.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var service *goapi.Service
|
||||||
|
|
||||||
|
if a.s != nil {
|
||||||
|
// we were given the service
|
||||||
|
service = a.s
|
||||||
|
} else if a.opts.Router != nil {
|
||||||
|
// try get service from router
|
||||||
|
s, err := a.opts.Router.Route(r)
|
||||||
|
if err != nil {
|
||||||
|
er := errors.InternalServerError("go.micro.api", err.Error())
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(500)
|
||||||
|
w.Write([]byte(er.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
service = s
|
||||||
|
} else {
|
||||||
|
// we have no way of routing the request
|
||||||
|
er := errors.InternalServerError("go.micro.api", "no route found")
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(500)
|
||||||
|
w.Write([]byte(er.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// create request and response
|
||||||
|
c := a.opts.Client
|
||||||
|
req := c.NewRequest(service.Name, service.Endpoint.Name, request)
|
||||||
|
rsp := &api.Response{}
|
||||||
|
|
||||||
|
// create the context from headers
|
||||||
|
cx := ctx.FromRequest(r)
|
||||||
|
// create strategy
|
||||||
|
so := selector.WithStrategy(strategy(service.Services))
|
||||||
|
|
||||||
|
if err := c.Call(cx, req, rsp, client.WithSelectOption(so)); err != nil {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
ce := errors.Parse(err.Error())
|
||||||
|
switch ce.Code {
|
||||||
|
case 0:
|
||||||
|
w.WriteHeader(500)
|
||||||
|
default:
|
||||||
|
w.WriteHeader(int(ce.Code))
|
||||||
|
}
|
||||||
|
w.Write([]byte(ce.Error()))
|
||||||
|
return
|
||||||
|
} else if rsp.StatusCode == 0 {
|
||||||
|
rsp.StatusCode = http.StatusOK
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, header := range rsp.GetHeader() {
|
||||||
|
for _, val := range header.Values {
|
||||||
|
w.Header().Add(header.Key, val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(w.Header().Get("Content-Type")) == 0 {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(int(rsp.StatusCode))
|
||||||
|
w.Write([]byte(rsp.Body))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *apiHandler) String() string {
|
||||||
|
return "api"
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHandler(opts ...handler.Option) handler.Handler {
|
||||||
|
options := handler.NewOptions(opts...)
|
||||||
|
return &apiHandler{
|
||||||
|
opts: options,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithService(s *goapi.Service, opts ...handler.Option) handler.Handler {
|
||||||
|
options := handler.NewOptions(opts...)
|
||||||
|
return &apiHandler{
|
||||||
|
opts: options,
|
||||||
|
s: s,
|
||||||
|
}
|
||||||
|
}
|
119
api/handler/api/util.go
Normal file
119
api/handler/api/util.go
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"mime"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
api "github.com/micro/go-micro/v2/api/proto"
|
||||||
|
"github.com/micro/go-micro/v2/client/selector"
|
||||||
|
"github.com/micro/go-micro/v2/registry"
|
||||||
|
"github.com/oxtoacart/bpool"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// need to calculate later to specify useful defaults
|
||||||
|
bufferPool = bpool.NewSizedBufferPool(1024, 8)
|
||||||
|
)
|
||||||
|
|
||||||
|
func requestToProto(r *http.Request) (*api.Request, error) {
|
||||||
|
if err := r.ParseForm(); err != nil {
|
||||||
|
return nil, fmt.Errorf("Error parsing form: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req := &api.Request{
|
||||||
|
Path: r.URL.Path,
|
||||||
|
Method: r.Method,
|
||||||
|
Header: make(map[string]*api.Pair),
|
||||||
|
Get: make(map[string]*api.Pair),
|
||||||
|
Post: make(map[string]*api.Pair),
|
||||||
|
Url: r.URL.String(),
|
||||||
|
}
|
||||||
|
|
||||||
|
ct, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
||||||
|
if err != nil {
|
||||||
|
ct = "text/plain; charset=UTF-8" //default CT is text/plain
|
||||||
|
r.Header.Set("Content-Type", ct)
|
||||||
|
}
|
||||||
|
|
||||||
|
//set the body:
|
||||||
|
if r.Body != nil {
|
||||||
|
switch ct {
|
||||||
|
case "application/x-www-form-urlencoded":
|
||||||
|
// expect form vals in Post data
|
||||||
|
default:
|
||||||
|
buf := bufferPool.Get()
|
||||||
|
defer bufferPool.Put(buf)
|
||||||
|
if _, err = buf.ReadFrom(r.Body); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
req.Body = buf.String()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set X-Forwarded-For if it does not exist
|
||||||
|
if ip, _, err := net.SplitHostPort(r.RemoteAddr); err == nil {
|
||||||
|
if prior, ok := r.Header["X-Forwarded-For"]; ok {
|
||||||
|
ip = strings.Join(prior, ", ") + ", " + ip
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the header
|
||||||
|
req.Header["X-Forwarded-For"] = &api.Pair{
|
||||||
|
Key: "X-Forwarded-For",
|
||||||
|
Values: []string{ip},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Host is stripped from net/http Headers so let's add it
|
||||||
|
req.Header["Host"] = &api.Pair{
|
||||||
|
Key: "Host",
|
||||||
|
Values: []string{r.Host},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get data
|
||||||
|
for key, vals := range r.URL.Query() {
|
||||||
|
header, ok := req.Get[key]
|
||||||
|
if !ok {
|
||||||
|
header = &api.Pair{
|
||||||
|
Key: key,
|
||||||
|
}
|
||||||
|
req.Get[key] = header
|
||||||
|
}
|
||||||
|
header.Values = vals
|
||||||
|
}
|
||||||
|
|
||||||
|
// Post data
|
||||||
|
for key, vals := range r.PostForm {
|
||||||
|
header, ok := req.Post[key]
|
||||||
|
if !ok {
|
||||||
|
header = &api.Pair{
|
||||||
|
Key: key,
|
||||||
|
}
|
||||||
|
req.Post[key] = header
|
||||||
|
}
|
||||||
|
header.Values = vals
|
||||||
|
}
|
||||||
|
|
||||||
|
for key, vals := range r.Header {
|
||||||
|
header, ok := req.Header[key]
|
||||||
|
if !ok {
|
||||||
|
header = &api.Pair{
|
||||||
|
Key: key,
|
||||||
|
}
|
||||||
|
req.Header[key] = header
|
||||||
|
}
|
||||||
|
header.Values = vals
|
||||||
|
}
|
||||||
|
|
||||||
|
return req, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// strategy is a hack for selection
|
||||||
|
func strategy(services []*registry.Service) selector.Strategy {
|
||||||
|
return func(_ []*registry.Service) selector.Next {
|
||||||
|
// ignore input to this function, use services above
|
||||||
|
return selector.Random(services)
|
||||||
|
}
|
||||||
|
}
|
46
api/handler/api/util_test.go
Normal file
46
api/handler/api/util_test.go
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRequestToProto(t *testing.T) {
|
||||||
|
testData := []*http.Request{
|
||||||
|
{
|
||||||
|
Method: "GET",
|
||||||
|
Header: http.Header{
|
||||||
|
"Header": []string{"test"},
|
||||||
|
},
|
||||||
|
URL: &url.URL{
|
||||||
|
Scheme: "http",
|
||||||
|
Host: "localhost",
|
||||||
|
Path: "/foo/bar",
|
||||||
|
RawQuery: "param1=value1",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, d := range testData {
|
||||||
|
p, err := requestToProto(d)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if p.Path != d.URL.Path {
|
||||||
|
t.Fatalf("Expected path %s got %s", d.URL.Path, p.Path)
|
||||||
|
}
|
||||||
|
if p.Method != d.Method {
|
||||||
|
t.Fatalf("Expected method %s got %s", d.Method, p.Method)
|
||||||
|
}
|
||||||
|
for k, v := range d.Header {
|
||||||
|
if val, ok := p.Header[k]; !ok {
|
||||||
|
t.Fatalf("Expected header %s", k)
|
||||||
|
} else {
|
||||||
|
if val.Values[0] != v[0] {
|
||||||
|
t.Fatalf("Expected val %s, got %s", val.Values[0], v[0])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
141
api/handler/event/event.go
Normal file
141
api/handler/event/event.go
Normal file
@@ -0,0 +1,141 @@
|
|||||||
|
// Package event provides a handler which publishes an event
|
||||||
|
package event
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"path"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/micro/go-micro/v2/api/handler"
|
||||||
|
proto "github.com/micro/go-micro/v2/api/proto"
|
||||||
|
"github.com/micro/go-micro/v2/util/ctx"
|
||||||
|
"github.com/oxtoacart/bpool"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
bufferPool = bpool.NewSizedBufferPool(1024, 8)
|
||||||
|
)
|
||||||
|
|
||||||
|
type event struct {
|
||||||
|
opts handler.Options
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
Handler = "event"
|
||||||
|
versionRe = regexp.MustCompilePOSIX("^v[0-9]+$")
|
||||||
|
)
|
||||||
|
|
||||||
|
func eventName(parts []string) string {
|
||||||
|
return strings.Join(parts, ".")
|
||||||
|
}
|
||||||
|
|
||||||
|
func evRoute(ns, p string) (string, string) {
|
||||||
|
p = path.Clean(p)
|
||||||
|
p = strings.TrimPrefix(p, "/")
|
||||||
|
|
||||||
|
if len(p) == 0 {
|
||||||
|
return ns, "event"
|
||||||
|
}
|
||||||
|
|
||||||
|
parts := strings.Split(p, "/")
|
||||||
|
|
||||||
|
// no path
|
||||||
|
if len(parts) == 0 {
|
||||||
|
// topic: namespace
|
||||||
|
// action: event
|
||||||
|
return strings.Trim(ns, "."), "event"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Treat /v[0-9]+ as versioning
|
||||||
|
// /v1/foo/bar => topic: v1.foo action: bar
|
||||||
|
if len(parts) >= 2 && versionRe.Match([]byte(parts[0])) {
|
||||||
|
topic := ns + "." + strings.Join(parts[:2], ".")
|
||||||
|
action := eventName(parts[1:])
|
||||||
|
return topic, action
|
||||||
|
}
|
||||||
|
|
||||||
|
// /foo => topic: ns.foo action: foo
|
||||||
|
// /foo/bar => topic: ns.foo action: bar
|
||||||
|
topic := ns + "." + strings.Join(parts[:1], ".")
|
||||||
|
action := eventName(parts[1:])
|
||||||
|
|
||||||
|
return topic, action
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *event) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
bsize := handler.DefaultMaxRecvSize
|
||||||
|
if e.opts.MaxRecvSize > 0 {
|
||||||
|
bsize = e.opts.MaxRecvSize
|
||||||
|
}
|
||||||
|
|
||||||
|
r.Body = http.MaxBytesReader(w, r.Body, bsize)
|
||||||
|
|
||||||
|
// request to topic:event
|
||||||
|
// create event
|
||||||
|
// publish to topic
|
||||||
|
|
||||||
|
topic, action := evRoute(e.opts.Namespace, r.URL.Path)
|
||||||
|
|
||||||
|
// create event
|
||||||
|
ev := &proto.Event{
|
||||||
|
Name: action,
|
||||||
|
// TODO: dedupe event
|
||||||
|
Id: fmt.Sprintf("%s-%s-%s", topic, action, uuid.New().String()),
|
||||||
|
Header: make(map[string]*proto.Pair),
|
||||||
|
Timestamp: time.Now().Unix(),
|
||||||
|
}
|
||||||
|
|
||||||
|
// set headers
|
||||||
|
for key, vals := range r.Header {
|
||||||
|
header, ok := ev.Header[key]
|
||||||
|
if !ok {
|
||||||
|
header = &proto.Pair{
|
||||||
|
Key: key,
|
||||||
|
}
|
||||||
|
ev.Header[key] = header
|
||||||
|
}
|
||||||
|
header.Values = vals
|
||||||
|
}
|
||||||
|
|
||||||
|
// set body
|
||||||
|
if r.Method == "GET" {
|
||||||
|
bytes, _ := json.Marshal(r.URL.Query())
|
||||||
|
ev.Data = string(bytes)
|
||||||
|
} else {
|
||||||
|
// Read body
|
||||||
|
buf := bufferPool.Get()
|
||||||
|
defer bufferPool.Put(buf)
|
||||||
|
if _, err := buf.ReadFrom(r.Body); err != nil {
|
||||||
|
http.Error(w, err.Error(), 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ev.Data = buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// get client
|
||||||
|
c := e.opts.Client
|
||||||
|
|
||||||
|
// create publication
|
||||||
|
p := c.NewMessage(topic, ev)
|
||||||
|
|
||||||
|
// publish event
|
||||||
|
if err := c.Publish(ctx.FromRequest(r), p); err != nil {
|
||||||
|
http.Error(w, err.Error(), 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *event) String() string {
|
||||||
|
return "event"
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHandler(opts ...handler.Option) handler.Handler {
|
||||||
|
return &event{
|
||||||
|
opts: handler.NewOptions(opts...),
|
||||||
|
}
|
||||||
|
}
|
14
api/handler/handler.go
Normal file
14
api/handler/handler.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
// Package handler provides http handlers
|
||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Handler represents a HTTP handler that manages a request
|
||||||
|
type Handler interface {
|
||||||
|
// standard http handler
|
||||||
|
http.Handler
|
||||||
|
// name of handler
|
||||||
|
String() string
|
||||||
|
}
|
100
api/handler/http/http.go
Normal file
100
api/handler/http/http.go
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
// Package http is a http reverse proxy handler
|
||||||
|
package http
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httputil"
|
||||||
|
"net/url"
|
||||||
|
|
||||||
|
"github.com/micro/go-micro/v2/api"
|
||||||
|
"github.com/micro/go-micro/v2/api/handler"
|
||||||
|
"github.com/micro/go-micro/v2/client/selector"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
Handler = "http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type httpHandler struct {
|
||||||
|
options handler.Options
|
||||||
|
|
||||||
|
// set with different initialiser
|
||||||
|
s *api.Service
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *httpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
service, err := h.getService(r)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(service) == 0 {
|
||||||
|
w.WriteHeader(404)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
rp, err := url.Parse(service)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
httputil.NewSingleHostReverseProxy(rp).ServeHTTP(w, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
// getService returns the service for this request from the selector
|
||||||
|
func (h *httpHandler) getService(r *http.Request) (string, error) {
|
||||||
|
var service *api.Service
|
||||||
|
|
||||||
|
if h.s != nil {
|
||||||
|
// we were given the service
|
||||||
|
service = h.s
|
||||||
|
} else if h.options.Router != nil {
|
||||||
|
// try get service from router
|
||||||
|
s, err := h.options.Router.Route(r)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
service = s
|
||||||
|
} else {
|
||||||
|
// we have no way of routing the request
|
||||||
|
return "", errors.New("no route found")
|
||||||
|
}
|
||||||
|
|
||||||
|
// create a random selector
|
||||||
|
next := selector.Random(service.Services)
|
||||||
|
|
||||||
|
// get the next node
|
||||||
|
s, err := next()
|
||||||
|
if err != nil {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("http://%s", s.Address), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *httpHandler) String() string {
|
||||||
|
return "http"
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewHandler returns a http proxy handler
|
||||||
|
func NewHandler(opts ...handler.Option) handler.Handler {
|
||||||
|
options := handler.NewOptions(opts...)
|
||||||
|
|
||||||
|
return &httpHandler{
|
||||||
|
options: options,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithService creates a handler with a service
|
||||||
|
func WithService(s *api.Service, opts ...handler.Option) handler.Handler {
|
||||||
|
options := handler.NewOptions(opts...)
|
||||||
|
|
||||||
|
return &httpHandler{
|
||||||
|
options: options,
|
||||||
|
s: s,
|
||||||
|
}
|
||||||
|
}
|
127
api/handler/http/http_test.go
Normal file
127
api/handler/http/http_test.go
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
package http
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/micro/go-micro/v2/api/handler"
|
||||||
|
"github.com/micro/go-micro/v2/api/resolver"
|
||||||
|
"github.com/micro/go-micro/v2/api/resolver/vpath"
|
||||||
|
"github.com/micro/go-micro/v2/api/router"
|
||||||
|
regRouter "github.com/micro/go-micro/v2/api/router/registry"
|
||||||
|
"github.com/micro/go-micro/v2/registry"
|
||||||
|
"github.com/micro/go-micro/v2/registry/memory"
|
||||||
|
)
|
||||||
|
|
||||||
|
func testHttp(t *testing.T, path, service, ns string) {
|
||||||
|
r := memory.NewRegistry()
|
||||||
|
|
||||||
|
l, err := net.Listen("tcp", "127.0.0.1:0")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer l.Close()
|
||||||
|
|
||||||
|
s := ®istry.Service{
|
||||||
|
Name: service,
|
||||||
|
Nodes: []*registry.Node{
|
||||||
|
{
|
||||||
|
Id: service + "-1",
|
||||||
|
Address: l.Addr().String(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
r.Register(s)
|
||||||
|
defer r.Deregister(s)
|
||||||
|
|
||||||
|
// setup the test handler
|
||||||
|
m := http.NewServeMux()
|
||||||
|
m.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Write([]byte(`you got served`))
|
||||||
|
})
|
||||||
|
|
||||||
|
// start http test serve
|
||||||
|
go http.Serve(l, m)
|
||||||
|
|
||||||
|
// create new request and writer
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
req, err := http.NewRequest("POST", path, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// initialise the handler
|
||||||
|
rt := regRouter.NewRouter(
|
||||||
|
router.WithHandler("http"),
|
||||||
|
router.WithRegistry(r),
|
||||||
|
router.WithResolver(vpath.NewResolver(
|
||||||
|
resolver.WithNamespace(resolver.StaticNamespace(ns)),
|
||||||
|
)),
|
||||||
|
)
|
||||||
|
|
||||||
|
p := NewHandler(handler.WithRouter(rt))
|
||||||
|
|
||||||
|
// execute the handler
|
||||||
|
p.ServeHTTP(w, req)
|
||||||
|
|
||||||
|
if w.Code != 200 {
|
||||||
|
t.Fatalf("Expected 200 response got %d %s", w.Code, w.Body.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
if w.Body.String() != "you got served" {
|
||||||
|
t.Fatalf("Expected body: you got served. Got: %s", w.Body.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHttpHandler(t *testing.T) {
|
||||||
|
testData := []struct {
|
||||||
|
path string
|
||||||
|
service string
|
||||||
|
namespace string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"/test/foo",
|
||||||
|
"go.micro.api.test",
|
||||||
|
"go.micro.api",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"/test/foo/baz",
|
||||||
|
"go.micro.api.test",
|
||||||
|
"go.micro.api",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"/v1/foo",
|
||||||
|
"go.micro.api.v1.foo",
|
||||||
|
"go.micro.api",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"/v1/foo/bar",
|
||||||
|
"go.micro.api.v1.foo",
|
||||||
|
"go.micro.api",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"/v2/baz",
|
||||||
|
"go.micro.api.v2.baz",
|
||||||
|
"go.micro.api",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"/v2/baz/bar",
|
||||||
|
"go.micro.api.v2.baz",
|
||||||
|
"go.micro.api",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"/v2/baz/bar",
|
||||||
|
"v2.baz",
|
||||||
|
"",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, d := range testData {
|
||||||
|
t.Run(d.service, func(t *testing.T) {
|
||||||
|
testHttp(t, d.path, d.service, d.namespace)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
70
api/handler/options.go
Normal file
70
api/handler/options.go
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/micro/go-micro/v2/api/router"
|
||||||
|
"github.com/micro/go-micro/v2/client"
|
||||||
|
"github.com/micro/go-micro/v2/client/grpc"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
DefaultMaxRecvSize int64 = 1024 * 1024 * 100 // 10Mb
|
||||||
|
)
|
||||||
|
|
||||||
|
type Options struct {
|
||||||
|
MaxRecvSize int64
|
||||||
|
Namespace string
|
||||||
|
Router router.Router
|
||||||
|
Client client.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
type Option func(o *Options)
|
||||||
|
|
||||||
|
// NewOptions fills in the blanks
|
||||||
|
func NewOptions(opts ...Option) Options {
|
||||||
|
var options Options
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&options)
|
||||||
|
}
|
||||||
|
|
||||||
|
if options.Client == nil {
|
||||||
|
WithClient(grpc.NewClient())(&options)
|
||||||
|
}
|
||||||
|
|
||||||
|
// set namespace if blank
|
||||||
|
if len(options.Namespace) == 0 {
|
||||||
|
WithNamespace("go.micro.api")(&options)
|
||||||
|
}
|
||||||
|
|
||||||
|
if options.MaxRecvSize == 0 {
|
||||||
|
options.MaxRecvSize = DefaultMaxRecvSize
|
||||||
|
}
|
||||||
|
|
||||||
|
return options
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithNamespace specifies the namespace for the handler
|
||||||
|
func WithNamespace(s string) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.Namespace = s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithRouter specifies a router to be used by the handler
|
||||||
|
func WithRouter(r router.Router) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.Router = r
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithClient(c client.Client) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.Client = c
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithmaxRecvSize specifies max body size
|
||||||
|
func WithMaxRecvSize(size int64) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.MaxRecvSize = size
|
||||||
|
}
|
||||||
|
}
|
522
api/handler/rpc/rpc.go
Normal file
522
api/handler/rpc/rpc.go
Normal file
@@ -0,0 +1,522 @@
|
|||||||
|
// Package rpc is a go-micro rpc handler.
|
||||||
|
package rpc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/textproto"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
jsonpatch "github.com/evanphx/json-patch/v5"
|
||||||
|
"github.com/micro/go-micro/v2/api"
|
||||||
|
"github.com/micro/go-micro/v2/api/handler"
|
||||||
|
"github.com/micro/go-micro/v2/api/internal/proto"
|
||||||
|
"github.com/micro/go-micro/v2/client"
|
||||||
|
"github.com/micro/go-micro/v2/client/selector"
|
||||||
|
"github.com/micro/go-micro/v2/codec"
|
||||||
|
"github.com/micro/go-micro/v2/codec/jsonrpc"
|
||||||
|
"github.com/micro/go-micro/v2/codec/protorpc"
|
||||||
|
"github.com/micro/go-micro/v2/errors"
|
||||||
|
"github.com/micro/go-micro/v2/logger"
|
||||||
|
"github.com/micro/go-micro/v2/metadata"
|
||||||
|
"github.com/micro/go-micro/v2/registry"
|
||||||
|
"github.com/micro/go-micro/v2/util/ctx"
|
||||||
|
"github.com/micro/go-micro/v2/util/qson"
|
||||||
|
"github.com/oxtoacart/bpool"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
Handler = "rpc"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// supported json codecs
|
||||||
|
jsonCodecs = []string{
|
||||||
|
"application/grpc+json",
|
||||||
|
"application/json",
|
||||||
|
"application/json-rpc",
|
||||||
|
}
|
||||||
|
|
||||||
|
// support proto codecs
|
||||||
|
protoCodecs = []string{
|
||||||
|
"application/grpc",
|
||||||
|
"application/grpc+proto",
|
||||||
|
"application/proto",
|
||||||
|
"application/protobuf",
|
||||||
|
"application/proto-rpc",
|
||||||
|
"application/octet-stream",
|
||||||
|
}
|
||||||
|
|
||||||
|
bufferPool = bpool.NewSizedBufferPool(1024, 8)
|
||||||
|
)
|
||||||
|
|
||||||
|
type rpcHandler struct {
|
||||||
|
opts handler.Options
|
||||||
|
s *api.Service
|
||||||
|
}
|
||||||
|
|
||||||
|
type buffer struct {
|
||||||
|
io.ReadCloser
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *buffer) Write(_ []byte) (int, error) {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// strategy is a hack for selection
|
||||||
|
func strategy(services []*registry.Service) selector.Strategy {
|
||||||
|
return func(_ []*registry.Service) selector.Next {
|
||||||
|
// ignore input to this function, use services above
|
||||||
|
return selector.Random(services)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *rpcHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
bsize := handler.DefaultMaxRecvSize
|
||||||
|
if h.opts.MaxRecvSize > 0 {
|
||||||
|
bsize = h.opts.MaxRecvSize
|
||||||
|
}
|
||||||
|
|
||||||
|
r.Body = http.MaxBytesReader(w, r.Body, bsize)
|
||||||
|
|
||||||
|
defer r.Body.Close()
|
||||||
|
var service *api.Service
|
||||||
|
|
||||||
|
if h.s != nil {
|
||||||
|
// we were given the service
|
||||||
|
service = h.s
|
||||||
|
} else if h.opts.Router != nil {
|
||||||
|
// try get service from router
|
||||||
|
s, err := h.opts.Router.Route(r)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, r, errors.InternalServerError("go.micro.api", err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
service = s
|
||||||
|
} else {
|
||||||
|
// we have no way of routing the request
|
||||||
|
writeError(w, r, errors.InternalServerError("go.micro.api", "no route found"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ct := r.Header.Get("Content-Type")
|
||||||
|
|
||||||
|
// Strip charset from Content-Type (like `application/json; charset=UTF-8`)
|
||||||
|
if idx := strings.IndexRune(ct, ';'); idx >= 0 {
|
||||||
|
ct = ct[:idx]
|
||||||
|
}
|
||||||
|
|
||||||
|
// micro client
|
||||||
|
c := h.opts.Client
|
||||||
|
|
||||||
|
// create context
|
||||||
|
cx := ctx.FromRequest(r)
|
||||||
|
// get context from http handler wrappers
|
||||||
|
md, ok := metadata.FromContext(r.Context())
|
||||||
|
if !ok {
|
||||||
|
md = make(metadata.Metadata)
|
||||||
|
}
|
||||||
|
// fill contex with http headers
|
||||||
|
md["Host"] = r.Host
|
||||||
|
md["Method"] = r.Method
|
||||||
|
// get canonical headers
|
||||||
|
for k, _ := range r.Header {
|
||||||
|
// may be need to get all values for key like r.Header.Values() provide in go 1.14
|
||||||
|
md[textproto.CanonicalMIMEHeaderKey(k)] = r.Header.Get(k)
|
||||||
|
}
|
||||||
|
|
||||||
|
// merge context with overwrite
|
||||||
|
cx = metadata.MergeContext(cx, md, true)
|
||||||
|
|
||||||
|
// set merged context to request
|
||||||
|
*r = *r.Clone(cx)
|
||||||
|
// if stream we currently only support json
|
||||||
|
if isStream(r, service) {
|
||||||
|
// drop older context as it can have timeouts and create new
|
||||||
|
// md, _ := metadata.FromContext(cx)
|
||||||
|
//serveWebsocket(context.TODO(), w, r, service, c)
|
||||||
|
serveWebsocket(cx, w, r, service, c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// create strategy
|
||||||
|
so := selector.WithStrategy(strategy(service.Services))
|
||||||
|
|
||||||
|
// walk the standard call path
|
||||||
|
// get payload
|
||||||
|
br, err := requestPayload(r)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var rsp []byte
|
||||||
|
|
||||||
|
switch {
|
||||||
|
// proto codecs
|
||||||
|
case hasCodec(ct, protoCodecs):
|
||||||
|
request := &proto.Message{}
|
||||||
|
// if the extracted payload isn't empty lets use it
|
||||||
|
if len(br) > 0 {
|
||||||
|
request = proto.NewMessage(br)
|
||||||
|
}
|
||||||
|
|
||||||
|
// create request/response
|
||||||
|
response := &proto.Message{}
|
||||||
|
|
||||||
|
req := c.NewRequest(
|
||||||
|
service.Name,
|
||||||
|
service.Endpoint.Name,
|
||||||
|
request,
|
||||||
|
client.WithContentType(ct),
|
||||||
|
)
|
||||||
|
|
||||||
|
// make the call
|
||||||
|
if err := c.Call(cx, req, response, client.WithSelectOption(so)); err != nil {
|
||||||
|
writeError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// marshall response
|
||||||
|
rsp, err = response.Marshal()
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
// if json codec is not present set to json
|
||||||
|
if !hasCodec(ct, jsonCodecs) {
|
||||||
|
ct = "application/json"
|
||||||
|
}
|
||||||
|
|
||||||
|
// default to trying json
|
||||||
|
var request json.RawMessage
|
||||||
|
// if the extracted payload isn't empty lets use it
|
||||||
|
if len(br) > 0 {
|
||||||
|
request = json.RawMessage(br)
|
||||||
|
}
|
||||||
|
|
||||||
|
// create request/response
|
||||||
|
var response json.RawMessage
|
||||||
|
|
||||||
|
req := c.NewRequest(
|
||||||
|
service.Name,
|
||||||
|
service.Endpoint.Name,
|
||||||
|
&request,
|
||||||
|
client.WithContentType(ct),
|
||||||
|
)
|
||||||
|
// make the call
|
||||||
|
if err := c.Call(cx, req, &response, client.WithSelectOption(so)); err != nil {
|
||||||
|
writeError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// marshall response
|
||||||
|
rsp, err = response.MarshalJSON()
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// write the response
|
||||||
|
writeResponse(w, r, rsp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rh *rpcHandler) String() string {
|
||||||
|
return "rpc"
|
||||||
|
}
|
||||||
|
|
||||||
|
func hasCodec(ct string, codecs []string) bool {
|
||||||
|
for _, codec := range codecs {
|
||||||
|
if ct == codec {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// requestPayload takes a *http.Request.
|
||||||
|
// If the request is a GET the query string parameters are extracted and marshaled to JSON and the raw bytes are returned.
|
||||||
|
// If the request method is a POST the request body is read and returned
|
||||||
|
func requestPayload(r *http.Request) ([]byte, error) {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
// we have to decode json-rpc and proto-rpc because we suck
|
||||||
|
// well actually because there's no proxy codec right now
|
||||||
|
|
||||||
|
ct := r.Header.Get("Content-Type")
|
||||||
|
switch {
|
||||||
|
case strings.Contains(ct, "application/json-rpc"):
|
||||||
|
msg := codec.Message{
|
||||||
|
Type: codec.Request,
|
||||||
|
Header: make(map[string]string),
|
||||||
|
}
|
||||||
|
c := jsonrpc.NewCodec(&buffer{r.Body})
|
||||||
|
if err = c.ReadHeader(&msg, codec.Request); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var raw json.RawMessage
|
||||||
|
if err = c.ReadBody(&raw); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return ([]byte)(raw), nil
|
||||||
|
case strings.Contains(ct, "application/proto-rpc"), strings.Contains(ct, "application/octet-stream"):
|
||||||
|
msg := codec.Message{
|
||||||
|
Type: codec.Request,
|
||||||
|
Header: make(map[string]string),
|
||||||
|
}
|
||||||
|
c := protorpc.NewCodec(&buffer{r.Body})
|
||||||
|
if err = c.ReadHeader(&msg, codec.Request); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var raw proto.Message
|
||||||
|
if err = c.ReadBody(&raw); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return raw.Marshal()
|
||||||
|
case strings.Contains(ct, "application/www-x-form-urlencoded"):
|
||||||
|
r.ParseForm()
|
||||||
|
|
||||||
|
// generate a new set of values from the form
|
||||||
|
vals := make(map[string]string)
|
||||||
|
for k, v := range r.Form {
|
||||||
|
vals[k] = strings.Join(v, ",")
|
||||||
|
}
|
||||||
|
|
||||||
|
// marshal
|
||||||
|
return json.Marshal(vals)
|
||||||
|
// TODO: application/grpc
|
||||||
|
}
|
||||||
|
|
||||||
|
// otherwise as per usual
|
||||||
|
ctx := r.Context()
|
||||||
|
// dont user meadata.FromContext as it mangles names
|
||||||
|
md, ok := metadata.FromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
md = make(map[string]string)
|
||||||
|
}
|
||||||
|
|
||||||
|
// allocate maximum
|
||||||
|
matches := make(map[string]interface{}, len(md))
|
||||||
|
bodydst := ""
|
||||||
|
|
||||||
|
// get fields from url path
|
||||||
|
for k, v := range md {
|
||||||
|
k = strings.ToLower(k)
|
||||||
|
// filter own keys
|
||||||
|
if strings.HasPrefix(k, "x-api-field-") {
|
||||||
|
matches[strings.TrimPrefix(k, "x-api-field-")] = v
|
||||||
|
delete(md, k)
|
||||||
|
} else if k == "x-api-body" {
|
||||||
|
bodydst = v
|
||||||
|
delete(md, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// map of all fields
|
||||||
|
req := make(map[string]interface{}, len(md))
|
||||||
|
|
||||||
|
// get fields from url values
|
||||||
|
if len(r.URL.RawQuery) > 0 {
|
||||||
|
umd := make(map[string]interface{})
|
||||||
|
err = qson.Unmarshal(&umd, r.URL.RawQuery)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for k, v := range umd {
|
||||||
|
matches[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// restore context without fields
|
||||||
|
*r = *r.Clone(metadata.NewContext(ctx, md))
|
||||||
|
|
||||||
|
for k, v := range matches {
|
||||||
|
ps := strings.Split(k, ".")
|
||||||
|
if len(ps) == 1 {
|
||||||
|
req[k] = v
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
em := make(map[string]interface{})
|
||||||
|
em[ps[len(ps)-1]] = v
|
||||||
|
for i := len(ps) - 2; i > 0; i-- {
|
||||||
|
nm := make(map[string]interface{})
|
||||||
|
nm[ps[i]] = em
|
||||||
|
em = nm
|
||||||
|
}
|
||||||
|
if vm, ok := req[ps[0]]; ok {
|
||||||
|
// nested map
|
||||||
|
nm := vm.(map[string]interface{})
|
||||||
|
for vk, vv := range em {
|
||||||
|
nm[vk] = vv
|
||||||
|
}
|
||||||
|
req[ps[0]] = nm
|
||||||
|
} else {
|
||||||
|
req[ps[0]] = em
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pathbuf := []byte("{}")
|
||||||
|
if len(req) > 0 {
|
||||||
|
pathbuf, err = json.Marshal(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
urlbuf := []byte("{}")
|
||||||
|
out, err := jsonpatch.MergeMergePatches(urlbuf, pathbuf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
switch r.Method {
|
||||||
|
case "GET":
|
||||||
|
// empty response
|
||||||
|
if strings.Contains(ct, "application/json") && string(out) == "{}" {
|
||||||
|
return out, nil
|
||||||
|
} else if string(out) == "{}" && !strings.Contains(ct, "application/json") {
|
||||||
|
return []byte{}, nil
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
case "PATCH", "POST", "PUT", "DELETE":
|
||||||
|
bodybuf := []byte("{}")
|
||||||
|
buf := bufferPool.Get()
|
||||||
|
defer bufferPool.Put(buf)
|
||||||
|
if _, err := buf.ReadFrom(r.Body); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if b := buf.Bytes(); len(b) > 0 {
|
||||||
|
bodybuf = b
|
||||||
|
}
|
||||||
|
if bodydst == "" || bodydst == "*" {
|
||||||
|
if out, err = jsonpatch.MergeMergePatches(out, bodybuf); err == nil {
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var jsonbody map[string]interface{}
|
||||||
|
if json.Valid(bodybuf) {
|
||||||
|
if err = json.Unmarshal(bodybuf, &jsonbody); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dstmap := make(map[string]interface{})
|
||||||
|
ps := strings.Split(bodydst, ".")
|
||||||
|
if len(ps) == 1 {
|
||||||
|
if jsonbody != nil {
|
||||||
|
dstmap[ps[0]] = jsonbody
|
||||||
|
} else {
|
||||||
|
// old unexpected behaviour
|
||||||
|
dstmap[ps[0]] = bodybuf
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
em := make(map[string]interface{})
|
||||||
|
if jsonbody != nil {
|
||||||
|
em[ps[len(ps)-1]] = jsonbody
|
||||||
|
} else {
|
||||||
|
// old unexpected behaviour
|
||||||
|
em[ps[len(ps)-1]] = bodybuf
|
||||||
|
}
|
||||||
|
for i := len(ps) - 2; i > 0; i-- {
|
||||||
|
nm := make(map[string]interface{})
|
||||||
|
nm[ps[i]] = em
|
||||||
|
em = nm
|
||||||
|
}
|
||||||
|
dstmap[ps[0]] = em
|
||||||
|
}
|
||||||
|
|
||||||
|
bodyout, err := json.Marshal(dstmap)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if out, err = jsonpatch.MergeMergePatches(out, bodyout); err == nil {
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//fallback to previous unknown behaviour
|
||||||
|
return bodybuf, nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return []byte{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeError(w http.ResponseWriter, r *http.Request, err error) {
|
||||||
|
ce := errors.Parse(err.Error())
|
||||||
|
|
||||||
|
switch ce.Code {
|
||||||
|
case 0:
|
||||||
|
// assuming it's totally screwed
|
||||||
|
ce.Code = 500
|
||||||
|
ce.Id = "go.micro.api"
|
||||||
|
ce.Status = http.StatusText(500)
|
||||||
|
ce.Detail = "error during request: " + ce.Detail
|
||||||
|
w.WriteHeader(500)
|
||||||
|
default:
|
||||||
|
w.WriteHeader(int(ce.Code))
|
||||||
|
}
|
||||||
|
|
||||||
|
// response content type
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
// Set trailers
|
||||||
|
if strings.Contains(r.Header.Get("Content-Type"), "application/grpc") {
|
||||||
|
w.Header().Set("Trailer", "grpc-status")
|
||||||
|
w.Header().Set("Trailer", "grpc-message")
|
||||||
|
w.Header().Set("grpc-status", "13")
|
||||||
|
w.Header().Set("grpc-message", ce.Detail)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, werr := w.Write([]byte(ce.Error()))
|
||||||
|
if werr != nil {
|
||||||
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
||||||
|
logger.Error(werr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeResponse(w http.ResponseWriter, r *http.Request, rsp []byte) {
|
||||||
|
w.Header().Set("Content-Type", r.Header.Get("Content-Type"))
|
||||||
|
w.Header().Set("Content-Length", strconv.Itoa(len(rsp)))
|
||||||
|
|
||||||
|
// Set trailers
|
||||||
|
if strings.Contains(r.Header.Get("Content-Type"), "application/grpc") {
|
||||||
|
w.Header().Set("Trailer", "grpc-status")
|
||||||
|
w.Header().Set("Trailer", "grpc-message")
|
||||||
|
w.Header().Set("grpc-status", "0")
|
||||||
|
w.Header().Set("grpc-message", "")
|
||||||
|
}
|
||||||
|
|
||||||
|
// write 204 status if rsp is nil
|
||||||
|
if len(rsp) == 0 {
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
}
|
||||||
|
|
||||||
|
// write response
|
||||||
|
_, err := w.Write(rsp)
|
||||||
|
if err != nil {
|
||||||
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
||||||
|
logger.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHandler(opts ...handler.Option) handler.Handler {
|
||||||
|
options := handler.NewOptions(opts...)
|
||||||
|
return &rpcHandler{
|
||||||
|
opts: options,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithService(s *api.Service, opts ...handler.Option) handler.Handler {
|
||||||
|
options := handler.NewOptions(opts...)
|
||||||
|
return &rpcHandler{
|
||||||
|
opts: options,
|
||||||
|
s: s,
|
||||||
|
}
|
||||||
|
}
|
112
api/handler/rpc/rpc_test.go
Normal file
112
api/handler/rpc/rpc_test.go
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
package rpc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/golang/protobuf/proto"
|
||||||
|
go_api "github.com/micro/go-micro/v2/api/proto"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRequestPayloadFromRequest(t *testing.T) {
|
||||||
|
|
||||||
|
// our test event so that we can validate serialising / deserializing of true protos works
|
||||||
|
protoEvent := go_api.Event{
|
||||||
|
Name: "Test",
|
||||||
|
}
|
||||||
|
|
||||||
|
protoBytes, err := proto.Marshal(&protoEvent)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("Failed to marshal proto", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonBytes, err := json.Marshal(protoEvent)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("Failed to marshal proto to JSON ", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonUrlBytes := []byte(`{"key1":"val1","key2":"val2","name":"Test"}`)
|
||||||
|
|
||||||
|
t.Run("extracting a json from a POST request with url params", func(t *testing.T) {
|
||||||
|
r, err := http.NewRequest("POST", "http://localhost/my/path?key1=val1&key2=val2", bytes.NewReader(jsonBytes))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to created http.Request: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
extByte, err := requestPayload(r)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to extract payload from request: %v", err)
|
||||||
|
}
|
||||||
|
if string(extByte) != string(jsonUrlBytes) {
|
||||||
|
t.Fatalf("Expected %v and %v to match", string(extByte), jsonUrlBytes)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("extracting a proto from a POST request", func(t *testing.T) {
|
||||||
|
r, err := http.NewRequest("POST", "http://localhost/my/path", bytes.NewReader(protoBytes))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to created http.Request: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
extByte, err := requestPayload(r)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to extract payload from request: %v", err)
|
||||||
|
}
|
||||||
|
if string(extByte) != string(protoBytes) {
|
||||||
|
t.Fatalf("Expected %v and %v to match", string(extByte), string(protoBytes))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("extracting JSON from a POST request", func(t *testing.T) {
|
||||||
|
r, err := http.NewRequest("POST", "http://localhost/my/path", bytes.NewReader(jsonBytes))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to created http.Request: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
extByte, err := requestPayload(r)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to extract payload from request: %v", err)
|
||||||
|
}
|
||||||
|
if string(extByte) != string(jsonBytes) {
|
||||||
|
t.Fatalf("Expected %v and %v to match", string(extByte), string(jsonBytes))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("extracting params from a GET request", func(t *testing.T) {
|
||||||
|
|
||||||
|
r, err := http.NewRequest("GET", "http://localhost/my/path", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to created http.Request: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
q := r.URL.Query()
|
||||||
|
q.Add("name", "Test")
|
||||||
|
r.URL.RawQuery = q.Encode()
|
||||||
|
|
||||||
|
extByte, err := requestPayload(r)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to extract payload from request: %v", err)
|
||||||
|
}
|
||||||
|
if string(extByte) != string(jsonBytes) {
|
||||||
|
t.Fatalf("Expected %v and %v to match", string(extByte), string(jsonBytes))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("GET request with no params", func(t *testing.T) {
|
||||||
|
|
||||||
|
r, err := http.NewRequest("GET", "http://localhost/my/path", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to created http.Request: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
extByte, err := requestPayload(r)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to extract payload from request: %v", err)
|
||||||
|
}
|
||||||
|
if string(extByte) != "" {
|
||||||
|
t.Fatalf("Expected %v and %v to match", string(extByte), "")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
259
api/handler/rpc/stream.go
Normal file
259
api/handler/rpc/stream.go
Normal file
@@ -0,0 +1,259 @@
|
|||||||
|
package rpc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gobwas/httphead"
|
||||||
|
"github.com/gobwas/ws"
|
||||||
|
"github.com/gobwas/ws/wsutil"
|
||||||
|
"github.com/micro/go-micro/v2/api"
|
||||||
|
"github.com/micro/go-micro/v2/client"
|
||||||
|
"github.com/micro/go-micro/v2/client/selector"
|
||||||
|
raw "github.com/micro/go-micro/v2/codec/bytes"
|
||||||
|
"github.com/micro/go-micro/v2/logger"
|
||||||
|
)
|
||||||
|
|
||||||
|
// serveWebsocket will stream rpc back over websockets assuming json
|
||||||
|
func serveWebsocket(ctx context.Context, w http.ResponseWriter, r *http.Request, service *api.Service, c client.Client) {
|
||||||
|
var op ws.OpCode
|
||||||
|
|
||||||
|
ct := r.Header.Get("Content-Type")
|
||||||
|
// Strip charset from Content-Type (like `application/json; charset=UTF-8`)
|
||||||
|
if idx := strings.IndexRune(ct, ';'); idx >= 0 {
|
||||||
|
ct = ct[:idx]
|
||||||
|
}
|
||||||
|
|
||||||
|
// check proto from request
|
||||||
|
switch ct {
|
||||||
|
case "application/json":
|
||||||
|
op = ws.OpText
|
||||||
|
default:
|
||||||
|
op = ws.OpBinary
|
||||||
|
}
|
||||||
|
|
||||||
|
hdr := make(http.Header)
|
||||||
|
if proto, ok := r.Header["Sec-WebSocket-Protocol"]; ok {
|
||||||
|
for _, p := range proto {
|
||||||
|
switch p {
|
||||||
|
case "binary":
|
||||||
|
hdr["Sec-WebSocket-Protocol"] = []string{"binary"}
|
||||||
|
op = ws.OpBinary
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
payload, err := requestPayload(r)
|
||||||
|
if err != nil {
|
||||||
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
||||||
|
logger.Error(err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
upgrader := ws.HTTPUpgrader{Timeout: 5 * time.Second,
|
||||||
|
Protocol: func(proto string) bool {
|
||||||
|
if strings.Contains(proto, "binary") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
// fallback to support all protocols now
|
||||||
|
return true
|
||||||
|
},
|
||||||
|
Extension: func(httphead.Option) bool {
|
||||||
|
// disable extensions for compatibility
|
||||||
|
return false
|
||||||
|
},
|
||||||
|
Header: hdr,
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, rw, _, err := upgrader.Upgrade(r, w)
|
||||||
|
if err != nil {
|
||||||
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
||||||
|
logger.Error(err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if err := conn.Close(); err != nil {
|
||||||
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
||||||
|
logger.Error(err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
var request interface{}
|
||||||
|
if !bytes.Equal(payload, []byte(`{}`)) {
|
||||||
|
switch ct {
|
||||||
|
case "application/json", "":
|
||||||
|
m := json.RawMessage(payload)
|
||||||
|
request = &m
|
||||||
|
default:
|
||||||
|
request = &raw.Frame{Data: payload}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// we always need to set content type for message
|
||||||
|
if ct == "" {
|
||||||
|
ct = "application/json"
|
||||||
|
}
|
||||||
|
req := c.NewRequest(
|
||||||
|
service.Name,
|
||||||
|
service.Endpoint.Name,
|
||||||
|
request,
|
||||||
|
client.WithContentType(ct),
|
||||||
|
client.StreamingRequest(),
|
||||||
|
)
|
||||||
|
|
||||||
|
so := selector.WithStrategy(strategy(service.Services))
|
||||||
|
// create a new stream
|
||||||
|
stream, err := c.Stream(ctx, req, client.WithSelectOption(so))
|
||||||
|
if err != nil {
|
||||||
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
||||||
|
logger.Error(err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if request != nil {
|
||||||
|
if err = stream.Send(request); err != nil {
|
||||||
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
||||||
|
logger.Error(err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
go writeLoop(rw, stream)
|
||||||
|
|
||||||
|
rsp := stream.Response()
|
||||||
|
|
||||||
|
// receive from stream and send to client
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case <-stream.Context().Done():
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
// read backend response body
|
||||||
|
buf, err := rsp.Read()
|
||||||
|
if err != nil {
|
||||||
|
// wants to avoid import grpc/status.Status
|
||||||
|
if strings.Contains(err.Error(), "context canceled") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
||||||
|
logger.Error(err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// write the response
|
||||||
|
if err := wsutil.WriteServerMessage(rw, op, buf); err != nil {
|
||||||
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
||||||
|
logger.Error(err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err = rw.Flush(); err != nil {
|
||||||
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
||||||
|
logger.Error(err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// writeLoop
|
||||||
|
func writeLoop(rw io.ReadWriter, stream client.Stream) {
|
||||||
|
// close stream when done
|
||||||
|
defer stream.Close()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-stream.Context().Done():
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
buf, op, err := wsutil.ReadClientData(rw)
|
||||||
|
if err != nil {
|
||||||
|
if wserr, ok := err.(wsutil.ClosedError); ok {
|
||||||
|
switch wserr.Code {
|
||||||
|
case ws.StatusGoingAway:
|
||||||
|
// this happens when user leave the page
|
||||||
|
return
|
||||||
|
case ws.StatusNormalClosure, ws.StatusNoStatusRcvd:
|
||||||
|
// this happens when user close ws connection, or we don't get any status
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
||||||
|
logger.Error(err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
switch op {
|
||||||
|
default:
|
||||||
|
// not relevant
|
||||||
|
continue
|
||||||
|
case ws.OpText, ws.OpBinary:
|
||||||
|
break
|
||||||
|
}
|
||||||
|
// send to backend
|
||||||
|
// default to trying json
|
||||||
|
// if the extracted payload isn't empty lets use it
|
||||||
|
request := &raw.Frame{Data: buf}
|
||||||
|
if err := stream.Send(request); err != nil {
|
||||||
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
||||||
|
logger.Error(err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func isStream(r *http.Request, srv *api.Service) bool {
|
||||||
|
// check if it's a web socket
|
||||||
|
if !isWebSocket(r) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// check if the endpoint supports streaming
|
||||||
|
for _, service := range srv.Services {
|
||||||
|
for _, ep := range service.Endpoints {
|
||||||
|
// skip if it doesn't match the name
|
||||||
|
if ep.Name != srv.Endpoint.Name {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// matched if the name
|
||||||
|
if v := ep.Metadata["stream"]; v == "true" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func isWebSocket(r *http.Request) bool {
|
||||||
|
contains := func(key, val string) bool {
|
||||||
|
vv := strings.Split(r.Header.Get(key), ",")
|
||||||
|
for _, v := range vv {
|
||||||
|
if val == strings.ToLower(strings.TrimSpace(v)) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if contains("Connection", "upgrade") && contains("Upgrade", "websocket") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
177
api/handler/web/web.go
Normal file
177
api/handler/web/web.go
Normal file
@@ -0,0 +1,177 @@
|
|||||||
|
// Package web contains the web handler including websocket support
|
||||||
|
package web
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httputil"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/micro/go-micro/v2/api"
|
||||||
|
"github.com/micro/go-micro/v2/api/handler"
|
||||||
|
"github.com/micro/go-micro/v2/client/selector"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
Handler = "web"
|
||||||
|
)
|
||||||
|
|
||||||
|
type webHandler struct {
|
||||||
|
opts handler.Options
|
||||||
|
s *api.Service
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wh *webHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
service, err := wh.getService(r)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(service) == 0 {
|
||||||
|
w.WriteHeader(404)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
rp, err := url.Parse(service)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if isWebSocket(r) {
|
||||||
|
wh.serveWebSocket(rp.Host, w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
httputil.NewSingleHostReverseProxy(rp).ServeHTTP(w, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
// getService returns the service for this request from the selector
|
||||||
|
func (wh *webHandler) getService(r *http.Request) (string, error) {
|
||||||
|
var service *api.Service
|
||||||
|
|
||||||
|
if wh.s != nil {
|
||||||
|
// we were given the service
|
||||||
|
service = wh.s
|
||||||
|
} else if wh.opts.Router != nil {
|
||||||
|
// try get service from router
|
||||||
|
s, err := wh.opts.Router.Route(r)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
service = s
|
||||||
|
} else {
|
||||||
|
// we have no way of routing the request
|
||||||
|
return "", errors.New("no route found")
|
||||||
|
}
|
||||||
|
|
||||||
|
// create a random selector
|
||||||
|
next := selector.Random(service.Services)
|
||||||
|
|
||||||
|
// get the next node
|
||||||
|
s, err := next()
|
||||||
|
if err != nil {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("http://%s", s.Address), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// serveWebSocket used to serve a web socket proxied connection
|
||||||
|
func (wh *webHandler) serveWebSocket(host string, w http.ResponseWriter, r *http.Request) {
|
||||||
|
req := new(http.Request)
|
||||||
|
*req = *r
|
||||||
|
|
||||||
|
if len(host) == 0 {
|
||||||
|
http.Error(w, "invalid host", 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// set x-forward-for
|
||||||
|
if clientIP, _, err := net.SplitHostPort(r.RemoteAddr); err == nil {
|
||||||
|
if ips, ok := req.Header["X-Forwarded-For"]; ok {
|
||||||
|
clientIP = strings.Join(ips, ", ") + ", " + clientIP
|
||||||
|
}
|
||||||
|
req.Header.Set("X-Forwarded-For", clientIP)
|
||||||
|
}
|
||||||
|
|
||||||
|
// connect to the backend host
|
||||||
|
conn, err := net.Dial("tcp", host)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// hijack the connection
|
||||||
|
hj, ok := w.(http.Hijacker)
|
||||||
|
if !ok {
|
||||||
|
http.Error(w, "failed to connect", 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
nc, _, err := hj.Hijack()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer nc.Close()
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
if err = req.Write(conn); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
errCh := make(chan error, 2)
|
||||||
|
|
||||||
|
cp := func(dst io.Writer, src io.Reader) {
|
||||||
|
_, err := io.Copy(dst, src)
|
||||||
|
errCh <- err
|
||||||
|
}
|
||||||
|
|
||||||
|
go cp(conn, nc)
|
||||||
|
go cp(nc, conn)
|
||||||
|
|
||||||
|
<-errCh
|
||||||
|
}
|
||||||
|
|
||||||
|
func isWebSocket(r *http.Request) bool {
|
||||||
|
contains := func(key, val string) bool {
|
||||||
|
vv := strings.Split(r.Header.Get(key), ",")
|
||||||
|
for _, v := range vv {
|
||||||
|
if val == strings.ToLower(strings.TrimSpace(v)) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if contains("Connection", "upgrade") && contains("Upgrade", "websocket") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wh *webHandler) String() string {
|
||||||
|
return "web"
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHandler(opts ...handler.Option) handler.Handler {
|
||||||
|
return &webHandler{
|
||||||
|
opts: handler.NewOptions(opts...),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithService(s *api.Service, opts ...handler.Option) handler.Handler {
|
||||||
|
options := handler.NewOptions(opts...)
|
||||||
|
|
||||||
|
return &webHandler{
|
||||||
|
opts: options,
|
||||||
|
s: s,
|
||||||
|
}
|
||||||
|
}
|
28
api/internal/proto/message.pb.go
Normal file
28
api/internal/proto/message.pb.go
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package proto
|
||||||
|
|
||||||
|
type Message struct {
|
||||||
|
data []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Message) ProtoMessage() {}
|
||||||
|
|
||||||
|
func (m *Message) Reset() {
|
||||||
|
*m = Message{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Message) String() string {
|
||||||
|
return string(m.data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Message) Marshal() ([]byte, error) {
|
||||||
|
return m.data, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Message) Unmarshal(data []byte) error {
|
||||||
|
m.data = data
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewMessage(data []byte) *Message {
|
||||||
|
return &Message{data}
|
||||||
|
}
|
335
api/proto/api.pb.go
Normal file
335
api/proto/api.pb.go
Normal file
@@ -0,0 +1,335 @@
|
|||||||
|
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||||
|
// source: api/proto/api.proto
|
||||||
|
|
||||||
|
package go_api
|
||||||
|
|
||||||
|
import (
|
||||||
|
fmt "fmt"
|
||||||
|
proto "github.com/golang/protobuf/proto"
|
||||||
|
math "math"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Reference imports to suppress errors if they are not otherwise used.
|
||||||
|
var _ = proto.Marshal
|
||||||
|
var _ = fmt.Errorf
|
||||||
|
var _ = math.Inf
|
||||||
|
|
||||||
|
// This is a compile-time assertion to ensure that this generated file
|
||||||
|
// is compatible with the proto package it is being compiled against.
|
||||||
|
// A compilation error at this line likely means your copy of the
|
||||||
|
// proto package needs to be updated.
|
||||||
|
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||||
|
|
||||||
|
type Pair struct {
|
||||||
|
Key string `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"`
|
||||||
|
Values []string `protobuf:"bytes,2,rep,name=values,proto3" json:"values,omitempty"`
|
||||||
|
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||||
|
XXX_unrecognized []byte `json:"-"`
|
||||||
|
XXX_sizecache int32 `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Pair) Reset() { *m = Pair{} }
|
||||||
|
func (m *Pair) String() string { return proto.CompactTextString(m) }
|
||||||
|
func (*Pair) ProtoMessage() {}
|
||||||
|
func (*Pair) Descriptor() ([]byte, []int) {
|
||||||
|
return fileDescriptor_2df576b66d12087a, []int{0}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Pair) XXX_Unmarshal(b []byte) error {
|
||||||
|
return xxx_messageInfo_Pair.Unmarshal(m, b)
|
||||||
|
}
|
||||||
|
func (m *Pair) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||||
|
return xxx_messageInfo_Pair.Marshal(b, m, deterministic)
|
||||||
|
}
|
||||||
|
func (m *Pair) XXX_Merge(src proto.Message) {
|
||||||
|
xxx_messageInfo_Pair.Merge(m, src)
|
||||||
|
}
|
||||||
|
func (m *Pair) XXX_Size() int {
|
||||||
|
return xxx_messageInfo_Pair.Size(m)
|
||||||
|
}
|
||||||
|
func (m *Pair) XXX_DiscardUnknown() {
|
||||||
|
xxx_messageInfo_Pair.DiscardUnknown(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
var xxx_messageInfo_Pair proto.InternalMessageInfo
|
||||||
|
|
||||||
|
func (m *Pair) GetKey() string {
|
||||||
|
if m != nil {
|
||||||
|
return m.Key
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Pair) GetValues() []string {
|
||||||
|
if m != nil {
|
||||||
|
return m.Values
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// A HTTP request as RPC
|
||||||
|
// Forward by the api handler
|
||||||
|
type Request struct {
|
||||||
|
Method string `protobuf:"bytes,1,opt,name=method,proto3" json:"method,omitempty"`
|
||||||
|
Path string `protobuf:"bytes,2,opt,name=path,proto3" json:"path,omitempty"`
|
||||||
|
Header map[string]*Pair `protobuf:"bytes,3,rep,name=header,proto3" json:"header,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
|
||||||
|
Get map[string]*Pair `protobuf:"bytes,4,rep,name=get,proto3" json:"get,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
|
||||||
|
Post map[string]*Pair `protobuf:"bytes,5,rep,name=post,proto3" json:"post,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
|
||||||
|
Body string `protobuf:"bytes,6,opt,name=body,proto3" json:"body,omitempty"`
|
||||||
|
Url string `protobuf:"bytes,7,opt,name=url,proto3" json:"url,omitempty"`
|
||||||
|
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||||
|
XXX_unrecognized []byte `json:"-"`
|
||||||
|
XXX_sizecache int32 `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Request) Reset() { *m = Request{} }
|
||||||
|
func (m *Request) String() string { return proto.CompactTextString(m) }
|
||||||
|
func (*Request) ProtoMessage() {}
|
||||||
|
func (*Request) Descriptor() ([]byte, []int) {
|
||||||
|
return fileDescriptor_2df576b66d12087a, []int{1}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Request) XXX_Unmarshal(b []byte) error {
|
||||||
|
return xxx_messageInfo_Request.Unmarshal(m, b)
|
||||||
|
}
|
||||||
|
func (m *Request) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||||
|
return xxx_messageInfo_Request.Marshal(b, m, deterministic)
|
||||||
|
}
|
||||||
|
func (m *Request) XXX_Merge(src proto.Message) {
|
||||||
|
xxx_messageInfo_Request.Merge(m, src)
|
||||||
|
}
|
||||||
|
func (m *Request) XXX_Size() int {
|
||||||
|
return xxx_messageInfo_Request.Size(m)
|
||||||
|
}
|
||||||
|
func (m *Request) XXX_DiscardUnknown() {
|
||||||
|
xxx_messageInfo_Request.DiscardUnknown(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
var xxx_messageInfo_Request proto.InternalMessageInfo
|
||||||
|
|
||||||
|
func (m *Request) GetMethod() string {
|
||||||
|
if m != nil {
|
||||||
|
return m.Method
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Request) GetPath() string {
|
||||||
|
if m != nil {
|
||||||
|
return m.Path
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Request) GetHeader() map[string]*Pair {
|
||||||
|
if m != nil {
|
||||||
|
return m.Header
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Request) GetGet() map[string]*Pair {
|
||||||
|
if m != nil {
|
||||||
|
return m.Get
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Request) GetPost() map[string]*Pair {
|
||||||
|
if m != nil {
|
||||||
|
return m.Post
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Request) GetBody() string {
|
||||||
|
if m != nil {
|
||||||
|
return m.Body
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Request) GetUrl() string {
|
||||||
|
if m != nil {
|
||||||
|
return m.Url
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// A HTTP response as RPC
|
||||||
|
// Expected response for the api handler
|
||||||
|
type Response struct {
|
||||||
|
StatusCode int32 `protobuf:"varint,1,opt,name=statusCode,proto3" json:"statusCode,omitempty"`
|
||||||
|
Header map[string]*Pair `protobuf:"bytes,2,rep,name=header,proto3" json:"header,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
|
||||||
|
Body string `protobuf:"bytes,3,opt,name=body,proto3" json:"body,omitempty"`
|
||||||
|
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||||
|
XXX_unrecognized []byte `json:"-"`
|
||||||
|
XXX_sizecache int32 `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Response) Reset() { *m = Response{} }
|
||||||
|
func (m *Response) String() string { return proto.CompactTextString(m) }
|
||||||
|
func (*Response) ProtoMessage() {}
|
||||||
|
func (*Response) Descriptor() ([]byte, []int) {
|
||||||
|
return fileDescriptor_2df576b66d12087a, []int{2}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Response) XXX_Unmarshal(b []byte) error {
|
||||||
|
return xxx_messageInfo_Response.Unmarshal(m, b)
|
||||||
|
}
|
||||||
|
func (m *Response) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||||
|
return xxx_messageInfo_Response.Marshal(b, m, deterministic)
|
||||||
|
}
|
||||||
|
func (m *Response) XXX_Merge(src proto.Message) {
|
||||||
|
xxx_messageInfo_Response.Merge(m, src)
|
||||||
|
}
|
||||||
|
func (m *Response) XXX_Size() int {
|
||||||
|
return xxx_messageInfo_Response.Size(m)
|
||||||
|
}
|
||||||
|
func (m *Response) XXX_DiscardUnknown() {
|
||||||
|
xxx_messageInfo_Response.DiscardUnknown(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
var xxx_messageInfo_Response proto.InternalMessageInfo
|
||||||
|
|
||||||
|
func (m *Response) GetStatusCode() int32 {
|
||||||
|
if m != nil {
|
||||||
|
return m.StatusCode
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Response) GetHeader() map[string]*Pair {
|
||||||
|
if m != nil {
|
||||||
|
return m.Header
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Response) GetBody() string {
|
||||||
|
if m != nil {
|
||||||
|
return m.Body
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// A HTTP event as RPC
|
||||||
|
// Forwarded by the event handler
|
||||||
|
type Event struct {
|
||||||
|
// e.g login
|
||||||
|
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
|
||||||
|
// uuid
|
||||||
|
Id string `protobuf:"bytes,2,opt,name=id,proto3" json:"id,omitempty"`
|
||||||
|
// unix timestamp of event
|
||||||
|
Timestamp int64 `protobuf:"varint,3,opt,name=timestamp,proto3" json:"timestamp,omitempty"`
|
||||||
|
// event headers
|
||||||
|
Header map[string]*Pair `protobuf:"bytes,4,rep,name=header,proto3" json:"header,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
|
||||||
|
// the event data
|
||||||
|
Data string `protobuf:"bytes,5,opt,name=data,proto3" json:"data,omitempty"`
|
||||||
|
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||||
|
XXX_unrecognized []byte `json:"-"`
|
||||||
|
XXX_sizecache int32 `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Event) Reset() { *m = Event{} }
|
||||||
|
func (m *Event) String() string { return proto.CompactTextString(m) }
|
||||||
|
func (*Event) ProtoMessage() {}
|
||||||
|
func (*Event) Descriptor() ([]byte, []int) {
|
||||||
|
return fileDescriptor_2df576b66d12087a, []int{3}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Event) XXX_Unmarshal(b []byte) error {
|
||||||
|
return xxx_messageInfo_Event.Unmarshal(m, b)
|
||||||
|
}
|
||||||
|
func (m *Event) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||||
|
return xxx_messageInfo_Event.Marshal(b, m, deterministic)
|
||||||
|
}
|
||||||
|
func (m *Event) XXX_Merge(src proto.Message) {
|
||||||
|
xxx_messageInfo_Event.Merge(m, src)
|
||||||
|
}
|
||||||
|
func (m *Event) XXX_Size() int {
|
||||||
|
return xxx_messageInfo_Event.Size(m)
|
||||||
|
}
|
||||||
|
func (m *Event) XXX_DiscardUnknown() {
|
||||||
|
xxx_messageInfo_Event.DiscardUnknown(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
var xxx_messageInfo_Event proto.InternalMessageInfo
|
||||||
|
|
||||||
|
func (m *Event) GetName() string {
|
||||||
|
if m != nil {
|
||||||
|
return m.Name
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Event) GetId() string {
|
||||||
|
if m != nil {
|
||||||
|
return m.Id
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Event) GetTimestamp() int64 {
|
||||||
|
if m != nil {
|
||||||
|
return m.Timestamp
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Event) GetHeader() map[string]*Pair {
|
||||||
|
if m != nil {
|
||||||
|
return m.Header
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Event) GetData() string {
|
||||||
|
if m != nil {
|
||||||
|
return m.Data
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
proto.RegisterType((*Pair)(nil), "go.api.Pair")
|
||||||
|
proto.RegisterType((*Request)(nil), "go.api.Request")
|
||||||
|
proto.RegisterMapType((map[string]*Pair)(nil), "go.api.Request.GetEntry")
|
||||||
|
proto.RegisterMapType((map[string]*Pair)(nil), "go.api.Request.HeaderEntry")
|
||||||
|
proto.RegisterMapType((map[string]*Pair)(nil), "go.api.Request.PostEntry")
|
||||||
|
proto.RegisterType((*Response)(nil), "go.api.Response")
|
||||||
|
proto.RegisterMapType((map[string]*Pair)(nil), "go.api.Response.HeaderEntry")
|
||||||
|
proto.RegisterType((*Event)(nil), "go.api.Event")
|
||||||
|
proto.RegisterMapType((map[string]*Pair)(nil), "go.api.Event.HeaderEntry")
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() { proto.RegisterFile("api/proto/api.proto", fileDescriptor_2df576b66d12087a) }
|
||||||
|
|
||||||
|
var fileDescriptor_2df576b66d12087a = []byte{
|
||||||
|
// 393 bytes of a gzipped FileDescriptorProto
|
||||||
|
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x53, 0xcd, 0xce, 0xd3, 0x30,
|
||||||
|
0x10, 0x54, 0xe2, 0x24, 0x6d, 0xb6, 0x08, 0x21, 0x23, 0x21, 0x53, 0x2a, 0x54, 0xe5, 0x54, 0x21,
|
||||||
|
0x91, 0x42, 0xcb, 0x01, 0x71, 0x85, 0xaa, 0x1c, 0x2b, 0xbf, 0x81, 0xab, 0x58, 0x6d, 0x44, 0x13,
|
||||||
|
0x9b, 0xd8, 0xa9, 0xd4, 0x87, 0xe3, 0xc0, 0x63, 0xf0, 0x36, 0xc8, 0x1b, 0xf7, 0xe7, 0xab, 0xfa,
|
||||||
|
0x5d, 0xbe, 0xaf, 0xb7, 0x89, 0x3d, 0x3b, 0x3b, 0x3b, 0xeb, 0xc0, 0x6b, 0xa1, 0xcb, 0xa9, 0x6e,
|
||||||
|
0x94, 0x55, 0x53, 0xa1, 0xcb, 0x1c, 0x11, 0x4d, 0x36, 0x2a, 0x17, 0xba, 0xcc, 0x3e, 0x41, 0xb4,
|
||||||
|
0x12, 0x65, 0x43, 0x5f, 0x01, 0xf9, 0x25, 0x0f, 0x2c, 0x18, 0x07, 0x93, 0x94, 0x3b, 0x48, 0xdf,
|
||||||
|
0x40, 0xb2, 0x17, 0xbb, 0x56, 0x1a, 0x16, 0x8e, 0xc9, 0x24, 0xe5, 0xfe, 0x2b, 0xfb, 0x4b, 0xa0,
|
||||||
|
0xc7, 0xe5, 0xef, 0x56, 0x1a, 0xeb, 0x38, 0x95, 0xb4, 0x5b, 0x55, 0xf8, 0x42, 0xff, 0x45, 0x29,
|
||||||
|
0x44, 0x5a, 0xd8, 0x2d, 0x0b, 0xf1, 0x14, 0x31, 0x9d, 0x43, 0xb2, 0x95, 0xa2, 0x90, 0x0d, 0x23,
|
||||||
|
0x63, 0x32, 0x19, 0xcc, 0xde, 0xe5, 0x9d, 0x85, 0xdc, 0x8b, 0xe5, 0x3f, 0xf1, 0x76, 0x51, 0xdb,
|
||||||
|
0xe6, 0xc0, 0x3d, 0x95, 0x7e, 0x00, 0xb2, 0x91, 0x96, 0x45, 0x58, 0xc1, 0xae, 0x2b, 0x96, 0xd2,
|
||||||
|
0x76, 0x74, 0x47, 0xa2, 0x1f, 0x21, 0xd2, 0xca, 0x58, 0x16, 0x23, 0xf9, 0xed, 0x35, 0x79, 0xa5,
|
||||||
|
0x8c, 0x67, 0x23, 0xcd, 0x79, 0x5c, 0xab, 0xe2, 0xc0, 0x92, 0xce, 0xa3, 0xc3, 0x2e, 0x85, 0xb6,
|
||||||
|
0xd9, 0xb1, 0x5e, 0x97, 0x42, 0xdb, 0xec, 0x86, 0x4b, 0x18, 0x5c, 0xf8, 0xba, 0x11, 0x53, 0x06,
|
||||||
|
0x31, 0x06, 0x83, 0xb3, 0x0e, 0x66, 0x2f, 0x8e, 0x6d, 0x5d, 0xaa, 0xbc, 0xbb, 0xfa, 0x16, 0x7e,
|
||||||
|
0x0d, 0x86, 0x3f, 0xa0, 0x7f, 0xb4, 0xfb, 0x0c, 0x95, 0x05, 0xa4, 0xa7, 0x39, 0x9e, 0x2e, 0x93,
|
||||||
|
0xfd, 0x09, 0xa0, 0xcf, 0xa5, 0xd1, 0xaa, 0x36, 0x92, 0xbe, 0x07, 0x30, 0x56, 0xd8, 0xd6, 0x7c,
|
||||||
|
0x57, 0x85, 0x44, 0xb5, 0x98, 0x5f, 0x9c, 0xd0, 0x2f, 0xa7, 0xc5, 0x85, 0x98, 0xec, 0xe8, 0x9c,
|
||||||
|
0x6c, 0xa7, 0x70, 0x73, 0x73, 0xc7, 0x78, 0xc9, 0x39, 0xde, 0xbb, 0x85, 0x99, 0xfd, 0x0b, 0x20,
|
||||||
|
0x5e, 0xec, 0x65, 0x8d, 0x5b, 0xac, 0x45, 0x25, 0xbd, 0x08, 0x62, 0xfa, 0x12, 0xc2, 0xb2, 0xf0,
|
||||||
|
0x6f, 0x2f, 0x2c, 0x0b, 0x3a, 0x82, 0xd4, 0x96, 0x95, 0x34, 0x56, 0x54, 0x1a, 0xfd, 0x10, 0x7e,
|
||||||
|
0x3e, 0xa0, 0x9f, 0x4f, 0xe3, 0x45, 0x0f, 0x1f, 0x0e, 0x36, 0x78, 0x6c, 0xb6, 0x42, 0x58, 0xc1,
|
||||||
|
0xe2, 0xae, 0xa9, 0xc3, 0x77, 0x9b, 0x6d, 0x9d, 0xe0, 0x0f, 0x3a, 0xff, 0x1f, 0x00, 0x00, 0xff,
|
||||||
|
0xff, 0xd4, 0x6d, 0x70, 0x51, 0xb7, 0x03, 0x00, 0x00,
|
||||||
|
}
|
21
api/proto/api.pb.micro.go
Normal file
21
api/proto/api.pb.micro.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
// Code generated by protoc-gen-micro. DO NOT EDIT.
|
||||||
|
// source: api/proto/api.proto
|
||||||
|
|
||||||
|
package go_api
|
||||||
|
|
||||||
|
import (
|
||||||
|
fmt "fmt"
|
||||||
|
proto "github.com/golang/protobuf/proto"
|
||||||
|
math "math"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Reference imports to suppress errors if they are not otherwise used.
|
||||||
|
var _ = proto.Marshal
|
||||||
|
var _ = fmt.Errorf
|
||||||
|
var _ = math.Inf
|
||||||
|
|
||||||
|
// This is a compile-time assertion to ensure that this generated file
|
||||||
|
// is compatible with the proto package it is being compiled against.
|
||||||
|
// A compilation error at this line likely means your copy of the
|
||||||
|
// proto package needs to be updated.
|
||||||
|
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
43
api/proto/api.proto
Normal file
43
api/proto/api.proto
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
package go.api;
|
||||||
|
|
||||||
|
message Pair {
|
||||||
|
string key = 1;
|
||||||
|
repeated string values = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
// A HTTP request as RPC
|
||||||
|
// Forward by the api handler
|
||||||
|
message Request {
|
||||||
|
string method = 1;
|
||||||
|
string path = 2;
|
||||||
|
map<string, Pair> header = 3;
|
||||||
|
map<string, Pair> get = 4;
|
||||||
|
map<string, Pair> post = 5;
|
||||||
|
string body = 6; // raw request body; if not application/x-www-form-urlencoded
|
||||||
|
string url = 7;
|
||||||
|
}
|
||||||
|
|
||||||
|
// A HTTP response as RPC
|
||||||
|
// Expected response for the api handler
|
||||||
|
message Response {
|
||||||
|
int32 statusCode = 1;
|
||||||
|
map<string, Pair> header = 2;
|
||||||
|
string body = 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
// A HTTP event as RPC
|
||||||
|
// Forwarded by the event handler
|
||||||
|
message Event {
|
||||||
|
// e.g login
|
||||||
|
string name = 1;
|
||||||
|
// uuid
|
||||||
|
string id = 2;
|
||||||
|
// unix timestamp of event
|
||||||
|
int64 timestamp = 3;
|
||||||
|
// event headers
|
||||||
|
map<string, Pair> header = 4;
|
||||||
|
// the event data
|
||||||
|
string data = 5;
|
||||||
|
}
|
38
api/resolver/grpc/grpc.go
Normal file
38
api/resolver/grpc/grpc.go
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
// Package grpc resolves a grpc service like /greeter.Say/Hello to greeter service
|
||||||
|
package grpc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/micro/go-micro/v2/api/resolver"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Resolver struct{}
|
||||||
|
|
||||||
|
func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) {
|
||||||
|
// /foo.Bar/Service
|
||||||
|
if req.URL.Path == "/" {
|
||||||
|
return nil, errors.New("unknown name")
|
||||||
|
}
|
||||||
|
// [foo.Bar, Service]
|
||||||
|
parts := strings.Split(req.URL.Path[1:], "/")
|
||||||
|
// [foo, Bar]
|
||||||
|
name := strings.Split(parts[0], ".")
|
||||||
|
// foo
|
||||||
|
return &resolver.Endpoint{
|
||||||
|
Name: strings.Join(name[:len(name)-1], "."),
|
||||||
|
Host: req.Host,
|
||||||
|
Method: req.Method,
|
||||||
|
Path: req.URL.Path,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Resolver) String() string {
|
||||||
|
return "grpc"
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewResolver(opts ...resolver.Option) resolver.Resolver {
|
||||||
|
return &Resolver{}
|
||||||
|
}
|
29
api/resolver/host/host.go
Normal file
29
api/resolver/host/host.go
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
// Package host resolves using http host
|
||||||
|
package host
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/micro/go-micro/v2/api/resolver"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Resolver struct {
|
||||||
|
opts resolver.Options
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) {
|
||||||
|
return &resolver.Endpoint{
|
||||||
|
Name: req.Host,
|
||||||
|
Host: req.Host,
|
||||||
|
Method: req.Method,
|
||||||
|
Path: req.URL.Path,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Resolver) String() string {
|
||||||
|
return "host"
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewResolver(opts ...resolver.Option) resolver.Resolver {
|
||||||
|
return &Resolver{opts: resolver.NewOptions(opts...)}
|
||||||
|
}
|
33
api/resolver/options.go
Normal file
33
api/resolver/options.go
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package resolver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewOptions returns new initialised options
|
||||||
|
func NewOptions(opts ...Option) Options {
|
||||||
|
var options Options
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&options)
|
||||||
|
}
|
||||||
|
|
||||||
|
if options.Namespace == nil {
|
||||||
|
options.Namespace = StaticNamespace("go.micro")
|
||||||
|
}
|
||||||
|
|
||||||
|
return options
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithHandler sets the handler being used
|
||||||
|
func WithHandler(h string) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.Handler = h
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithNamespace sets the function which determines the namespace for a request
|
||||||
|
func WithNamespace(n func(*http.Request) string) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.Namespace = n
|
||||||
|
}
|
||||||
|
}
|
37
api/resolver/path/path.go
Normal file
37
api/resolver/path/path.go
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
// Package path resolves using http path
|
||||||
|
package path
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/micro/go-micro/v2/api/resolver"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Resolver struct {
|
||||||
|
opts resolver.Options
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) {
|
||||||
|
if req.URL.Path == "/" {
|
||||||
|
return nil, resolver.ErrNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
parts := strings.Split(req.URL.Path[1:], "/")
|
||||||
|
ns := r.opts.Namespace(req)
|
||||||
|
|
||||||
|
return &resolver.Endpoint{
|
||||||
|
Name: ns + "." + parts[0],
|
||||||
|
Host: req.Host,
|
||||||
|
Method: req.Method,
|
||||||
|
Path: req.URL.Path,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Resolver) String() string {
|
||||||
|
return "path"
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewResolver(opts ...resolver.Option) resolver.Resolver {
|
||||||
|
return &Resolver{opts: resolver.NewOptions(opts...)}
|
||||||
|
}
|
44
api/resolver/resolver.go
Normal file
44
api/resolver/resolver.go
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
// Package resolver resolves a http request to an endpoint
|
||||||
|
package resolver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrNotFound = errors.New("not found")
|
||||||
|
ErrInvalidPath = errors.New("invalid path")
|
||||||
|
)
|
||||||
|
|
||||||
|
// Resolver resolves requests to endpoints
|
||||||
|
type Resolver interface {
|
||||||
|
Resolve(r *http.Request) (*Endpoint, error)
|
||||||
|
String() string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Endpoint is the endpoint for a http request
|
||||||
|
type Endpoint struct {
|
||||||
|
// e.g greeter
|
||||||
|
Name string
|
||||||
|
// HTTP Host e.g example.com
|
||||||
|
Host string
|
||||||
|
// HTTP Methods e.g GET, POST
|
||||||
|
Method string
|
||||||
|
// HTTP Path e.g /greeter.
|
||||||
|
Path string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Options struct {
|
||||||
|
Handler string
|
||||||
|
Namespace func(*http.Request) string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Option func(o *Options)
|
||||||
|
|
||||||
|
// StaticNamespace returns the same namespace for each request
|
||||||
|
func StaticNamespace(ns string) func(*http.Request) string {
|
||||||
|
return func(*http.Request) string {
|
||||||
|
return ns
|
||||||
|
}
|
||||||
|
}
|
69
api/resolver/vpath/vpath.go
Normal file
69
api/resolver/vpath/vpath.go
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
// Package vpath resolves using http path and recognised versioned urls
|
||||||
|
package vpath
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/micro/go-micro/v2/api/resolver"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewResolver(opts ...resolver.Option) resolver.Resolver {
|
||||||
|
return &Resolver{opts: resolver.NewOptions(opts...)}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Resolver struct {
|
||||||
|
opts resolver.Options
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
re = regexp.MustCompile("^v[0-9]+$")
|
||||||
|
)
|
||||||
|
|
||||||
|
func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) {
|
||||||
|
if req.URL.Path == "/" {
|
||||||
|
return nil, errors.New("unknown name")
|
||||||
|
}
|
||||||
|
|
||||||
|
parts := strings.Split(req.URL.Path[1:], "/")
|
||||||
|
if len(parts) == 1 {
|
||||||
|
return &resolver.Endpoint{
|
||||||
|
Name: r.withNamespace(req, parts...),
|
||||||
|
Host: req.Host,
|
||||||
|
Method: req.Method,
|
||||||
|
Path: req.URL.Path,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// /v1/foo
|
||||||
|
if re.MatchString(parts[0]) {
|
||||||
|
return &resolver.Endpoint{
|
||||||
|
Name: r.withNamespace(req, parts[0:2]...),
|
||||||
|
Host: req.Host,
|
||||||
|
Method: req.Method,
|
||||||
|
Path: req.URL.Path,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return &resolver.Endpoint{
|
||||||
|
Name: r.withNamespace(req, parts[0]),
|
||||||
|
Host: req.Host,
|
||||||
|
Method: req.Method,
|
||||||
|
Path: req.URL.Path,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Resolver) String() string {
|
||||||
|
return "path"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Resolver) withNamespace(req *http.Request, parts ...string) string {
|
||||||
|
ns := r.opts.Namespace(req)
|
||||||
|
if len(ns) == 0 {
|
||||||
|
return strings.Join(parts, ".")
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Join(append([]string{ns}, parts...), ".")
|
||||||
|
}
|
52
api/router/options.go
Normal file
52
api/router/options.go
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package router
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/micro/go-micro/v2/api/resolver"
|
||||||
|
"github.com/micro/go-micro/v2/api/resolver/vpath"
|
||||||
|
"github.com/micro/go-micro/v2/registry"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Options struct {
|
||||||
|
Handler string
|
||||||
|
Registry registry.Registry
|
||||||
|
Resolver resolver.Resolver
|
||||||
|
}
|
||||||
|
|
||||||
|
type Option func(o *Options)
|
||||||
|
|
||||||
|
func NewOptions(opts ...Option) Options {
|
||||||
|
options := Options{
|
||||||
|
Handler: "meta",
|
||||||
|
Registry: registry.DefaultRegistry,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&options)
|
||||||
|
}
|
||||||
|
|
||||||
|
if options.Resolver == nil {
|
||||||
|
options.Resolver = vpath.NewResolver(
|
||||||
|
resolver.WithHandler(options.Handler),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return options
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithHandler(h string) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.Handler = h
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithRegistry(r registry.Registry) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.Registry = r
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithResolver(r resolver.Resolver) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.Resolver = r
|
||||||
|
}
|
||||||
|
}
|
498
api/router/registry/registry.go
Normal file
498
api/router/registry/registry.go
Normal file
@@ -0,0 +1,498 @@
|
|||||||
|
// Package registry provides a dynamic api service router
|
||||||
|
package registry
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/micro/go-micro/v2/api"
|
||||||
|
"github.com/micro/go-micro/v2/api/router"
|
||||||
|
"github.com/micro/go-micro/v2/api/router/util"
|
||||||
|
"github.com/micro/go-micro/v2/logger"
|
||||||
|
"github.com/micro/go-micro/v2/metadata"
|
||||||
|
"github.com/micro/go-micro/v2/registry"
|
||||||
|
"github.com/micro/go-micro/v2/registry/cache"
|
||||||
|
)
|
||||||
|
|
||||||
|
// endpoint struct, that holds compiled pcre
|
||||||
|
type endpoint struct {
|
||||||
|
hostregs []*regexp.Regexp
|
||||||
|
pathregs []util.Pattern
|
||||||
|
pcreregs []*regexp.Regexp
|
||||||
|
}
|
||||||
|
|
||||||
|
// router is the default router
|
||||||
|
type registryRouter struct {
|
||||||
|
exit chan bool
|
||||||
|
opts router.Options
|
||||||
|
|
||||||
|
// registry cache
|
||||||
|
rc cache.Cache
|
||||||
|
|
||||||
|
sync.RWMutex
|
||||||
|
eps map[string]*api.Service
|
||||||
|
// compiled regexp for host and path
|
||||||
|
ceps map[string]*endpoint
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *registryRouter) isClosed() bool {
|
||||||
|
select {
|
||||||
|
case <-r.exit:
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// refresh list of api services
|
||||||
|
func (r *registryRouter) refresh() {
|
||||||
|
var attempts int
|
||||||
|
|
||||||
|
for {
|
||||||
|
services, err := r.opts.Registry.ListServices()
|
||||||
|
if err != nil {
|
||||||
|
attempts++
|
||||||
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
||||||
|
logger.Errorf("unable to list services: %v", err)
|
||||||
|
}
|
||||||
|
time.Sleep(time.Duration(attempts) * time.Second)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
attempts = 0
|
||||||
|
|
||||||
|
// for each service, get service and store endpoints
|
||||||
|
for _, s := range services {
|
||||||
|
service, err := r.rc.GetService(s.Name)
|
||||||
|
if err != nil {
|
||||||
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
||||||
|
logger.Errorf("unable to get service: %v", err)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
r.store(service)
|
||||||
|
}
|
||||||
|
|
||||||
|
// refresh list in 10 minutes... cruft
|
||||||
|
// use registry watching
|
||||||
|
select {
|
||||||
|
case <-time.After(time.Minute * 10):
|
||||||
|
case <-r.exit:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// process watch event
|
||||||
|
func (r *registryRouter) process(res *registry.Result) {
|
||||||
|
// skip these things
|
||||||
|
if res == nil || res.Service == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// get entry from cache
|
||||||
|
service, err := r.rc.GetService(res.Service.Name)
|
||||||
|
if err != nil {
|
||||||
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
||||||
|
logger.Errorf("unable to get service: %v", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// update our local endpoints
|
||||||
|
r.store(service)
|
||||||
|
}
|
||||||
|
|
||||||
|
// store local endpoint cache
|
||||||
|
func (r *registryRouter) store(services []*registry.Service) {
|
||||||
|
// endpoints
|
||||||
|
eps := map[string]*api.Service{}
|
||||||
|
|
||||||
|
// services
|
||||||
|
names := map[string]bool{}
|
||||||
|
|
||||||
|
// create a new endpoint mapping
|
||||||
|
for _, service := range services {
|
||||||
|
// set names we need later
|
||||||
|
names[service.Name] = true
|
||||||
|
|
||||||
|
// map per endpoint
|
||||||
|
for _, sep := range service.Endpoints {
|
||||||
|
// create a key service:endpoint_name
|
||||||
|
key := fmt.Sprintf("%s.%s", service.Name, sep.Name)
|
||||||
|
// decode endpoint
|
||||||
|
end := api.Decode(sep.Metadata)
|
||||||
|
|
||||||
|
// if we got nothing skip
|
||||||
|
if err := api.Validate(end); err != nil {
|
||||||
|
if logger.V(logger.TraceLevel, logger.DefaultLogger) {
|
||||||
|
logger.Tracef("endpoint validation failed: %v", err)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// try get endpoint
|
||||||
|
ep, ok := eps[key]
|
||||||
|
if !ok {
|
||||||
|
ep = &api.Service{Name: service.Name}
|
||||||
|
}
|
||||||
|
|
||||||
|
// overwrite the endpoint
|
||||||
|
ep.Endpoint = end
|
||||||
|
// append services
|
||||||
|
ep.Services = append(ep.Services, service)
|
||||||
|
// store it
|
||||||
|
eps[key] = ep
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
r.Lock()
|
||||||
|
defer r.Unlock()
|
||||||
|
|
||||||
|
// delete any existing eps for services we know
|
||||||
|
for key, service := range r.eps {
|
||||||
|
// skip what we don't care about
|
||||||
|
if !names[service.Name] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// ok we know this thing
|
||||||
|
// delete delete delete
|
||||||
|
delete(r.eps, key)
|
||||||
|
}
|
||||||
|
|
||||||
|
// now set the eps we have
|
||||||
|
for name, ep := range eps {
|
||||||
|
r.eps[name] = ep
|
||||||
|
cep := &endpoint{}
|
||||||
|
|
||||||
|
for _, h := range ep.Endpoint.Host {
|
||||||
|
if h == "" || h == "*" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
hostreg, err := regexp.CompilePOSIX(h)
|
||||||
|
if err != nil {
|
||||||
|
if logger.V(logger.TraceLevel, logger.DefaultLogger) {
|
||||||
|
logger.Tracef("endpoint have invalid host regexp: %v", err)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
cep.hostregs = append(cep.hostregs, hostreg)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, p := range ep.Endpoint.Path {
|
||||||
|
var pcreok bool
|
||||||
|
|
||||||
|
if p[0] == '^' && p[len(p)-1] == '$' {
|
||||||
|
pcrereg, err := regexp.CompilePOSIX(p)
|
||||||
|
if err == nil {
|
||||||
|
cep.pcreregs = append(cep.pcreregs, pcrereg)
|
||||||
|
pcreok = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rule, err := util.Parse(p)
|
||||||
|
if err != nil && !pcreok {
|
||||||
|
if logger.V(logger.TraceLevel, logger.DefaultLogger) {
|
||||||
|
logger.Tracef("endpoint have invalid path pattern: %v", err)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
} else if err != nil && pcreok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
tpl := rule.Compile()
|
||||||
|
pathreg, err := util.NewPattern(tpl.Version, tpl.OpCodes, tpl.Pool, "")
|
||||||
|
if err != nil {
|
||||||
|
if logger.V(logger.TraceLevel, logger.DefaultLogger) {
|
||||||
|
logger.Tracef("endpoint have invalid path pattern: %v", err)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
cep.pathregs = append(cep.pathregs, pathreg)
|
||||||
|
}
|
||||||
|
|
||||||
|
r.ceps[name] = cep
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// watch for endpoint changes
|
||||||
|
func (r *registryRouter) watch() {
|
||||||
|
var attempts int
|
||||||
|
|
||||||
|
for {
|
||||||
|
if r.isClosed() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// watch for changes
|
||||||
|
w, err := r.opts.Registry.Watch()
|
||||||
|
if err != nil {
|
||||||
|
attempts++
|
||||||
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
||||||
|
logger.Errorf("error watching endpoints: %v", err)
|
||||||
|
}
|
||||||
|
time.Sleep(time.Duration(attempts) * time.Second)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
ch := make(chan bool)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
select {
|
||||||
|
case <-ch:
|
||||||
|
w.Stop()
|
||||||
|
case <-r.exit:
|
||||||
|
w.Stop()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// reset if we get here
|
||||||
|
attempts = 0
|
||||||
|
|
||||||
|
for {
|
||||||
|
// process next event
|
||||||
|
res, err := w.Next()
|
||||||
|
if err != nil {
|
||||||
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
||||||
|
logger.Errorf("error getting next endoint: %v", err)
|
||||||
|
}
|
||||||
|
close(ch)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
r.process(res)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *registryRouter) Options() router.Options {
|
||||||
|
return r.opts
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *registryRouter) Close() error {
|
||||||
|
select {
|
||||||
|
case <-r.exit:
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
close(r.exit)
|
||||||
|
r.rc.Stop()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *registryRouter) Register(ep *api.Endpoint) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *registryRouter) Deregister(ep *api.Endpoint) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *registryRouter) Endpoint(req *http.Request) (*api.Service, error) {
|
||||||
|
if r.isClosed() {
|
||||||
|
return nil, errors.New("router closed")
|
||||||
|
}
|
||||||
|
|
||||||
|
r.RLock()
|
||||||
|
defer r.RUnlock()
|
||||||
|
|
||||||
|
var idx int
|
||||||
|
if len(req.URL.Path) > 0 && req.URL.Path != "/" {
|
||||||
|
idx = 1
|
||||||
|
}
|
||||||
|
path := strings.Split(req.URL.Path[idx:], "/")
|
||||||
|
|
||||||
|
// use the first match
|
||||||
|
// TODO: weighted matching
|
||||||
|
for n, e := range r.eps {
|
||||||
|
cep, ok := r.ceps[n]
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ep := e.Endpoint
|
||||||
|
var mMatch, hMatch, pMatch bool
|
||||||
|
// 1. try method
|
||||||
|
for _, m := range ep.Method {
|
||||||
|
if m == req.Method {
|
||||||
|
mMatch = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !mMatch {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
||||||
|
logger.Debugf("api method match %s", req.Method)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. try host
|
||||||
|
if len(ep.Host) == 0 {
|
||||||
|
hMatch = true
|
||||||
|
} else {
|
||||||
|
for idx, h := range ep.Host {
|
||||||
|
if h == "" || h == "*" {
|
||||||
|
hMatch = true
|
||||||
|
break
|
||||||
|
} else {
|
||||||
|
if cep.hostregs[idx].MatchString(req.URL.Host) {
|
||||||
|
hMatch = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !hMatch {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
||||||
|
logger.Debugf("api host match %s", req.URL.Host)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. try path via google.api path matching
|
||||||
|
for _, pathreg := range cep.pathregs {
|
||||||
|
matches, err := pathreg.Match(path, "")
|
||||||
|
if err != nil {
|
||||||
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
||||||
|
logger.Debugf("api gpath not match %s != %v", path, pathreg)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
||||||
|
logger.Debugf("api gpath match %s = %v", path, pathreg)
|
||||||
|
}
|
||||||
|
pMatch = true
|
||||||
|
ctx := req.Context()
|
||||||
|
md, ok := metadata.FromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
md = make(metadata.Metadata)
|
||||||
|
}
|
||||||
|
for k, v := range matches {
|
||||||
|
md[fmt.Sprintf("x-api-field-%s", k)] = v
|
||||||
|
}
|
||||||
|
md["x-api-body"] = ep.Body
|
||||||
|
*req = *req.Clone(metadata.NewContext(ctx, md))
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if !pMatch {
|
||||||
|
// 4. try path via pcre path matching
|
||||||
|
for _, pathreg := range cep.pcreregs {
|
||||||
|
if !pathreg.MatchString(req.URL.Path) {
|
||||||
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
||||||
|
logger.Debugf("api pcre path not match %s != %v", path, pathreg)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
||||||
|
logger.Debugf("api pcre path match %s != %v", path, pathreg)
|
||||||
|
}
|
||||||
|
pMatch = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !pMatch {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Percentage traffic
|
||||||
|
// we got here, so its a match
|
||||||
|
return e, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// no match
|
||||||
|
return nil, errors.New("not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *registryRouter) Route(req *http.Request) (*api.Service, error) {
|
||||||
|
if r.isClosed() {
|
||||||
|
return nil, errors.New("router closed")
|
||||||
|
}
|
||||||
|
|
||||||
|
// try get an endpoint
|
||||||
|
ep, err := r.Endpoint(req)
|
||||||
|
if err == nil {
|
||||||
|
return ep, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// error not nil
|
||||||
|
// ignore that shit
|
||||||
|
// TODO: don't ignore that shit
|
||||||
|
|
||||||
|
// get the service name
|
||||||
|
rp, err := r.opts.Resolver.Resolve(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// service name
|
||||||
|
name := rp.Name
|
||||||
|
|
||||||
|
// get service
|
||||||
|
services, err := r.rc.GetService(name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// only use endpoint matching when the meta handler is set aka api.Default
|
||||||
|
switch r.opts.Handler {
|
||||||
|
// rpc handlers
|
||||||
|
case "meta", "api", "rpc":
|
||||||
|
handler := r.opts.Handler
|
||||||
|
|
||||||
|
// set default handler to api
|
||||||
|
if r.opts.Handler == "meta" {
|
||||||
|
handler = "rpc"
|
||||||
|
}
|
||||||
|
|
||||||
|
// construct api service
|
||||||
|
return &api.Service{
|
||||||
|
Name: name,
|
||||||
|
Endpoint: &api.Endpoint{
|
||||||
|
Name: rp.Method,
|
||||||
|
Handler: handler,
|
||||||
|
},
|
||||||
|
Services: services,
|
||||||
|
}, nil
|
||||||
|
// http handler
|
||||||
|
case "http", "proxy", "web":
|
||||||
|
// construct api service
|
||||||
|
return &api.Service{
|
||||||
|
Name: name,
|
||||||
|
Endpoint: &api.Endpoint{
|
||||||
|
Name: req.URL.String(),
|
||||||
|
Handler: r.opts.Handler,
|
||||||
|
Host: []string{req.Host},
|
||||||
|
Method: []string{req.Method},
|
||||||
|
Path: []string{req.URL.Path},
|
||||||
|
},
|
||||||
|
Services: services,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, errors.New("unknown handler")
|
||||||
|
}
|
||||||
|
|
||||||
|
func newRouter(opts ...router.Option) *registryRouter {
|
||||||
|
options := router.NewOptions(opts...)
|
||||||
|
r := ®istryRouter{
|
||||||
|
exit: make(chan bool),
|
||||||
|
opts: options,
|
||||||
|
rc: cache.New(options.Registry),
|
||||||
|
eps: make(map[string]*api.Service),
|
||||||
|
ceps: make(map[string]*endpoint),
|
||||||
|
}
|
||||||
|
go r.watch()
|
||||||
|
go r.refresh()
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewRouter returns the default router
|
||||||
|
func NewRouter(opts ...router.Option) router.Router {
|
||||||
|
return newRouter(opts...)
|
||||||
|
}
|
34
api/router/registry/registry_test.go
Normal file
34
api/router/registry/registry_test.go
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package registry
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/micro/go-micro/v2/registry"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestStoreRegex(t *testing.T) {
|
||||||
|
router := newRouter()
|
||||||
|
router.store([]*registry.Service{
|
||||||
|
{
|
||||||
|
Name: "Foobar",
|
||||||
|
Version: "latest",
|
||||||
|
Endpoints: []*registry.Endpoint{
|
||||||
|
{
|
||||||
|
Name: "foo",
|
||||||
|
Metadata: map[string]string{
|
||||||
|
"endpoint": "FooEndpoint",
|
||||||
|
"description": "Some description",
|
||||||
|
"method": "POST",
|
||||||
|
"path": "^/foo/$",
|
||||||
|
"handler": "rpc",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Metadata: map[string]string{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert.Len(t, router.ceps["Foobar.foo"].pcreregs, 1)
|
||||||
|
}
|
24
api/router/router.go
Normal file
24
api/router/router.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
// Package router provides api service routing
|
||||||
|
package router
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/micro/go-micro/v2/api"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Router is used to determine an endpoint for a request
|
||||||
|
type Router interface {
|
||||||
|
// Returns options
|
||||||
|
Options() Options
|
||||||
|
// Stop the router
|
||||||
|
Close() error
|
||||||
|
// Endpoint returns an api.Service endpoint or an error if it does not exist
|
||||||
|
Endpoint(r *http.Request) (*api.Service, error)
|
||||||
|
// Register endpoint in router
|
||||||
|
Register(ep *api.Endpoint) error
|
||||||
|
// Deregister endpoint from router
|
||||||
|
Deregister(ep *api.Endpoint) error
|
||||||
|
// Route returns an api.Service route
|
||||||
|
Route(r *http.Request) (*api.Service, error)
|
||||||
|
}
|
245
api/router/router_test.go
Normal file
245
api/router/router_test.go
Normal file
@@ -0,0 +1,245 @@
|
|||||||
|
package router_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/micro/go-micro/v2/api"
|
||||||
|
"github.com/micro/go-micro/v2/api/handler"
|
||||||
|
"github.com/micro/go-micro/v2/api/handler/rpc"
|
||||||
|
"github.com/micro/go-micro/v2/api/router"
|
||||||
|
rregistry "github.com/micro/go-micro/v2/api/router/registry"
|
||||||
|
rstatic "github.com/micro/go-micro/v2/api/router/static"
|
||||||
|
"github.com/micro/go-micro/v2/client"
|
||||||
|
gcli "github.com/micro/go-micro/v2/client/grpc"
|
||||||
|
rmemory "github.com/micro/go-micro/v2/registry/memory"
|
||||||
|
"github.com/micro/go-micro/v2/server"
|
||||||
|
gsrv "github.com/micro/go-micro/v2/server/grpc"
|
||||||
|
pb "github.com/micro/go-micro/v2/server/grpc/proto"
|
||||||
|
)
|
||||||
|
|
||||||
|
// server is used to implement helloworld.GreeterServer.
|
||||||
|
type testServer struct {
|
||||||
|
msgCount int
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestHello implements helloworld.GreeterServer
|
||||||
|
func (s *testServer) Call(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
|
||||||
|
rsp.Msg = "Hello " + req.Uuid
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestHello implements helloworld.GreeterServer
|
||||||
|
func (s *testServer) CallPcre(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
|
||||||
|
rsp.Msg = "Hello " + req.Uuid
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestHello implements helloworld.GreeterServer
|
||||||
|
func (s *testServer) CallPcreInvalid(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
|
||||||
|
rsp.Msg = "Hello " + req.Uuid
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func initial(t *testing.T) (server.Server, client.Client) {
|
||||||
|
r := rmemory.NewRegistry()
|
||||||
|
|
||||||
|
// create a new client
|
||||||
|
s := gsrv.NewServer(
|
||||||
|
server.Name("foo"),
|
||||||
|
server.Registry(r),
|
||||||
|
)
|
||||||
|
|
||||||
|
// create a new server
|
||||||
|
c := gcli.NewClient(
|
||||||
|
client.Registry(r),
|
||||||
|
)
|
||||||
|
|
||||||
|
h := &testServer{}
|
||||||
|
pb.RegisterTestHandler(s, h)
|
||||||
|
|
||||||
|
if err := s.Start(); err != nil {
|
||||||
|
t.Fatalf("failed to start: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return s, c
|
||||||
|
}
|
||||||
|
|
||||||
|
func check(t *testing.T, addr string, path string, expected string) {
|
||||||
|
req, err := http.NewRequest("POST", fmt.Sprintf(path, addr), nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to created http.Request: %v", err)
|
||||||
|
}
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
rsp, err := (&http.Client{}).Do(req)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to created http.Request: %v", err)
|
||||||
|
}
|
||||||
|
defer rsp.Body.Close()
|
||||||
|
|
||||||
|
buf, err := ioutil.ReadAll(rsp.Body)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonMsg := expected
|
||||||
|
if string(buf) != jsonMsg {
|
||||||
|
t.Fatalf("invalid message received, parsing error %s != %s", buf, jsonMsg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRouterRegistryPcre(t *testing.T) {
|
||||||
|
s, c := initial(t)
|
||||||
|
defer s.Stop()
|
||||||
|
|
||||||
|
router := rregistry.NewRouter(
|
||||||
|
router.WithHandler(rpc.Handler),
|
||||||
|
router.WithRegistry(s.Options().Registry),
|
||||||
|
)
|
||||||
|
hrpc := rpc.NewHandler(
|
||||||
|
handler.WithClient(c),
|
||||||
|
handler.WithRouter(router),
|
||||||
|
)
|
||||||
|
hsrv := &http.Server{
|
||||||
|
Handler: hrpc,
|
||||||
|
Addr: "127.0.0.1:6543",
|
||||||
|
WriteTimeout: 15 * time.Second,
|
||||||
|
ReadTimeout: 15 * time.Second,
|
||||||
|
IdleTimeout: 20 * time.Second,
|
||||||
|
MaxHeaderBytes: 1024 * 1024 * 1, // 1Mb
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
log.Println(hsrv.ListenAndServe())
|
||||||
|
}()
|
||||||
|
|
||||||
|
defer hsrv.Close()
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
check(t, hsrv.Addr, "http://%s/api/v0/test/call/TEST", `{"msg":"Hello TEST"}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRouterStaticPcre(t *testing.T) {
|
||||||
|
s, c := initial(t)
|
||||||
|
defer s.Stop()
|
||||||
|
|
||||||
|
router := rstatic.NewRouter(
|
||||||
|
router.WithHandler(rpc.Handler),
|
||||||
|
router.WithRegistry(s.Options().Registry),
|
||||||
|
)
|
||||||
|
|
||||||
|
err := router.Register(&api.Endpoint{
|
||||||
|
Name: "foo.Test.Call",
|
||||||
|
Method: []string{"POST"},
|
||||||
|
Path: []string{"^/api/v0/test/call/?$"},
|
||||||
|
Handler: "rpc",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
hrpc := rpc.NewHandler(
|
||||||
|
handler.WithClient(c),
|
||||||
|
handler.WithRouter(router),
|
||||||
|
)
|
||||||
|
hsrv := &http.Server{
|
||||||
|
Handler: hrpc,
|
||||||
|
Addr: "127.0.0.1:6543",
|
||||||
|
WriteTimeout: 15 * time.Second,
|
||||||
|
ReadTimeout: 15 * time.Second,
|
||||||
|
IdleTimeout: 20 * time.Second,
|
||||||
|
MaxHeaderBytes: 1024 * 1024 * 1, // 1Mb
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
log.Println(hsrv.ListenAndServe())
|
||||||
|
}()
|
||||||
|
defer hsrv.Close()
|
||||||
|
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
check(t, hsrv.Addr, "http://%s/api/v0/test/call", `{"msg":"Hello "}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRouterStaticGpath(t *testing.T) {
|
||||||
|
s, c := initial(t)
|
||||||
|
defer s.Stop()
|
||||||
|
|
||||||
|
router := rstatic.NewRouter(
|
||||||
|
router.WithHandler(rpc.Handler),
|
||||||
|
router.WithRegistry(s.Options().Registry),
|
||||||
|
)
|
||||||
|
|
||||||
|
err := router.Register(&api.Endpoint{
|
||||||
|
Name: "foo.Test.Call",
|
||||||
|
Method: []string{"POST"},
|
||||||
|
Path: []string{"/api/v0/test/call/{uuid}"},
|
||||||
|
Handler: "rpc",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
hrpc := rpc.NewHandler(
|
||||||
|
handler.WithClient(c),
|
||||||
|
handler.WithRouter(router),
|
||||||
|
)
|
||||||
|
hsrv := &http.Server{
|
||||||
|
Handler: hrpc,
|
||||||
|
Addr: "127.0.0.1:6543",
|
||||||
|
WriteTimeout: 15 * time.Second,
|
||||||
|
ReadTimeout: 15 * time.Second,
|
||||||
|
IdleTimeout: 20 * time.Second,
|
||||||
|
MaxHeaderBytes: 1024 * 1024 * 1, // 1Mb
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
log.Println(hsrv.ListenAndServe())
|
||||||
|
}()
|
||||||
|
defer hsrv.Close()
|
||||||
|
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
check(t, hsrv.Addr, "http://%s/api/v0/test/call/TEST", `{"msg":"Hello TEST"}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRouterStaticPcreInvalid(t *testing.T) {
|
||||||
|
var ep *api.Endpoint
|
||||||
|
var err error
|
||||||
|
|
||||||
|
s, c := initial(t)
|
||||||
|
defer s.Stop()
|
||||||
|
|
||||||
|
router := rstatic.NewRouter(
|
||||||
|
router.WithHandler(rpc.Handler),
|
||||||
|
router.WithRegistry(s.Options().Registry),
|
||||||
|
)
|
||||||
|
|
||||||
|
ep = &api.Endpoint{
|
||||||
|
Name: "foo.Test.Call",
|
||||||
|
Method: []string{"POST"},
|
||||||
|
Path: []string{"^/api/v0/test/call/?"},
|
||||||
|
Handler: "rpc",
|
||||||
|
}
|
||||||
|
|
||||||
|
err = router.Register(ep)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("invalid endpoint %v", ep)
|
||||||
|
}
|
||||||
|
|
||||||
|
ep = &api.Endpoint{
|
||||||
|
Name: "foo.Test.Call",
|
||||||
|
Method: []string{"POST"},
|
||||||
|
Path: []string{"/api/v0/test/call/?$"},
|
||||||
|
Handler: "rpc",
|
||||||
|
}
|
||||||
|
|
||||||
|
err = router.Register(ep)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("invalid endpoint %v", ep)
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = c
|
||||||
|
}
|
356
api/router/static/static.go
Normal file
356
api/router/static/static.go
Normal file
@@ -0,0 +1,356 @@
|
|||||||
|
package static
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/micro/go-micro/v2/api"
|
||||||
|
"github.com/micro/go-micro/v2/api/router"
|
||||||
|
"github.com/micro/go-micro/v2/api/router/util"
|
||||||
|
"github.com/micro/go-micro/v2/logger"
|
||||||
|
"github.com/micro/go-micro/v2/metadata"
|
||||||
|
"github.com/micro/go-micro/v2/registry"
|
||||||
|
rutil "github.com/micro/go-micro/v2/util/registry"
|
||||||
|
)
|
||||||
|
|
||||||
|
type endpoint struct {
|
||||||
|
apiep *api.Endpoint
|
||||||
|
hostregs []*regexp.Regexp
|
||||||
|
pathregs []util.Pattern
|
||||||
|
pcreregs []*regexp.Regexp
|
||||||
|
}
|
||||||
|
|
||||||
|
// router is the default router
|
||||||
|
type staticRouter struct {
|
||||||
|
exit chan bool
|
||||||
|
opts router.Options
|
||||||
|
sync.RWMutex
|
||||||
|
eps map[string]*endpoint
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *staticRouter) isClosed() bool {
|
||||||
|
select {
|
||||||
|
case <-r.exit:
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
// watch for endpoint changes
|
||||||
|
func (r *staticRouter) watch() {
|
||||||
|
var attempts int
|
||||||
|
|
||||||
|
for {
|
||||||
|
if r.isClosed() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// watch for changes
|
||||||
|
w, err := r.opts.Registry.Watch()
|
||||||
|
if err != nil {
|
||||||
|
attempts++
|
||||||
|
log.Println("Error watching endpoints", err)
|
||||||
|
time.Sleep(time.Duration(attempts) * time.Second)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
ch := make(chan bool)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
select {
|
||||||
|
case <-ch:
|
||||||
|
w.Stop()
|
||||||
|
case <-r.exit:
|
||||||
|
w.Stop()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// reset if we get here
|
||||||
|
attempts = 0
|
||||||
|
|
||||||
|
for {
|
||||||
|
// process next event
|
||||||
|
res, err := w.Next()
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error getting next endpoint", err)
|
||||||
|
close(ch)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
r.process(res)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
func (r *staticRouter) Register(ep *api.Endpoint) error {
|
||||||
|
if err := api.Validate(ep); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var pathregs []util.Pattern
|
||||||
|
var hostregs []*regexp.Regexp
|
||||||
|
var pcreregs []*regexp.Regexp
|
||||||
|
|
||||||
|
for _, h := range ep.Host {
|
||||||
|
if h == "" || h == "*" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
hostreg, err := regexp.CompilePOSIX(h)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
hostregs = append(hostregs, hostreg)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, p := range ep.Path {
|
||||||
|
var pcreok bool
|
||||||
|
|
||||||
|
// pcre only when we have start and end markers
|
||||||
|
if p[0] == '^' && p[len(p)-1] == '$' {
|
||||||
|
pcrereg, err := regexp.CompilePOSIX(p)
|
||||||
|
if err == nil {
|
||||||
|
pcreregs = append(pcreregs, pcrereg)
|
||||||
|
pcreok = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rule, err := util.Parse(p)
|
||||||
|
if err != nil && !pcreok {
|
||||||
|
return err
|
||||||
|
} else if err != nil && pcreok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
tpl := rule.Compile()
|
||||||
|
pathreg, err := util.NewPattern(tpl.Version, tpl.OpCodes, tpl.Pool, "")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
pathregs = append(pathregs, pathreg)
|
||||||
|
}
|
||||||
|
|
||||||
|
r.Lock()
|
||||||
|
r.eps[ep.Name] = &endpoint{
|
||||||
|
apiep: ep,
|
||||||
|
pcreregs: pcreregs,
|
||||||
|
pathregs: pathregs,
|
||||||
|
hostregs: hostregs,
|
||||||
|
}
|
||||||
|
r.Unlock()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *staticRouter) Deregister(ep *api.Endpoint) error {
|
||||||
|
if err := api.Validate(ep); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
r.Lock()
|
||||||
|
delete(r.eps, ep.Name)
|
||||||
|
r.Unlock()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *staticRouter) Options() router.Options {
|
||||||
|
return r.opts
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *staticRouter) Close() error {
|
||||||
|
select {
|
||||||
|
case <-r.exit:
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
close(r.exit)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *staticRouter) Endpoint(req *http.Request) (*api.Service, error) {
|
||||||
|
ep, err := r.endpoint(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
epf := strings.Split(ep.apiep.Name, ".")
|
||||||
|
services, err := r.opts.Registry.GetService(epf[0])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// hack for stream endpoint
|
||||||
|
if ep.apiep.Stream {
|
||||||
|
svcs := rutil.Copy(services)
|
||||||
|
for _, svc := range svcs {
|
||||||
|
if len(svc.Endpoints) == 0 {
|
||||||
|
e := ®istry.Endpoint{}
|
||||||
|
e.Name = strings.Join(epf[1:], ".")
|
||||||
|
e.Metadata = make(map[string]string)
|
||||||
|
e.Metadata["stream"] = "true"
|
||||||
|
svc.Endpoints = append(svc.Endpoints, e)
|
||||||
|
}
|
||||||
|
for _, e := range svc.Endpoints {
|
||||||
|
e.Name = strings.Join(epf[1:], ".")
|
||||||
|
e.Metadata = make(map[string]string)
|
||||||
|
e.Metadata["stream"] = "true"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
services = svcs
|
||||||
|
}
|
||||||
|
|
||||||
|
svc := &api.Service{
|
||||||
|
Name: epf[0],
|
||||||
|
Endpoint: &api.Endpoint{
|
||||||
|
Name: strings.Join(epf[1:], "."),
|
||||||
|
Handler: "rpc",
|
||||||
|
Host: ep.apiep.Host,
|
||||||
|
Method: ep.apiep.Method,
|
||||||
|
Path: ep.apiep.Path,
|
||||||
|
Body: ep.apiep.Body,
|
||||||
|
Stream: ep.apiep.Stream,
|
||||||
|
},
|
||||||
|
Services: services,
|
||||||
|
}
|
||||||
|
|
||||||
|
return svc, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *staticRouter) endpoint(req *http.Request) (*endpoint, error) {
|
||||||
|
if r.isClosed() {
|
||||||
|
return nil, errors.New("router closed")
|
||||||
|
}
|
||||||
|
|
||||||
|
r.RLock()
|
||||||
|
defer r.RUnlock()
|
||||||
|
|
||||||
|
var idx int
|
||||||
|
if len(req.URL.Path) > 0 && req.URL.Path != "/" {
|
||||||
|
idx = 1
|
||||||
|
}
|
||||||
|
path := strings.Split(req.URL.Path[idx:], "/")
|
||||||
|
// use the first match
|
||||||
|
// TODO: weighted matching
|
||||||
|
|
||||||
|
for _, ep := range r.eps {
|
||||||
|
var mMatch, hMatch, pMatch bool
|
||||||
|
|
||||||
|
// 1. try method
|
||||||
|
for _, m := range ep.apiep.Method {
|
||||||
|
if m == req.Method {
|
||||||
|
mMatch = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !mMatch {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
||||||
|
logger.Debugf("api method match %s", req.Method)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. try host
|
||||||
|
if len(ep.apiep.Host) == 0 {
|
||||||
|
hMatch = true
|
||||||
|
} else {
|
||||||
|
for idx, h := range ep.apiep.Host {
|
||||||
|
if h == "" || h == "*" {
|
||||||
|
hMatch = true
|
||||||
|
break
|
||||||
|
} else {
|
||||||
|
if ep.hostregs[idx].MatchString(req.URL.Host) {
|
||||||
|
hMatch = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !hMatch {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
||||||
|
logger.Debugf("api host match %s", req.URL.Host)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. try google.api path
|
||||||
|
for _, pathreg := range ep.pathregs {
|
||||||
|
matches, err := pathreg.Match(path, "")
|
||||||
|
if err != nil {
|
||||||
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
||||||
|
logger.Debugf("api gpath not match %s != %v", path, pathreg)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
||||||
|
logger.Debugf("api gpath match %s = %v", path, pathreg)
|
||||||
|
}
|
||||||
|
pMatch = true
|
||||||
|
ctx := req.Context()
|
||||||
|
md, ok := metadata.FromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
md = make(metadata.Metadata)
|
||||||
|
}
|
||||||
|
for k, v := range matches {
|
||||||
|
md[fmt.Sprintf("x-api-field-%s", k)] = v
|
||||||
|
}
|
||||||
|
md["x-api-body"] = ep.apiep.Body
|
||||||
|
*req = *req.Clone(metadata.NewContext(ctx, md))
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if !pMatch {
|
||||||
|
// 4. try path via pcre path matching
|
||||||
|
for _, pathreg := range ep.pcreregs {
|
||||||
|
if !pathreg.MatchString(req.URL.Path) {
|
||||||
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
||||||
|
logger.Debugf("api pcre path not match %s != %v", req.URL.Path, pathreg)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
pMatch = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !pMatch {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// TODO: Percentage traffic
|
||||||
|
|
||||||
|
// we got here, so its a match
|
||||||
|
return ep, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// no match
|
||||||
|
return nil, fmt.Errorf("endpoint not found for %v", req.URL)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *staticRouter) Route(req *http.Request) (*api.Service, error) {
|
||||||
|
if r.isClosed() {
|
||||||
|
return nil, errors.New("router closed")
|
||||||
|
}
|
||||||
|
|
||||||
|
// try get an endpoint
|
||||||
|
ep, err := r.Endpoint(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return ep, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRouter(opts ...router.Option) *staticRouter {
|
||||||
|
options := router.NewOptions(opts...)
|
||||||
|
r := &staticRouter{
|
||||||
|
exit: make(chan bool),
|
||||||
|
opts: options,
|
||||||
|
eps: make(map[string]*endpoint),
|
||||||
|
}
|
||||||
|
//go r.watch()
|
||||||
|
//go r.refresh()
|
||||||
|
return r
|
||||||
|
}
|
27
api/router/util/LICENSE.txt
Normal file
27
api/router/util/LICENSE.txt
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
Copyright (c) 2015, Gengo, Inc.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without modification,
|
||||||
|
are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer.
|
||||||
|
|
||||||
|
* Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer in the documentation
|
||||||
|
and/or other materials provided with the distribution.
|
||||||
|
|
||||||
|
* Neither the name of Gengo, Inc. nor the names of its
|
||||||
|
contributors may be used to endorse or promote products derived from this
|
||||||
|
software without specific prior written permission.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||||
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||||
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||||
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||||
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||||
|
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
115
api/router/util/compile.go
Normal file
115
api/router/util/compile.go
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
// download from https://raw.githubusercontent.com/grpc-ecosystem/grpc-gateway/master/protoc-gen-grpc-gateway/httprule/compile.go
|
||||||
|
|
||||||
|
const (
|
||||||
|
opcodeVersion = 1
|
||||||
|
)
|
||||||
|
|
||||||
|
// Template is a compiled representation of path templates.
|
||||||
|
type Template struct {
|
||||||
|
// Version is the version number of the format.
|
||||||
|
Version int
|
||||||
|
// OpCodes is a sequence of operations.
|
||||||
|
OpCodes []int
|
||||||
|
// Pool is a constant pool
|
||||||
|
Pool []string
|
||||||
|
// Verb is a VERB part in the template.
|
||||||
|
Verb string
|
||||||
|
// Fields is a list of field paths bound in this template.
|
||||||
|
Fields []string
|
||||||
|
// Original template (example: /v1/a_bit_of_everything)
|
||||||
|
Template string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compiler compiles utilities representation of path templates into marshallable operations.
|
||||||
|
// They can be unmarshalled by runtime.NewPattern.
|
||||||
|
type Compiler interface {
|
||||||
|
Compile() Template
|
||||||
|
}
|
||||||
|
|
||||||
|
type op struct {
|
||||||
|
// code is the opcode of the operation
|
||||||
|
code OpCode
|
||||||
|
|
||||||
|
// str is a string operand of the code.
|
||||||
|
// operand is ignored if str is not empty.
|
||||||
|
str string
|
||||||
|
|
||||||
|
// operand is a numeric operand of the code.
|
||||||
|
operand int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w wildcard) compile() []op {
|
||||||
|
return []op{
|
||||||
|
{code: OpPush},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w deepWildcard) compile() []op {
|
||||||
|
return []op{
|
||||||
|
{code: OpPushM},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l literal) compile() []op {
|
||||||
|
return []op{
|
||||||
|
{
|
||||||
|
code: OpLitPush,
|
||||||
|
str: string(l),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v variable) compile() []op {
|
||||||
|
var ops []op
|
||||||
|
for _, s := range v.segments {
|
||||||
|
ops = append(ops, s.compile()...)
|
||||||
|
}
|
||||||
|
ops = append(ops, op{
|
||||||
|
code: OpConcatN,
|
||||||
|
operand: len(v.segments),
|
||||||
|
}, op{
|
||||||
|
code: OpCapture,
|
||||||
|
str: v.path,
|
||||||
|
})
|
||||||
|
|
||||||
|
return ops
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t template) Compile() Template {
|
||||||
|
var rawOps []op
|
||||||
|
for _, s := range t.segments {
|
||||||
|
rawOps = append(rawOps, s.compile()...)
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
ops []int
|
||||||
|
pool []string
|
||||||
|
fields []string
|
||||||
|
)
|
||||||
|
consts := make(map[string]int)
|
||||||
|
for _, op := range rawOps {
|
||||||
|
ops = append(ops, int(op.code))
|
||||||
|
if op.str == "" {
|
||||||
|
ops = append(ops, op.operand)
|
||||||
|
} else {
|
||||||
|
if _, ok := consts[op.str]; !ok {
|
||||||
|
consts[op.str] = len(pool)
|
||||||
|
pool = append(pool, op.str)
|
||||||
|
}
|
||||||
|
ops = append(ops, consts[op.str])
|
||||||
|
}
|
||||||
|
if op.code == OpCapture {
|
||||||
|
fields = append(fields, op.str)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Template{
|
||||||
|
Version: opcodeVersion,
|
||||||
|
OpCodes: ops,
|
||||||
|
Pool: pool,
|
||||||
|
Verb: t.verb,
|
||||||
|
Fields: fields,
|
||||||
|
Template: t.template,
|
||||||
|
}
|
||||||
|
}
|
122
api/router/util/compile_test.go
Normal file
122
api/router/util/compile_test.go
Normal file
@@ -0,0 +1,122 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
// download from https://raw.githubusercontent.com/grpc-ecosystem/grpc-gateway/master/protoc-gen-grpc-gateway/httprule/compile_test.go
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
operandFiller = 0
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCompile(t *testing.T) {
|
||||||
|
for _, spec := range []struct {
|
||||||
|
segs []segment
|
||||||
|
verb string
|
||||||
|
|
||||||
|
ops []int
|
||||||
|
pool []string
|
||||||
|
fields []string
|
||||||
|
}{
|
||||||
|
{},
|
||||||
|
{
|
||||||
|
segs: []segment{
|
||||||
|
wildcard{},
|
||||||
|
},
|
||||||
|
ops: []int{int(OpPush), operandFiller},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
segs: []segment{
|
||||||
|
deepWildcard{},
|
||||||
|
},
|
||||||
|
ops: []int{int(OpPushM), operandFiller},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
segs: []segment{
|
||||||
|
literal("v1"),
|
||||||
|
},
|
||||||
|
ops: []int{int(OpLitPush), 0},
|
||||||
|
pool: []string{"v1"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
segs: []segment{
|
||||||
|
literal("v1"),
|
||||||
|
},
|
||||||
|
verb: "LOCK",
|
||||||
|
ops: []int{int(OpLitPush), 0},
|
||||||
|
pool: []string{"v1"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
segs: []segment{
|
||||||
|
variable{
|
||||||
|
path: "name.nested",
|
||||||
|
segments: []segment{
|
||||||
|
wildcard{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ops: []int{
|
||||||
|
int(OpPush), operandFiller,
|
||||||
|
int(OpConcatN), 1,
|
||||||
|
int(OpCapture), 0,
|
||||||
|
},
|
||||||
|
pool: []string{"name.nested"},
|
||||||
|
fields: []string{"name.nested"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
segs: []segment{
|
||||||
|
literal("obj"),
|
||||||
|
variable{
|
||||||
|
path: "name.nested",
|
||||||
|
segments: []segment{
|
||||||
|
literal("a"),
|
||||||
|
wildcard{},
|
||||||
|
literal("b"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
variable{
|
||||||
|
path: "obj",
|
||||||
|
segments: []segment{
|
||||||
|
deepWildcard{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ops: []int{
|
||||||
|
int(OpLitPush), 0,
|
||||||
|
int(OpLitPush), 1,
|
||||||
|
int(OpPush), operandFiller,
|
||||||
|
int(OpLitPush), 2,
|
||||||
|
int(OpConcatN), 3,
|
||||||
|
int(OpCapture), 3,
|
||||||
|
int(OpPushM), operandFiller,
|
||||||
|
int(OpConcatN), 1,
|
||||||
|
int(OpCapture), 0,
|
||||||
|
},
|
||||||
|
pool: []string{"obj", "a", "b", "name.nested"},
|
||||||
|
fields: []string{"name.nested", "obj"},
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
tmpl := template{
|
||||||
|
segments: spec.segs,
|
||||||
|
verb: spec.verb,
|
||||||
|
}
|
||||||
|
compiled := tmpl.Compile()
|
||||||
|
if got, want := compiled.Version, opcodeVersion; got != want {
|
||||||
|
t.Errorf("tmpl.Compile().Version = %d; want %d; segs=%#v, verb=%q", got, want, spec.segs, spec.verb)
|
||||||
|
}
|
||||||
|
if got, want := compiled.OpCodes, spec.ops; !reflect.DeepEqual(got, want) {
|
||||||
|
t.Errorf("tmpl.Compile().OpCodes = %v; want %v; segs=%#v, verb=%q", got, want, spec.segs, spec.verb)
|
||||||
|
}
|
||||||
|
if got, want := compiled.Pool, spec.pool; !reflect.DeepEqual(got, want) {
|
||||||
|
t.Errorf("tmpl.Compile().Pool = %q; want %q; segs=%#v, verb=%q", got, want, spec.segs, spec.verb)
|
||||||
|
}
|
||||||
|
if got, want := compiled.Verb, spec.verb; got != want {
|
||||||
|
t.Errorf("tmpl.Compile().Verb = %q; want %q; segs=%#v, verb=%q", got, want, spec.segs, spec.verb)
|
||||||
|
}
|
||||||
|
if got, want := compiled.Fields, spec.fields; !reflect.DeepEqual(got, want) {
|
||||||
|
t.Errorf("tmpl.Compile().Fields = %q; want %q; segs=%#v, verb=%q", got, want, spec.segs, spec.verb)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
363
api/router/util/parse.go
Normal file
363
api/router/util/parse.go
Normal file
@@ -0,0 +1,363 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
// download from https://raw.githubusercontent.com/grpc-ecosystem/grpc-gateway/master/protoc-gen-grpc-gateway/httprule/parse.go
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/micro/go-micro/v2/logger"
|
||||||
|
)
|
||||||
|
|
||||||
|
// InvalidTemplateError indicates that the path template is not valid.
|
||||||
|
type InvalidTemplateError struct {
|
||||||
|
tmpl string
|
||||||
|
msg string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e InvalidTemplateError) Error() string {
|
||||||
|
return fmt.Sprintf("%s: %s", e.msg, e.tmpl)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse parses the string representation of path template
|
||||||
|
func Parse(tmpl string) (Compiler, error) {
|
||||||
|
if !strings.HasPrefix(tmpl, "/") {
|
||||||
|
return template{}, InvalidTemplateError{tmpl: tmpl, msg: "no leading /"}
|
||||||
|
}
|
||||||
|
tokens, verb := tokenize(tmpl[1:])
|
||||||
|
|
||||||
|
p := parser{tokens: tokens}
|
||||||
|
segs, err := p.topLevelSegments()
|
||||||
|
if err != nil {
|
||||||
|
return template{}, InvalidTemplateError{tmpl: tmpl, msg: err.Error()}
|
||||||
|
}
|
||||||
|
|
||||||
|
return template{
|
||||||
|
segments: segs,
|
||||||
|
verb: verb,
|
||||||
|
template: tmpl,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func tokenize(path string) (tokens []string, verb string) {
|
||||||
|
if path == "" {
|
||||||
|
return []string{eof}, ""
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
init = iota
|
||||||
|
field
|
||||||
|
nested
|
||||||
|
)
|
||||||
|
var (
|
||||||
|
st = init
|
||||||
|
)
|
||||||
|
for path != "" {
|
||||||
|
var idx int
|
||||||
|
switch st {
|
||||||
|
case init:
|
||||||
|
idx = strings.IndexAny(path, "/{")
|
||||||
|
case field:
|
||||||
|
idx = strings.IndexAny(path, ".=}")
|
||||||
|
case nested:
|
||||||
|
idx = strings.IndexAny(path, "/}")
|
||||||
|
}
|
||||||
|
if idx < 0 {
|
||||||
|
tokens = append(tokens, path)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
switch r := path[idx]; r {
|
||||||
|
case '/', '.':
|
||||||
|
case '{':
|
||||||
|
st = field
|
||||||
|
case '=':
|
||||||
|
st = nested
|
||||||
|
case '}':
|
||||||
|
st = init
|
||||||
|
}
|
||||||
|
if idx == 0 {
|
||||||
|
tokens = append(tokens, path[idx:idx+1])
|
||||||
|
} else {
|
||||||
|
tokens = append(tokens, path[:idx], path[idx:idx+1])
|
||||||
|
}
|
||||||
|
path = path[idx+1:]
|
||||||
|
}
|
||||||
|
|
||||||
|
l := len(tokens)
|
||||||
|
t := tokens[l-1]
|
||||||
|
if idx := strings.LastIndex(t, ":"); idx == 0 {
|
||||||
|
tokens, verb = tokens[:l-1], t[1:]
|
||||||
|
} else if idx > 0 {
|
||||||
|
tokens[l-1], verb = t[:idx], t[idx+1:]
|
||||||
|
}
|
||||||
|
tokens = append(tokens, eof)
|
||||||
|
return tokens, verb
|
||||||
|
}
|
||||||
|
|
||||||
|
// parser is a parser of the template syntax defined in github.com/googleapis/googleapis/google/api/http.proto.
|
||||||
|
type parser struct {
|
||||||
|
tokens []string
|
||||||
|
accepted []string
|
||||||
|
}
|
||||||
|
|
||||||
|
// topLevelSegments is the target of this parser.
|
||||||
|
func (p *parser) topLevelSegments() ([]segment, error) {
|
||||||
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
||||||
|
logger.Debugf("Parsing %q", p.tokens)
|
||||||
|
}
|
||||||
|
segs, err := p.segments()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
||||||
|
logger.Debugf("accept segments: %q; %q", p.accepted, p.tokens)
|
||||||
|
}
|
||||||
|
if _, err := p.accept(typeEOF); err != nil {
|
||||||
|
return nil, fmt.Errorf("unexpected token %q after segments %q", p.tokens[0], strings.Join(p.accepted, ""))
|
||||||
|
}
|
||||||
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
||||||
|
logger.Debugf("accept eof: %q; %q", p.accepted, p.tokens)
|
||||||
|
}
|
||||||
|
return segs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *parser) segments() ([]segment, error) {
|
||||||
|
s, err := p.segment()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
||||||
|
logger.Debugf("accept segment: %q; %q", p.accepted, p.tokens)
|
||||||
|
}
|
||||||
|
segs := []segment{s}
|
||||||
|
for {
|
||||||
|
if _, err := p.accept("/"); err != nil {
|
||||||
|
return segs, nil
|
||||||
|
}
|
||||||
|
s, err := p.segment()
|
||||||
|
if err != nil {
|
||||||
|
return segs, err
|
||||||
|
}
|
||||||
|
segs = append(segs, s)
|
||||||
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
||||||
|
logger.Debugf("accept segment: %q; %q", p.accepted, p.tokens)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *parser) segment() (segment, error) {
|
||||||
|
if _, err := p.accept("*"); err == nil {
|
||||||
|
return wildcard{}, nil
|
||||||
|
}
|
||||||
|
if _, err := p.accept("**"); err == nil {
|
||||||
|
return deepWildcard{}, nil
|
||||||
|
}
|
||||||
|
if l, err := p.literal(); err == nil {
|
||||||
|
return l, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
v, err := p.variable()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("segment neither wildcards, literal or variable: %v", err)
|
||||||
|
}
|
||||||
|
return v, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *parser) literal() (segment, error) {
|
||||||
|
lit, err := p.accept(typeLiteral)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return literal(lit), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *parser) variable() (segment, error) {
|
||||||
|
if _, err := p.accept("{"); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
path, err := p.fieldPath()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var segs []segment
|
||||||
|
if _, err := p.accept("="); err == nil {
|
||||||
|
segs, err = p.segments()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("invalid segment in variable %q: %v", path, err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
segs = []segment{wildcard{}}
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := p.accept("}"); err != nil {
|
||||||
|
return nil, fmt.Errorf("unterminated variable segment: %s", path)
|
||||||
|
}
|
||||||
|
return variable{
|
||||||
|
path: path,
|
||||||
|
segments: segs,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *parser) fieldPath() (string, error) {
|
||||||
|
c, err := p.accept(typeIdent)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
components := []string{c}
|
||||||
|
for {
|
||||||
|
if _, err = p.accept("."); err != nil {
|
||||||
|
return strings.Join(components, "."), nil
|
||||||
|
}
|
||||||
|
c, err := p.accept(typeIdent)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("invalid field path component: %v", err)
|
||||||
|
}
|
||||||
|
components = append(components, c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A termType is a type of terminal symbols.
|
||||||
|
type termType string
|
||||||
|
|
||||||
|
// These constants define some of valid values of termType.
|
||||||
|
// They improve readability of parse functions.
|
||||||
|
//
|
||||||
|
// You can also use "/", "*", "**", "." or "=" as valid values.
|
||||||
|
const (
|
||||||
|
typeIdent = termType("ident")
|
||||||
|
typeLiteral = termType("literal")
|
||||||
|
typeEOF = termType("$")
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// eof is the terminal symbol which always appears at the end of token sequence.
|
||||||
|
eof = "\u0000"
|
||||||
|
)
|
||||||
|
|
||||||
|
// accept tries to accept a token in "p".
|
||||||
|
// This function consumes a token and returns it if it matches to the specified "term".
|
||||||
|
// If it doesn't match, the function does not consume any tokens and return an error.
|
||||||
|
func (p *parser) accept(term termType) (string, error) {
|
||||||
|
t := p.tokens[0]
|
||||||
|
switch term {
|
||||||
|
case "/", "*", "**", ".", "=", "{", "}":
|
||||||
|
if t != string(term) && t != "/" {
|
||||||
|
return "", fmt.Errorf("expected %q but got %q", term, t)
|
||||||
|
}
|
||||||
|
case typeEOF:
|
||||||
|
if t != eof {
|
||||||
|
return "", fmt.Errorf("expected EOF but got %q", t)
|
||||||
|
}
|
||||||
|
case typeIdent:
|
||||||
|
if err := expectIdent(t); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
case typeLiteral:
|
||||||
|
if err := expectPChars(t); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return "", fmt.Errorf("unknown termType %q", term)
|
||||||
|
}
|
||||||
|
p.tokens = p.tokens[1:]
|
||||||
|
p.accepted = append(p.accepted, t)
|
||||||
|
return t, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// expectPChars determines if "t" consists of only pchars defined in RFC3986.
|
||||||
|
//
|
||||||
|
// https://www.ietf.org/rfc/rfc3986.txt, P.49
|
||||||
|
// pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
|
||||||
|
// unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
|
||||||
|
// sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
|
||||||
|
// / "*" / "+" / "," / ";" / "="
|
||||||
|
// pct-encoded = "%" HEXDIG HEXDIG
|
||||||
|
func expectPChars(t string) error {
|
||||||
|
const (
|
||||||
|
init = iota
|
||||||
|
pct1
|
||||||
|
pct2
|
||||||
|
)
|
||||||
|
st := init
|
||||||
|
for _, r := range t {
|
||||||
|
if st != init {
|
||||||
|
if !isHexDigit(r) {
|
||||||
|
return fmt.Errorf("invalid hexdigit: %c(%U)", r, r)
|
||||||
|
}
|
||||||
|
switch st {
|
||||||
|
case pct1:
|
||||||
|
st = pct2
|
||||||
|
case pct2:
|
||||||
|
st = init
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// unreserved
|
||||||
|
switch {
|
||||||
|
case 'A' <= r && r <= 'Z':
|
||||||
|
continue
|
||||||
|
case 'a' <= r && r <= 'z':
|
||||||
|
continue
|
||||||
|
case '0' <= r && r <= '9':
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
switch r {
|
||||||
|
case '-', '.', '_', '~':
|
||||||
|
// unreserved
|
||||||
|
case '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=':
|
||||||
|
// sub-delims
|
||||||
|
case ':', '@':
|
||||||
|
// rest of pchar
|
||||||
|
case '%':
|
||||||
|
// pct-encoded
|
||||||
|
st = pct1
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("invalid character in path segment: %q(%U)", r, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if st != init {
|
||||||
|
return fmt.Errorf("invalid percent-encoding in %q", t)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// expectIdent determines if "ident" is a valid identifier in .proto schema ([[:alpha:]_][[:alphanum:]_]*).
|
||||||
|
func expectIdent(ident string) error {
|
||||||
|
if ident == "" {
|
||||||
|
return fmt.Errorf("empty identifier")
|
||||||
|
}
|
||||||
|
for pos, r := range ident {
|
||||||
|
switch {
|
||||||
|
case '0' <= r && r <= '9':
|
||||||
|
if pos == 0 {
|
||||||
|
return fmt.Errorf("identifier starting with digit: %s", ident)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
case 'A' <= r && r <= 'Z':
|
||||||
|
continue
|
||||||
|
case 'a' <= r && r <= 'z':
|
||||||
|
continue
|
||||||
|
case r == '_':
|
||||||
|
continue
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("invalid character %q(%U) in identifier: %s", r, r, ident)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func isHexDigit(r rune) bool {
|
||||||
|
switch {
|
||||||
|
case '0' <= r && r <= '9':
|
||||||
|
return true
|
||||||
|
case 'A' <= r && r <= 'F':
|
||||||
|
return true
|
||||||
|
case 'a' <= r && r <= 'f':
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
321
api/router/util/parse_test.go
Normal file
321
api/router/util/parse_test.go
Normal file
@@ -0,0 +1,321 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
// download from https://raw.githubusercontent.com/grpc-ecosystem/grpc-gateway/master/protoc-gen-grpc-gateway/httprule/parse_test.go
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/micro/go-micro/v2/logger"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTokenize(t *testing.T) {
|
||||||
|
for _, spec := range []struct {
|
||||||
|
src string
|
||||||
|
tokens []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
src: "",
|
||||||
|
tokens: []string{eof},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
src: "v1",
|
||||||
|
tokens: []string{"v1", eof},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
src: "v1/b",
|
||||||
|
tokens: []string{"v1", "/", "b", eof},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
src: "v1/endpoint/*",
|
||||||
|
tokens: []string{"v1", "/", "endpoint", "/", "*", eof},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
src: "v1/endpoint/**",
|
||||||
|
tokens: []string{"v1", "/", "endpoint", "/", "**", eof},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
src: "v1/b/{bucket_name=*}",
|
||||||
|
tokens: []string{
|
||||||
|
"v1", "/",
|
||||||
|
"b", "/",
|
||||||
|
"{", "bucket_name", "=", "*", "}",
|
||||||
|
eof,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
src: "v1/b/{bucket_name=buckets/*}",
|
||||||
|
tokens: []string{
|
||||||
|
"v1", "/",
|
||||||
|
"b", "/",
|
||||||
|
"{", "bucket_name", "=", "buckets", "/", "*", "}",
|
||||||
|
eof,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
src: "v1/b/{bucket_name=buckets/*}/o",
|
||||||
|
tokens: []string{
|
||||||
|
"v1", "/",
|
||||||
|
"b", "/",
|
||||||
|
"{", "bucket_name", "=", "buckets", "/", "*", "}", "/",
|
||||||
|
"o",
|
||||||
|
eof,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
src: "v1/b/{bucket_name=buckets/*}/o/{name}",
|
||||||
|
tokens: []string{
|
||||||
|
"v1", "/",
|
||||||
|
"b", "/",
|
||||||
|
"{", "bucket_name", "=", "buckets", "/", "*", "}", "/",
|
||||||
|
"o", "/", "{", "name", "}",
|
||||||
|
eof,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
src: "v1/a=b&c=d;e=f:g/endpoint.rdf",
|
||||||
|
tokens: []string{
|
||||||
|
"v1", "/",
|
||||||
|
"a=b&c=d;e=f:g", "/",
|
||||||
|
"endpoint.rdf",
|
||||||
|
eof,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
tokens, verb := tokenize(spec.src)
|
||||||
|
if got, want := tokens, spec.tokens; !reflect.DeepEqual(got, want) {
|
||||||
|
t.Errorf("tokenize(%q) = %q, _; want %q, _", spec.src, got, want)
|
||||||
|
}
|
||||||
|
if got, want := verb, ""; got != want {
|
||||||
|
t.Errorf("tokenize(%q) = _, %q; want _, %q", spec.src, got, want)
|
||||||
|
}
|
||||||
|
|
||||||
|
src := fmt.Sprintf("%s:%s", spec.src, "LOCK")
|
||||||
|
tokens, verb = tokenize(src)
|
||||||
|
if got, want := tokens, spec.tokens; !reflect.DeepEqual(got, want) {
|
||||||
|
t.Errorf("tokenize(%q) = %q, _; want %q, _", src, got, want)
|
||||||
|
}
|
||||||
|
if got, want := verb, "LOCK"; got != want {
|
||||||
|
t.Errorf("tokenize(%q) = _, %q; want _, %q", src, got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseSegments(t *testing.T) {
|
||||||
|
flag.Set("v", "3")
|
||||||
|
for _, spec := range []struct {
|
||||||
|
tokens []string
|
||||||
|
want []segment
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
tokens: []string{"v1", eof},
|
||||||
|
want: []segment{
|
||||||
|
literal("v1"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
tokens: []string{"/", eof},
|
||||||
|
want: []segment{
|
||||||
|
wildcard{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
tokens: []string{"-._~!$&'()*+,;=:@", eof},
|
||||||
|
want: []segment{
|
||||||
|
literal("-._~!$&'()*+,;=:@"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
tokens: []string{"%e7%ac%ac%e4%b8%80%e7%89%88", eof},
|
||||||
|
want: []segment{
|
||||||
|
literal("%e7%ac%ac%e4%b8%80%e7%89%88"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
tokens: []string{"v1", "/", "*", eof},
|
||||||
|
want: []segment{
|
||||||
|
literal("v1"),
|
||||||
|
wildcard{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
tokens: []string{"v1", "/", "**", eof},
|
||||||
|
want: []segment{
|
||||||
|
literal("v1"),
|
||||||
|
deepWildcard{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
tokens: []string{"{", "name", "}", eof},
|
||||||
|
want: []segment{
|
||||||
|
variable{
|
||||||
|
path: "name",
|
||||||
|
segments: []segment{
|
||||||
|
wildcard{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
tokens: []string{"{", "name", "=", "*", "}", eof},
|
||||||
|
want: []segment{
|
||||||
|
variable{
|
||||||
|
path: "name",
|
||||||
|
segments: []segment{
|
||||||
|
wildcard{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
tokens: []string{"{", "field", ".", "nested", ".", "nested2", "=", "*", "}", eof},
|
||||||
|
want: []segment{
|
||||||
|
variable{
|
||||||
|
path: "field.nested.nested2",
|
||||||
|
segments: []segment{
|
||||||
|
wildcard{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
tokens: []string{"{", "name", "=", "a", "/", "b", "/", "*", "}", eof},
|
||||||
|
want: []segment{
|
||||||
|
variable{
|
||||||
|
path: "name",
|
||||||
|
segments: []segment{
|
||||||
|
literal("a"),
|
||||||
|
literal("b"),
|
||||||
|
wildcard{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
tokens: []string{
|
||||||
|
"v1", "/",
|
||||||
|
"{",
|
||||||
|
"name", ".", "nested", ".", "nested2",
|
||||||
|
"=",
|
||||||
|
"a", "/", "b", "/", "*",
|
||||||
|
"}", "/",
|
||||||
|
"o", "/",
|
||||||
|
"{",
|
||||||
|
"another_name",
|
||||||
|
"=",
|
||||||
|
"a", "/", "b", "/", "*", "/", "c",
|
||||||
|
"}", "/",
|
||||||
|
"**",
|
||||||
|
eof},
|
||||||
|
want: []segment{
|
||||||
|
literal("v1"),
|
||||||
|
variable{
|
||||||
|
path: "name.nested.nested2",
|
||||||
|
segments: []segment{
|
||||||
|
literal("a"),
|
||||||
|
literal("b"),
|
||||||
|
wildcard{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
literal("o"),
|
||||||
|
variable{
|
||||||
|
path: "another_name",
|
||||||
|
segments: []segment{
|
||||||
|
literal("a"),
|
||||||
|
literal("b"),
|
||||||
|
wildcard{},
|
||||||
|
literal("c"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
deepWildcard{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
p := parser{tokens: spec.tokens}
|
||||||
|
segs, err := p.topLevelSegments()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("parser{%q}.segments() failed with %v; want success", spec.tokens, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if got, want := segs, spec.want; !reflect.DeepEqual(got, want) {
|
||||||
|
t.Errorf("parser{%q}.segments() = %#v; want %#v", spec.tokens, got, want)
|
||||||
|
}
|
||||||
|
if got := p.tokens; len(got) > 0 {
|
||||||
|
t.Errorf("p.tokens = %q; want []; spec.tokens=%q", got, spec.tokens)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseSegmentsWithErrors(t *testing.T) {
|
||||||
|
flag.Set("v", "3")
|
||||||
|
for _, spec := range []struct {
|
||||||
|
tokens []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
// double slash
|
||||||
|
tokens: []string{"//", eof},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// invalid literal
|
||||||
|
tokens: []string{"a?b", eof},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// invalid percent-encoding
|
||||||
|
tokens: []string{"%", eof},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// invalid percent-encoding
|
||||||
|
tokens: []string{"%2", eof},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// invalid percent-encoding
|
||||||
|
tokens: []string{"a%2z", eof},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// empty segments
|
||||||
|
tokens: []string{eof},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// unterminated variable
|
||||||
|
tokens: []string{"{", "name", eof},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// unterminated variable
|
||||||
|
tokens: []string{"{", "name", "=", eof},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// unterminated variable
|
||||||
|
tokens: []string{"{", "name", "=", "*", eof},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// empty component in field path
|
||||||
|
tokens: []string{"{", "name", ".", "}", eof},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// empty component in field path
|
||||||
|
tokens: []string{"{", "name", ".", ".", "nested", "}", eof},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// invalid character in identifier
|
||||||
|
tokens: []string{"{", "field-name", "}", eof},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// no slash between segments
|
||||||
|
tokens: []string{"v1", "endpoint", eof},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// no slash between segments
|
||||||
|
tokens: []string{"v1", "{", "name", "}", eof},
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
p := parser{tokens: spec.tokens}
|
||||||
|
segs, err := p.topLevelSegments()
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("parser{%q}.segments() succeeded; want InvalidTemplateError; accepted %#v", spec.tokens, segs)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
logger.Info(err)
|
||||||
|
}
|
||||||
|
}
|
24
api/router/util/pattern.go
Normal file
24
api/router/util/pattern.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
// download from https://raw.githubusercontent.com/grpc-ecosystem/grpc-gateway/master/utilities/pattern.go
|
||||||
|
|
||||||
|
// An OpCode is a opcode of compiled path patterns.
|
||||||
|
type OpCode int
|
||||||
|
|
||||||
|
// These constants are the valid values of OpCode.
|
||||||
|
const (
|
||||||
|
// OpNop does nothing
|
||||||
|
OpNop = OpCode(iota)
|
||||||
|
// OpPush pushes a component to stack
|
||||||
|
OpPush
|
||||||
|
// OpLitPush pushes a component to stack if it matches to the literal
|
||||||
|
OpLitPush
|
||||||
|
// OpPushM concatenates the remaining components and pushes it to stack
|
||||||
|
OpPushM
|
||||||
|
// OpConcatN pops N items from stack, concatenates them and pushes it back to stack
|
||||||
|
OpConcatN
|
||||||
|
// OpCapture pops an item and binds it to the variable
|
||||||
|
OpCapture
|
||||||
|
// OpEnd is the least positive invalid opcode.
|
||||||
|
OpEnd
|
||||||
|
)
|
283
api/router/util/runtime.go
Normal file
283
api/router/util/runtime.go
Normal file
@@ -0,0 +1,283 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
// download from https://raw.githubusercontent.com/grpc-ecosystem/grpc-gateway/master/runtime/pattern.go
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/micro/go-micro/v2/logger"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ErrNotMatch indicates that the given HTTP request path does not match to the pattern.
|
||||||
|
ErrNotMatch = errors.New("not match to the path pattern")
|
||||||
|
// ErrInvalidPattern indicates that the given definition of Pattern is not valid.
|
||||||
|
ErrInvalidPattern = errors.New("invalid pattern")
|
||||||
|
)
|
||||||
|
|
||||||
|
type rop struct {
|
||||||
|
code OpCode
|
||||||
|
operand int
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pattern is a template pattern of http request paths defined in github.com/googleapis/googleapis/google/api/http.proto.
|
||||||
|
type Pattern struct {
|
||||||
|
// ops is a list of operations
|
||||||
|
ops []rop
|
||||||
|
// pool is a constant pool indexed by the operands or vars.
|
||||||
|
pool []string
|
||||||
|
// vars is a list of variables names to be bound by this pattern
|
||||||
|
vars []string
|
||||||
|
// stacksize is the max depth of the stack
|
||||||
|
stacksize int
|
||||||
|
// tailLen is the length of the fixed-size segments after a deep wildcard
|
||||||
|
tailLen int
|
||||||
|
// verb is the VERB part of the path pattern. It is empty if the pattern does not have VERB part.
|
||||||
|
verb string
|
||||||
|
// assumeColonVerb indicates whether a path suffix after a final
|
||||||
|
// colon may only be interpreted as a verb.
|
||||||
|
assumeColonVerb bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type patternOptions struct {
|
||||||
|
assumeColonVerb bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// PatternOpt is an option for creating Patterns.
|
||||||
|
type PatternOpt func(*patternOptions)
|
||||||
|
|
||||||
|
// NewPattern returns a new Pattern from the given definition values.
|
||||||
|
// "ops" is a sequence of op codes. "pool" is a constant pool.
|
||||||
|
// "verb" is the verb part of the pattern. It is empty if the pattern does not have the part.
|
||||||
|
// "version" must be 1 for now.
|
||||||
|
// It returns an error if the given definition is invalid.
|
||||||
|
func NewPattern(version int, ops []int, pool []string, verb string, opts ...PatternOpt) (Pattern, error) {
|
||||||
|
options := patternOptions{
|
||||||
|
assumeColonVerb: true,
|
||||||
|
}
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&options)
|
||||||
|
}
|
||||||
|
|
||||||
|
if version != 1 {
|
||||||
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
||||||
|
logger.Debugf("unsupported version: %d", version)
|
||||||
|
}
|
||||||
|
return Pattern{}, ErrInvalidPattern
|
||||||
|
}
|
||||||
|
|
||||||
|
l := len(ops)
|
||||||
|
if l%2 != 0 {
|
||||||
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
||||||
|
logger.Debugf("odd number of ops codes: %d", l)
|
||||||
|
}
|
||||||
|
return Pattern{}, ErrInvalidPattern
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
typedOps []rop
|
||||||
|
stack, maxstack int
|
||||||
|
tailLen int
|
||||||
|
pushMSeen bool
|
||||||
|
vars []string
|
||||||
|
)
|
||||||
|
for i := 0; i < l; i += 2 {
|
||||||
|
op := rop{code: OpCode(ops[i]), operand: ops[i+1]}
|
||||||
|
switch op.code {
|
||||||
|
case OpNop:
|
||||||
|
continue
|
||||||
|
case OpPush:
|
||||||
|
if pushMSeen {
|
||||||
|
tailLen++
|
||||||
|
}
|
||||||
|
stack++
|
||||||
|
case OpPushM:
|
||||||
|
if pushMSeen {
|
||||||
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
||||||
|
logger.Debug("pushM appears twice")
|
||||||
|
}
|
||||||
|
return Pattern{}, ErrInvalidPattern
|
||||||
|
}
|
||||||
|
pushMSeen = true
|
||||||
|
stack++
|
||||||
|
case OpLitPush:
|
||||||
|
if op.operand < 0 || len(pool) <= op.operand {
|
||||||
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
||||||
|
logger.Debugf("negative literal index: %d", op.operand)
|
||||||
|
}
|
||||||
|
return Pattern{}, ErrInvalidPattern
|
||||||
|
}
|
||||||
|
if pushMSeen {
|
||||||
|
tailLen++
|
||||||
|
}
|
||||||
|
stack++
|
||||||
|
case OpConcatN:
|
||||||
|
if op.operand <= 0 {
|
||||||
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
||||||
|
logger.Debugf("negative concat size: %d", op.operand)
|
||||||
|
}
|
||||||
|
return Pattern{}, ErrInvalidPattern
|
||||||
|
}
|
||||||
|
stack -= op.operand
|
||||||
|
if stack < 0 {
|
||||||
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
||||||
|
logger.Debug("stack underflow")
|
||||||
|
}
|
||||||
|
return Pattern{}, ErrInvalidPattern
|
||||||
|
}
|
||||||
|
stack++
|
||||||
|
case OpCapture:
|
||||||
|
if op.operand < 0 || len(pool) <= op.operand {
|
||||||
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
||||||
|
logger.Debugf("variable name index out of bound: %d", op.operand)
|
||||||
|
}
|
||||||
|
return Pattern{}, ErrInvalidPattern
|
||||||
|
}
|
||||||
|
v := pool[op.operand]
|
||||||
|
op.operand = len(vars)
|
||||||
|
vars = append(vars, v)
|
||||||
|
stack--
|
||||||
|
if stack < 0 {
|
||||||
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
||||||
|
logger.Debug("stack underflow")
|
||||||
|
}
|
||||||
|
return Pattern{}, ErrInvalidPattern
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
||||||
|
logger.Debugf("invalid opcode: %d", op.code)
|
||||||
|
}
|
||||||
|
return Pattern{}, ErrInvalidPattern
|
||||||
|
}
|
||||||
|
|
||||||
|
if maxstack < stack {
|
||||||
|
maxstack = stack
|
||||||
|
}
|
||||||
|
typedOps = append(typedOps, op)
|
||||||
|
}
|
||||||
|
return Pattern{
|
||||||
|
ops: typedOps,
|
||||||
|
pool: pool,
|
||||||
|
vars: vars,
|
||||||
|
stacksize: maxstack,
|
||||||
|
tailLen: tailLen,
|
||||||
|
verb: verb,
|
||||||
|
assumeColonVerb: options.assumeColonVerb,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustPattern is a helper function which makes it easier to call NewPattern in variable initialization.
|
||||||
|
func MustPattern(p Pattern, err error) Pattern {
|
||||||
|
if err != nil {
|
||||||
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
||||||
|
logger.Fatalf("Pattern initialization failed: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// Match examines components if it matches to the Pattern.
|
||||||
|
// If it matches, the function returns a mapping from field paths to their captured values.
|
||||||
|
// If otherwise, the function returns an error.
|
||||||
|
func (p Pattern) Match(components []string, verb string) (map[string]string, error) {
|
||||||
|
if p.verb != verb {
|
||||||
|
if p.assumeColonVerb || p.verb != "" {
|
||||||
|
return nil, ErrNotMatch
|
||||||
|
}
|
||||||
|
if len(components) == 0 {
|
||||||
|
components = []string{":" + verb}
|
||||||
|
} else {
|
||||||
|
components = append([]string{}, components...)
|
||||||
|
components[len(components)-1] += ":" + verb
|
||||||
|
}
|
||||||
|
verb = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
var pos int
|
||||||
|
stack := make([]string, 0, p.stacksize)
|
||||||
|
captured := make([]string, len(p.vars))
|
||||||
|
l := len(components)
|
||||||
|
for _, op := range p.ops {
|
||||||
|
switch op.code {
|
||||||
|
case OpNop:
|
||||||
|
continue
|
||||||
|
case OpPush, OpLitPush:
|
||||||
|
if pos >= l {
|
||||||
|
return nil, ErrNotMatch
|
||||||
|
}
|
||||||
|
c := components[pos]
|
||||||
|
if op.code == OpLitPush {
|
||||||
|
if lit := p.pool[op.operand]; c != lit {
|
||||||
|
return nil, ErrNotMatch
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stack = append(stack, c)
|
||||||
|
pos++
|
||||||
|
case OpPushM:
|
||||||
|
end := len(components)
|
||||||
|
if end < pos+p.tailLen {
|
||||||
|
return nil, ErrNotMatch
|
||||||
|
}
|
||||||
|
end -= p.tailLen
|
||||||
|
stack = append(stack, strings.Join(components[pos:end], "/"))
|
||||||
|
pos = end
|
||||||
|
case OpConcatN:
|
||||||
|
n := op.operand
|
||||||
|
l := len(stack) - n
|
||||||
|
stack = append(stack[:l], strings.Join(stack[l:], "/"))
|
||||||
|
case OpCapture:
|
||||||
|
n := len(stack) - 1
|
||||||
|
captured[op.operand] = stack[n]
|
||||||
|
stack = stack[:n]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if pos < l {
|
||||||
|
return nil, ErrNotMatch
|
||||||
|
}
|
||||||
|
bindings := make(map[string]string)
|
||||||
|
for i, val := range captured {
|
||||||
|
bindings[p.vars[i]] = val
|
||||||
|
}
|
||||||
|
return bindings, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verb returns the verb part of the Pattern.
|
||||||
|
func (p Pattern) Verb() string { return p.verb }
|
||||||
|
|
||||||
|
func (p Pattern) String() string {
|
||||||
|
var stack []string
|
||||||
|
for _, op := range p.ops {
|
||||||
|
switch op.code {
|
||||||
|
case OpNop:
|
||||||
|
continue
|
||||||
|
case OpPush:
|
||||||
|
stack = append(stack, "*")
|
||||||
|
case OpLitPush:
|
||||||
|
stack = append(stack, p.pool[op.operand])
|
||||||
|
case OpPushM:
|
||||||
|
stack = append(stack, "**")
|
||||||
|
case OpConcatN:
|
||||||
|
n := op.operand
|
||||||
|
l := len(stack) - n
|
||||||
|
stack = append(stack[:l], strings.Join(stack[l:], "/"))
|
||||||
|
case OpCapture:
|
||||||
|
n := len(stack) - 1
|
||||||
|
stack[n] = fmt.Sprintf("{%s=%s}", p.vars[op.operand], stack[n])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
segs := strings.Join(stack, "/")
|
||||||
|
if p.verb != "" {
|
||||||
|
return fmt.Sprintf("/%s:%s", segs, p.verb)
|
||||||
|
}
|
||||||
|
return "/" + segs
|
||||||
|
}
|
||||||
|
|
||||||
|
// AssumeColonVerbOpt indicates whether a path suffix after a final
|
||||||
|
// colon may only be interpreted as a verb.
|
||||||
|
func AssumeColonVerbOpt(val bool) PatternOpt {
|
||||||
|
return PatternOpt(func(o *patternOptions) {
|
||||||
|
o.assumeColonVerb = val
|
||||||
|
})
|
||||||
|
}
|
62
api/router/util/types.go
Normal file
62
api/router/util/types.go
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
// download from https://raw.githubusercontent.com/grpc-ecosystem/grpc-gateway/master/protoc-gen-grpc-gateway/httprule/types.go
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type template struct {
|
||||||
|
segments []segment
|
||||||
|
verb string
|
||||||
|
template string
|
||||||
|
}
|
||||||
|
|
||||||
|
type segment interface {
|
||||||
|
fmt.Stringer
|
||||||
|
compile() (ops []op)
|
||||||
|
}
|
||||||
|
|
||||||
|
type wildcard struct{}
|
||||||
|
|
||||||
|
type deepWildcard struct{}
|
||||||
|
|
||||||
|
type literal string
|
||||||
|
|
||||||
|
type variable struct {
|
||||||
|
path string
|
||||||
|
segments []segment
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wildcard) String() string {
|
||||||
|
return "*"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (deepWildcard) String() string {
|
||||||
|
return "**"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l literal) String() string {
|
||||||
|
return string(l)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v variable) String() string {
|
||||||
|
var segs []string
|
||||||
|
for _, s := range v.segments {
|
||||||
|
segs = append(segs, s.String())
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("{%s=%s}", v.path, strings.Join(segs, "/"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t template) String() string {
|
||||||
|
var segs []string
|
||||||
|
for _, s := range t.segments {
|
||||||
|
segs = append(segs, s.String())
|
||||||
|
}
|
||||||
|
str := strings.Join(segs, "/")
|
||||||
|
if t.verb != "" {
|
||||||
|
str = fmt.Sprintf("%s:%s", str, t.verb)
|
||||||
|
}
|
||||||
|
return "/" + str
|
||||||
|
}
|
93
api/router/util/types_test.go
Normal file
93
api/router/util/types_test.go
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
// download from https://raw.githubusercontent.com/grpc-ecosystem/grpc-gateway/master/protoc-gen-grpc-gateway/httprule/types_test.go
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTemplateStringer(t *testing.T) {
|
||||||
|
for _, spec := range []struct {
|
||||||
|
segs []segment
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
segs: []segment{
|
||||||
|
literal("v1"),
|
||||||
|
},
|
||||||
|
want: "/v1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
segs: []segment{
|
||||||
|
wildcard{},
|
||||||
|
},
|
||||||
|
want: "/*",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
segs: []segment{
|
||||||
|
deepWildcard{},
|
||||||
|
},
|
||||||
|
want: "/**",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
segs: []segment{
|
||||||
|
variable{
|
||||||
|
path: "name",
|
||||||
|
segments: []segment{
|
||||||
|
literal("a"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
want: "/{name=a}",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
segs: []segment{
|
||||||
|
variable{
|
||||||
|
path: "name",
|
||||||
|
segments: []segment{
|
||||||
|
literal("a"),
|
||||||
|
wildcard{},
|
||||||
|
literal("b"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
want: "/{name=a/*/b}",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
segs: []segment{
|
||||||
|
literal("v1"),
|
||||||
|
variable{
|
||||||
|
path: "name",
|
||||||
|
segments: []segment{
|
||||||
|
literal("a"),
|
||||||
|
wildcard{},
|
||||||
|
literal("b"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
literal("c"),
|
||||||
|
variable{
|
||||||
|
path: "field.nested",
|
||||||
|
segments: []segment{
|
||||||
|
wildcard{},
|
||||||
|
literal("d"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
wildcard{},
|
||||||
|
literal("e"),
|
||||||
|
deepWildcard{},
|
||||||
|
},
|
||||||
|
want: "/v1/{name=a/*/b}/c/{field.nested=*/d}/*/e/**",
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
tmpl := template{segments: spec.segs}
|
||||||
|
if got, want := tmpl.String(), spec.want; got != want {
|
||||||
|
t.Errorf("%#v.String() = %q; want %q", tmpl, got, want)
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpl.verb = "LOCK"
|
||||||
|
if got, want := tmpl.String(), fmt.Sprintf("%s:LOCK", spec.want); got != want {
|
||||||
|
t.Errorf("%#v.String() = %q; want %q", tmpl, got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
28
api/server/acme/acme.go
Normal file
28
api/server/acme/acme.go
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
// Package acme abstracts away various ACME libraries
|
||||||
|
package acme
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"errors"
|
||||||
|
"net"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ErrProviderNotImplemented can be returned when attempting to
|
||||||
|
// instantiate an unimplemented provider
|
||||||
|
ErrProviderNotImplemented = errors.New("Provider not implemented")
|
||||||
|
)
|
||||||
|
|
||||||
|
// Provider is a ACME provider interface
|
||||||
|
type Provider interface {
|
||||||
|
// Listen returns a new listener
|
||||||
|
Listen(...string) (net.Listener, error)
|
||||||
|
// TLSConfig returns a tls config
|
||||||
|
TLSConfig(...string) (*tls.Config, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The Let's Encrypt ACME endpoints
|
||||||
|
const (
|
||||||
|
LetsEncryptStagingCA = "https://acme-staging-v02.api.letsencrypt.org/directory"
|
||||||
|
LetsEncryptProductionCA = "https://acme-v02.api.letsencrypt.org/directory"
|
||||||
|
)
|
46
api/server/acme/autocert/autocert.go
Normal file
46
api/server/acme/autocert/autocert.go
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
// Package autocert is the ACME provider from golang.org/x/crypto/acme/autocert
|
||||||
|
// This provider does not take any config.
|
||||||
|
package autocert
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/micro/go-micro/v2/api/server/acme"
|
||||||
|
"github.com/micro/go-micro/v2/logger"
|
||||||
|
"golang.org/x/crypto/acme/autocert"
|
||||||
|
)
|
||||||
|
|
||||||
|
// autoCertACME is the ACME provider from golang.org/x/crypto/acme/autocert
|
||||||
|
type autocertProvider struct{}
|
||||||
|
|
||||||
|
// Listen implements acme.Provider
|
||||||
|
func (a *autocertProvider) Listen(hosts ...string) (net.Listener, error) {
|
||||||
|
return autocert.NewListener(hosts...), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// TLSConfig returns a new tls config
|
||||||
|
func (a *autocertProvider) TLSConfig(hosts ...string) (*tls.Config, error) {
|
||||||
|
// create a new manager
|
||||||
|
m := &autocert.Manager{
|
||||||
|
Prompt: autocert.AcceptTOS,
|
||||||
|
}
|
||||||
|
if len(hosts) > 0 {
|
||||||
|
m.HostPolicy = autocert.HostWhitelist(hosts...)
|
||||||
|
}
|
||||||
|
dir := cacheDir()
|
||||||
|
if err := os.MkdirAll(dir, 0700); err != nil {
|
||||||
|
if logger.V(logger.InfoLevel, logger.DefaultLogger) {
|
||||||
|
logger.Infof("warning: autocert not using a cache: %v", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
m.Cache = autocert.DirCache(dir)
|
||||||
|
}
|
||||||
|
return m.TLSConfig(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// New returns an autocert acme.Provider
|
||||||
|
func NewProvider() acme.Provider {
|
||||||
|
return &autocertProvider{}
|
||||||
|
}
|
16
api/server/acme/autocert/autocert_test.go
Normal file
16
api/server/acme/autocert/autocert_test.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package autocert
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAutocert(t *testing.T) {
|
||||||
|
l := NewProvider()
|
||||||
|
if _, ok := l.(*autocertProvider); !ok {
|
||||||
|
t.Error("NewProvider() didn't return an autocertProvider")
|
||||||
|
}
|
||||||
|
// TODO: Travis CI doesn't let us bind :443
|
||||||
|
// if _, err := l.NewListener(); err != nil {
|
||||||
|
// t.Error(err.Error())
|
||||||
|
// }
|
||||||
|
}
|
37
api/server/acme/autocert/cache.go
Normal file
37
api/server/acme/autocert/cache.go
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package autocert
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func homeDir() string {
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
return os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
|
||||||
|
}
|
||||||
|
if h := os.Getenv("HOME"); h != "" {
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
return "/"
|
||||||
|
}
|
||||||
|
|
||||||
|
func cacheDir() string {
|
||||||
|
const base = "golang-autocert"
|
||||||
|
switch runtime.GOOS {
|
||||||
|
case "darwin":
|
||||||
|
return filepath.Join(homeDir(), "Library", "Caches", base)
|
||||||
|
case "windows":
|
||||||
|
for _, ev := range []string{"APPDATA", "CSIDL_APPDATA", "TEMP", "TMP"} {
|
||||||
|
if v := os.Getenv(ev); v != "" {
|
||||||
|
return filepath.Join(v, base)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Worst case:
|
||||||
|
return filepath.Join(homeDir(), base)
|
||||||
|
}
|
||||||
|
if xdg := os.Getenv("XDG_CACHE_HOME"); xdg != "" {
|
||||||
|
return filepath.Join(xdg, base)
|
||||||
|
}
|
||||||
|
return filepath.Join(homeDir(), ".cache", base)
|
||||||
|
}
|
68
api/server/acme/certmagic/certmagic.go
Normal file
68
api/server/acme/certmagic/certmagic.go
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
// Package certmagic is the ACME provider from github.com/caddyserver/certmagic
|
||||||
|
package certmagic
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"math/rand"
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/caddyserver/certmagic"
|
||||||
|
"github.com/micro/go-micro/v2/api/server/acme"
|
||||||
|
"github.com/micro/go-micro/v2/logger"
|
||||||
|
)
|
||||||
|
|
||||||
|
type certmagicProvider struct {
|
||||||
|
opts acme.Options
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: set self-contained options
|
||||||
|
func (c *certmagicProvider) setup() {
|
||||||
|
certmagic.DefaultACME.CA = c.opts.CA
|
||||||
|
if c.opts.ChallengeProvider != nil {
|
||||||
|
// Enabling DNS Challenge disables the other challenges
|
||||||
|
certmagic.DefaultACME.DNSProvider = c.opts.ChallengeProvider
|
||||||
|
}
|
||||||
|
if c.opts.OnDemand {
|
||||||
|
certmagic.Default.OnDemand = new(certmagic.OnDemandConfig)
|
||||||
|
}
|
||||||
|
if c.opts.Cache != nil {
|
||||||
|
// already validated by new()
|
||||||
|
certmagic.Default.Storage = c.opts.Cache.(certmagic.Storage)
|
||||||
|
}
|
||||||
|
// If multiple instances of the provider are running, inject some
|
||||||
|
// randomness so they don't collide
|
||||||
|
// RenewalWindowRatio [0.33 - 0.50)
|
||||||
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
randomRatio := float64(rand.Intn(17)+33) * 0.01
|
||||||
|
certmagic.Default.RenewalWindowRatio = randomRatio
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *certmagicProvider) Listen(hosts ...string) (net.Listener, error) {
|
||||||
|
c.setup()
|
||||||
|
return certmagic.Listen(hosts)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *certmagicProvider) TLSConfig(hosts ...string) (*tls.Config, error) {
|
||||||
|
c.setup()
|
||||||
|
return certmagic.TLS(hosts)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewProvider returns a certmagic provider
|
||||||
|
func NewProvider(options ...acme.Option) acme.Provider {
|
||||||
|
opts := acme.DefaultOptions()
|
||||||
|
|
||||||
|
for _, o := range options {
|
||||||
|
o(&opts)
|
||||||
|
}
|
||||||
|
|
||||||
|
if opts.Cache != nil {
|
||||||
|
if _, ok := opts.Cache.(certmagic.Storage); !ok {
|
||||||
|
logger.Fatal("ACME: cache provided doesn't implement certmagic's Storage interface")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &certmagicProvider{
|
||||||
|
opts: opts,
|
||||||
|
}
|
||||||
|
}
|
147
api/server/acme/certmagic/storage.go
Normal file
147
api/server/acme/certmagic/storage.go
Normal file
@@ -0,0 +1,147 @@
|
|||||||
|
package certmagic
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/gob"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/caddyserver/certmagic"
|
||||||
|
"github.com/micro/go-micro/v2/store"
|
||||||
|
"github.com/micro/go-micro/v2/sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
// File represents a "File" that will be stored in store.Store - the contents and last modified time
|
||||||
|
type File struct {
|
||||||
|
// last modified time
|
||||||
|
LastModified time.Time
|
||||||
|
// Contents
|
||||||
|
Contents []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// storage is an implementation of certmagic.Storage using micro's sync.Map and store.Store interfaces.
|
||||||
|
// As certmagic storage expects a filesystem (with stat() abilities) we have to implement
|
||||||
|
// the bare minimum of metadata.
|
||||||
|
type storage struct {
|
||||||
|
lock sync.Sync
|
||||||
|
store store.Store
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *storage) Lock(key string) error {
|
||||||
|
return s.lock.Lock(key, sync.LockTTL(10*time.Minute))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *storage) Unlock(key string) error {
|
||||||
|
return s.lock.Unlock(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *storage) Store(key string, value []byte) error {
|
||||||
|
f := File{
|
||||||
|
LastModified: time.Now(),
|
||||||
|
Contents: value,
|
||||||
|
}
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
e := gob.NewEncoder(buf)
|
||||||
|
if err := e.Encode(f); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
r := &store.Record{
|
||||||
|
Key: key,
|
||||||
|
Value: buf.Bytes(),
|
||||||
|
}
|
||||||
|
return s.store.Write(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *storage) Load(key string) ([]byte, error) {
|
||||||
|
if !s.Exists(key) {
|
||||||
|
return nil, certmagic.ErrNotExist(errors.New(key + " doesn't exist"))
|
||||||
|
}
|
||||||
|
records, err := s.store.Read(key)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(records) != 1 {
|
||||||
|
return nil, fmt.Errorf("ACME Storage: multiple records matched key %s", key)
|
||||||
|
}
|
||||||
|
b := bytes.NewBuffer(records[0].Value)
|
||||||
|
d := gob.NewDecoder(b)
|
||||||
|
var f File
|
||||||
|
err = d.Decode(&f)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return f.Contents, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *storage) Delete(key string) error {
|
||||||
|
return s.store.Delete(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *storage) Exists(key string) bool {
|
||||||
|
if _, err := s.store.Read(key); err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *storage) List(prefix string, recursive bool) ([]string, error) {
|
||||||
|
keys, err := s.store.List()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
//nolint:prealloc
|
||||||
|
var results []string
|
||||||
|
for _, k := range keys {
|
||||||
|
if strings.HasPrefix(k, prefix) {
|
||||||
|
results = append(results, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if recursive {
|
||||||
|
return results, nil
|
||||||
|
}
|
||||||
|
keysMap := make(map[string]bool)
|
||||||
|
for _, key := range results {
|
||||||
|
dir := strings.Split(strings.TrimPrefix(key, prefix+"/"), "/")
|
||||||
|
keysMap[dir[0]] = true
|
||||||
|
}
|
||||||
|
results = make([]string, 0)
|
||||||
|
for k := range keysMap {
|
||||||
|
results = append(results, path.Join(prefix, k))
|
||||||
|
}
|
||||||
|
return results, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *storage) Stat(key string) (certmagic.KeyInfo, error) {
|
||||||
|
records, err := s.store.Read(key)
|
||||||
|
if err != nil {
|
||||||
|
return certmagic.KeyInfo{}, err
|
||||||
|
}
|
||||||
|
if len(records) != 1 {
|
||||||
|
return certmagic.KeyInfo{}, fmt.Errorf("ACME Storage: multiple records matched key %s", key)
|
||||||
|
}
|
||||||
|
b := bytes.NewBuffer(records[0].Value)
|
||||||
|
d := gob.NewDecoder(b)
|
||||||
|
var f File
|
||||||
|
err = d.Decode(&f)
|
||||||
|
if err != nil {
|
||||||
|
return certmagic.KeyInfo{}, err
|
||||||
|
}
|
||||||
|
return certmagic.KeyInfo{
|
||||||
|
Key: key,
|
||||||
|
Modified: f.LastModified,
|
||||||
|
Size: int64(len(f.Contents)),
|
||||||
|
IsTerminal: false,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewStorage returns a certmagic.Storage backed by a go-micro/lock and go-micro/store
|
||||||
|
func NewStorage(lock sync.Sync, store store.Store) certmagic.Storage {
|
||||||
|
return &storage{
|
||||||
|
lock: lock,
|
||||||
|
store: store,
|
||||||
|
}
|
||||||
|
}
|
73
api/server/acme/options.go
Normal file
73
api/server/acme/options.go
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
package acme
|
||||||
|
|
||||||
|
import "github.com/go-acme/lego/v3/challenge"
|
||||||
|
|
||||||
|
// Option (or Options) are passed to New() to configure providers
|
||||||
|
type Option func(o *Options)
|
||||||
|
|
||||||
|
// Options represents various options you can present to ACME providers
|
||||||
|
type Options struct {
|
||||||
|
// AcceptTLS must be set to true to indicate that you have read your
|
||||||
|
// provider's terms of service.
|
||||||
|
AcceptToS bool
|
||||||
|
// CA is the CA to use
|
||||||
|
CA string
|
||||||
|
// ChallengeProvider is a go-acme/lego challenge provider. Set this if you
|
||||||
|
// want to use DNS Challenges. Otherwise, tls-alpn-01 will be used
|
||||||
|
ChallengeProvider challenge.Provider
|
||||||
|
// Issue certificates for domains on demand. Otherwise, certs will be
|
||||||
|
// retrieved / issued on start-up.
|
||||||
|
OnDemand bool
|
||||||
|
// Cache is a storage interface. Most ACME libraries have an cache, but
|
||||||
|
// there's no defined interface, so if you consume this option
|
||||||
|
// sanity check it before using.
|
||||||
|
Cache interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// AcceptToS indicates whether you accept your CA's terms of service
|
||||||
|
func AcceptToS(b bool) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.AcceptToS = b
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// CA sets the CA of an acme.Options
|
||||||
|
func CA(CA string) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.CA = CA
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ChallengeProvider sets the Challenge provider of an acme.Options
|
||||||
|
// if set, it enables the DNS challenge, otherwise tls-alpn-01 will be used.
|
||||||
|
func ChallengeProvider(p challenge.Provider) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.ChallengeProvider = p
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnDemand enables on-demand certificate issuance. Not recommended for use
|
||||||
|
// with the DNS challenge, as the first connection may be very slow.
|
||||||
|
func OnDemand(b bool) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.OnDemand = b
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cache provides a cache / storage interface to the underlying ACME library
|
||||||
|
// as there is no standard, this needs to be validated by the underlying
|
||||||
|
// implentation.
|
||||||
|
func Cache(c interface{}) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.Cache = c
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// DefaultOptions uses the Let's Encrypt Production CA, with DNS Challenge disabled.
|
||||||
|
func DefaultOptions() Options {
|
||||||
|
return Options{
|
||||||
|
AcceptToS: true,
|
||||||
|
CA: LetsEncryptProductionCA,
|
||||||
|
OnDemand: true,
|
||||||
|
}
|
||||||
|
}
|
44
api/server/cors/cors.go
Normal file
44
api/server/cors/cors.go
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
package cors
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CombinedCORSHandler wraps a server and provides CORS headers
|
||||||
|
func CombinedCORSHandler(h http.Handler) http.Handler {
|
||||||
|
return corsHandler{h}
|
||||||
|
}
|
||||||
|
|
||||||
|
type corsHandler struct {
|
||||||
|
handler http.Handler
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c corsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
SetHeaders(w, r)
|
||||||
|
|
||||||
|
if r.Method == "OPTIONS" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.handler.ServeHTTP(w, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetHeaders sets the CORS headers
|
||||||
|
func SetHeaders(w http.ResponseWriter, r *http.Request) {
|
||||||
|
set := func(w http.ResponseWriter, k, v string) {
|
||||||
|
if v := w.Header().Get(k); len(v) > 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set(k, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
if origin := r.Header.Get("Origin"); len(origin) > 0 {
|
||||||
|
set(w, "Access-Control-Allow-Origin", origin)
|
||||||
|
} else {
|
||||||
|
set(w, "Access-Control-Allow-Origin", "*")
|
||||||
|
}
|
||||||
|
|
||||||
|
set(w, "Access-Control-Allow-Credentials", "true")
|
||||||
|
set(w, "Access-Control-Allow-Methods", "POST, PATCH, GET, OPTIONS, PUT, DELETE")
|
||||||
|
set(w, "Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
|
||||||
|
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user