-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit cd77814
Showing
117 changed files
with
8,151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# EditorConfig is awesome: https://EditorConfig.org | ||
|
||
# top-most EditorConfig file | ||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 2 | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.go|*.go.tpl] | ||
indent_style = tab | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false | ||
|
||
|
||
[*.py] | ||
indent_size = 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
*.iml | ||
*.o | ||
*.a | ||
*.so | ||
_obj | ||
_test | ||
|
||
*.[568vq] | ||
[568vq].out | ||
|
||
*.cgo1.go | ||
*.cgo2.c | ||
_cgo_defun.c | ||
_cgo_gotypes.go | ||
_cgo_export.* | ||
|
||
_testmain.go | ||
|
||
*.exe | ||
*.test | ||
*.prof | ||
|
||
.DS_Store | ||
.idea/ | ||
coverage.out | ||
|
||
/dist | ||
/config.yaml | ||
logs/*.log | ||
logs/*.gz | ||
__debug_bin | ||
.idea | ||
.venv | ||
go.sum | ||
|
||
!.gitkeep | ||
!.gitignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
yinheli <[email protected]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
(The MIT License) | ||
|
||
Copyright (c) 2018-2019 xinpianchang.com | ||
Copyright (c) 2019 Tang Ye <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
'Software'), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# xservice [WIP] | ||
|
||
Another excellent micro service framework | ||
|
||
## Features | ||
|
||
- RESTFull api (base echo/v4) | ||
- gRPC & gRPC gateway service & swagger document generation | ||
- Service discovery (with ETCD/v3) | ||
- Embed toolset for code generation | ||
|
||
## Quick start | ||
|
||
Install toolset. | ||
|
||
```bash | ||
go install github.com/xinpianchang/xservice/tools/xservice@latest | ||
``` | ||
|
||
Create new project via toolset. | ||
|
||
```bash | ||
mkdir hello | ||
cd hello | ||
xservice new --module github.com/example/hello | ||
``` | ||
|
||
Open the generated `README.md` file, following the initialize steps, and happing coding. 🎉 | ||
|
||
## Resource | ||
|
||
- go-zero https://github.com/tal-tech/go-zero (special thanks) | ||
- micro https://github.com/asim/go-micro | ||
- gRPC generate tool/buf https://buf.build/ | ||
- gRPC validate https://github.com/envoyproxy/protoc-gen-validate | ||
- RESTful validate https://github.com/go-playground/validator | ||
- gRPC-Gateway https://grpc-ecosystem.github.io/grpc-gateway/ | ||
- jaeger https://www.jaegertracing.io/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package core | ||
|
||
type ContextKey string | ||
|
||
const ( | ||
// context | ||
ContextHeaderXRequestID ContextKey = "X-Request-ID" // requestId key | ||
|
||
DefaultServiceName = "xservice-default" | ||
|
||
// env key | ||
EnvServiceName = "XSERVICE_NAME" | ||
EnvServiceVersion = "XSERVICE_VERSION" | ||
EnvEtcd = "XSERVICE_ETCD" | ||
EnvEtcdUser = "XSERVICE_ETCD_USER" | ||
EnvEtcdPassword = "XSERVICE_ETCD_PASSWORD" | ||
|
||
// config key | ||
ConfigServiceAddr = "http.address" | ||
ConfigServiceAdviceAddr = "http.advice_address" | ||
|
||
ServiceConfigKeyPrefix = "xservice/config" | ||
ServiceRegisterKeyPrefix = "xservice/register" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
_ "net/http/pprof" | ||
|
||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
func Pprof() echo.MiddlewareFunc { | ||
return echo.WrapMiddleware(func(handler http.Handler) http.Handler { | ||
return http.DefaultServeMux | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package middleware | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/labstack/echo/v4" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
) | ||
|
||
func Prometheus(namespace, subsystem string) echo.MiddlewareFunc { | ||
requests := promauto.NewCounterVec(prometheus.CounterOpts{ | ||
Namespace: namespace, | ||
Subsystem: subsystem, | ||
Name: "requests_total", | ||
Help: "Number of requests", | ||
}, []string{"status", "method", "handler"}) | ||
|
||
durations := promauto.NewHistogramVec(prometheus.HistogramOpts{ | ||
Namespace: namespace, | ||
Subsystem: subsystem, | ||
Name: "request_duration_millisecond", | ||
Help: "Request duration", | ||
Buckets: []float64{50, 100, 200, 300, 500, 1000, 2000, 3000, 5000}, | ||
}, []string{"method", "handler"}) | ||
|
||
return func(next echo.HandlerFunc) echo.HandlerFunc { | ||
return func(c echo.Context) error { | ||
method := c.Request().Method | ||
path := c.Request().URL.Path | ||
start := time.Now() | ||
err := next(c) | ||
durations.WithLabelValues(method, path).Observe(float64(time.Since(start).Milliseconds())) | ||
requests.WithLabelValues(fmt.Sprint(c.Response().Status), method, path).Inc() | ||
return err | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package middleware | ||
|
||
import ( | ||
"github.com/labstack/echo-contrib/jaegertracing" | ||
"github.com/labstack/echo/v4" | ||
"github.com/labstack/echo/v4/middleware" | ||
"github.com/opentracing/opentracing-go" | ||
) | ||
|
||
func Trace(bodyDump bool, skipper middleware.Skipper) echo.MiddlewareFunc { | ||
return jaegertracing.TraceWithConfig(jaegertracing.TraceConfig{ | ||
Tracer: opentracing.GlobalTracer(), | ||
Skipper: skipper, | ||
IsBodyDump: bodyDump, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package xservice | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
resolver "go.etcd.io/etcd/client/v3/naming/resolver" | ||
"go.uber.org/zap" | ||
"google.golang.org/grpc" | ||
gresolver "google.golang.org/grpc/resolver" | ||
|
||
"github.com/xinpianchang/xservice/core" | ||
"github.com/xinpianchang/xservice/pkg/log" | ||
) | ||
|
||
type Client interface { | ||
GrpcClientConn(ctx context.Context, service string, desc *grpc.ServiceDesc, endpoint ...string) (*grpc.ClientConn, error) | ||
} | ||
|
||
type clientImpl struct { | ||
options *Options | ||
resolver gresolver.Builder | ||
} | ||
|
||
func newClient(opts *Options) Client { | ||
client := &clientImpl{ | ||
options: opts, | ||
} | ||
|
||
if os.Getenv(core.EnvEtcd) != "" { | ||
cli, err := serviceEtcdClient() | ||
if err != nil { | ||
log.Fatal("etcd client", zap.Error(err)) | ||
} | ||
client.resolver, err = resolver.NewBuilder(cli) | ||
if err != nil { | ||
log.Fatal("endpoints manager", zap.Error(err)) | ||
} | ||
} | ||
|
||
return client | ||
} | ||
|
||
func (t *clientImpl) GrpcClientConn(ctx context.Context, service string, desc *grpc.ServiceDesc, endpoint ...string) (*grpc.ClientConn, error) { | ||
if len(endpoint) > 0 { | ||
return grpc.DialContext(ctx, endpoint[0], grpc.WithInsecure()) | ||
} | ||
|
||
if os.Getenv(core.EnvEtcd) == "" { | ||
log.Fatal("etcd not configured") | ||
} | ||
target := fmt.Sprint("etcd:///", serviceKeyPrefix(service, desc)) | ||
log.For(ctx).Debug("client conn", zap.String("target", target)) | ||
return grpc.DialContext(ctx, target, grpc.WithInsecure(), grpc.WithBlock(), grpc.WithResolvers(t.resolver)) | ||
} |
Oops, something went wrong.