Compare commits
45 Commits
Author | SHA1 | Date | |
---|---|---|---|
7a39d86018 | |||
06a0a2f336 | |||
|
776ce8591a | ||
48aaef3842 | |||
36aa9f6a28 | |||
|
4e54cd6f7d | ||
7886f3160d | |||
0e86aff88c | |||
|
5d23148038 | ||
8c61439bc5 | |||
7ad1a9c38e | |||
5c829fe029 | |||
3e1ffcb989 | |||
c1e0ce26b5 | |||
26e34124e0 | |||
c1526f70ce | |||
28d23f388f | |||
d3d67eca97 | |||
919bf1486f | |||
88ef21ba9e | |||
|
254b01f75a | ||
|
325b8a43ef | ||
|
d94860c32d | ||
|
778095179d | ||
f71ae35f12 | |||
|
4d27c79081 | ||
|
1eebef8dbc | ||
|
cd2c0b65e1 | ||
|
97e0bf75bb | ||
|
9f56d8b6b5 | ||
|
21f608d728 | ||
ba283695dd | |||
b18465083b | |||
|
11020aa6e5 | ||
|
43c0eb4fda | ||
5ab606c2b5 | |||
6c661c9d72 | |||
a215521821 | |||
|
ec5af022b3 | ||
|
bd88f6e485 | ||
|
34a39be017 | ||
|
360703d8bb | ||
2660a0d94d | |||
|
6e9a693b51 | ||
|
06d197e356 |
19
.github/dependabot.yml
vendored
Normal file
19
.github/dependabot.yml
vendored
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
# 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"
|
||||||
|
|
||||||
|
# Maintain dependencies for Golang
|
||||||
|
- package-ecosystem: "gomod"
|
||||||
|
directory: "/"
|
||||||
|
schedule:
|
||||||
|
interval: "daily"
|
20
.github/renovate.json
vendored
20
.github/renovate.json
vendored
@@ -1,20 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": [
|
|
||||||
"config:base"
|
|
||||||
],
|
|
||||||
"postUpdateOptions": ["gomodTidy"],
|
|
||||||
"packageRules": [
|
|
||||||
{
|
|
||||||
"matchUpdateTypes": ["minor", "patch", "pin", "digest"],
|
|
||||||
"automerge": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"groupName": "all deps",
|
|
||||||
"separateMajorMinor": true,
|
|
||||||
"groupSlug": "all",
|
|
||||||
"packagePatterns": [
|
|
||||||
"*"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
13
.github/stale.sh
vendored
13
.github/stale.sh
vendored
@@ -1,13 +0,0 @@
|
|||||||
#!/bin/bash -ex
|
|
||||||
|
|
||||||
export PATH=$PATH:$(pwd)/bin
|
|
||||||
export GO111MODULE=on
|
|
||||||
export GOBIN=$(pwd)/bin
|
|
||||||
|
|
||||||
#go get github.com/rvflash/goup@v0.4.1
|
|
||||||
|
|
||||||
#goup -v ./...
|
|
||||||
#go get github.com/psampaz/go-mod-outdated@v0.6.0
|
|
||||||
go list -u -m -mod=mod -json all | go-mod-outdated -update -direct -ci || true
|
|
||||||
|
|
||||||
#go list -u -m -json all | go-mod-outdated -update
|
|
75
.github/workflows/codeql-analysis.yml
vendored
Normal file
75
.github/workflows/codeql-analysis.yml
vendored
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
# 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 ]
|
||||||
|
pull_request:
|
||||||
|
# The branches below must be a subset of the branches above
|
||||||
|
branches: [ master ]
|
||||||
|
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 repository
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
|
# Initializes the CodeQL tools for scanning.
|
||||||
|
- name: Initialize CodeQL
|
||||||
|
uses: github/codeql-action/init@v1
|
||||||
|
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@v1
|
||||||
|
|
||||||
|
# ℹ️ 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: Perform CodeQL Analysis
|
||||||
|
uses: github/codeql-action/analyze@v1
|
66
.github/workflows/dependabot-automerge.yml
vendored
Normal file
66
.github/workflows/dependabot-automerge.yml
vendored
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
name: "prautomerge"
|
||||||
|
|
||||||
|
on:
|
||||||
|
workflow_run:
|
||||||
|
workflows: ["prbuild"]
|
||||||
|
types:
|
||||||
|
- completed
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
pull-requests: write
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
Dependabot-Automerge:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
# Contains workaround to execute if dependabot updates the PR by checking for the base branch in the linked PR
|
||||||
|
# The the github.event.workflow_run.event value is 'push' and not 'pull_request'
|
||||||
|
# dont work with multiple workflows when last returns success
|
||||||
|
if: >-
|
||||||
|
github.event.workflow_run.conclusion == 'success'
|
||||||
|
&& github.actor == 'dependabot[bot]'
|
||||||
|
&& github.event.sender.login == 'dependabot[bot]'
|
||||||
|
&& github.event.sender.type == 'Bot'
|
||||||
|
&& (github.event.workflow_run.event == 'pull_request'
|
||||||
|
|| (github.event.workflow_run.event == 'push' && github.event.workflow_run.pull_requests[0].base.ref == github.event.repository.default_branch ))
|
||||||
|
steps:
|
||||||
|
- name: Approve Changes and Merge changes if label 'dependencies' is set
|
||||||
|
uses: actions/github-script@v5
|
||||||
|
with:
|
||||||
|
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
script: |
|
||||||
|
console.log(context.payload.workflow_run);
|
||||||
|
|
||||||
|
var labelNames = await github.paginate(
|
||||||
|
github.issues.listLabelsOnIssue,
|
||||||
|
{
|
||||||
|
repo: context.repo.repo,
|
||||||
|
owner: context.repo.owner,
|
||||||
|
issue_number: context.payload.workflow_run.pull_requests[0].number,
|
||||||
|
},
|
||||||
|
(response) => response.data.map(
|
||||||
|
(label) => label.name
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
console.log(labelNames);
|
||||||
|
|
||||||
|
if (labelNames.includes('dependencies')) {
|
||||||
|
console.log('Found label');
|
||||||
|
|
||||||
|
await github.pulls.createReview({
|
||||||
|
repo: context.repo.repo,
|
||||||
|
owner: context.repo.owner,
|
||||||
|
pull_number: context.payload.workflow_run.pull_requests[0].number,
|
||||||
|
event: 'APPROVE'
|
||||||
|
});
|
||||||
|
console.log('Approved PR');
|
||||||
|
|
||||||
|
await github.pulls.merge({
|
||||||
|
repo: context.repo.repo,
|
||||||
|
owner: context.repo.owner,
|
||||||
|
pull_number: context.payload.workflow_run.pull_requests[0].number,
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('Merged PR');
|
||||||
|
}
|
44
.golangci.yml
Normal file
44
.golangci.yml
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
run:
|
||||||
|
concurrency: 4
|
||||||
|
deadline: 5m
|
||||||
|
issues-exit-code: 1
|
||||||
|
tests: true
|
||||||
|
|
||||||
|
linters-settings:
|
||||||
|
govet:
|
||||||
|
check-shadowing: true
|
||||||
|
enable:
|
||||||
|
- fieldalignment
|
||||||
|
|
||||||
|
linters:
|
||||||
|
enable:
|
||||||
|
- govet
|
||||||
|
- deadcode
|
||||||
|
- errcheck
|
||||||
|
- govet
|
||||||
|
- ineffassign
|
||||||
|
- staticcheck
|
||||||
|
- structcheck
|
||||||
|
- typecheck
|
||||||
|
- unused
|
||||||
|
- varcheck
|
||||||
|
- bodyclose
|
||||||
|
- gci
|
||||||
|
- goconst
|
||||||
|
- gocritic
|
||||||
|
- gosimple
|
||||||
|
- gofmt
|
||||||
|
- gofumpt
|
||||||
|
- goimports
|
||||||
|
- golint
|
||||||
|
- gosec
|
||||||
|
- makezero
|
||||||
|
- misspell
|
||||||
|
- nakedret
|
||||||
|
- nestif
|
||||||
|
- nilerr
|
||||||
|
- noctx
|
||||||
|
- prealloc
|
||||||
|
- unconvert
|
||||||
|
- unparam
|
||||||
|
disable-all: false
|
6
go.mod
6
go.mod
@@ -3,8 +3,6 @@ module github.com/unistack-org/micro-server-http/v3
|
|||||||
go 1.16
|
go 1.16
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/unistack-org/micro/v3 v3.3.13
|
github.com/unistack-org/micro/v3 v3.7.6
|
||||||
golang.org/x/net v0.0.0-20210415231046-e915ea6b2b7d
|
golang.org/x/net v0.0.0-20210928044308-7d9f5e0b762b
|
||||||
)
|
)
|
||||||
|
|
||||||
//replace github.com/unistack-org/micro/v3 => ../../micro
|
|
||||||
|
29
go.sum
29
go.sum
@@ -1,21 +1,28 @@
|
|||||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
|
|
||||||
github.com/ef-ds/deque v1.0.4/go.mod h1:gXDnTC3yqvBcHbq2lcExjtAcVrOnJCbMcZXmuj8Z4tg=
|
github.com/ef-ds/deque v1.0.4/go.mod h1:gXDnTC3yqvBcHbq2lcExjtAcVrOnJCbMcZXmuj8Z4tg=
|
||||||
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
|
github.com/golang-jwt/jwt/v4 v4.1.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
|
||||||
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||||
|
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||||
|
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
|
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
|
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
||||||
github.com/silas/dag v0.0.0-20210121180416-41cf55125c34/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
|
github.com/silas/dag v0.0.0-20210626123444-3804bac2d6d4/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
|
||||||
github.com/unistack-org/micro/v3 v3.3.13 h1:y4bDDkbwnjgOckrhFkC6D/o42tr75X33UbrB+Ko0M68=
|
github.com/unistack-org/micro-proto v0.0.9 h1:KrWLS4FUX7UAWNAilQf70uad6ZPf/0EudeddCXllRVc=
|
||||||
github.com/unistack-org/micro/v3 v3.3.13/go.mod h1:98hNcMXp/WyWJwLwCuwrhN1Jm7aCWaRNsMfRjK8Fq+Y=
|
github.com/unistack-org/micro-proto v0.0.9/go.mod h1:Cckwmzd89gvS7ThxzZp9kQR/EOdksFQcsTAtDDyKwrg=
|
||||||
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
|
github.com/unistack-org/micro/v3 v3.7.6 h1:cobNkaicZR+8nbDWRUmX3/CSLh6ZNSytK2zWth4s4IM=
|
||||||
golang.org/x/net v0.0.0-20210415231046-e915ea6b2b7d h1:BgJvlyh+UqCUaPlscHJ+PN8GcpfrFdr7NHjd1JL0+Gs=
|
github.com/unistack-org/micro/v3 v3.7.6/go.mod h1:Ke/8WJlNZi4ZYwL9HcsANAbQ66/HocTBEZM+od99/mM=
|
||||||
golang.org/x/net v0.0.0-20210415231046-e915ea6b2b7d/go.mod h1:9tjilg8BloeKEkVJvy7fQ90B1CfIiPueXVOjqfkSzI8=
|
golang.org/x/net v0.0.0-20210928044308-7d9f5e0b762b h1:eB48h3HiRycXNy8E0Gf5e0hv7YT6Kt14L/D73G1fuwo=
|
||||||
|
golang.org/x/net v0.0.0-20210928044308-7d9f5e0b762b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
|
||||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
|
||||||
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||||
|
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
|
||||||
|
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
146
handler.go
146
handler.go
@@ -9,55 +9,42 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/unistack-org/micro/v3/codec"
|
|
||||||
"github.com/unistack-org/micro/v3/errors"
|
"github.com/unistack-org/micro/v3/errors"
|
||||||
"github.com/unistack-org/micro/v3/logger"
|
"github.com/unistack-org/micro/v3/logger"
|
||||||
"github.com/unistack-org/micro/v3/metadata"
|
"github.com/unistack-org/micro/v3/metadata"
|
||||||
"github.com/unistack-org/micro/v3/register"
|
"github.com/unistack-org/micro/v3/register"
|
||||||
"github.com/unistack-org/micro/v3/server"
|
"github.com/unistack-org/micro/v3/server"
|
||||||
|
rhttp "github.com/unistack-org/micro/v3/util/http"
|
||||||
rflutil "github.com/unistack-org/micro/v3/util/reflect"
|
rflutil "github.com/unistack-org/micro/v3/util/reflect"
|
||||||
rutil "github.com/unistack-org/micro/v3/util/router"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
DefaultErrorHandler = func(ctx context.Context, s server.Handler, w http.ResponseWriter, r *http.Request, err error, status int) {
|
DefaultErrorHandler = func(ctx context.Context, s server.Handler, w http.ResponseWriter, r *http.Request, err error, status int) {
|
||||||
w.WriteHeader(status)
|
w.WriteHeader(status)
|
||||||
w.Write([]byte(err.Error()))
|
if _, cerr := w.Write([]byte(err.Error())); cerr != nil {
|
||||||
|
logger.DefaultLogger.Errorf(ctx, "write failed: %v", cerr)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
DefaultContentType = "application/json"
|
DefaultContentType = "application/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
type patHandler struct {
|
type patHandler struct {
|
||||||
pat rutil.Pattern
|
|
||||||
mtype *methodType
|
mtype *methodType
|
||||||
name string
|
|
||||||
rcvr reflect.Value
|
rcvr reflect.Value
|
||||||
|
name string
|
||||||
|
pat *rhttp.Trie
|
||||||
}
|
}
|
||||||
|
|
||||||
type httpHandler struct {
|
type httpHandler struct {
|
||||||
name string
|
|
||||||
opts server.HandlerOptions
|
opts server.HandlerOptions
|
||||||
sopts server.Options
|
|
||||||
eps []*register.Endpoint
|
|
||||||
hd interface{}
|
hd interface{}
|
||||||
handlers map[string][]patHandler
|
handlers map[string][]patHandler
|
||||||
|
name string
|
||||||
|
eps []*register.Endpoint
|
||||||
|
sopts server.Options
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *httpHandler) newCodec(ct string) (codec.Codec, error) {
|
|
||||||
h.RLock()
|
|
||||||
defer h.RUnlock()
|
|
||||||
|
|
||||||
if idx := strings.IndexRune(ct, ';'); idx >= 0 {
|
|
||||||
ct = ct[:idx]
|
|
||||||
}
|
|
||||||
|
|
||||||
if cf, ok := h.sopts.Codecs[ct]; ok {
|
|
||||||
return cf, nil
|
|
||||||
}
|
|
||||||
return nil, codec.ErrUnknownContentType
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *httpHandler) Name() string {
|
func (h *httpHandler) Name() string {
|
||||||
return h.name
|
return h.name
|
||||||
}
|
}
|
||||||
@@ -75,12 +62,9 @@ func (h *httpHandler) Options() server.HandlerOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if ph, _, ok := h.pathHandlers.Search(r.Method, r.URL.Path); ok {
|
||||||
for exp, ph := range h.pathHandlers {
|
ph.(http.HandlerFunc)(w, r)
|
||||||
if exp.MatchString(r.URL.String()) {
|
return
|
||||||
ph(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ct := DefaultContentType
|
ct := DefaultContentType
|
||||||
@@ -95,7 +79,23 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := metadata.NewContext(r.Context(), nil)
|
ctx := context.WithValue(r.Context(), rspCodeKey{}, &rspCodeVal{})
|
||||||
|
md, ok := metadata.FromIncomingContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
md = metadata.New(len(r.Header))
|
||||||
|
}
|
||||||
|
for k, v := range r.Header {
|
||||||
|
md.Set(k, strings.Join(v, ", "))
|
||||||
|
}
|
||||||
|
md.Set("RemoteAddr", r.RemoteAddr)
|
||||||
|
md.Set("Method", r.Method)
|
||||||
|
md.Set("URL", r.URL.String())
|
||||||
|
md.Set("Proto", r.Proto)
|
||||||
|
md.Set("ContentLength", fmt.Sprintf("%d", r.ContentLength))
|
||||||
|
md.Set("TransferEncoding", strings.Join(r.TransferEncoding, ","))
|
||||||
|
md.Set("Host", r.Host)
|
||||||
|
md.Set("RequestURI", r.RequestURI)
|
||||||
|
ctx = metadata.NewIncomingContext(ctx, md)
|
||||||
|
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
|
|
||||||
@@ -111,29 +111,19 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
components := strings.Split(path[1:], "/")
|
|
||||||
l := len(components)
|
|
||||||
var verb string
|
|
||||||
idx := strings.LastIndex(components[l-1], ":")
|
|
||||||
if idx == 0 {
|
|
||||||
h.errorHandler(ctx, nil, w, r, fmt.Errorf("not found"), http.StatusNotFound)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if idx > 0 {
|
|
||||||
c := components[l-1]
|
|
||||||
components[l-1], verb = c[:idx], c[idx+1:]
|
|
||||||
}
|
|
||||||
|
|
||||||
matches := make(map[string]interface{})
|
matches := make(map[string]interface{})
|
||||||
|
|
||||||
var match bool
|
var match bool
|
||||||
var hldr patHandler
|
var hldr patHandler
|
||||||
var handler *httpHandler
|
var handler *httpHandler
|
||||||
|
|
||||||
|
//fmt.Printf("try to find handler\n")
|
||||||
for _, hpat := range h.handlers {
|
for _, hpat := range h.handlers {
|
||||||
handlertmp := hpat.(*httpHandler)
|
handlertmp := hpat.(*httpHandler)
|
||||||
for _, hldrtmp := range handlertmp.handlers[r.Method] {
|
for _, hldrtmp := range handlertmp.handlers[r.Method] {
|
||||||
mp, err := hldrtmp.pat.Match(components, verb)
|
//fmt.Printf("ssss method %v path %v %#+v\n", r.Method, path, hldrtmp)
|
||||||
if err == nil {
|
_, mp, ok := hldrtmp.pat.Search(r.Method, path)
|
||||||
|
if ok {
|
||||||
match = true
|
match = true
|
||||||
for k, v := range mp {
|
for k, v := range mp {
|
||||||
matches[k] = v
|
matches[k] = v
|
||||||
@@ -150,20 +140,11 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
md, ok := metadata.FromIncomingContext(ctx)
|
|
||||||
if !ok {
|
|
||||||
md = metadata.New(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
for k, v := range r.Header {
|
|
||||||
md.Set(k, strings.Join(v, ", "))
|
|
||||||
}
|
|
||||||
|
|
||||||
// get fields from url values
|
// get fields from url values
|
||||||
if len(r.URL.RawQuery) > 0 {
|
if len(r.URL.RawQuery) > 0 {
|
||||||
umd, err := rflutil.URLMap(r.URL.RawQuery)
|
umd, cerr := rflutil.URLMap(r.URL.RawQuery)
|
||||||
if err != nil {
|
if cerr != nil {
|
||||||
h.errorHandler(ctx, handler, w, r, err, http.StatusBadRequest)
|
h.errorHandler(ctx, handler, w, r, cerr, http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for k, v := range umd {
|
for k, v := range umd {
|
||||||
@@ -190,7 +171,7 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
replyv = reflect.New(hldr.mtype.ReplyType.Elem())
|
replyv = reflect.New(hldr.mtype.ReplyType.Elem())
|
||||||
|
|
||||||
function := hldr.mtype.method.Func
|
function := hldr.mtype.method.Func
|
||||||
//function := hldr.rcvr
|
// function := hldr.rcvr
|
||||||
var returnValues []reflect.Value
|
var returnValues []reflect.Value
|
||||||
|
|
||||||
if err = cf.ReadBody(r.Body, argv.Interface()); err != nil && err != io.EOF {
|
if err = cf.ReadBody(r.Body, argv.Interface()); err != nil && err != io.EOF {
|
||||||
@@ -217,24 +198,28 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
method: fmt.Sprintf("%s.%s", hldr.name, hldr.mtype.method.Name),
|
method: fmt.Sprintf("%s.%s", hldr.name, hldr.mtype.method.Name),
|
||||||
body: b,
|
body: b,
|
||||||
payload: argv.Interface(),
|
payload: argv.Interface(),
|
||||||
|
header: md,
|
||||||
}
|
}
|
||||||
|
|
||||||
var scode int
|
|
||||||
// define the handler func
|
// define the handler func
|
||||||
fn := func(fctx context.Context, req server.Request, rsp interface{}) (err error) {
|
fn := func(fctx context.Context, req server.Request, rsp interface{}) (err error) {
|
||||||
fctx = context.WithValue(fctx, rspCodeKey{}, &rspCodeVal{})
|
|
||||||
fctx = metadata.NewIncomingContext(fctx, md)
|
|
||||||
returnValues = function.Call([]reflect.Value{hldr.rcvr, hldr.mtype.prepareContext(fctx), reflect.ValueOf(argv.Interface()), reflect.ValueOf(rsp)})
|
returnValues = function.Call([]reflect.Value{hldr.rcvr, hldr.mtype.prepareContext(fctx), reflect.ValueOf(argv.Interface()), reflect.ValueOf(rsp)})
|
||||||
|
|
||||||
scode = GetRspCode(fctx)
|
|
||||||
// The return value for the method is an error.
|
// The return value for the method is an error.
|
||||||
if rerr := returnValues[0].Interface(); rerr != nil {
|
if rerr := returnValues[0].Interface(); rerr != nil {
|
||||||
err = rerr.(error)
|
err = rerr.(error)
|
||||||
}
|
}
|
||||||
|
|
||||||
if md, ok := metadata.FromOutgoingContext(fctx); ok {
|
md, ok := metadata.FromOutgoingContext(ctx)
|
||||||
metadata.SetOutgoingContext(ctx, md)
|
if !ok {
|
||||||
|
md = metadata.New(0)
|
||||||
}
|
}
|
||||||
|
if nmd, ok := metadata.FromOutgoingContext(fctx); ok {
|
||||||
|
for k, v := range nmd {
|
||||||
|
md.Set(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
metadata.SetOutgoingContext(ctx, md)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -253,7 +238,23 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
ct = DefaultContentType
|
ct = DefaultContentType
|
||||||
}
|
}
|
||||||
|
|
||||||
if appErr := fn(ctx, hr, replyv.Interface()); appErr != nil {
|
scode := int(200)
|
||||||
|
appErr := fn(ctx, hr, replyv.Interface())
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", ct)
|
||||||
|
if md, ok := metadata.FromOutgoingContext(ctx); ok {
|
||||||
|
for k, v := range md {
|
||||||
|
w.Header().Set(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if nct := w.Header().Get("Content-Type"); nct != ct {
|
||||||
|
if cf, err = h.newCodec(nct); err != nil {
|
||||||
|
h.errorHandler(ctx, nil, w, r, err, http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if appErr != nil {
|
||||||
switch verr := appErr.(type) {
|
switch verr := appErr.(type) {
|
||||||
case *errors.Error:
|
case *errors.Error:
|
||||||
scode = int(verr.Code)
|
scode = int(verr.Code)
|
||||||
@@ -266,23 +267,18 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
} else {
|
} else {
|
||||||
b, err = cf.Marshal(replyv.Interface())
|
b, err = cf.Marshal(replyv.Interface())
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil && handler.sopts.Logger.V(logger.ErrorLevel) {
|
if err != nil && handler.sopts.Logger.V(logger.ErrorLevel) {
|
||||||
handler.sopts.Logger.Errorf(handler.sopts.Context, "handler err: %v", err)
|
handler.sopts.Logger.Errorf(handler.sopts.Context, "handler err: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if md, ok := metadata.FromOutgoingContext(ctx); ok {
|
if nscode := GetRspCode(ctx); nscode != 0 {
|
||||||
for k, v := range md {
|
scode = nscode
|
||||||
w.Header().Set(k, v)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
w.WriteHeader(scode)
|
||||||
|
|
||||||
w.Header().Set("content-Type", ct)
|
if _, cerr := w.Write(b); cerr != nil {
|
||||||
if scode != 0 {
|
logger.DefaultLogger.Errorf(ctx, "write failed: %v", cerr)
|
||||||
w.WriteHeader(scode)
|
|
||||||
} else {
|
|
||||||
//handler.sopts.Logger.Warn(handler.sopts.Context, "response code not set in handler via SetRspCode(ctx, http.StatusXXX)")
|
|
||||||
w.WriteHeader(200)
|
|
||||||
}
|
}
|
||||||
w.Write(b)
|
|
||||||
}
|
}
|
||||||
|
206
http.go
206
http.go
@@ -8,7 +8,6 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -19,36 +18,46 @@ import (
|
|||||||
"github.com/unistack-org/micro/v3/logger"
|
"github.com/unistack-org/micro/v3/logger"
|
||||||
"github.com/unistack-org/micro/v3/register"
|
"github.com/unistack-org/micro/v3/register"
|
||||||
"github.com/unistack-org/micro/v3/server"
|
"github.com/unistack-org/micro/v3/server"
|
||||||
rutil "github.com/unistack-org/micro/v3/util/router"
|
rhttp "github.com/unistack-org/micro/v3/util/http"
|
||||||
"golang.org/x/net/netutil"
|
"golang.org/x/net/netutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var httpAllMethods = []string{
|
||||||
|
http.MethodConnect,
|
||||||
|
http.MethodDelete,
|
||||||
|
http.MethodGet,
|
||||||
|
http.MethodHead,
|
||||||
|
http.MethodOptions,
|
||||||
|
http.MethodPatch,
|
||||||
|
http.MethodPost,
|
||||||
|
http.MethodPut,
|
||||||
|
http.MethodTrace,
|
||||||
|
}
|
||||||
|
|
||||||
type httpServer struct {
|
type httpServer struct {
|
||||||
sync.RWMutex
|
hd server.Handler
|
||||||
opts server.Options
|
|
||||||
handlers map[string]server.Handler
|
|
||||||
hd server.Handler
|
|
||||||
exit chan chan error
|
|
||||||
subscribers map[*httpSubscriber][]broker.Subscriber
|
|
||||||
// used for first registration
|
|
||||||
registered bool
|
|
||||||
// register service instance
|
|
||||||
rsvc *register.Service
|
rsvc *register.Service
|
||||||
init bool
|
handlers map[string]server.Handler
|
||||||
|
exit chan chan error
|
||||||
|
subscribers map[*httpSubscriber][]broker.Subscriber
|
||||||
errorHandler func(context.Context, server.Handler, http.ResponseWriter, *http.Request, error, int)
|
errorHandler func(context.Context, server.Handler, http.ResponseWriter, *http.Request, error, int)
|
||||||
pathHandlers map[*regexp.Regexp]http.HandlerFunc
|
pathHandlers *rhttp.Trie
|
||||||
contentTypeHandlers map[string]http.HandlerFunc
|
contentTypeHandlers map[string]http.HandlerFunc
|
||||||
|
opts server.Options
|
||||||
|
registerRPC bool
|
||||||
|
sync.RWMutex
|
||||||
|
registered bool
|
||||||
|
init bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *httpServer) newCodec(ct string) (codec.Codec, error) {
|
func (h *httpServer) newCodec(ct string) (codec.Codec, error) {
|
||||||
h.RLock()
|
|
||||||
defer h.RUnlock()
|
|
||||||
|
|
||||||
if idx := strings.IndexRune(ct, ';'); idx >= 0 {
|
if idx := strings.IndexRune(ct, ';'); idx >= 0 {
|
||||||
ct = ct[:idx]
|
ct = ct[:idx]
|
||||||
}
|
}
|
||||||
|
h.RLock()
|
||||||
if cf, ok := h.opts.Codecs[ct]; ok {
|
cf, ok := h.opts.Codecs[ct]
|
||||||
|
h.RUnlock()
|
||||||
|
if ok {
|
||||||
return cf, nil
|
return cf, nil
|
||||||
}
|
}
|
||||||
return nil, codec.ErrUnknownContentType
|
return nil, codec.ErrUnknownContentType
|
||||||
@@ -67,7 +76,6 @@ func (h *httpServer) Init(opts ...server.Option) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
h.Lock()
|
h.Lock()
|
||||||
defer h.Unlock()
|
|
||||||
|
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(&h.opts)
|
o(&h.opts)
|
||||||
@@ -79,18 +87,19 @@ func (h *httpServer) Init(opts ...server.Option) error {
|
|||||||
h.handlers = make(map[string]server.Handler)
|
h.handlers = make(map[string]server.Handler)
|
||||||
}
|
}
|
||||||
if h.pathHandlers == nil {
|
if h.pathHandlers == nil {
|
||||||
h.pathHandlers = make(map[*regexp.Regexp]http.HandlerFunc)
|
h.pathHandlers = rhttp.NewTrie()
|
||||||
}
|
}
|
||||||
if h.contentTypeHandlers == nil {
|
if h.contentTypeHandlers == nil {
|
||||||
h.contentTypeHandlers = make(map[string]http.HandlerFunc)
|
h.contentTypeHandlers = make(map[string]http.HandlerFunc)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if v, ok := h.opts.Context.Value(registerRPCHandlerKey{}).(bool); ok {
|
||||||
|
h.registerRPC = v
|
||||||
|
}
|
||||||
|
|
||||||
if phs, ok := h.opts.Context.Value(pathHandlerKey{}).(*pathHandlerVal); ok && phs.h != nil {
|
if phs, ok := h.opts.Context.Value(pathHandlerKey{}).(*pathHandlerVal); ok && phs.h != nil {
|
||||||
for pp, ph := range phs.h {
|
for pp, ph := range phs.h {
|
||||||
exp, err := regexp.Compile(pp)
|
h.pathHandlers.Insert(httpAllMethods, pp, ph)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
h.pathHandlers[exp] = ph
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if phs, ok := h.opts.Context.Value(contentTypeHandlerKey{}).(*contentTypeHandlerVal); ok && phs.h != nil {
|
if phs, ok := h.opts.Context.Value(contentTypeHandlerKey{}).(*contentTypeHandlerVal); ok && phs.h != nil {
|
||||||
@@ -98,64 +107,81 @@ func (h *httpServer) Init(opts ...server.Option) error {
|
|||||||
h.contentTypeHandlers[pp] = ph
|
h.contentTypeHandlers[pp] = ph
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
h.Unlock()
|
||||||
|
|
||||||
|
h.RLock()
|
||||||
if err := h.opts.Register.Init(); err != nil {
|
if err := h.opts.Register.Init(); err != nil {
|
||||||
|
h.RUnlock()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := h.opts.Broker.Init(); err != nil {
|
if err := h.opts.Broker.Init(); err != nil {
|
||||||
|
h.RUnlock()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := h.opts.Tracer.Init(); err != nil {
|
if err := h.opts.Tracer.Init(); err != nil {
|
||||||
|
h.RUnlock()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := h.opts.Auth.Init(); err != nil {
|
if err := h.opts.Auth.Init(); err != nil {
|
||||||
|
h.RUnlock()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := h.opts.Logger.Init(); err != nil {
|
if err := h.opts.Logger.Init(); err != nil {
|
||||||
|
h.RUnlock()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := h.opts.Meter.Init(); err != nil {
|
if err := h.opts.Meter.Init(); err != nil {
|
||||||
|
h.RUnlock()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := h.opts.Transport.Init(); err != nil {
|
if err := h.opts.Transport.Init(); err != nil {
|
||||||
|
h.RUnlock()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
h.RUnlock()
|
||||||
|
|
||||||
|
h.Lock()
|
||||||
h.init = true
|
h.init = true
|
||||||
|
h.Unlock()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *httpServer) Handle(handler server.Handler) error {
|
func (h *httpServer) Handle(handler server.Handler) error {
|
||||||
h.Lock()
|
hdlr, ok := handler.(*httpHandler)
|
||||||
if hdlr, ok := handler.(*httpHandler); ok {
|
if !ok {
|
||||||
if _, ok := hdlr.hd.(http.Handler); ok {
|
h.Lock()
|
||||||
h.hd = handler
|
|
||||||
} else {
|
|
||||||
if h.handlers == nil {
|
|
||||||
h.handlers = make(map[string]server.Handler)
|
|
||||||
}
|
|
||||||
h.handlers[handler.Name()] = handler
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
h.hd = handler
|
h.hd = handler
|
||||||
|
h.Unlock()
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if _, ok := hdlr.hd.(http.Handler); ok {
|
||||||
|
h.Lock()
|
||||||
|
h.hd = handler
|
||||||
|
h.Unlock()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
h.Lock()
|
||||||
|
if h.handlers == nil {
|
||||||
|
h.handlers = make(map[string]server.Handler)
|
||||||
|
}
|
||||||
|
h.handlers[handler.Name()] = handler
|
||||||
h.Unlock()
|
h.Unlock()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *httpServer) NewHandler(handler interface{}, opts ...server.HandlerOption) server.Handler {
|
func (h *httpServer) NewHandler(handler interface{}, opts ...server.HandlerOption) server.Handler {
|
||||||
options := server.NewHandlerOptions(opts...)
|
options := server.NewHandlerOptions(opts...)
|
||||||
|
|
||||||
var eps []*register.Endpoint
|
eps := make([]*register.Endpoint, 0, len(options.Metadata))
|
||||||
|
for name, metadata := range options.Metadata {
|
||||||
if !options.Internal {
|
eps = append(eps, ®ister.Endpoint{
|
||||||
for name, metadata := range options.Metadata {
|
Name: name,
|
||||||
eps = append(eps, ®ister.Endpoint{
|
Metadata: metadata,
|
||||||
Name: name,
|
})
|
||||||
Metadata: metadata,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
hdlr := &httpHandler{
|
hdlr := &httpHandler{
|
||||||
@@ -166,35 +192,12 @@ func (h *httpServer) NewHandler(handler interface{}, opts ...server.HandlerOptio
|
|||||||
}
|
}
|
||||||
|
|
||||||
tp := reflect.TypeOf(handler)
|
tp := reflect.TypeOf(handler)
|
||||||
|
type nilHandler struct{}
|
||||||
/*
|
|
||||||
for m := 0; m < tp.NumMethod(); m++ {
|
|
||||||
if e := register.ExtractEndpoint(tp.Method(m)); e != nil {
|
|
||||||
e.Name = name + "." + e.Name
|
|
||||||
|
|
||||||
for k, v := range options.Metadata[e.Name] {
|
|
||||||
e.Metadata[k] = v
|
|
||||||
}
|
|
||||||
|
|
||||||
eps = append(eps, e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
hdlr.handlers = make(map[string][]patHandler)
|
hdlr.handlers = make(map[string][]patHandler)
|
||||||
for hn, md := range options.Metadata {
|
for hn, md := range options.Metadata {
|
||||||
cmp, err := rutil.Parse(md["Path"])
|
pat := rhttp.NewTrie()
|
||||||
if err != nil && h.opts.Logger.V(logger.ErrorLevel) {
|
pat.Insert([]string{md["Method"]}, md["Path"], &nilHandler{})
|
||||||
h.opts.Logger.Errorf(h.opts.Context, "parsing path pattern err: %v", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
tpl := cmp.Compile()
|
|
||||||
pat, err := rutil.NewPattern(tpl.Version, tpl.OpCodes, tpl.Pool, tpl.Verb)
|
|
||||||
if err != nil && h.opts.Logger.V(logger.ErrorLevel) {
|
|
||||||
h.opts.Logger.Errorf(h.opts.Context, "creating new pattern err: %v", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
var method reflect.Method
|
var method reflect.Method
|
||||||
mname := hn[strings.Index(hn, ".")+1:]
|
mname := hn[strings.Index(hn, ".")+1:]
|
||||||
@@ -226,6 +229,16 @@ func (h *httpServer) NewHandler(handler interface{}, opts ...server.HandlerOptio
|
|||||||
pth := patHandler{pat: pat, mtype: mtype, name: name, rcvr: rcvr}
|
pth := patHandler{pat: pat, mtype: mtype, name: name, rcvr: rcvr}
|
||||||
hdlr.name = name
|
hdlr.name = name
|
||||||
hdlr.handlers[md["Method"]] = append(hdlr.handlers[md["Method"]], pth)
|
hdlr.handlers[md["Method"]] = append(hdlr.handlers[md["Method"]], pth)
|
||||||
|
|
||||||
|
if !h.registerRPC {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
rpat := rhttp.NewTrie()
|
||||||
|
rpat.Insert([]string{http.MethodPost}, "/"+hn, &nilHandler{})
|
||||||
|
|
||||||
|
pth = patHandler{pat: rpat, mtype: mtype, name: name, rcvr: rcvr}
|
||||||
|
hdlr.handlers[http.MethodPost] = append(hdlr.handlers[http.MethodPost], pth)
|
||||||
}
|
}
|
||||||
|
|
||||||
return hdlr
|
return hdlr
|
||||||
@@ -248,13 +261,15 @@ func (h *httpServer) Subscribe(sb server.Subscriber) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
h.Lock()
|
h.RLock()
|
||||||
defer h.Unlock()
|
|
||||||
_, ok = h.subscribers[sub]
|
_, ok = h.subscribers[sub]
|
||||||
|
h.RUnlock()
|
||||||
if ok {
|
if ok {
|
||||||
return fmt.Errorf("subscriber %v already exists", h)
|
return fmt.Errorf("subscriber %v already exists", h)
|
||||||
}
|
}
|
||||||
|
h.Lock()
|
||||||
h.subscribers[sub] = nil
|
h.subscribers[sub] = nil
|
||||||
|
h.Unlock()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -284,12 +299,10 @@ func (h *httpServer) Register() error {
|
|||||||
service.Endpoints = eps
|
service.Endpoints = eps
|
||||||
|
|
||||||
h.Lock()
|
h.Lock()
|
||||||
var subscriberList []*httpSubscriber
|
subscriberList := make([]*httpSubscriber, 0, len(h.subscribers))
|
||||||
for e := range h.subscribers {
|
for e := range h.subscribers {
|
||||||
// Only advertise non internal subscribers
|
// Only advertise non internal subscribers
|
||||||
if !e.Options().Internal {
|
subscriberList = append(subscriberList, e)
|
||||||
subscriberList = append(subscriberList, e)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
sort.Slice(subscriberList, func(i, j int) bool {
|
sort.Slice(subscriberList, func(i, j int) bool {
|
||||||
return subscriberList[i].topic > subscriberList[j].topic
|
return subscriberList[i].topic > subscriberList[j].topic
|
||||||
@@ -306,7 +319,7 @@ func (h *httpServer) Register() error {
|
|||||||
|
|
||||||
if !registered {
|
if !registered {
|
||||||
if config.Logger.V(logger.InfoLevel) {
|
if config.Logger.V(logger.InfoLevel) {
|
||||||
config.Logger.Infof(config.Context, "Register [%s] Registering node: %s", config.Register.String(), service.Nodes[0].Id)
|
config.Logger.Infof(config.Context, "Register [%s] Registering node: %s", config.Register.String(), service.Nodes[0].ID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -321,8 +334,6 @@ func (h *httpServer) Register() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
h.Lock()
|
h.Lock()
|
||||||
defer h.Unlock()
|
|
||||||
|
|
||||||
for sb := range h.subscribers {
|
for sb := range h.subscribers {
|
||||||
handler := h.createSubHandler(sb, config)
|
handler := h.createSubHandler(sb, config)
|
||||||
var opts []broker.SubscribeOption
|
var opts []broker.SubscribeOption
|
||||||
@@ -339,6 +350,7 @@ func (h *httpServer) Register() error {
|
|||||||
|
|
||||||
sub, err := config.Broker.Subscribe(subCtx, sb.Topic(), handler, opts...)
|
sub, err := config.Broker.Subscribe(subCtx, sb.Topic(), handler, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
h.Unlock()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
h.subscribers[sb] = []broker.Subscriber{sub}
|
h.subscribers[sb] = []broker.Subscriber{sub}
|
||||||
@@ -346,6 +358,7 @@ func (h *httpServer) Register() error {
|
|||||||
|
|
||||||
h.registered = true
|
h.registered = true
|
||||||
h.rsvc = service
|
h.rsvc = service
|
||||||
|
h.Unlock()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -361,7 +374,7 @@ func (h *httpServer) Deregister() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if config.Logger.V(logger.InfoLevel) {
|
if config.Logger.V(logger.InfoLevel) {
|
||||||
config.Logger.Infof(config.Context, "Deregistering node: %s", service.Nodes[0].Id)
|
config.Logger.Infof(config.Context, "Deregistering node: %s", service.Nodes[0].ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := server.DefaultDeregisterFunc(service, config); err != nil {
|
if err := server.DefaultDeregisterFunc(service, config); err != nil {
|
||||||
@@ -387,6 +400,7 @@ func (h *httpServer) Deregister() error {
|
|||||||
for _, sub := range subs {
|
for _, sub := range subs {
|
||||||
config.Logger.Infof(config.Context, "Unsubscribing from topic: %s", sub.Topic())
|
config.Logger.Infof(config.Context, "Unsubscribing from topic: %s", sub.Topic())
|
||||||
if err := sub.Unsubscribe(subCtx); err != nil {
|
if err := sub.Unsubscribe(subCtx); err != nil {
|
||||||
|
h.Unlock()
|
||||||
config.Logger.Errorf(config.Context, "failed to unsubscribe topic: %s, error: %v", sb.Topic(), err)
|
config.Logger.Errorf(config.Context, "failed to unsubscribe topic: %s, error: %v", sb.Topic(), err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -437,6 +451,7 @@ func (h *httpServer) Start() error {
|
|||||||
var handler http.Handler
|
var handler http.Handler
|
||||||
var srvFunc func(net.Listener) error
|
var srvFunc func(net.Listener) error
|
||||||
|
|
||||||
|
// nolint: nestif
|
||||||
if h.opts.Context != nil {
|
if h.opts.Context != nil {
|
||||||
if hs, ok := h.opts.Context.Value(serverKey{}).(*http.Server); ok && hs != nil {
|
if hs, ok := h.opts.Context.Value(serverKey{}).(*http.Server); ok && hs != nil {
|
||||||
if hs.Handler == nil && h.hd != nil {
|
if hs.Handler == nil && h.hd != nil {
|
||||||
@@ -468,7 +483,7 @@ func (h *httpServer) Start() error {
|
|||||||
|
|
||||||
if err := config.RegisterCheck(h.opts.Context); err != nil {
|
if err := config.RegisterCheck(h.opts.Context); err != nil {
|
||||||
if config.Logger.V(logger.ErrorLevel) {
|
if config.Logger.V(logger.ErrorLevel) {
|
||||||
config.Logger.Errorf(config.Context, "Server %s-%s register check error: %s", config.Name, config.Id, err)
|
config.Logger.Errorf(config.Context, "Server %s-%s register check error: %s", config.Name, config.ID, err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if err = h.Register(); err != nil {
|
if err = h.Register(); err != nil {
|
||||||
@@ -492,9 +507,17 @@ func (h *httpServer) Start() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if srvFunc != nil {
|
if srvFunc != nil {
|
||||||
go srvFunc(ts)
|
go func() {
|
||||||
|
if cerr := srvFunc(ts); cerr != nil && !strings.Contains(cerr.Error(), "use of closed network connection") {
|
||||||
|
h.opts.Logger.Error(h.opts.Context, cerr)
|
||||||
|
}
|
||||||
|
}()
|
||||||
} else {
|
} else {
|
||||||
go http.Serve(ts, fn)
|
go func() {
|
||||||
|
if cerr := http.Serve(ts, fn); cerr != nil && !strings.Contains(cerr.Error(), "use of closed network connection") {
|
||||||
|
h.opts.Logger.Error(h.opts.Context, cerr)
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
@@ -518,25 +541,26 @@ func (h *httpServer) Start() error {
|
|||||||
registered := h.registered
|
registered := h.registered
|
||||||
h.RUnlock()
|
h.RUnlock()
|
||||||
rerr := config.RegisterCheck(h.opts.Context)
|
rerr := config.RegisterCheck(h.opts.Context)
|
||||||
|
// nolint: nestif
|
||||||
if rerr != nil && registered {
|
if rerr != nil && registered {
|
||||||
if config.Logger.V(logger.ErrorLevel) {
|
if config.Logger.V(logger.ErrorLevel) {
|
||||||
config.Logger.Errorf(config.Context, "Server %s-%s register check error: %s, deregister it", config.Name, config.Id, rerr)
|
config.Logger.Errorf(config.Context, "Server %s-%s register check error: %s, deregister it", config.Name, config.ID, rerr)
|
||||||
}
|
}
|
||||||
// deregister self in case of error
|
// deregister self in case of error
|
||||||
if err := h.Deregister(); err != nil {
|
if err := h.Deregister(); err != nil {
|
||||||
if config.Logger.V(logger.ErrorLevel) {
|
if config.Logger.V(logger.ErrorLevel) {
|
||||||
config.Logger.Errorf(config.Context, "Server %s-%s deregister error: %s", config.Name, config.Id, err)
|
config.Logger.Errorf(config.Context, "Server %s-%s deregister error: %s", config.Name, config.ID, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if rerr != nil && !registered {
|
} else if rerr != nil && !registered {
|
||||||
if config.Logger.V(logger.ErrorLevel) {
|
if config.Logger.V(logger.ErrorLevel) {
|
||||||
config.Logger.Errorf(config.Context, "Server %s-%s register check error: %s", config.Name, config.Id, rerr)
|
config.Logger.Errorf(config.Context, "Server %s-%s register check error: %s", config.Name, config.ID, rerr)
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if err := h.Register(); err != nil {
|
if err := h.Register(); err != nil {
|
||||||
if config.Logger.V(logger.ErrorLevel) {
|
if config.Logger.V(logger.ErrorLevel) {
|
||||||
config.Logger.Errorf(config.Context, "Server %s-%s register error: %s", config.Name, config.Id, err)
|
config.Logger.Errorf(config.Context, "Server %s-%s register error: %s", config.Name, config.ID, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -549,8 +573,6 @@ func (h *httpServer) Start() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ch <- ts.Close()
|
|
||||||
|
|
||||||
// deregister
|
// deregister
|
||||||
if err := h.Deregister(); err != nil {
|
if err := h.Deregister(); err != nil {
|
||||||
config.Logger.Errorf(config.Context, "Server deregister error: %s", err)
|
config.Logger.Errorf(config.Context, "Server deregister error: %s", err)
|
||||||
@@ -559,6 +581,8 @@ func (h *httpServer) Start() error {
|
|||||||
if err := config.Broker.Disconnect(config.Context); err != nil {
|
if err := config.Broker.Disconnect(config.Context); err != nil {
|
||||||
config.Logger.Errorf(config.Context, "Broker disconnect error: %s", err)
|
config.Logger.Errorf(config.Context, "Broker disconnect error: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ch <- ts.Close()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -585,6 +609,6 @@ func NewServer(opts ...server.Option) server.Server {
|
|||||||
exit: make(chan chan error),
|
exit: make(chan chan error),
|
||||||
subscribers: make(map[*httpSubscriber][]broker.Subscriber),
|
subscribers: make(map[*httpSubscriber][]broker.Subscriber),
|
||||||
errorHandler: DefaultErrorHandler,
|
errorHandler: DefaultErrorHandler,
|
||||||
pathHandlers: make(map[*regexp.Regexp]http.HandlerFunc),
|
pathHandlers: rhttp.NewTrie(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -6,12 +6,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type httpMessage struct {
|
type httpMessage struct {
|
||||||
topic string
|
|
||||||
payload interface{}
|
payload interface{}
|
||||||
contentType string
|
|
||||||
header metadata.Metadata
|
|
||||||
body []byte
|
|
||||||
codec codec.Codec
|
codec codec.Codec
|
||||||
|
header metadata.Metadata
|
||||||
|
topic string
|
||||||
|
contentType string
|
||||||
|
body []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *httpMessage) Topic() string {
|
func (r *httpMessage) Topic() string {
|
||||||
|
47
options.go
47
options.go
@@ -8,16 +8,19 @@ import (
|
|||||||
"github.com/unistack-org/micro/v3/server"
|
"github.com/unistack-org/micro/v3/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
type rspCodeKey struct{}
|
|
||||||
type rspCodeVal struct {
|
|
||||||
code int
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetError pass error to caller
|
// SetError pass error to caller
|
||||||
func SetError(err interface{}) error {
|
func SetError(err interface{}) error {
|
||||||
return &Error{err: err}
|
return &Error{err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetError return underline error
|
||||||
|
func GetError(err interface{}) interface{} {
|
||||||
|
if verr, ok := err.(*Error); ok {
|
||||||
|
return verr.err
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// Error struct holds error
|
// Error struct holds error
|
||||||
type Error struct {
|
type Error struct {
|
||||||
err interface{}
|
err interface{}
|
||||||
@@ -28,6 +31,13 @@ func (err *Error) Error() string {
|
|||||||
return fmt.Sprintf("%v", err.err)
|
return fmt.Sprintf("%v", err.err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type (
|
||||||
|
rspCodeKey struct{}
|
||||||
|
rspCodeVal struct {
|
||||||
|
code int
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
// SetRspCode saves response code in context, must be used by handler to specify http code
|
// SetRspCode saves response code in context, must be used by handler to specify http code
|
||||||
func SetRspCode(ctx context.Context, code int) {
|
func SetRspCode(ctx context.Context, code int) {
|
||||||
if rsp, ok := ctx.Value(rspCodeKey{}).(*rspCodeVal); ok {
|
if rsp, ok := ctx.Value(rspCodeKey{}).(*rspCodeVal); ok {
|
||||||
@@ -65,10 +75,12 @@ func ErrorHandler(fn func(ctx context.Context, s server.Handler, w http.Response
|
|||||||
return server.SetOption(errorHandlerKey{}, fn)
|
return server.SetOption(errorHandlerKey{}, fn)
|
||||||
}
|
}
|
||||||
|
|
||||||
type pathHandlerKey struct{}
|
type (
|
||||||
type pathHandlerVal struct {
|
pathHandlerKey struct{}
|
||||||
h map[string]http.HandlerFunc
|
pathHandlerVal struct {
|
||||||
}
|
h map[string]http.HandlerFunc
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
// PathHandler specifies http handler for path regexp
|
// PathHandler specifies http handler for path regexp
|
||||||
func PathHandler(path string, h http.HandlerFunc) server.Option {
|
func PathHandler(path string, h http.HandlerFunc) server.Option {
|
||||||
@@ -85,10 +97,12 @@ func PathHandler(path string, h http.HandlerFunc) server.Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type contentTypeHandlerKey struct{}
|
type (
|
||||||
type contentTypeHandlerVal struct {
|
contentTypeHandlerKey struct{}
|
||||||
h map[string]http.HandlerFunc
|
contentTypeHandlerVal struct {
|
||||||
}
|
h map[string]http.HandlerFunc
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
// ContentTypeHandler specifies http handler for Content-Type
|
// ContentTypeHandler specifies http handler for Content-Type
|
||||||
func ContentTypeHandler(ct string, h http.HandlerFunc) server.Option {
|
func ContentTypeHandler(ct string, h http.HandlerFunc) server.Option {
|
||||||
@@ -104,3 +118,10 @@ func ContentTypeHandler(ct string, h http.HandlerFunc) server.Option {
|
|||||||
o.Context = context.WithValue(o.Context, contentTypeHandlerKey{}, v)
|
o.Context = context.WithValue(o.Context, contentTypeHandlerKey{}, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type registerRPCHandlerKey struct{}
|
||||||
|
|
||||||
|
// RegisterRPCHandler registers compatibility endpoints with /ServiceName.ServiceEndpoint method POST
|
||||||
|
func RegisterRPCHandler(b bool) server.Option {
|
||||||
|
return server.SetOption(registerRPCHandlerKey{}, b)
|
||||||
|
}
|
||||||
|
25
request.go
25
request.go
@@ -5,29 +5,34 @@ import (
|
|||||||
|
|
||||||
"github.com/unistack-org/micro/v3/codec"
|
"github.com/unistack-org/micro/v3/codec"
|
||||||
"github.com/unistack-org/micro/v3/metadata"
|
"github.com/unistack-org/micro/v3/metadata"
|
||||||
|
"github.com/unistack-org/micro/v3/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ server.Request = &rpcRequest{}
|
||||||
|
_ server.Message = &rpcMessage{}
|
||||||
)
|
)
|
||||||
|
|
||||||
type rpcRequest struct {
|
type rpcRequest struct {
|
||||||
rw io.ReadWriter
|
rw io.ReadWriter
|
||||||
service string
|
payload interface{}
|
||||||
method string
|
|
||||||
endpoint string
|
|
||||||
target string
|
|
||||||
contentType string
|
|
||||||
codec codec.Codec
|
codec codec.Codec
|
||||||
header metadata.Metadata
|
header metadata.Metadata
|
||||||
|
method string
|
||||||
|
endpoint string
|
||||||
|
contentType string
|
||||||
|
service string
|
||||||
body []byte
|
body []byte
|
||||||
stream bool
|
stream bool
|
||||||
payload interface{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type rpcMessage struct {
|
type rpcMessage struct {
|
||||||
|
payload interface{}
|
||||||
|
codec codec.Codec
|
||||||
|
header metadata.Metadata
|
||||||
topic string
|
topic string
|
||||||
contentType string
|
contentType string
|
||||||
payload interface{}
|
|
||||||
header metadata.Metadata
|
|
||||||
body []byte
|
body []byte
|
||||||
codec codec.Codec
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *rpcRequest) ContentType() string {
|
func (r *rpcRequest) ContentType() string {
|
||||||
@@ -43,7 +48,7 @@ func (r *rpcRequest) Method() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *rpcRequest) Endpoint() string {
|
func (r *rpcRequest) Endpoint() string {
|
||||||
return r.method
|
return r.endpoint
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *rpcRequest) Codec() codec.Codec {
|
func (r *rpcRequest) Codec() codec.Codec {
|
||||||
|
@@ -11,10 +11,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type methodType struct {
|
type methodType struct {
|
||||||
method reflect.Method
|
|
||||||
ArgType reflect.Type
|
ArgType reflect.Type
|
||||||
ReplyType reflect.Type
|
ReplyType reflect.Type
|
||||||
ContextType reflect.Type
|
ContextType reflect.Type
|
||||||
|
method reflect.Method
|
||||||
stream bool
|
stream bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,15 +62,14 @@ func prepareEndpoint(method reflect.Method) (*methodType, error) {
|
|||||||
return nil, fmt.Errorf("method %v of %v has wrong number of ins: %v", mname, mtype, mtype.NumIn())
|
return nil, fmt.Errorf("method %v of %v has wrong number of ins: %v", mname, mtype, mtype.NumIn())
|
||||||
}
|
}
|
||||||
|
|
||||||
if stream {
|
switch stream {
|
||||||
|
case true:
|
||||||
// check stream type
|
// check stream type
|
||||||
streamType := reflect.TypeOf((*server.Stream)(nil)).Elem()
|
streamType := reflect.TypeOf((*server.Stream)(nil)).Elem()
|
||||||
if !argType.Implements(streamType) {
|
if !argType.Implements(streamType) {
|
||||||
return nil, fmt.Errorf("%v argument does not implement Streamer interface: %v", mname, argType)
|
return nil, fmt.Errorf("%v argument does not implement Streamer interface: %v", mname, argType)
|
||||||
}
|
}
|
||||||
} else {
|
default:
|
||||||
// if not stream check the replyType
|
|
||||||
|
|
||||||
// First arg need not be a pointer.
|
// First arg need not be a pointer.
|
||||||
if !isExportedOrBuiltinType(argType) {
|
if !isExportedOrBuiltinType(argType) {
|
||||||
return nil, fmt.Errorf("%v argument type not exported: %v", mname, argType)
|
return nil, fmt.Errorf("%v argument type not exported: %v", mname, argType)
|
||||||
|
@@ -14,16 +14,12 @@ import (
|
|||||||
"github.com/unistack-org/micro/v3/server"
|
"github.com/unistack-org/micro/v3/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
subSig = "func(context.Context, interface{}) error"
|
|
||||||
)
|
|
||||||
|
|
||||||
var typeOfError = reflect.TypeOf((*error)(nil)).Elem()
|
var typeOfError = reflect.TypeOf((*error)(nil)).Elem()
|
||||||
|
|
||||||
type handler struct {
|
type handler struct {
|
||||||
method reflect.Value
|
|
||||||
reqType reflect.Type
|
reqType reflect.Type
|
||||||
ctxType reflect.Type
|
ctxType reflect.Type
|
||||||
|
method reflect.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
type httpSubscriber struct {
|
type httpSubscriber struct {
|
||||||
@@ -116,7 +112,7 @@ func (s *httpServer) createSubHandler(sb *httpSubscriber, opts server.Options) b
|
|||||||
|
|
||||||
hdr := metadata.Copy(msg.Header)
|
hdr := metadata.Copy(msg.Header)
|
||||||
delete(hdr, "Content-Type")
|
delete(hdr, "Content-Type")
|
||||||
ctx := metadata.NewContext(context.Background(), hdr)
|
ctx := metadata.NewIncomingContext(context.Background(), hdr)
|
||||||
|
|
||||||
results := make(chan error, len(sb.handlers))
|
results := make(chan error, len(sb.handlers))
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user