diff --git a/broker/nats/context.go b/broker/nats/context.go new file mode 100644 index 0000000..894d125 --- /dev/null +++ b/broker/nats/context.go @@ -0,0 +1,37 @@ +package nats + +import ( + "context" + + "github.com/stack-labs/stack-rpc/broker" +) + +// setSubscribeOption returns a function to setup a context with given value +func setSubscribeOption(k, v interface{}) broker.SubscribeOption { + return func(o *broker.SubscribeOptions) { + if o.Context == nil { + o.Context = context.Background() + } + o.Context = context.WithValue(o.Context, k, v) + } +} + +// setBrokerOption returns a function to setup a context with given value +func setBrokerOption(k, v interface{}) broker.Option { + return func(o *broker.Options) { + if o.Context == nil { + o.Context = context.Background() + } + o.Context = context.WithValue(o.Context, k, v) + } +} + +// setPublishOption returns a function to setup a context with given value +func setPublishOption(k, v interface{}) broker.PublishOption { + return func(o *broker.PublishOptions) { + if o.Context == nil { + o.Context = context.Background() + } + o.Context = context.WithValue(o.Context, k, v) + } +} diff --git a/broker/nats/go.mod b/broker/nats/go.mod new file mode 100644 index 0000000..85da7ef --- /dev/null +++ b/broker/nats/go.mod @@ -0,0 +1,11 @@ +module github.com/stack-labs/stack-rpc-plugins/broker/nats + +go 1.14 + +replace ( + github.com/stack-labs/stack-rpc v1.0.0 => ../../../stack-rpc +) + +require ( + github.com/stack-labs/stack-rpc v1.0.0 +) diff --git a/broker/nats/nats.go b/broker/nats/nats.go new file mode 100644 index 0000000..c69a980 --- /dev/null +++ b/broker/nats/nats.go @@ -0,0 +1,256 @@ +// Package nats provides a NATS broker +package nats + +import ( + "context" + "errors" + "strings" + "sync" + + nats "github.com/nats-io/nats.go" + "github.com/stack-labs/stack-rpc/broker" + "github.com/stack-labs/stack-rpc/codec/json" +) + +type natsBroker struct { + sync.Once + sync.RWMutex + addrs []string + conn *nats.Conn + opts broker.Options + nopts nats.Options + drain bool + closeCh chan (error) +} + +type subscriber struct { + s *nats.Subscription + opts broker.SubscribeOptions +} + +type publication struct { + t string + m *broker.Message +} + +func (p *publication) Topic() string { + return p.t +} + +func (p *publication) Message() *broker.Message { + return p.m +} + +func (p *publication) Ack() error { + // nats does not support acking + return nil +} + +func (s *subscriber) Options() broker.SubscribeOptions { + return s.opts +} + +func (s *subscriber) Topic() string { + return s.s.Subject +} + +func (s *subscriber) Unsubscribe() error { + return s.s.Unsubscribe() +} + +func (n *natsBroker) Address() string { + if n.conn != nil && n.conn.IsConnected() { + return n.conn.ConnectedUrl() + } + if len(n.addrs) > 0 { + return n.addrs[0] + } + + return "" +} + +func setAddrs(addrs []string) []string { + //nolint:prealloc + var cAddrs []string + for _, addr := range addrs { + if len(addr) == 0 { + continue + } + if !strings.HasPrefix(addr, "nats://") { + addr = "nats://" + addr + } + cAddrs = append(cAddrs, addr) + } + if len(cAddrs) == 0 { + cAddrs = []string{nats.DefaultURL} + } + return cAddrs +} + +func (n *natsBroker) Connect() error { + n.Lock() + defer n.Unlock() + + status := nats.CLOSED + if n.conn != nil { + status = n.conn.Status() + } + + switch status { + case nats.CONNECTED, nats.RECONNECTING, nats.CONNECTING: + return nil + default: // DISCONNECTED or CLOSED or DRAINING + opts := n.nopts + opts.Servers = n.addrs + opts.Secure = n.opts.Secure + opts.TLSConfig = n.opts.TLSConfig + + // secure might not be set + if n.opts.TLSConfig != nil { + opts.Secure = true + } + + c, err := opts.Connect() + if err != nil { + return err + } + n.conn = c + return nil + } +} + +func (n *natsBroker) Disconnect() error { + n.RLock() + defer n.RUnlock() + if n.drain { + n.conn.Drain() + return <-n.closeCh + } + n.conn.Close() + return nil +} + +func (n *natsBroker) Init(opts ...broker.Option) error { + n.setOption(opts...) + return nil +} + +func (n *natsBroker) Options() broker.Options { + return n.opts +} + +func (n *natsBroker) Publish(topic string, msg *broker.Message, opts ...broker.PublishOption) error { + b, err := n.opts.Codec.Marshal(msg) + if err != nil { + return err + } + n.RLock() + defer n.RUnlock() + return n.conn.Publish(topic, b) +} + +func (n *natsBroker) Subscribe(topic string, handler broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) { + if n.conn == nil { + return nil, errors.New("not connected") + } + + opt := broker.SubscribeOptions{ + AutoAck: true, + Context: context.Background(), + } + + for _, o := range opts { + o(&opt) + } + + fn := func(msg *nats.Msg) { + var m broker.Message + if err := n.opts.Codec.Unmarshal(msg.Data, &m); err != nil { + return + } + handler(&publication{m: &m, t: msg.Subject}) + } + + var sub *nats.Subscription + var err error + + n.RLock() + if len(opt.Queue) > 0 { + sub, err = n.conn.QueueSubscribe(topic, opt.Queue, fn) + } else { + sub, err = n.conn.Subscribe(topic, fn) + } + n.RUnlock() + if err != nil { + return nil, err + } + return &subscriber{s: sub, opts: opt}, nil +} + +func (n *natsBroker) String() string { + return "nats" +} + +func NewBroker(opts ...broker.Option) broker.Broker { + options := broker.Options{ + // Default codec + Codec: json.Marshaler{}, + Context: context.Background(), + } + + n := &natsBroker{ + opts: options, + } + n.setOption(opts...) + + return n +} + +func (n *natsBroker) setOption(opts ...broker.Option) { + for _, o := range opts { + o(&n.opts) + } + + n.Once.Do(func() { + n.nopts = nats.GetDefaultOptions() + }) + + if nopts, ok := n.opts.Context.Value(optionsKey{}).(nats.Options); ok { + n.nopts = nopts + } + + // broker.Options have higher priority than nats.Options + // only if Addrs, Secure or TLSConfig were not set through a broker.Option + // we read them from nats.Option + if len(n.opts.Addrs) == 0 { + n.opts.Addrs = n.nopts.Servers + } + + if !n.opts.Secure { + n.opts.Secure = n.nopts.Secure + } + + if n.opts.TLSConfig == nil { + n.opts.TLSConfig = n.nopts.TLSConfig + } + n.addrs = setAddrs(n.opts.Addrs) + + if n.opts.Context.Value(drainConnectionKey{}) != nil { + n.drain = true + n.closeCh = make(chan error) + n.nopts.ClosedCB = n.onClose + n.nopts.AsyncErrorCB = n.onAsyncError + } +} + +func (n *natsBroker) onClose(conn *nats.Conn) { + n.closeCh <- nil +} + +func (n *natsBroker) onAsyncError(conn *nats.Conn, sub *nats.Subscription, err error) { + // There are kinds of different async error nats might callback, but we are interested + // in ErrDrainTimeout only here. + if err == nats.ErrDrainTimeout { + n.closeCh <- err + } +} diff --git a/broker/nats/nats_test.go b/broker/nats/nats_test.go new file mode 100644 index 0000000..fc0bbd9 --- /dev/null +++ b/broker/nats/nats_test.go @@ -0,0 +1,98 @@ +package nats + +import ( + "fmt" + "testing" + + nats "github.com/nats-io/nats.go" + "github.com/stack-labs/stack-rpc/broker" +) + +var addrTestCases = []struct { + name string + description string + addrs map[string]string // expected address : set address +}{ + { + "brokerOpts", + "set broker addresses through a broker.Option in constructor", + map[string]string{ + "nats://192.168.10.1:5222": "192.168.10.1:5222", + "nats://10.20.10.0:4222": "10.20.10.0:4222"}, + }, + { + "brokerInit", + "set broker addresses through a broker.Option in broker.Init()", + map[string]string{ + "nats://192.168.10.1:5222": "192.168.10.1:5222", + "nats://10.20.10.0:4222": "10.20.10.0:4222"}, + }, + { + "natsOpts", + "set broker addresses through the nats.Option in constructor", + map[string]string{ + "nats://192.168.10.1:5222": "192.168.10.1:5222", + "nats://10.20.10.0:4222": "10.20.10.0:4222"}, + }, + { + "default", + "check if default Address is set correctly", + map[string]string{ + "nats://127.0.0.1:4222": "", + }, + }, +} + +// TestInitAddrs tests issue #100. Ensures that if the addrs is set by an option in init it will be used. +func TestInitAddrs(t *testing.T) { + + for _, tc := range addrTestCases { + t.Run(fmt.Sprintf("%s: %s", tc.name, tc.description), func(t *testing.T) { + + var br broker.Broker + var addrs []string + + for _, addr := range tc.addrs { + addrs = append(addrs, addr) + } + + switch tc.name { + case "brokerOpts": + // we know that there are just two addrs in the dict + br = NewBroker(broker.Addrs(addrs[0], addrs[1])) + br.Init() + case "brokerInit": + br = NewBroker() + // we know that there are just two addrs in the dict + br.Init(broker.Addrs(addrs[0], addrs[1])) + case "natsOpts": + nopts := nats.GetDefaultOptions() + nopts.Servers = addrs + br = NewBroker(Options(nopts)) + br.Init() + case "default": + br = NewBroker() + br.Init() + } + + natsBroker, ok := br.(*natsBroker) + if !ok { + t.Fatal("Expected broker to be of types *natsBroker") + } + // check if the same amount of addrs we set has actually been set, default + // have only 1 address nats://127.0.0.1:4222 (current nats code) or + // nats://localhost:4222 (older code version) + if len(natsBroker.addrs) != len(tc.addrs) && tc.name != "default" { + t.Errorf("Expected Addr count = %d, Actual Addr count = %d", + len(natsBroker.addrs), len(tc.addrs)) + } + + for _, addr := range natsBroker.addrs { + _, ok := tc.addrs[addr] + if !ok { + t.Errorf("Expected '%s' has not been set", addr) + } + } + }) + } +} diff --git a/broker/nats/options.go b/broker/nats/options.go new file mode 100644 index 0000000..7a1de5a --- /dev/null +++ b/broker/nats/options.go @@ -0,0 +1,19 @@ +package nats + +import ( + nats "github.com/nats-io/nats.go" + "github.com/stack-labs/stack-rpc/broker" +) + +type optionsKey struct{} +type drainConnectionKey struct{} + +// Options accepts nats.Options +func Options(opts nats.Options) broker.Option { + return setBrokerOption(optionsKey{}, opts) +} + +// DrainConnection will drain subscription on close +func DrainConnection() broker.Option { + return setBrokerOption(drainConnectionKey{}, struct{}{}) +} diff --git a/broker/nats/plugin.go b/broker/nats/plugin.go new file mode 100644 index 0000000..74f33ce --- /dev/null +++ b/broker/nats/plugin.go @@ -0,0 +1,24 @@ +package nats + +import ( + "github.com/stack-labs/stack-rpc/broker" + "github.com/stack-labs/stack-rpc/plugin" +) + +type natsBrokerPlugin struct{} + +func (n *natsBrokerPlugin) Name() string { + return "nats" +} + +func (n *natsBrokerPlugin) Options() []broker.Option { + return nil +} + +func (n *natsBrokerPlugin) New(opts ...broker.Option) broker.Broker { + return NewBroker(opts...) +} + +func init() { + plugin.BrokerPlugins["nats"] = &natsBrokerPlugin{} +} diff --git a/config/source/apollo/agollo/protocol/http/request.go b/config/source/apollo/agollo/protocol/http/request.go index ee727a3..8c90023 100644 --- a/config/source/apollo/agollo/protocol/http/request.go +++ b/config/source/apollo/agollo/protocol/http/request.go @@ -144,7 +144,7 @@ func Request(requestURL string, connectionConfig *env.ConnectConfig, callBack *C } return nil, nil case http.StatusNotModified: - log.Debug("Config Not Modified:", err) + log.Debugf("Config Not Modified: %s", err) if callBack != nil && callBack.NotModifyCallBack != nil { return nil, callBack.NotModifyCallBack() } diff --git a/logger/logrus/go.mod b/logger/logrus/go.mod index 4ebccc4..a75f1a6 100644 --- a/logger/logrus/go.mod +++ b/logger/logrus/go.mod @@ -2,6 +2,9 @@ module github.com/stack-labs/stack-rpc-plugins/logger/logrus go 1.14 +replace ( + github.com/stack-labs/stack-rpc v1.0.0 => ../../../stack-rpc +) require ( github.com/BurntSushi/toml v0.3.1 github.com/sirupsen/logrus v1.4.2 diff --git a/logger/logrus/level_hooks.go b/logger/logrus/level_hooks.go index f188366..59c4c4a 100644 --- a/logger/logrus/level_hooks.go +++ b/logger/logrus/level_hooks.go @@ -4,7 +4,7 @@ import ( "fmt" "io" - ls "github.com/sirupsen/logrus" + ls "github.com/stack-labs/stack-rpc-plugins/logger/logrus/logrus" "github.com/stack-labs/stack-rpc-plugins/logger/logrus/lumberjack.v2" "github.com/stack-labs/stack-rpc/logger" ) diff --git a/logger/logrus/logrus.go b/logger/logrus/logrus.go index 9a2a4ba..db3406a 100644 --- a/logger/logrus/logrus.go +++ b/logger/logrus/logrus.go @@ -6,7 +6,7 @@ import ( "io/ioutil" "os" - "github.com/sirupsen/logrus" + "github.com/stack-labs/stack-rpc-plugins/logger/logrus/logrus" "github.com/stack-labs/stack-rpc-plugins/logger/logrus/lumberjack.v2" "github.com/stack-labs/stack-rpc/logger" sLog "github.com/stack-labs/stack-rpc/util/log" @@ -50,6 +50,32 @@ func (l *logrusLogger) Init(opts ...logger.Option) error { l.opts.SplitLevel = splitLevel } + if withoutKey, ok := l.opts.Context.Value(withoutKeyKey{}).(bool); ok { + l.opts.WithoutKey = withoutKey + } + + if withoutQuote, ok := l.opts.Context.Value(withoutQuoteKey{}).(bool); ok { + l.opts.WithoutQuote = withoutQuote + } + + if timestampFormat, ok := l.opts.Context.Value(timestampFormat{}).(string); ok { + l.opts.TimestampFormat = timestampFormat + } + + if l.opts.Formatter != nil { + if txtFormatter, ok := l.opts.Formatter.(*logrus.TextFormatter); ok { + if l.opts.WithoutKey { + txtFormatter.WithoutKey = l.opts.WithoutKey + } + if l.opts.WithoutQuote { + txtFormatter.WithoutQuote = l.opts.WithoutQuote + } + if len(l.opts.TimestampFormat) > 0 { + txtFormatter.TimestampFormat = l.opts.TimestampFormat // "2006-01-02 15:04:05.999" + } + } + } + if l.opts.Persistence != nil && l.opts.Persistence.Enable && l.opts.Out == nil { var dir = l.opts.Persistence.Dir if dir == "" { @@ -141,6 +167,8 @@ func (l *logrusLogger) Options() logger.Options { // New builds a new logger based on options func NewLogger(opts ...logger.Option) logger.Logger { + formatter := new(logrus.TextFormatter) + // Default options options := Options{ Options: logger.Options{ @@ -148,7 +176,7 @@ func NewLogger(opts ...logger.Option) logger.Logger { Fields: make(map[string]interface{}), Context: context.Background(), }, - Formatter: new(logrus.TextFormatter), + Formatter: formatter, ReportCaller: false, ExitFunc: os.Exit, } diff --git a/logger/logrus/logrus/CHANGELOG.md b/logger/logrus/logrus/CHANGELOG.md new file mode 100644 index 0000000..51a7ab0 --- /dev/null +++ b/logger/logrus/logrus/CHANGELOG.md @@ -0,0 +1,200 @@ +# 1.4.2 + * Fixes build break for plan9, nacl, solaris +# 1.4.1 +This new release introduces: + * Enhance TextFormatter to not print caller information when they are empty (#944) + * Remove dependency on golang.org/x/crypto (#932, #943) + +Fixes: + * Fix Entry.WithContext method to return a copy of the initial entry (#941) + +# 1.4.0 +This new release introduces: + * Add `DeferExitHandler`, similar to `RegisterExitHandler` but prepending the handler to the list of handlers (semantically like `defer`) (#848). + * Add `CallerPrettyfier` to `JSONFormatter` and `TextFormatter (#909, #911) + * Add `Entry.WithContext()` and `Entry.Context`, to set a context on entries to be used e.g. in hooks (#919). + +Fixes: + * Fix wrong method calls `Logger.Print` and `Logger.Warningln` (#893). + * Update `Entry.Logf` to not do string formatting unless the log level is enabled (#903) + * Fix infinite recursion on unknown `Level.String()` (#907) + * Fix race condition in `getCaller` (#916). + + +# 1.3.0 +This new release introduces: + * Log, Logf, Logln functions for Logger and Entry that take a Level + +Fixes: + * Building prometheus node_exporter on AIX (#840) + * Race condition in TextFormatter (#468) + * Travis CI import path (#868) + * Remove coloured output on Windows (#862) + * Pointer to func as field in JSONFormatter (#870) + * Properly marshal Levels (#873) + +# 1.2.0 +This new release introduces: + * A new method `SetReportCaller` in the `Logger` to enable the file, line and calling function from which the trace has been issued + * A new trace level named `Trace` whose level is below `Debug` + * A configurable exit function to be called upon a Fatal trace + * The `Level` object now implements `encoding.TextUnmarshaler` interface + +# 1.1.1 +This is a bug fix release. + * fix the build break on Solaris + * don't drop a whole trace in JSONFormatter when a field param is a function pointer which can not be serialized + +# 1.1.0 +This new release introduces: + * several fixes: + * a fix for a race condition on entry formatting + * proper cleanup of previously used entries before putting them back in the pool + * the extra new line at the end of message in text formatter has been removed + * a new global public API to check if a level is activated: IsLevelEnabled + * the following methods have been added to the Logger object + * IsLevelEnabled + * SetFormatter + * SetOutput + * ReplaceHooks + * introduction of go module + * an indent configuration for the json formatter + * output colour support for windows + * the field sort function is now configurable for text formatter + * the CLICOLOR and CLICOLOR\_FORCE environment variable support in text formater + +# 1.0.6 + +This new release introduces: + * a new api WithTime which allows to easily force the time of the log entry + which is mostly useful for logger wrapper + * a fix reverting the immutability of the entry given as parameter to the hooks + a new configuration field of the json formatter in order to put all the fields + in a nested dictionnary + * a new SetOutput method in the Logger + * a new configuration of the textformatter to configure the name of the default keys + * a new configuration of the text formatter to disable the level truncation + +# 1.0.5 + +* Fix hooks race (#707) +* Fix panic deadlock (#695) + +# 1.0.4 + +* Fix race when adding hooks (#612) +* Fix terminal check in AppEngine (#635) + +# 1.0.3 + +* Replace example files with testable examples + +# 1.0.2 + +* bug: quote non-string values in text formatter (#583) +* Make (*Logger) SetLevel a public method + +# 1.0.1 + +* bug: fix escaping in text formatter (#575) + +# 1.0.0 + +* Officially changed name to lower-case +* bug: colors on Windows 10 (#541) +* bug: fix race in accessing level (#512) + +# 0.11.5 + +* feature: add writer and writerlevel to entry (#372) + +# 0.11.4 + +* bug: fix undefined variable on solaris (#493) + +# 0.11.3 + +* formatter: configure quoting of empty values (#484) +* formatter: configure quoting character (default is `"`) (#484) +* bug: fix not importing io correctly in non-linux environments (#481) + +# 0.11.2 + +* bug: fix windows terminal detection (#476) + +# 0.11.1 + +* bug: fix tty detection with custom out (#471) + +# 0.11.0 + +* performance: Use bufferpool to allocate (#370) +* terminal: terminal detection for app-engine (#343) +* feature: exit handler (#375) + +# 0.10.0 + +* feature: Add a test hook (#180) +* feature: `ParseLevel` is now case-insensitive (#326) +* feature: `FieldLogger` interface that generalizes `Logger` and `Entry` (#308) +* performance: avoid re-allocations on `WithFields` (#335) + +# 0.9.0 + +* logrus/text_formatter: don't emit empty msg +* logrus/hooks/airbrake: move out of main repository +* logrus/hooks/sentry: move out of main repository +* logrus/hooks/papertrail: move out of main repository +* logrus/hooks/bugsnag: move out of main repository +* logrus/core: run tests with `-race` +* logrus/core: detect TTY based on `stderr` +* logrus/core: support `WithError` on logger +* logrus/core: Solaris support + +# 0.8.7 + +* logrus/core: fix possible race (#216) +* logrus/doc: small typo fixes and doc improvements + + +# 0.8.6 + +* hooks/raven: allow passing an initialized client + +# 0.8.5 + +* logrus/core: revert #208 + +# 0.8.4 + +* formatter/text: fix data race (#218) + +# 0.8.3 + +* logrus/core: fix entry log level (#208) +* logrus/core: improve performance of text formatter by 40% +* logrus/core: expose `LevelHooks` type +* logrus/core: add support for DragonflyBSD and NetBSD +* formatter/text: print structs more verbosely + +# 0.8.2 + +* logrus: fix more Fatal family functions + +# 0.8.1 + +* logrus: fix not exiting on `Fatalf` and `Fatalln` + +# 0.8.0 + +* logrus: defaults to stderr instead of stdout +* hooks/sentry: add special field for `*http.Request` +* formatter/text: ignore Windows for colors + +# 0.7.3 + +* formatter/\*: allow configuration of timestamp layout + +# 0.7.2 + +* formatter/text: Add configuration option for time format (#158) diff --git a/logger/logrus/logrus/LICENSE b/logger/logrus/logrus/LICENSE new file mode 100644 index 0000000..f090cb4 --- /dev/null +++ b/logger/logrus/logrus/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014 Simon Eskildsen + +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. diff --git a/logger/logrus/logrus/README.md b/logger/logrus/logrus/README.md new file mode 100644 index 0000000..a4796eb --- /dev/null +++ b/logger/logrus/logrus/README.md @@ -0,0 +1,495 @@ +# Logrus :walrus: [![Build Status](https://travis-ci.org/sirupsen/logrus.svg?branch=master)](https://travis-ci.org/sirupsen/logrus) [![GoDoc](https://godoc.org/github.com/sirupsen/logrus?status.svg)](https://godoc.org/github.com/sirupsen/logrus) + +Logrus is a structured logger for Go (golang), completely API compatible with +the standard library logger. + +**Seeing weird case-sensitive problems?** It's in the past been possible to +import Logrus as both upper- and lower-case. Due to the Go package environment, +this caused issues in the community and we needed a standard. Some environments +experienced problems with the upper-case variant, so the lower-case was decided. +Everything using `logrus` will need to use the lower-case: +`github.com/sirupsen/logrus`. Any package that isn't, should be changed. + +To fix Glide, see [these +comments](https://github.com/sirupsen/logrus/issues/553#issuecomment-306591437). +For an in-depth explanation of the casing issue, see [this +comment](https://github.com/sirupsen/logrus/issues/570#issuecomment-313933276). + +**Are you interested in assisting in maintaining Logrus?** Currently I have a +lot of obligations, and I am unable to provide Logrus with the maintainership it +needs. If you'd like to help, please reach out to me at `simon at author's +username dot com`. + +Nicely color-coded in development (when a TTY is attached, otherwise just +plain text): + +![Colored](http://i.imgur.com/PY7qMwd.png) + +With `log.SetFormatter(&log.JSONFormatter{})`, for easy parsing by logstash +or Splunk: + +```json +{"animal":"walrus","level":"info","msg":"A group of walrus emerges from the +ocean","size":10,"time":"2014-03-10 19:57:38.562264131 -0400 EDT"} + +{"level":"warning","msg":"The group's number increased tremendously!", +"number":122,"omg":true,"time":"2014-03-10 19:57:38.562471297 -0400 EDT"} + +{"animal":"walrus","level":"info","msg":"A giant walrus appears!", +"size":10,"time":"2014-03-10 19:57:38.562500591 -0400 EDT"} + +{"animal":"walrus","level":"info","msg":"Tremendously sized cow enters the ocean.", +"size":9,"time":"2014-03-10 19:57:38.562527896 -0400 EDT"} + +{"level":"fatal","msg":"The ice breaks!","number":100,"omg":true, +"time":"2014-03-10 19:57:38.562543128 -0400 EDT"} +``` + +With the default `log.SetFormatter(&log.TextFormatter{})` when a TTY is not +attached, the output is compatible with the +[logfmt](http://godoc.org/github.com/kr/logfmt) format: + +```text +time="2015-03-26T01:27:38-04:00" level=debug msg="Started observing beach" animal=walrus number=8 +time="2015-03-26T01:27:38-04:00" level=info msg="A group of walrus emerges from the ocean" animal=walrus size=10 +time="2015-03-26T01:27:38-04:00" level=warning msg="The group's number increased tremendously!" number=122 omg=true +time="2015-03-26T01:27:38-04:00" level=debug msg="Temperature changes" temperature=-4 +time="2015-03-26T01:27:38-04:00" level=panic msg="It's over 9000!" animal=orca size=9009 +time="2015-03-26T01:27:38-04:00" level=fatal msg="The ice breaks!" err=&{0x2082280c0 map[animal:orca size:9009] 2015-03-26 01:27:38.441574009 -0400 EDT panic It's over 9000!} number=100 omg=true +``` +To ensure this behaviour even if a TTY is attached, set your formatter as follows: + +```go + log.SetFormatter(&log.TextFormatter{ + DisableColors: true, + FullTimestamp: true, + }) +``` + +#### Logging Method Name + +If you wish to add the calling method as a field, instruct the logger via: +```go +log.SetReportCaller(true) +``` +This adds the caller as 'method' like so: + +```json +{"animal":"penguin","level":"fatal","method":"github.com/sirupsen/arcticcreatures.migrate","msg":"a penguin swims by", +"time":"2014-03-10 19:57:38.562543129 -0400 EDT"} +``` + +```text +time="2015-03-26T01:27:38-04:00" level=fatal method=github.com/sirupsen/arcticcreatures.migrate msg="a penguin swims by" animal=penguin +``` +Note that this does add measurable overhead - the cost will depend on the version of Go, but is +between 20 and 40% in recent tests with 1.6 and 1.7. You can validate this in your +environment via benchmarks: +``` +go test -bench=.*CallerTracing +``` + + +#### Case-sensitivity + +The organization's name was changed to lower-case--and this will not be changed +back. If you are getting import conflicts due to case sensitivity, please use +the lower-case import: `github.com/sirupsen/logrus`. + +#### Example + +The simplest way to use Logrus is simply the package-level exported logger: + +```go +package main + +import ( + log "github.com/sirupsen/logrus" +) + +func main() { + log.WithFields(log.Fields{ + "animal": "walrus", + }).Info("A walrus appears") +} +``` + +Note that it's completely api-compatible with the stdlib logger, so you can +replace your `log` imports everywhere with `log "github.com/sirupsen/logrus"` +and you'll now have the flexibility of Logrus. You can customize it all you +want: + +```go +package main + +import ( + "os" + log "github.com/sirupsen/logrus" +) + +func init() { + // Log as JSON instead of the default ASCII formatter. + log.SetFormatter(&log.JSONFormatter{}) + + // Output to stdout instead of the default stderr + // Can be any io.Writer, see below for File example + log.SetOutput(os.Stdout) + + // Only log the warning severity or above. + log.SetLevel(log.WarnLevel) +} + +func main() { + log.WithFields(log.Fields{ + "animal": "walrus", + "size": 10, + }).Info("A group of walrus emerges from the ocean") + + log.WithFields(log.Fields{ + "omg": true, + "number": 122, + }).Warn("The group's number increased tremendously!") + + log.WithFields(log.Fields{ + "omg": true, + "number": 100, + }).Fatal("The ice breaks!") + + // A common pattern is to re-use fields between logging statements by re-using + // the logrus.Entry returned from WithFields() + contextLogger := log.WithFields(log.Fields{ + "common": "this is a common field", + "other": "I also should be logged always", + }) + + contextLogger.Info("I'll be logged with common and other field") + contextLogger.Info("Me too") +} +``` + +For more advanced usage such as logging to multiple locations from the same +application, you can also create an instance of the `logrus` Logger: + +```go +package main + +import ( + "os" + "github.com/sirupsen/logrus" +) + +// Create a new instance of the logger. You can have any number of instances. +var log = logrus.New() + +func main() { + // The API for setting attributes is a little different than the package level + // exported logger. See Godoc. + log.Out = os.Stdout + + // You could set this to any `io.Writer` such as a file + // file, err := os.OpenFile("logrus.log", os.O_CREATE|os.O_WRONLY, 0666) + // if err == nil { + // log.Out = file + // } else { + // log.Info("Failed to log to file, using default stderr") + // } + + log.WithFields(logrus.Fields{ + "animal": "walrus", + "size": 10, + }).Info("A group of walrus emerges from the ocean") +} +``` + +#### Fields + +Logrus encourages careful, structured logging through logging fields instead of +long, unparseable error messages. For example, instead of: `log.Fatalf("Failed +to send event %s to topic %s with key %d")`, you should log the much more +discoverable: + +```go +log.WithFields(log.Fields{ + "event": event, + "topic": topic, + "key": key, +}).Fatal("Failed to send event") +``` + +We've found this API forces you to think about logging in a way that produces +much more useful logging messages. We've been in countless situations where just +a single added field to a log statement that was already there would've saved us +hours. The `WithFields` call is optional. + +In general, with Logrus using any of the `printf`-family functions should be +seen as a hint you should add a field, however, you can still use the +`printf`-family functions with Logrus. + +#### Default Fields + +Often it's helpful to have fields _always_ attached to log statements in an +application or parts of one. For example, you may want to always log the +`request_id` and `user_ip` in the context of a request. Instead of writing +`log.WithFields(log.Fields{"request_id": request_id, "user_ip": user_ip})` on +every line, you can create a `logrus.Entry` to pass around instead: + +```go +requestLogger := log.WithFields(log.Fields{"request_id": request_id, "user_ip": user_ip}) +requestLogger.Info("something happened on that request") # will log request_id and user_ip +requestLogger.Warn("something not great happened") +``` + +#### Hooks + +You can add hooks for logging levels. For example to send errors to an exception +tracking service on `Error`, `Fatal` and `Panic`, info to StatsD or log to +multiple places simultaneously, e.g. syslog. + +Logrus comes with [built-in hooks](hooks/). Add those, or your custom hook, in +`init`: + +```go +import ( + log "github.com/sirupsen/logrus" + "gopkg.in/gemnasium/logrus-airbrake-hook.v2" // the package is named "airbrake" + logrus_syslog "github.com/sirupsen/logrus/hooks/syslog" + "log/syslog" +) + +func init() { + + // Use the Airbrake hook to report errors that have Error severity or above to + // an exception tracker. You can create custom hooks, see the Hooks section. + log.AddHook(airbrake.NewHook(123, "xyz", "production")) + + hook, err := logrus_syslog.NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, "") + if err != nil { + log.Error("Unable to connect to local syslog daemon") + } else { + log.AddHook(hook) + } +} +``` +Note: Syslog hook also support connecting to local syslog (Ex. "/dev/log" or "/var/run/syslog" or "/var/run/log"). For the detail, please check the [syslog hook README](hooks/syslog/README.md). + +A list of currently known of service hook can be found in this wiki [page](https://github.com/sirupsen/logrus/wiki/Hooks) + + +#### Level logging + +Logrus has seven logging levels: Trace, Debug, Info, Warning, Error, Fatal and Panic. + +```go +log.Trace("Something very low level.") +log.Debug("Useful debugging information.") +log.Info("Something noteworthy happened!") +log.Warn("You should probably take a look at this.") +log.Error("Something failed but I'm not quitting.") +// Calls os.Exit(1) after logging +log.Fatal("Bye.") +// Calls panic() after logging +log.Panic("I'm bailing.") +``` + +You can set the logging level on a `Logger`, then it will only log entries with +that severity or anything above it: + +```go +// Will log anything that is info or above (warn, error, fatal, panic). Default. +log.SetLevel(log.InfoLevel) +``` + +It may be useful to set `log.Level = logrus.DebugLevel` in a debug or verbose +environment if your application has that. + +#### Entries + +Besides the fields added with `WithField` or `WithFields` some fields are +automatically added to all logging events: + +1. `time`. The timestamp when the entry was created. +2. `msg`. The logging message passed to `{Info,Warn,Error,Fatal,Panic}` after + the `AddFields` call. E.g. `Failed to send event.` +3. `level`. The logging level. E.g. `info`. + +#### Environments + +Logrus has no notion of environment. + +If you wish for hooks and formatters to only be used in specific environments, +you should handle that yourself. For example, if your application has a global +variable `Environment`, which is a string representation of the environment you +could do: + +```go +import ( + log "github.com/sirupsen/logrus" +) + +init() { + // do something here to set environment depending on an environment variable + // or command-line flag + if Environment == "production" { + log.SetFormatter(&log.JSONFormatter{}) + } else { + // The TextFormatter is default, you don't actually have to do this. + log.SetFormatter(&log.TextFormatter{}) + } +} +``` + +This configuration is how `logrus` was intended to be used, but JSON in +production is mostly only useful if you do log aggregation with tools like +Splunk or Logstash. + +#### Formatters + +The built-in logging formatters are: + +* `logrus.TextFormatter`. Logs the event in colors if stdout is a tty, otherwise + without colors. + * *Note:* to force colored output when there is no TTY, set the `ForceColors` + field to `true`. To force no colored output even if there is a TTY set the + `DisableColors` field to `true`. For Windows, see + [github.com/mattn/go-colorable](https://github.com/mattn/go-colorable). + * When colors are enabled, levels are truncated to 4 characters by default. To disable + truncation set the `DisableLevelTruncation` field to `true`. + * All options are listed in the [generated docs](https://godoc.org/github.com/sirupsen/logrus#TextFormatter). +* `logrus.JSONFormatter`. Logs fields as JSON. + * All options are listed in the [generated docs](https://godoc.org/github.com/sirupsen/logrus#JSONFormatter). + +Third party logging formatters: + +* [`FluentdFormatter`](https://github.com/joonix/log). Formats entries that can be parsed by Kubernetes and Google Container Engine. +* [`GELF`](https://github.com/fabienm/go-logrus-formatters). Formats entries so they comply to Graylog's [GELF 1.1 specification](http://docs.graylog.org/en/2.4/pages/gelf.html). +* [`logstash`](https://github.com/bshuster-repo/logrus-logstash-hook). Logs fields as [Logstash](http://logstash.net) Events. +* [`prefixed`](https://github.com/x-cray/logrus-prefixed-formatter). Displays log entry source along with alternative layout. +* [`zalgo`](https://github.com/aybabtme/logzalgo). Invoking the P͉̫o̳̼̊w̖͈̰͎e̬͔̭͂r͚̼̹̲ ̫͓͉̳͈ō̠͕͖̚f̝͍̠ ͕̲̞͖͑Z̖̫̤̫ͪa͉̬͈̗l͖͎g̳̥o̰̥̅!̣͔̲̻͊̄ ̙̘̦̹̦. +* [`nested-logrus-formatter`](https://github.com/antonfisher/nested-logrus-formatter). Converts logrus fields to a nested structure. + +You can define your formatter by implementing the `Formatter` interface, +requiring a `Format` method. `Format` takes an `*Entry`. `entry.Data` is a +`Fields` type (`map[string]interface{}`) with all your fields as well as the +default ones (see Entries section above): + +```go +type MyJSONFormatter struct { +} + +log.SetFormatter(new(MyJSONFormatter)) + +func (f *MyJSONFormatter) Format(entry *Entry) ([]byte, error) { + // Note this doesn't include Time, Level and Message which are available on + // the Entry. Consult `godoc` on information about those fields or read the + // source of the official loggers. + serialized, err := json.Marshal(entry.Data) + if err != nil { + return nil, fmt.Errorf("Failed to marshal fields to JSON, %v", err) + } + return append(serialized, '\n'), nil +} +``` + +#### Logger as an `io.Writer` + +Logrus can be transformed into an `io.Writer`. That writer is the end of an `io.Pipe` and it is your responsibility to close it. + +```go +w := logger.Writer() +defer w.Close() + +srv := http.Server{ + // create a stdlib log.Logger that writes to + // logrus.Logger. + ErrorLog: log.New(w, "", 0), +} +``` + +Each line written to that writer will be printed the usual way, using formatters +and hooks. The level for those entries is `info`. + +This means that we can override the standard library logger easily: + +```go +logger := logrus.New() +logger.Formatter = &logrus.JSONFormatter{} + +// Use logrus for standard log output +// Note that `log` here references stdlib's log +// Not logrus imported under the name `log`. +log.SetOutput(logger.Writer()) +``` + +#### Rotation + +Log rotation is not provided with Logrus. Log rotation should be done by an +external program (like `logrotate(8)`) that can compress and delete old log +entries. It should not be a feature of the application-level logger. + +#### Tools + +| Tool | Description | +| ---- | ----------- | +|[Logrus Mate](https://github.com/gogap/logrus_mate)|Logrus mate is a tool for Logrus to manage loggers, you can initial logger's level, hook and formatter by config file, the logger will generated with different config at different environment.| +|[Logrus Viper Helper](https://github.com/heirko/go-contrib/tree/master/logrusHelper)|An Helper around Logrus to wrap with spf13/Viper to load configuration with fangs! And to simplify Logrus configuration use some behavior of [Logrus Mate](https://github.com/gogap/logrus_mate). [sample](https://github.com/heirko/iris-contrib/blob/master/middleware/logrus-logger/example) | + +#### Testing + +Logrus has a built in facility for asserting the presence of log messages. This is implemented through the `test` hook and provides: + +* decorators for existing logger (`test.NewLocal` and `test.NewGlobal`) which basically just add the `test` hook +* a test logger (`test.NewNullLogger`) that just records log messages (and does not output any): + +```go +import( + "github.com/sirupsen/logrus" + "github.com/sirupsen/logrus/hooks/test" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestSomething(t*testing.T){ + logger, hook := test.NewNullLogger() + logger.Error("Helloerror") + + assert.Equal(t, 1, len(hook.Entries)) + assert.Equal(t, logrus.ErrorLevel, hook.LastEntry().Level) + assert.Equal(t, "Helloerror", hook.LastEntry().Message) + + hook.Reset() + assert.Nil(t, hook.LastEntry()) +} +``` + +#### Fatal handlers + +Logrus can register one or more functions that will be called when any `fatal` +level message is logged. The registered handlers will be executed before +logrus performs a `os.Exit(1)`. This behavior may be helpful if callers need +to gracefully shutdown. Unlike a `panic("Something went wrong...")` call which can be intercepted with a deferred `recover` a call to `os.Exit(1)` can not be intercepted. + +``` +... +handler := func() { + // gracefully shutdown something... +} +logrus.RegisterExitHandler(handler) +... +``` + +#### Thread safety + +By default, Logger is protected by a mutex for concurrent writes. The mutex is held when calling hooks and writing logs. +If you are sure such locking is not needed, you can call logger.SetNoLock() to disable the locking. + +Situation when locking is not needed includes: + +* You have no hooks registered, or hooks calling is already thread-safe. + +* Writing to logger.Out is already thread-safe, for example: + + 1) logger.Out is protected by locks. + + 2) logger.Out is a os.File handler opened with `O_APPEND` flag, and every write is smaller than 4k. (This allow multi-thread/multi-process writing) + + (Refer to http://www.notthewizard.com/2014/06/17/are-files-appends-really-atomic/) diff --git a/logger/logrus/logrus/alt_exit.go b/logger/logrus/logrus/alt_exit.go new file mode 100644 index 0000000..8fd189e --- /dev/null +++ b/logger/logrus/logrus/alt_exit.go @@ -0,0 +1,76 @@ +package logrus + +// The following code was sourced and modified from the +// https://github.com/tebeka/atexit package governed by the following license: +// +// Copyright (c) 2012 Miki Tebeka . +// +// 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. + +import ( + "fmt" + "os" +) + +var handlers = []func(){} + +func runHandler(handler func()) { + defer func() { + if err := recover(); err != nil { + fmt.Fprintln(os.Stderr, "Error: Logrus exit handler error:", err) + } + }() + + handler() +} + +func runHandlers() { + for _, handler := range handlers { + runHandler(handler) + } +} + +// Exit runs all the Logrus atexit handlers and then terminates the program using os.Exit(code) +func Exit(code int) { + runHandlers() + os.Exit(code) +} + +// RegisterExitHandler appends a Logrus Exit handler to the list of handlers, +// call logrus.Exit to invoke all handlers. The handlers will also be invoked when +// any Fatal log entry is made. +// +// This method is useful when a caller wishes to use logrus to log a fatal +// message but also needs to gracefully shutdown. An example usecase could be +// closing database connections, or sending a alert that the application is +// closing. +func RegisterExitHandler(handler func()) { + handlers = append(handlers, handler) +} + +// DeferExitHandler prepends a Logrus Exit handler to the list of handlers, +// call logrus.Exit to invoke all handlers. The handlers will also be invoked when +// any Fatal log entry is made. +// +// This method is useful when a caller wishes to use logrus to log a fatal +// message but also needs to gracefully shutdown. An example usecase could be +// closing database connections, or sending a alert that the application is +// closing. +func DeferExitHandler(handler func()) { + handlers = append([]func(){handler}, handlers...) +} diff --git a/logger/logrus/logrus/alt_exit_test.go b/logger/logrus/logrus/alt_exit_test.go new file mode 100644 index 0000000..54d503c --- /dev/null +++ b/logger/logrus/logrus/alt_exit_test.go @@ -0,0 +1,151 @@ +package logrus + +import ( + "io/ioutil" + "log" + "os" + "os/exec" + "path/filepath" + "runtime" + "strings" + "testing" + "time" +) + +func TestRegister(t *testing.T) { + current := len(handlers) + + var results []string + + h1 := func() { results = append(results, "first") } + h2 := func() { results = append(results, "second") } + + RegisterExitHandler(h1) + RegisterExitHandler(h2) + + if len(handlers) != current+2 { + t.Fatalf("expected %d handlers, got %d", current+2, len(handlers)) + } + + runHandlers() + + if len(results) != 2 { + t.Fatalf("expected 2 handlers to be run, ran %d", len(results)) + } + + if results[0] != "first" { + t.Fatal("expected handler h1 to be run first, but it wasn't") + } + + if results[1] != "second" { + t.Fatal("expected handler h2 to be run second, but it wasn't") + } +} + +func TestDefer(t *testing.T) { + current := len(handlers) + + var results []string + + h1 := func() { results = append(results, "first") } + h2 := func() { results = append(results, "second") } + + DeferExitHandler(h1) + DeferExitHandler(h2) + + if len(handlers) != current+2 { + t.Fatalf("expected %d handlers, got %d", current+2, len(handlers)) + } + + runHandlers() + + if len(results) != 2 { + t.Fatalf("expected 2 handlers to be run, ran %d", len(results)) + } + + if results[0] != "second" { + t.Fatal("expected handler h2 to be run first, but it wasn't") + } + + if results[1] != "first" { + t.Fatal("expected handler h1 to be run second, but it wasn't") + } +} + +func TestHandler(t *testing.T) { + testprog := testprogleader + testprog = append(testprog, getPackage()...) + testprog = append(testprog, testprogtrailer...) + tempDir, err := ioutil.TempDir("", "test_handler") + if err != nil { + log.Fatalf("can't create temp dir. %q", err) + } + defer os.RemoveAll(tempDir) + + gofile := filepath.Join(tempDir, "gofile.go") + if err := ioutil.WriteFile(gofile, testprog, 0666); err != nil { + t.Fatalf("can't create go file. %q", err) + } + + outfile := filepath.Join(tempDir, "outfile.out") + arg := time.Now().UTC().String() + err = exec.Command("go", "run", gofile, outfile, arg).Run() + if err == nil { + t.Fatalf("completed normally, should have failed") + } + + data, err := ioutil.ReadFile(outfile) + if err != nil { + t.Fatalf("can't read output file %s. %q", outfile, err) + } + + if string(data) != arg { + t.Fatalf("bad data. Expected %q, got %q", data, arg) + } +} + +// getPackage returns the name of the current package, which makes running this +// test in a fork simpler +func getPackage() []byte { + pc, _, _, _ := runtime.Caller(0) + fullFuncName := runtime.FuncForPC(pc).Name() + idx := strings.LastIndex(fullFuncName, ".") + return []byte(fullFuncName[:idx]) // trim off function details +} + +var testprogleader = []byte(` +// Test program for atexit, gets output file and data as arguments and writes +// data to output file in atexit handler. +package main + +import ( + "`) +var testprogtrailer = []byte( + `" + "flag" + "fmt" + "io/ioutil" +) + +var outfile = "" +var data = "" + +func handler() { + ioutil.WriteFile(outfile, []byte(data), 0666) +} + +func badHandler() { + n := 0 + fmt.Println(1/n) +} + +func main() { + flag.Parse() + outfile = flag.Arg(0) + data = flag.Arg(1) + + logrus.RegisterExitHandler(handler) + logrus.RegisterExitHandler(badHandler) + logrus.Fatal("Bye bye") +} +`) diff --git a/logger/logrus/logrus/appveyor.yml b/logger/logrus/logrus/appveyor.yml new file mode 100644 index 0000000..b4ffca2 --- /dev/null +++ b/logger/logrus/logrus/appveyor.yml @@ -0,0 +1,14 @@ +version: "{build}" +platform: x64 +clone_folder: c:\gopath\src\github.com\sirupsen\logrus +environment: + GOPATH: c:\gopath +branches: + only: + - master +install: + - set PATH=%GOPATH%\bin;c:\go\bin;%PATH% + - go version +build_script: + - go get -t + - go test diff --git a/logger/logrus/logrus/doc.go b/logger/logrus/logrus/doc.go new file mode 100644 index 0000000..cdbcff4 --- /dev/null +++ b/logger/logrus/logrus/doc.go @@ -0,0 +1,26 @@ +/* +Package logrus is a structured logger for Go, completely API compatible with the standard library logger. + + +The simplest way to use Logrus is simply the package-level exported logger: + + package main + + import ( + log "github.com/stack-labs/stack-rpc-plugins/logger/logrus/logrus" + ) + + func main() { + log.WithFields(log.Fields{ + "animal": "walrus", + "number": 1, + "size": 10, + }).Info("A walrus appears") + } + +Output: + time="2015-09-07T08:48:33Z" level=info msg="A walrus appears" animal=walrus number=1 size=10 + +For a full guide visit https://github.com/sirupsen/logrus +*/ +package logrus diff --git a/logger/logrus/logrus/entry.go b/logger/logrus/logrus/entry.go new file mode 100644 index 0000000..63e2558 --- /dev/null +++ b/logger/logrus/logrus/entry.go @@ -0,0 +1,407 @@ +package logrus + +import ( + "bytes" + "context" + "fmt" + "os" + "reflect" + "runtime" + "strings" + "sync" + "time" +) + +var ( + bufferPool *sync.Pool + + // qualified package name, cached at first use + logrusPackage string + + // Positions in the call stack when tracing to report the calling method + minimumCallerDepth int + + // Used for caller information initialisation + callerInitOnce sync.Once +) + +const ( + maximumCallerDepth int = 25 + knownLogrusFrames int = 4 +) + +func init() { + bufferPool = &sync.Pool{ + New: func() interface{} { + return new(bytes.Buffer) + }, + } + + // start at the bottom of the stack before the package-name cache is primed + minimumCallerDepth = 1 +} + +// Defines the key when adding errors using WithError. +var ErrorKey = "error" + +// An entry is the final or intermediate Logrus logging entry. It contains all +// the fields passed with WithField{,s}. It's finally logged when Trace, Debug, +// Info, Warn, Error, Fatal or Panic is called on it. These objects can be +// reused and passed around as much as you wish to avoid field duplication. +type Entry struct { + Logger *Logger + + // Contains all the fields set by the user. + Data Fields + + // Time at which the log entry was created + Time time.Time + + // Level the log entry was logged at: Trace, Debug, Info, Warn, Error, Fatal or Panic + // This field will be set on entry firing and the value will be equal to the one in Logger struct field. + Level Level + + // Calling method, with package name + Caller *runtime.Frame + + // Message passed to Trace, Debug, Info, Warn, Error, Fatal or Panic + Message string + + // When formatter is called in entry.log(), a Buffer may be set to entry + Buffer *bytes.Buffer + + // Contains the context set by the user. Useful for hook processing etc. + Context context.Context + + // err may contain a field formatting error + err string +} + +func NewEntry(logger *Logger) *Entry { + return &Entry{ + Logger: logger, + // Default is three fields, plus one optional. Give a little extra room. + Data: make(Fields, 6), + } +} + +// Returns the string representation from the reader and ultimately the +// formatter. +func (entry *Entry) String() (string, error) { + serialized, err := entry.Logger.Formatter.Format(entry) + if err != nil { + return "", err + } + str := string(serialized) + return str, nil +} + +// Add an error as single field (using the key defined in ErrorKey) to the Entry. +func (entry *Entry) WithError(err error) *Entry { + return entry.WithField(ErrorKey, err) +} + +// Add a context to the Entry. +func (entry *Entry) WithContext(ctx context.Context) *Entry { + return &Entry{Logger: entry.Logger, Data: entry.Data, Time: entry.Time, err: entry.err, Context: ctx} +} + +// Add a single field to the Entry. +func (entry *Entry) WithField(key string, value interface{}) *Entry { + return entry.WithFields(Fields{key: value}) +} + +// Add a map of fields to the Entry. +func (entry *Entry) WithFields(fields Fields) *Entry { + data := make(Fields, len(entry.Data)+len(fields)) + for k, v := range entry.Data { + data[k] = v + } + fieldErr := entry.err + for k, v := range fields { + isErrField := false + if t := reflect.TypeOf(v); t != nil { + switch t.Kind() { + case reflect.Func: + isErrField = true + case reflect.Ptr: + isErrField = t.Elem().Kind() == reflect.Func + } + } + if isErrField { + tmp := fmt.Sprintf("can not add field %q", k) + if fieldErr != "" { + fieldErr = entry.err + ", " + tmp + } else { + fieldErr = tmp + } + } else { + data[k] = v + } + } + return &Entry{Logger: entry.Logger, Data: data, Time: entry.Time, err: fieldErr, Context: entry.Context} +} + +// Overrides the time of the Entry. +func (entry *Entry) WithTime(t time.Time) *Entry { + return &Entry{Logger: entry.Logger, Data: entry.Data, Time: t, err: entry.err, Context: entry.Context} +} + +// getPackageName reduces a fully qualified function name to the package name +// There really ought to be to be a better way... +func getPackageName(f string) string { + for { + lastPeriod := strings.LastIndex(f, ".") + lastSlash := strings.LastIndex(f, "/") + if lastPeriod > lastSlash { + f = f[:lastPeriod] + } else { + break + } + } + + return f +} + +// getCaller retrieves the name of the first non-logrus calling function +func getCaller() *runtime.Frame { + + // cache this package's fully-qualified name + callerInitOnce.Do(func() { + pcs := make([]uintptr, 2) + _ = runtime.Callers(0, pcs) + logrusPackage = getPackageName(runtime.FuncForPC(pcs[1]).Name()) + + // now that we have the cache, we can skip a minimum count of known-logrus functions + // XXX this is dubious, the number of frames may vary + minimumCallerDepth = knownLogrusFrames + }) + + // Restrict the lookback frames to avoid runaway lookups + pcs := make([]uintptr, maximumCallerDepth) + depth := runtime.Callers(minimumCallerDepth, pcs) + frames := runtime.CallersFrames(pcs[:depth]) + + for f, again := frames.Next(); again; f, again = frames.Next() { + pkg := getPackageName(f.Function) + + // If the caller isn't part of this package, we're done + if pkg != logrusPackage { + return &f + } + } + + // if we got here, we failed to find the caller's context + return nil +} + +func (entry Entry) HasCaller() (has bool) { + return entry.Logger != nil && + entry.Logger.ReportCaller && + entry.Caller != nil +} + +// This function is not declared with a pointer value because otherwise +// race conditions will occur when using multiple goroutines +func (entry Entry) log(level Level, msg string) { + var buffer *bytes.Buffer + + // Default to now, but allow users to override if they want. + // + // We don't have to worry about polluting future calls to Entry#log() + // with this assignment because this function is declared with a + // non-pointer receiver. + if entry.Time.IsZero() { + entry.Time = time.Now() + } + + entry.Level = level + entry.Message = msg + if entry.Logger.ReportCaller { + entry.Caller = getCaller() + } + + entry.fireHooks() + + buffer = bufferPool.Get().(*bytes.Buffer) + buffer.Reset() + defer bufferPool.Put(buffer) + entry.Buffer = buffer + + entry.write() + + entry.Buffer = nil + + // To avoid Entry#log() returning a value that only would make sense for + // panic() to use in Entry#Panic(), we avoid the allocation by checking + // directly here. + if level <= PanicLevel { + panic(&entry) + } +} + +func (entry *Entry) fireHooks() { + entry.Logger.mu.Lock() + defer entry.Logger.mu.Unlock() + err := entry.Logger.Hooks.Fire(entry.Level, entry) + if err != nil { + fmt.Fprintf(os.Stderr, "Failed to fire hook: %v\n", err) + } +} + +func (entry *Entry) write() { + entry.Logger.mu.Lock() + defer entry.Logger.mu.Unlock() + serialized, err := entry.Logger.Formatter.Format(entry) + if err != nil { + fmt.Fprintf(os.Stderr, "Failed to obtain reader, %v\n", err) + } else { + _, err = entry.Logger.Out.Write(serialized) + if err != nil { + fmt.Fprintf(os.Stderr, "Failed to write to log, %v\n", err) + } + } +} + +func (entry *Entry) Log(level Level, args ...interface{}) { + if entry.Logger.IsLevelEnabled(level) { + entry.log(level, fmt.Sprint(args...)) + } +} + +func (entry *Entry) Trace(args ...interface{}) { + entry.Log(TraceLevel, args...) +} + +func (entry *Entry) Debug(args ...interface{}) { + entry.Log(DebugLevel, args...) +} + +func (entry *Entry) Print(args ...interface{}) { + entry.Info(args...) +} + +func (entry *Entry) Info(args ...interface{}) { + entry.Log(InfoLevel, args...) +} + +func (entry *Entry) Warn(args ...interface{}) { + entry.Log(WarnLevel, args...) +} + +func (entry *Entry) Warning(args ...interface{}) { + entry.Warn(args...) +} + +func (entry *Entry) Error(args ...interface{}) { + entry.Log(ErrorLevel, args...) +} + +func (entry *Entry) Fatal(args ...interface{}) { + entry.Log(FatalLevel, args...) + entry.Logger.Exit(1) +} + +func (entry *Entry) Panic(args ...interface{}) { + entry.Log(PanicLevel, args...) + panic(fmt.Sprint(args...)) +} + +// Entry Printf family functions + +func (entry *Entry) Logf(level Level, format string, args ...interface{}) { + if entry.Logger.IsLevelEnabled(level) { + entry.Log(level, fmt.Sprintf(format, args...)) + } +} + +func (entry *Entry) Tracef(format string, args ...interface{}) { + entry.Logf(TraceLevel, format, args...) +} + +func (entry *Entry) Debugf(format string, args ...interface{}) { + entry.Logf(DebugLevel, format, args...) +} + +func (entry *Entry) Infof(format string, args ...interface{}) { + entry.Logf(InfoLevel, format, args...) +} + +func (entry *Entry) Printf(format string, args ...interface{}) { + entry.Infof(format, args...) +} + +func (entry *Entry) Warnf(format string, args ...interface{}) { + entry.Logf(WarnLevel, format, args...) +} + +func (entry *Entry) Warningf(format string, args ...interface{}) { + entry.Warnf(format, args...) +} + +func (entry *Entry) Errorf(format string, args ...interface{}) { + entry.Logf(ErrorLevel, format, args...) +} + +func (entry *Entry) Fatalf(format string, args ...interface{}) { + entry.Logf(FatalLevel, format, args...) + entry.Logger.Exit(1) +} + +func (entry *Entry) Panicf(format string, args ...interface{}) { + entry.Logf(PanicLevel, format, args...) +} + +// Entry Println family functions + +func (entry *Entry) Logln(level Level, args ...interface{}) { + if entry.Logger.IsLevelEnabled(level) { + entry.Log(level, entry.sprintlnn(args...)) + } +} + +func (entry *Entry) Traceln(args ...interface{}) { + entry.Logln(TraceLevel, args...) +} + +func (entry *Entry) Debugln(args ...interface{}) { + entry.Logln(DebugLevel, args...) +} + +func (entry *Entry) Infoln(args ...interface{}) { + entry.Logln(InfoLevel, args...) +} + +func (entry *Entry) Println(args ...interface{}) { + entry.Infoln(args...) +} + +func (entry *Entry) Warnln(args ...interface{}) { + entry.Logln(WarnLevel, args...) +} + +func (entry *Entry) Warningln(args ...interface{}) { + entry.Warnln(args...) +} + +func (entry *Entry) Errorln(args ...interface{}) { + entry.Logln(ErrorLevel, args...) +} + +func (entry *Entry) Fatalln(args ...interface{}) { + entry.Logln(FatalLevel, args...) + entry.Logger.Exit(1) +} + +func (entry *Entry) Panicln(args ...interface{}) { + entry.Logln(PanicLevel, args...) +} + +// Sprintlnn => Sprint no newline. This is to get the behavior of how +// fmt.Sprintln where spaces are always added between operands, regardless of +// their type. Instead of vendoring the Sprintln implementation to spare a +// string allocation, we do the simplest thing. +func (entry *Entry) sprintlnn(args ...interface{}) string { + msg := fmt.Sprintln(args...) + return msg[:len(msg)-1] +} diff --git a/logger/logrus/logrus/entry_test.go b/logger/logrus/logrus/entry_test.go new file mode 100644 index 0000000..f764085 --- /dev/null +++ b/logger/logrus/logrus/entry_test.go @@ -0,0 +1,169 @@ +package logrus + +import ( + "bytes" + "context" + "fmt" + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestEntryWithError(t *testing.T) { + + assert := assert.New(t) + + defer func() { + ErrorKey = "error" + }() + + err := fmt.Errorf("kaboom at layer %d", 4711) + + assert.Equal(err, WithError(err).Data["error"]) + + logger := New() + logger.Out = &bytes.Buffer{} + entry := NewEntry(logger) + + assert.Equal(err, entry.WithError(err).Data["error"]) + + ErrorKey = "err" + + assert.Equal(err, entry.WithError(err).Data["err"]) + +} + +func TestEntryWithContext(t *testing.T) { + assert := assert.New(t) + ctx := context.WithValue(context.Background(), "foo", "bar") + + assert.Equal(ctx, WithContext(ctx).Context) + + logger := New() + logger.Out = &bytes.Buffer{} + entry := NewEntry(logger) + + assert.Equal(ctx, entry.WithContext(ctx).Context) +} + +func TestEntryPanicln(t *testing.T) { + errBoom := fmt.Errorf("boom time") + + defer func() { + p := recover() + assert.NotNil(t, p) + + switch pVal := p.(type) { + case *Entry: + assert.Equal(t, "kaboom", pVal.Message) + assert.Equal(t, errBoom, pVal.Data["err"]) + default: + t.Fatalf("want type *Entry, got %T: %#v", pVal, pVal) + } + }() + + logger := New() + logger.Out = &bytes.Buffer{} + entry := NewEntry(logger) + entry.WithField("err", errBoom).Panicln("kaboom") +} + +func TestEntryPanicf(t *testing.T) { + errBoom := fmt.Errorf("boom again") + + defer func() { + p := recover() + assert.NotNil(t, p) + + switch pVal := p.(type) { + case *Entry: + assert.Equal(t, "kaboom true", pVal.Message) + assert.Equal(t, errBoom, pVal.Data["err"]) + default: + t.Fatalf("want type *Entry, got %T: %#v", pVal, pVal) + } + }() + + logger := New() + logger.Out = &bytes.Buffer{} + entry := NewEntry(logger) + entry.WithField("err", errBoom).Panicf("kaboom %v", true) +} + +const ( + badMessage = "this is going to panic" + panicMessage = "this is broken" +) + +type panickyHook struct{} + +func (p *panickyHook) Levels() []Level { + return []Level{InfoLevel} +} + +func (p *panickyHook) Fire(entry *Entry) error { + if entry.Message == badMessage { + panic(panicMessage) + } + + return nil +} + +func TestEntryHooksPanic(t *testing.T) { + logger := New() + logger.Out = &bytes.Buffer{} + logger.Level = InfoLevel + logger.Hooks.Add(&panickyHook{}) + + defer func() { + p := recover() + assert.NotNil(t, p) + assert.Equal(t, panicMessage, p) + + entry := NewEntry(logger) + entry.Info("another message") + }() + + entry := NewEntry(logger) + entry.Info(badMessage) +} + +func TestEntryWithIncorrectField(t *testing.T) { + assert := assert.New(t) + + fn := func() {} + + e := Entry{} + eWithFunc := e.WithFields(Fields{"func": fn}) + eWithFuncPtr := e.WithFields(Fields{"funcPtr": &fn}) + + assert.Equal(eWithFunc.err, `can not add field "func"`) + assert.Equal(eWithFuncPtr.err, `can not add field "funcPtr"`) + + eWithFunc = eWithFunc.WithField("not_a_func", "it is a string") + eWithFuncPtr = eWithFuncPtr.WithField("not_a_func", "it is a string") + + assert.Equal(eWithFunc.err, `can not add field "func"`) + assert.Equal(eWithFuncPtr.err, `can not add field "funcPtr"`) + + eWithFunc = eWithFunc.WithTime(time.Now()) + eWithFuncPtr = eWithFuncPtr.WithTime(time.Now()) + + assert.Equal(eWithFunc.err, `can not add field "func"`) + assert.Equal(eWithFuncPtr.err, `can not add field "funcPtr"`) +} + +func TestEntryLogfLevel(t *testing.T) { + logger := New() + buffer := &bytes.Buffer{} + logger.Out = buffer + logger.SetLevel(InfoLevel) + entry := NewEntry(logger) + + entry.Logf(DebugLevel, "%s", "debug") + assert.NotContains(t, buffer.String(), "debug", ) + + entry.Logf(WarnLevel, "%s", "warn") + assert.Contains(t, buffer.String(), "warn", ) +} \ No newline at end of file diff --git a/logger/logrus/logrus/example_basic_test.go b/logger/logrus/logrus/example_basic_test.go new file mode 100644 index 0000000..59f7ffc --- /dev/null +++ b/logger/logrus/logrus/example_basic_test.go @@ -0,0 +1,77 @@ +package logrus_test + +import ( + "os" + + "github.com/stack-labs/stack-rpc-plugins/logger/logrus/logrus" +) + +func Example_basic() { + var log = logrus.New() + log.Formatter = new(logrus.JSONFormatter) + log.Formatter = new(logrus.TextFormatter) //default + log.Formatter.(*logrus.TextFormatter).DisableColors = true // remove colors + log.Formatter.(*logrus.TextFormatter).DisableTimestamp = true // remove timestamp from test output + log.Level = logrus.TraceLevel + log.Out = os.Stdout + + // file, err := os.OpenFile("logrus.log", os.O_CREATE|os.O_WRONLY, 0666) + // if err == nil { + // log.Out = file + // } else { + // log.Info("Failed to log to file, using default stderr") + // } + + defer func() { + err := recover() + if err != nil { + entry := err.(*logrus.Entry) + log.WithFields(logrus.Fields{ + "omg": true, + "err_animal": entry.Data["animal"], + "err_size": entry.Data["size"], + "err_level": entry.Level, + "err_message": entry.Message, + "number": 100, + }).Error("The ice breaks!") // or use Fatal() to force the process to exit with a nonzero code + } + }() + + log.WithFields(logrus.Fields{ + "animal": "walrus", + "number": 0, + }).Trace("Went to the beach") + + log.WithFields(logrus.Fields{ + "animal": "walrus", + "number": 8, + }).Debug("Started observing beach") + + log.WithFields(logrus.Fields{ + "animal": "walrus", + "size": 10, + }).Info("A group of walrus emerges from the ocean") + + log.WithFields(logrus.Fields{ + "omg": true, + "number": 122, + }).Warn("The group's number increased tremendously!") + + log.WithFields(logrus.Fields{ + "temperature": -4, + }).Debug("Temperature changes") + + log.WithFields(logrus.Fields{ + "animal": "orca", + "size": 9009, + }).Panic("It's over 9000!") + + // Output: + // level=trace msg="Went to the beach" animal=walrus number=0 + // level=debug msg="Started observing beach" animal=walrus number=8 + // level=info msg="A group of walrus emerges from the ocean" animal=walrus size=10 + // level=warning msg="The group's number increased tremendously!" number=122 omg=true + // level=debug msg="Temperature changes" temperature=-4 + // level=panic msg="It's over 9000!" animal=orca size=9009 + // level=error msg="The ice breaks!" err_animal=orca err_level=panic err_message="It's over 9000!" err_size=9009 number=100 omg=true +} diff --git a/logger/logrus/logrus/example_custom_caller_test.go b/logger/logrus/logrus/example_custom_caller_test.go new file mode 100644 index 0000000..a27708f --- /dev/null +++ b/logger/logrus/logrus/example_custom_caller_test.go @@ -0,0 +1,28 @@ +package logrus_test + +import ( + "os" + "path" + "runtime" + "strings" + + "github.com/stack-labs/stack-rpc-plugins/logger/logrus/logrus" +) + +func ExampleCustomFormatter() { + l := logrus.New() + l.SetReportCaller(true) + l.Out = os.Stdout + l.Formatter = &logrus.JSONFormatter{ + DisableTimestamp: true, + CallerPrettyfier: func(f *runtime.Frame) (string, string) { + s := strings.Split(f.Function, ".") + funcname := s[len(s)-1] + _, filename := path.Split(f.File) + return funcname, filename + }, + } + l.Info("example of custom format caller") + // Output: + // {"file":"example_custom_caller_test.go","func":"ExampleCustomFormatter","level":"info","msg":"example of custom format caller"} +} diff --git a/logger/logrus/logrus/example_default_field_value_test.go b/logger/logrus/logrus/example_default_field_value_test.go new file mode 100644 index 0000000..76fc9c3 --- /dev/null +++ b/logger/logrus/logrus/example_default_field_value_test.go @@ -0,0 +1,30 @@ +package logrus_test + +import ( + "github.com/stack-labs/stack-rpc-plugins/logger/logrus/logrus" + "os" +) + +type DefaultFieldHook struct { + GetValue func() string +} + +func (h *DefaultFieldHook) Levels() []logrus.Level { + return logrus.AllLevels +} + +func (h *DefaultFieldHook) Fire(e *logrus.Entry) error { + e.Data["aDefaultField"] = h.GetValue() + return nil +} + +func ExampleDefaultField() { + l := logrus.New() + l.Out = os.Stdout + l.Formatter = &logrus.TextFormatter{DisableTimestamp: true, DisableColors: true} + + l.AddHook(&DefaultFieldHook{GetValue: func() string { return "with its default value" }}) + l.Info("first log") + // Output: + // level=info msg="first log" aDefaultField="with its default value" +} diff --git a/logger/logrus/logrus/example_global_hook_test.go b/logger/logrus/logrus/example_global_hook_test.go new file mode 100644 index 0000000..0992698 --- /dev/null +++ b/logger/logrus/logrus/example_global_hook_test.go @@ -0,0 +1,36 @@ +package logrus_test + +import ( + "github.com/stack-labs/stack-rpc-plugins/logger/logrus/logrus" + "os" +) + +var ( + mystring string +) + +type GlobalHook struct { +} + +func (h *GlobalHook) Levels() []logrus.Level { + return logrus.AllLevels +} + +func (h *GlobalHook) Fire(e *logrus.Entry) error { + e.Data["mystring"] = mystring + return nil +} + +func ExampleGlobalVariableHook() { + l := logrus.New() + l.Out = os.Stdout + l.Formatter = &logrus.TextFormatter{DisableTimestamp: true, DisableColors: true} + l.AddHook(&GlobalHook{}) + mystring = "first value" + l.Info("first log") + mystring = "another value" + l.Info("second log") + // Output: + // level=info msg="first log" mystring="first value" + // level=info msg="second log" mystring="another value" +} diff --git a/logger/logrus/logrus/example_hook_test.go b/logger/logrus/logrus/example_hook_test.go new file mode 100644 index 0000000..dc7bd2c --- /dev/null +++ b/logger/logrus/logrus/example_hook_test.go @@ -0,0 +1,43 @@ +// +build !windows + +package logrus_test + +import ( + "log/syslog" + "os" + + "github.com/stack-labs/stack-rpc-plugins/logger/logrus/logrus" + slhooks "github.com/sirupsen/logrus/hooks/syslog" +) + +// An example on how to use a hook +func Example_hook() { + var log = logrus.New() + log.Formatter = new(logrus.TextFormatter) // default + log.Formatter.(*logrus.TextFormatter).DisableColors = true // remove colors + log.Formatter.(*logrus.TextFormatter).DisableTimestamp = true // remove timestamp from test output + if sl, err := slhooks.NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, ""); err == nil { + log.Hooks.Add(sl) + } + log.Out = os.Stdout + + log.WithFields(logrus.Fields{ + "animal": "walrus", + "size": 10, + }).Info("A group of walrus emerges from the ocean") + + log.WithFields(logrus.Fields{ + "omg": true, + "number": 122, + }).Warn("The group's number increased tremendously!") + + log.WithFields(logrus.Fields{ + "omg": true, + "number": 100, + }).Error("The ice breaks!") + + // Output: + // level=info msg="A group of walrus emerges from the ocean" animal=walrus size=10 + // level=warning msg="The group's number increased tremendously!" number=122 omg=true + // level=error msg="The ice breaks!" number=100 omg=true +} diff --git a/logger/logrus/logrus/exported.go b/logger/logrus/logrus/exported.go new file mode 100644 index 0000000..62fc2f2 --- /dev/null +++ b/logger/logrus/logrus/exported.go @@ -0,0 +1,225 @@ +package logrus + +import ( + "context" + "io" + "time" +) + +var ( + // std is the name of the standard logger in stdlib `log` + std = New() +) + +func StandardLogger() *Logger { + return std +} + +// SetOutput sets the standard logger output. +func SetOutput(out io.Writer) { + std.SetOutput(out) +} + +// SetFormatter sets the standard logger formatter. +func SetFormatter(formatter Formatter) { + std.SetFormatter(formatter) +} + +// SetReportCaller sets whether the standard logger will include the calling +// method as a field. +func SetReportCaller(include bool) { + std.SetReportCaller(include) +} + +// SetLevel sets the standard logger level. +func SetLevel(level Level) { + std.SetLevel(level) +} + +// GetLevel returns the standard logger level. +func GetLevel() Level { + return std.GetLevel() +} + +// IsLevelEnabled checks if the log level of the standard logger is greater than the level param +func IsLevelEnabled(level Level) bool { + return std.IsLevelEnabled(level) +} + +// AddHook adds a hook to the standard logger hooks. +func AddHook(hook Hook) { + std.AddHook(hook) +} + +// WithError creates an entry from the standard logger and adds an error to it, using the value defined in ErrorKey as key. +func WithError(err error) *Entry { + return std.WithField(ErrorKey, err) +} + +// WithContext creates an entry from the standard logger and adds a context to it. +func WithContext(ctx context.Context) *Entry { + return std.WithContext(ctx) +} + +// WithField creates an entry from the standard logger and adds a field to +// it. If you want multiple fields, use `WithFields`. +// +// Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal +// or Panic on the Entry it returns. +func WithField(key string, value interface{}) *Entry { + return std.WithField(key, value) +} + +// WithFields creates an entry from the standard logger and adds multiple +// fields to it. This is simply a helper for `WithField`, invoking it +// once for each field. +// +// Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal +// or Panic on the Entry it returns. +func WithFields(fields Fields) *Entry { + return std.WithFields(fields) +} + +// WithTime creats an entry from the standard logger and overrides the time of +// logs generated with it. +// +// Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal +// or Panic on the Entry it returns. +func WithTime(t time.Time) *Entry { + return std.WithTime(t) +} + +// Trace logs a message at level Trace on the standard logger. +func Trace(args ...interface{}) { + std.Trace(args...) +} + +// Debug logs a message at level Debug on the standard logger. +func Debug(args ...interface{}) { + std.Debug(args...) +} + +// Print logs a message at level Info on the standard logger. +func Print(args ...interface{}) { + std.Print(args...) +} + +// Info logs a message at level Info on the standard logger. +func Info(args ...interface{}) { + std.Info(args...) +} + +// Warn logs a message at level Warn on the standard logger. +func Warn(args ...interface{}) { + std.Warn(args...) +} + +// Warning logs a message at level Warn on the standard logger. +func Warning(args ...interface{}) { + std.Warning(args...) +} + +// Error logs a message at level Error on the standard logger. +func Error(args ...interface{}) { + std.Error(args...) +} + +// Panic logs a message at level Panic on the standard logger. +func Panic(args ...interface{}) { + std.Panic(args...) +} + +// Fatal logs a message at level Fatal on the standard logger then the process will exit with status set to 1. +func Fatal(args ...interface{}) { + std.Fatal(args...) +} + +// Tracef logs a message at level Trace on the standard logger. +func Tracef(format string, args ...interface{}) { + std.Tracef(format, args...) +} + +// Debugf logs a message at level Debug on the standard logger. +func Debugf(format string, args ...interface{}) { + std.Debugf(format, args...) +} + +// Printf logs a message at level Info on the standard logger. +func Printf(format string, args ...interface{}) { + std.Printf(format, args...) +} + +// Infof logs a message at level Info on the standard logger. +func Infof(format string, args ...interface{}) { + std.Infof(format, args...) +} + +// Warnf logs a message at level Warn on the standard logger. +func Warnf(format string, args ...interface{}) { + std.Warnf(format, args...) +} + +// Warningf logs a message at level Warn on the standard logger. +func Warningf(format string, args ...interface{}) { + std.Warningf(format, args...) +} + +// Errorf logs a message at level Error on the standard logger. +func Errorf(format string, args ...interface{}) { + std.Errorf(format, args...) +} + +// Panicf logs a message at level Panic on the standard logger. +func Panicf(format string, args ...interface{}) { + std.Panicf(format, args...) +} + +// Fatalf logs a message at level Fatal on the standard logger then the process will exit with status set to 1. +func Fatalf(format string, args ...interface{}) { + std.Fatalf(format, args...) +} + +// Traceln logs a message at level Trace on the standard logger. +func Traceln(args ...interface{}) { + std.Traceln(args...) +} + +// Debugln logs a message at level Debug on the standard logger. +func Debugln(args ...interface{}) { + std.Debugln(args...) +} + +// Println logs a message at level Info on the standard logger. +func Println(args ...interface{}) { + std.Println(args...) +} + +// Infoln logs a message at level Info on the standard logger. +func Infoln(args ...interface{}) { + std.Infoln(args...) +} + +// Warnln logs a message at level Warn on the standard logger. +func Warnln(args ...interface{}) { + std.Warnln(args...) +} + +// Warningln logs a message at level Warn on the standard logger. +func Warningln(args ...interface{}) { + std.Warningln(args...) +} + +// Errorln logs a message at level Error on the standard logger. +func Errorln(args ...interface{}) { + std.Errorln(args...) +} + +// Panicln logs a message at level Panic on the standard logger. +func Panicln(args ...interface{}) { + std.Panicln(args...) +} + +// Fatalln logs a message at level Fatal on the standard logger then the process will exit with status set to 1. +func Fatalln(args ...interface{}) { + std.Fatalln(args...) +} diff --git a/logger/logrus/logrus/formatter.go b/logger/logrus/logrus/formatter.go new file mode 100644 index 0000000..4088837 --- /dev/null +++ b/logger/logrus/logrus/formatter.go @@ -0,0 +1,78 @@ +package logrus + +import "time" + +// Default key names for the default fields +const ( + defaultTimestampFormat = time.RFC3339 + FieldKeyMsg = "msg" + FieldKeyLevel = "level" + FieldKeyTime = "time" + FieldKeyLogrusError = "logrus_error" + FieldKeyFunc = "func" + FieldKeyFile = "file" +) + +// The Formatter interface is used to implement a custom Formatter. It takes an +// `Entry`. It exposes all the fields, including the default ones: +// +// * `entry.Data["msg"]`. The message passed from Info, Warn, Error .. +// * `entry.Data["time"]`. The timestamp. +// * `entry.Data["level"]. The level the entry was logged at. +// +// Any additional fields added with `WithField` or `WithFields` are also in +// `entry.Data`. Format is expected to return an array of bytes which are then +// logged to `logger.Out`. +type Formatter interface { + Format(*Entry) ([]byte, error) +} + +// This is to not silently overwrite `time`, `msg`, `func` and `level` fields when +// dumping it. If this code wasn't there doing: +// +// logrus.WithField("level", 1).Info("hello") +// +// Would just silently drop the user provided level. Instead with this code +// it'll logged as: +// +// {"level": "info", "fields.level": 1, "msg": "hello", "time": "..."} +// +// It's not exported because it's still using Data in an opinionated way. It's to +// avoid code duplication between the two default formatters. +func prefixFieldClashes(data Fields, fieldMap FieldMap, reportCaller bool) { + timeKey := fieldMap.resolve(FieldKeyTime) + if t, ok := data[timeKey]; ok { + data["fields."+timeKey] = t + delete(data, timeKey) + } + + msgKey := fieldMap.resolve(FieldKeyMsg) + if m, ok := data[msgKey]; ok { + data["fields."+msgKey] = m + delete(data, msgKey) + } + + levelKey := fieldMap.resolve(FieldKeyLevel) + if l, ok := data[levelKey]; ok { + data["fields."+levelKey] = l + delete(data, levelKey) + } + + logrusErrKey := fieldMap.resolve(FieldKeyLogrusError) + if l, ok := data[logrusErrKey]; ok { + data["fields."+logrusErrKey] = l + delete(data, logrusErrKey) + } + + // If reportCaller is not set, 'func' will not conflict. + if reportCaller { + funcKey := fieldMap.resolve(FieldKeyFunc) + if l, ok := data[funcKey]; ok { + data["fields."+funcKey] = l + } + fileKey := fieldMap.resolve(FieldKeyFile) + if l, ok := data[fileKey]; ok { + data["fields."+fileKey] = l + } + } +} diff --git a/logger/logrus/logrus/formatter_bench_test.go b/logger/logrus/logrus/formatter_bench_test.go new file mode 100644 index 0000000..d948158 --- /dev/null +++ b/logger/logrus/logrus/formatter_bench_test.go @@ -0,0 +1,101 @@ +package logrus + +import ( + "fmt" + "testing" + "time" +) + +// smallFields is a small size data set for benchmarking +var smallFields = Fields{ + "foo": "bar", + "baz": "qux", + "one": "two", + "three": "four", +} + +// largeFields is a large size data set for benchmarking +var largeFields = Fields{ + "foo": "bar", + "baz": "qux", + "one": "two", + "three": "four", + "five": "six", + "seven": "eight", + "nine": "ten", + "eleven": "twelve", + "thirteen": "fourteen", + "fifteen": "sixteen", + "seventeen": "eighteen", + "nineteen": "twenty", + "a": "b", + "c": "d", + "e": "f", + "g": "h", + "i": "j", + "k": "l", + "m": "n", + "o": "p", + "q": "r", + "s": "t", + "u": "v", + "w": "x", + "y": "z", + "this": "will", + "make": "thirty", + "entries": "yeah", +} + +var errorFields = Fields{ + "foo": fmt.Errorf("bar"), + "baz": fmt.Errorf("qux"), +} + +func BenchmarkErrorTextFormatter(b *testing.B) { + doBenchmark(b, &TextFormatter{DisableColors: true}, errorFields) +} + +func BenchmarkSmallTextFormatter(b *testing.B) { + doBenchmark(b, &TextFormatter{DisableColors: true}, smallFields) +} + +func BenchmarkLargeTextFormatter(b *testing.B) { + doBenchmark(b, &TextFormatter{DisableColors: true}, largeFields) +} + +func BenchmarkSmallColoredTextFormatter(b *testing.B) { + doBenchmark(b, &TextFormatter{ForceColors: true}, smallFields) +} + +func BenchmarkLargeColoredTextFormatter(b *testing.B) { + doBenchmark(b, &TextFormatter{ForceColors: true}, largeFields) +} + +func BenchmarkSmallJSONFormatter(b *testing.B) { + doBenchmark(b, &JSONFormatter{}, smallFields) +} + +func BenchmarkLargeJSONFormatter(b *testing.B) { + doBenchmark(b, &JSONFormatter{}, largeFields) +} + +func doBenchmark(b *testing.B, formatter Formatter, fields Fields) { + logger := New() + + entry := &Entry{ + Time: time.Time{}, + Level: InfoLevel, + Message: "message", + Data: fields, + Logger: logger, + } + var d []byte + var err error + for i := 0; i < b.N; i++ { + d, err = formatter.Format(entry) + if err != nil { + b.Fatal(err) + } + b.SetBytes(int64(len(d))) + } +} diff --git a/logger/logrus/logrus/go.sum b/logger/logrus/logrus/go.sum new file mode 100644 index 0000000..596c318 --- /dev/null +++ b/logger/logrus/logrus/go.sum @@ -0,0 +1,16 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/konsorten/go-windows-terminal-sequences v0.0.0-20180402223658-b729f2633dfe h1:CHRGQ8V7OlCYtwaKPJi3iA7J+YdNKdo8j7nG5IgDhjs= +github.com/konsorten/go-windows-terminal-sequences v0.0.0-20180402223658-b729f2633dfe/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33 h1:I6FyU15t786LL7oL/hn43zqTuEGr4PN7F4XJ1p4E3Y8= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/logger/logrus/logrus/hook_test.go b/logger/logrus/logrus/hook_test.go new file mode 100644 index 0000000..9c342b8 --- /dev/null +++ b/logger/logrus/logrus/hook_test.go @@ -0,0 +1,216 @@ +package logrus_test + +import ( + "bytes" + "encoding/json" + "sync" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + . "github.com/stack-labs/stack-rpc-plugins/logger/logrus/logrus" + . "github.com/stack-labs/stack-rpc-plugins/logger/logrus/logrus/internal/testutils" +) + +type TestHook struct { + Fired bool +} + +func (hook *TestHook) Fire(entry *Entry) error { + hook.Fired = true + return nil +} + +func (hook *TestHook) Levels() []Level { + return []Level{ + TraceLevel, + DebugLevel, + InfoLevel, + WarnLevel, + ErrorLevel, + FatalLevel, + PanicLevel, + } +} + +func TestHookFires(t *testing.T) { + hook := new(TestHook) + + LogAndAssertJSON(t, func(log *Logger) { + log.Hooks.Add(hook) + assert.Equal(t, hook.Fired, false) + + log.Print("test") + }, func(fields Fields) { + assert.Equal(t, hook.Fired, true) + }) +} + +type ModifyHook struct { +} + +func (hook *ModifyHook) Fire(entry *Entry) error { + entry.Data["wow"] = "whale" + return nil +} + +func (hook *ModifyHook) Levels() []Level { + return []Level{ + TraceLevel, + DebugLevel, + InfoLevel, + WarnLevel, + ErrorLevel, + FatalLevel, + PanicLevel, + } +} + +func TestHookCanModifyEntry(t *testing.T) { + hook := new(ModifyHook) + + LogAndAssertJSON(t, func(log *Logger) { + log.Hooks.Add(hook) + log.WithField("wow", "elephant").Print("test") + }, func(fields Fields) { + assert.Equal(t, fields["wow"], "whale") + }) +} + +func TestCanFireMultipleHooks(t *testing.T) { + hook1 := new(ModifyHook) + hook2 := new(TestHook) + + LogAndAssertJSON(t, func(log *Logger) { + log.Hooks.Add(hook1) + log.Hooks.Add(hook2) + + log.WithField("wow", "elephant").Print("test") + }, func(fields Fields) { + assert.Equal(t, fields["wow"], "whale") + assert.Equal(t, hook2.Fired, true) + }) +} + +type SingleLevelModifyHook struct { + ModifyHook +} + +func (h *SingleLevelModifyHook) Levels() []Level { + return []Level{InfoLevel} +} + +func TestHookEntryIsPristine(t *testing.T) { + l := New() + b := &bytes.Buffer{} + l.Formatter = &JSONFormatter{} + l.Out = b + l.AddHook(&SingleLevelModifyHook{}) + + l.Error("error message") + data := map[string]string{} + err := json.Unmarshal(b.Bytes(), &data) + require.NoError(t, err) + _, ok := data["wow"] + require.False(t, ok) + b.Reset() + + l.Info("error message") + data = map[string]string{} + err = json.Unmarshal(b.Bytes(), &data) + require.NoError(t, err) + _, ok = data["wow"] + require.True(t, ok) + b.Reset() + + l.Error("error message") + data = map[string]string{} + err = json.Unmarshal(b.Bytes(), &data) + require.NoError(t, err) + _, ok = data["wow"] + require.False(t, ok) + b.Reset() +} + +type ErrorHook struct { + Fired bool +} + +func (hook *ErrorHook) Fire(entry *Entry) error { + hook.Fired = true + return nil +} + +func (hook *ErrorHook) Levels() []Level { + return []Level{ + ErrorLevel, + } +} + +func TestErrorHookShouldntFireOnInfo(t *testing.T) { + hook := new(ErrorHook) + + LogAndAssertJSON(t, func(log *Logger) { + log.Hooks.Add(hook) + log.Info("test") + }, func(fields Fields) { + assert.Equal(t, hook.Fired, false) + }) +} + +func TestErrorHookShouldFireOnError(t *testing.T) { + hook := new(ErrorHook) + + LogAndAssertJSON(t, func(log *Logger) { + log.Hooks.Add(hook) + log.Error("test") + }, func(fields Fields) { + assert.Equal(t, hook.Fired, true) + }) +} + +func TestAddHookRace(t *testing.T) { + var wg sync.WaitGroup + wg.Add(2) + hook := new(ErrorHook) + LogAndAssertJSON(t, func(log *Logger) { + go func() { + defer wg.Done() + log.AddHook(hook) + }() + go func() { + defer wg.Done() + log.Error("test") + }() + wg.Wait() + }, func(fields Fields) { + // the line may have been logged + // before the hook was added, so we can't + // actually assert on the hook + }) +} + +type HookCallFunc struct { + F func() +} + +func (h *HookCallFunc) Levels() []Level { + return AllLevels +} + +func (h *HookCallFunc) Fire(e *Entry) error { + h.F() + return nil +} + +func TestHookFireOrder(t *testing.T) { + checkers := []string{} + h := LevelHooks{} + h.Add(&HookCallFunc{F: func() { checkers = append(checkers, "first hook") }}) + h.Add(&HookCallFunc{F: func() { checkers = append(checkers, "second hook") }}) + h.Add(&HookCallFunc{F: func() { checkers = append(checkers, "third hook") }}) + + h.Fire(InfoLevel, &Entry{}) + require.Equal(t, []string{"first hook", "second hook", "third hook"}, checkers) +} diff --git a/logger/logrus/logrus/hooks.go b/logger/logrus/logrus/hooks.go new file mode 100644 index 0000000..3f151cd --- /dev/null +++ b/logger/logrus/logrus/hooks.go @@ -0,0 +1,34 @@ +package logrus + +// A hook to be fired when logging on the logging levels returned from +// `Levels()` on your implementation of the interface. Note that this is not +// fired in a goroutine or a channel with workers, you should handle such +// functionality yourself if your call is non-blocking and you don't wish for +// the logging calls for levels returned from `Levels()` to block. +type Hook interface { + Levels() []Level + Fire(*Entry) error +} + +// Internal type for storing the hooks on a logger instance. +type LevelHooks map[Level][]Hook + +// Add a hook to an instance of logger. This is called with +// `log.Hooks.Add(new(MyHook))` where `MyHook` implements the `Hook` interface. +func (hooks LevelHooks) Add(hook Hook) { + for _, level := range hook.Levels() { + hooks[level] = append(hooks[level], hook) + } +} + +// Fire all the hooks for the passed level. Used by `entry.log` to fire +// appropriate hooks for a log entry. +func (hooks LevelHooks) Fire(level Level, entry *Entry) error { + for _, hook := range hooks[level] { + if err := hook.Fire(entry); err != nil { + return err + } + } + + return nil +} diff --git a/logger/logrus/logrus/hooks/syslog/README.md b/logger/logrus/logrus/hooks/syslog/README.md new file mode 100644 index 0000000..1bbc0f7 --- /dev/null +++ b/logger/logrus/logrus/hooks/syslog/README.md @@ -0,0 +1,39 @@ +# Syslog Hooks for Logrus :walrus: + +## Usage + +```go +import ( + "log/syslog" + "github.com/sirupsen/logrus" + lSyslog "github.com/sirupsen/logrus/hooks/syslog" +) + +func main() { + log := logrus.New() + hook, err := lSyslog.NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, "") + + if err == nil { + log.Hooks.Add(hook) + } +} +``` + +If you want to connect to local syslog (Ex. "/dev/log" or "/var/run/syslog" or "/var/run/log"). Just assign empty string to the first two parameters of `NewSyslogHook`. It should look like the following. + +```go +import ( + "log/syslog" + "github.com/sirupsen/logrus" + lSyslog "github.com/sirupsen/logrus/hooks/syslog" +) + +func main() { + log := logrus.New() + hook, err := lSyslog.NewSyslogHook("", "", syslog.LOG_INFO, "") + + if err == nil { + log.Hooks.Add(hook) + } +} +``` diff --git a/logger/logrus/logrus/hooks/syslog/syslog.go b/logger/logrus/logrus/hooks/syslog/syslog.go new file mode 100644 index 0000000..dafd449 --- /dev/null +++ b/logger/logrus/logrus/hooks/syslog/syslog.go @@ -0,0 +1,55 @@ +// +build !windows,!nacl,!plan9 + +package syslog + +import ( + "fmt" + "log/syslog" + "os" + + "github.com/stack-labs/stack-rpc-plugins/logger/logrus/logrus" +) + +// SyslogHook to send logs via syslog. +type SyslogHook struct { + Writer *syslog.Writer + SyslogNetwork string + SyslogRaddr string +} + +// Creates a hook to be added to an instance of logger. This is called with +// `hook, err := NewSyslogHook("udp", "localhost:514", syslog.LOG_DEBUG, "")` +// `if err == nil { log.Hooks.Add(hook) }` +func NewSyslogHook(network, raddr string, priority syslog.Priority, tag string) (*SyslogHook, error) { + w, err := syslog.Dial(network, raddr, priority, tag) + return &SyslogHook{w, network, raddr}, err +} + +func (hook *SyslogHook) Fire(entry *logrus.Entry) error { + line, err := entry.String() + if err != nil { + fmt.Fprintf(os.Stderr, "Unable to read entry, %v", err) + return err + } + + switch entry.Level { + case logrus.PanicLevel: + return hook.Writer.Crit(line) + case logrus.FatalLevel: + return hook.Writer.Crit(line) + case logrus.ErrorLevel: + return hook.Writer.Err(line) + case logrus.WarnLevel: + return hook.Writer.Warning(line) + case logrus.InfoLevel: + return hook.Writer.Info(line) + case logrus.DebugLevel, logrus.TraceLevel: + return hook.Writer.Debug(line) + default: + return nil + } +} + +func (hook *SyslogHook) Levels() []logrus.Level { + return logrus.AllLevels +} diff --git a/logger/logrus/logrus/hooks/syslog/syslog_test.go b/logger/logrus/logrus/hooks/syslog/syslog_test.go new file mode 100644 index 0000000..a28f920 --- /dev/null +++ b/logger/logrus/logrus/hooks/syslog/syslog_test.go @@ -0,0 +1,29 @@ +// +build !windows,!nacl,!plan9 + +package syslog + +import ( + "log/syslog" + "testing" + + "github.com/stack-labs/stack-rpc-plugins/logger/logrus/logrus" +) + +func TestLocalhostAddAndPrint(t *testing.T) { + log := logrus.New() + hook, err := NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, "") + + if err != nil { + t.Errorf("Unable to connect to local syslog.") + } + + log.Hooks.Add(hook) + + for _, level := range hook.Levels() { + if len(log.Hooks[level]) != 1 { + t.Errorf("SyslogHook was not added. The length of log.Hooks[%v]: %v", level, len(log.Hooks[level])) + } + } + + log.Info("Congratulations!") +} diff --git a/logger/logrus/logrus/hooks/test/test.go b/logger/logrus/logrus/hooks/test/test.go new file mode 100644 index 0000000..e9122f2 --- /dev/null +++ b/logger/logrus/logrus/hooks/test/test.go @@ -0,0 +1,92 @@ +// The Test package is used for testing logrus. It is here for backwards +// compatibility from when logrus' organization was upper-case. Please use +// lower-case logrus and the `null` package instead of this one. +package test + +import ( + "io/ioutil" + "sync" + + "github.com/stack-labs/stack-rpc-plugins/logger/logrus/logrus" +) + +// Hook is a hook designed for dealing with logs in test scenarios. +type Hook struct { + // Entries is an array of all entries that have been received by this hook. + // For safe access, use the AllEntries() method, rather than reading this + // value directly. + Entries []logrus.Entry + mu sync.RWMutex +} + +// NewGlobal installs a test hook for the global logger. +func NewGlobal() *Hook { + + hook := new(Hook) + logrus.AddHook(hook) + + return hook + +} + +// NewLocal installs a test hook for a given local logger. +func NewLocal(logger *logrus.Logger) *Hook { + + hook := new(Hook) + logger.Hooks.Add(hook) + + return hook + +} + +// NewNullLogger creates a discarding logger and installs the test hook. +func NewNullLogger() (*logrus.Logger, *Hook) { + + logger := logrus.New() + logger.Out = ioutil.Discard + + return logger, NewLocal(logger) + +} + +func (t *Hook) Fire(e *logrus.Entry) error { + t.mu.Lock() + defer t.mu.Unlock() + t.Entries = append(t.Entries, *e) + return nil +} + +func (t *Hook) Levels() []logrus.Level { + return logrus.AllLevels +} + +// LastEntry returns the last entry that was logged or nil. +func (t *Hook) LastEntry() *logrus.Entry { + t.mu.RLock() + defer t.mu.RUnlock() + i := len(t.Entries) - 1 + if i < 0 { + return nil + } + return &t.Entries[i] +} + +// AllEntries returns all entries that were logged. +func (t *Hook) AllEntries() []*logrus.Entry { + t.mu.RLock() + defer t.mu.RUnlock() + // Make a copy so the returned value won't race with future log requests + entries := make([]*logrus.Entry, len(t.Entries)) + for i := 0; i < len(t.Entries); i++ { + // Make a copy, for safety + entries[i] = &t.Entries[i] + } + return entries +} + +// Reset removes all Entries from this test hook. +func (t *Hook) Reset() { + t.mu.Lock() + defer t.mu.Unlock() + t.Entries = make([]logrus.Entry, 0) +} diff --git a/logger/logrus/logrus/hooks/test/test_test.go b/logger/logrus/logrus/hooks/test/test_test.go new file mode 100644 index 0000000..a578d0a --- /dev/null +++ b/logger/logrus/logrus/hooks/test/test_test.go @@ -0,0 +1,85 @@ +package test + +import ( + "math/rand" + "sync" + "testing" + "time" + + "github.com/stack-labs/stack-rpc-plugins/logger/logrus/logrus" + "github.com/stretchr/testify/assert" +) + +func TestAllHooks(t *testing.T) { + assert := assert.New(t) + + logger, hook := NewNullLogger() + assert.Nil(hook.LastEntry()) + assert.Equal(0, len(hook.Entries)) + + logger.Error("Hello error") + assert.Equal(logrus.ErrorLevel, hook.LastEntry().Level) + assert.Equal("Hello error", hook.LastEntry().Message) + assert.Equal(1, len(hook.Entries)) + + logger.Warn("Hello warning") + assert.Equal(logrus.WarnLevel, hook.LastEntry().Level) + assert.Equal("Hello warning", hook.LastEntry().Message) + assert.Equal(2, len(hook.Entries)) + + hook.Reset() + assert.Nil(hook.LastEntry()) + assert.Equal(0, len(hook.Entries)) + + hook = NewGlobal() + + logrus.Error("Hello error") + assert.Equal(logrus.ErrorLevel, hook.LastEntry().Level) + assert.Equal("Hello error", hook.LastEntry().Message) + assert.Equal(1, len(hook.Entries)) +} + +func TestLoggingWithHooksRace(t *testing.T) { + + rand.Seed(time.Now().Unix()) + unlocker := rand.Int() % 100 + + assert := assert.New(t) + logger, hook := NewNullLogger() + + var wgOne, wgAll sync.WaitGroup + wgOne.Add(1) + wgAll.Add(100) + + for i := 0; i < 100; i++ { + go func(i int) { + logger.Info("info") + wgAll.Done() + if i == unlocker { + wgOne.Done() + } + }(i) + } + + wgOne.Wait() + + assert.Equal(logrus.InfoLevel, hook.LastEntry().Level) + assert.Equal("info", hook.LastEntry().Message) + + wgAll.Wait() + + entries := hook.AllEntries() + assert.Equal(100, len(entries)) +} + +func TestFatalWithAlternateExit(t *testing.T) { + assert := assert.New(t) + + logger, hook := NewNullLogger() + logger.ExitFunc = func(code int) {} + + logger.Fatal("something went very wrong") + assert.Equal(logrus.FatalLevel, hook.LastEntry().Level) + assert.Equal("something went very wrong", hook.LastEntry().Message) + assert.Equal(1, len(hook.Entries)) +} diff --git a/logger/logrus/logrus/internal/testutils/testutils.go b/logger/logrus/logrus/internal/testutils/testutils.go new file mode 100644 index 0000000..d1acdb7 --- /dev/null +++ b/logger/logrus/logrus/internal/testutils/testutils.go @@ -0,0 +1,58 @@ +package testutils + +import ( + "bytes" + "encoding/json" + "strconv" + "strings" + "testing" + + . "github.com/stack-labs/stack-rpc-plugins/logger/logrus/logrus" + + "github.com/stretchr/testify/require" +) + +func LogAndAssertJSON(t *testing.T, log func(*Logger), assertions func(fields Fields)) { + var buffer bytes.Buffer + var fields Fields + + logger := New() + logger.Out = &buffer + logger.Formatter = new(JSONFormatter) + + log(logger) + + err := json.Unmarshal(buffer.Bytes(), &fields) + require.Nil(t, err) + + assertions(fields) +} + +func LogAndAssertText(t *testing.T, log func(*Logger), assertions func(fields map[string]string)) { + var buffer bytes.Buffer + + logger := New() + logger.Out = &buffer + logger.Formatter = &TextFormatter{ + DisableColors: true, + } + + log(logger) + + fields := make(map[string]string) + for _, kv := range strings.Split(strings.TrimRight(buffer.String(), "\n"), " ") { + if !strings.Contains(kv, "=") { + continue + } + kvArr := strings.Split(kv, "=") + key := strings.TrimSpace(kvArr[0]) + val := kvArr[1] + if kvArr[1][0] == '"' { + var err error + val, err = strconv.Unquote(val) + require.NoError(t, err) + } + fields[key] = val + } + assertions(fields) +} diff --git a/logger/logrus/logrus/json_formatter.go b/logger/logrus/logrus/json_formatter.go new file mode 100644 index 0000000..098a21a --- /dev/null +++ b/logger/logrus/logrus/json_formatter.go @@ -0,0 +1,121 @@ +package logrus + +import ( + "bytes" + "encoding/json" + "fmt" + "runtime" +) + +type fieldKey string + +// FieldMap allows customization of the key names for default fields. +type FieldMap map[fieldKey]string + +func (f FieldMap) resolve(key fieldKey) string { + if k, ok := f[key]; ok { + return k + } + + return string(key) +} + +// JSONFormatter formats logs into parsable json +type JSONFormatter struct { + // TimestampFormat sets the format used for marshaling timestamps. + TimestampFormat string + + // DisableTimestamp allows disabling automatic timestamps in output + DisableTimestamp bool + + // DataKey allows users to put all the log entry parameters into a nested dictionary at a given key. + DataKey string + + // FieldMap allows users to customize the names of keys for default fields. + // As an example: + // formatter := &JSONFormatter{ + // FieldMap: FieldMap{ + // FieldKeyTime: "@timestamp", + // FieldKeyLevel: "@level", + // FieldKeyMsg: "@message", + // FieldKeyFunc: "@caller", + // }, + // } + FieldMap FieldMap + + // CallerPrettyfier can be set by the user to modify the content + // of the function and file keys in the json data when ReportCaller is + // activated. If any of the returned value is the empty string the + // corresponding key will be removed from json fields. + CallerPrettyfier func(*runtime.Frame) (function string, file string) + + // PrettyPrint will indent all json logs + PrettyPrint bool +} + +// Format renders a single log entry +func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) { + data := make(Fields, len(entry.Data)+4) + for k, v := range entry.Data { + switch v := v.(type) { + case error: + // Otherwise errors are ignored by `encoding/json` + // https://github.com/sirupsen/logrus/issues/137 + data[k] = v.Error() + default: + data[k] = v + } + } + + if f.DataKey != "" { + newData := make(Fields, 4) + newData[f.DataKey] = data + data = newData + } + + prefixFieldClashes(data, f.FieldMap, entry.HasCaller()) + + timestampFormat := f.TimestampFormat + if timestampFormat == "" { + timestampFormat = defaultTimestampFormat + } + + if entry.err != "" { + data[f.FieldMap.resolve(FieldKeyLogrusError)] = entry.err + } + if !f.DisableTimestamp { + data[f.FieldMap.resolve(FieldKeyTime)] = entry.Time.Format(timestampFormat) + } + data[f.FieldMap.resolve(FieldKeyMsg)] = entry.Message + data[f.FieldMap.resolve(FieldKeyLevel)] = entry.Level.String() + if entry.HasCaller() { + funcVal := entry.Caller.Function + fileVal := fmt.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line) + if f.CallerPrettyfier != nil { + funcVal, fileVal = f.CallerPrettyfier(entry.Caller) + } + if funcVal != "" { + data[f.FieldMap.resolve(FieldKeyFunc)] = funcVal + } + if fileVal != "" { + data[f.FieldMap.resolve(FieldKeyFile)] = fileVal + } + } + + var b *bytes.Buffer + if entry.Buffer != nil { + b = entry.Buffer + } else { + b = &bytes.Buffer{} + } + + encoder := json.NewEncoder(b) + if f.PrettyPrint { + encoder.SetIndent("", " ") + } + if err := encoder.Encode(data); err != nil { + return nil, fmt.Errorf("failed to marshal fields to JSON, %v", err) + } + + return b.Bytes(), nil +} diff --git a/logger/logrus/logrus/json_formatter_test.go b/logger/logrus/logrus/json_formatter_test.go new file mode 100644 index 0000000..695c36e --- /dev/null +++ b/logger/logrus/logrus/json_formatter_test.go @@ -0,0 +1,346 @@ +package logrus + +import ( + "encoding/json" + "errors" + "fmt" + "runtime" + "strings" + "testing" +) + +func TestErrorNotLost(t *testing.T) { + formatter := &JSONFormatter{} + + b, err := formatter.Format(WithField("error", errors.New("wild walrus"))) + if err != nil { + t.Fatal("Unable to format entry: ", err) + } + + entry := make(map[string]interface{}) + err = json.Unmarshal(b, &entry) + if err != nil { + t.Fatal("Unable to unmarshal formatted entry: ", err) + } + + if entry["error"] != "wild walrus" { + t.Fatal("Error field not set") + } +} + +func TestErrorNotLostOnFieldNotNamedError(t *testing.T) { + formatter := &JSONFormatter{} + + b, err := formatter.Format(WithField("omg", errors.New("wild walrus"))) + if err != nil { + t.Fatal("Unable to format entry: ", err) + } + + entry := make(map[string]interface{}) + err = json.Unmarshal(b, &entry) + if err != nil { + t.Fatal("Unable to unmarshal formatted entry: ", err) + } + + if entry["omg"] != "wild walrus" { + t.Fatal("Error field not set") + } +} + +func TestFieldClashWithTime(t *testing.T) { + formatter := &JSONFormatter{} + + b, err := formatter.Format(WithField("time", "right now!")) + if err != nil { + t.Fatal("Unable to format entry: ", err) + } + + entry := make(map[string]interface{}) + err = json.Unmarshal(b, &entry) + if err != nil { + t.Fatal("Unable to unmarshal formatted entry: ", err) + } + + if entry["fields.time"] != "right now!" { + t.Fatal("fields.time not set to original time field") + } + + if entry["time"] != "0001-01-01T00:00:00Z" { + t.Fatal("time field not set to current time, was: ", entry["time"]) + } +} + +func TestFieldClashWithMsg(t *testing.T) { + formatter := &JSONFormatter{} + + b, err := formatter.Format(WithField("msg", "something")) + if err != nil { + t.Fatal("Unable to format entry: ", err) + } + + entry := make(map[string]interface{}) + err = json.Unmarshal(b, &entry) + if err != nil { + t.Fatal("Unable to unmarshal formatted entry: ", err) + } + + if entry["fields.msg"] != "something" { + t.Fatal("fields.msg not set to original msg field") + } +} + +func TestFieldClashWithLevel(t *testing.T) { + formatter := &JSONFormatter{} + + b, err := formatter.Format(WithField("level", "something")) + if err != nil { + t.Fatal("Unable to format entry: ", err) + } + + entry := make(map[string]interface{}) + err = json.Unmarshal(b, &entry) + if err != nil { + t.Fatal("Unable to unmarshal formatted entry: ", err) + } + + if entry["fields.level"] != "something" { + t.Fatal("fields.level not set to original level field") + } +} + +func TestFieldClashWithRemappedFields(t *testing.T) { + formatter := &JSONFormatter{ + FieldMap: FieldMap{ + FieldKeyTime: "@timestamp", + FieldKeyLevel: "@level", + FieldKeyMsg: "@message", + }, + } + + b, err := formatter.Format(WithFields(Fields{ + "@timestamp": "@timestamp", + "@level": "@level", + "@message": "@message", + "timestamp": "timestamp", + "level": "level", + "msg": "msg", + })) + if err != nil { + t.Fatal("Unable to format entry: ", err) + } + + entry := make(map[string]interface{}) + err = json.Unmarshal(b, &entry) + if err != nil { + t.Fatal("Unable to unmarshal formatted entry: ", err) + } + + for _, field := range []string{"timestamp", "level", "msg"} { + if entry[field] != field { + t.Errorf("Expected field %v to be untouched; got %v", field, entry[field]) + } + + remappedKey := fmt.Sprintf("fields.%s", field) + if remapped, ok := entry[remappedKey]; ok { + t.Errorf("Expected %s to be empty; got %v", remappedKey, remapped) + } + } + + for _, field := range []string{"@timestamp", "@level", "@message"} { + if entry[field] == field { + t.Errorf("Expected field %v to be mapped to an Entry value", field) + } + + remappedKey := fmt.Sprintf("fields.%s", field) + if remapped, ok := entry[remappedKey]; ok { + if remapped != field { + t.Errorf("Expected field %v to be copied to %s; got %v", field, remappedKey, remapped) + } + } else { + t.Errorf("Expected field %v to be copied to %s; was absent", field, remappedKey) + } + } +} + +func TestFieldsInNestedDictionary(t *testing.T) { + formatter := &JSONFormatter{ + DataKey: "args", + } + + logEntry := WithFields(Fields{ + "level": "level", + "test": "test", + }) + logEntry.Level = InfoLevel + + b, err := formatter.Format(logEntry) + if err != nil { + t.Fatal("Unable to format entry: ", err) + } + + entry := make(map[string]interface{}) + err = json.Unmarshal(b, &entry) + if err != nil { + t.Fatal("Unable to unmarshal formatted entry: ", err) + } + + args := entry["args"].(map[string]interface{}) + + for _, field := range []string{"test", "level"} { + if value, present := args[field]; !present || value != field { + t.Errorf("Expected field %v to be present under 'args'; untouched", field) + } + } + + for _, field := range []string{"test", "fields.level"} { + if _, present := entry[field]; present { + t.Errorf("Expected field %v not to be present at top level", field) + } + } + + // with nested object, "level" shouldn't clash + if entry["level"] != "info" { + t.Errorf("Expected 'level' field to contain 'info'") + } +} + +func TestJSONEntryEndsWithNewline(t *testing.T) { + formatter := &JSONFormatter{} + + b, err := formatter.Format(WithField("level", "something")) + if err != nil { + t.Fatal("Unable to format entry: ", err) + } + + if b[len(b)-1] != '\n' { + t.Fatal("Expected JSON log entry to end with a newline") + } +} + +func TestJSONMessageKey(t *testing.T) { + formatter := &JSONFormatter{ + FieldMap: FieldMap{ + FieldKeyMsg: "message", + }, + } + + b, err := formatter.Format(&Entry{Message: "oh hai"}) + if err != nil { + t.Fatal("Unable to format entry: ", err) + } + s := string(b) + if !(strings.Contains(s, "message") && strings.Contains(s, "oh hai")) { + t.Fatal("Expected JSON to format message key") + } +} + +func TestJSONLevelKey(t *testing.T) { + formatter := &JSONFormatter{ + FieldMap: FieldMap{ + FieldKeyLevel: "somelevel", + }, + } + + b, err := formatter.Format(WithField("level", "something")) + if err != nil { + t.Fatal("Unable to format entry: ", err) + } + s := string(b) + if !strings.Contains(s, "somelevel") { + t.Fatal("Expected JSON to format level key") + } +} + +func TestJSONTimeKey(t *testing.T) { + formatter := &JSONFormatter{ + FieldMap: FieldMap{ + FieldKeyTime: "timeywimey", + }, + } + + b, err := formatter.Format(WithField("level", "something")) + if err != nil { + t.Fatal("Unable to format entry: ", err) + } + s := string(b) + if !strings.Contains(s, "timeywimey") { + t.Fatal("Expected JSON to format time key") + } +} + +func TestFieldDoesNotClashWithCaller(t *testing.T) { + SetReportCaller(false) + formatter := &JSONFormatter{} + + b, err := formatter.Format(WithField("func", "howdy pardner")) + if err != nil { + t.Fatal("Unable to format entry: ", err) + } + + entry := make(map[string]interface{}) + err = json.Unmarshal(b, &entry) + if err != nil { + t.Fatal("Unable to unmarshal formatted entry: ", err) + } + + if entry["func"] != "howdy pardner" { + t.Fatal("func field replaced when ReportCaller=false") + } +} + +func TestFieldClashWithCaller(t *testing.T) { + SetReportCaller(true) + formatter := &JSONFormatter{} + e := WithField("func", "howdy pardner") + e.Caller = &runtime.Frame{Function: "somefunc"} + b, err := formatter.Format(e) + if err != nil { + t.Fatal("Unable to format entry: ", err) + } + + entry := make(map[string]interface{}) + err = json.Unmarshal(b, &entry) + if err != nil { + t.Fatal("Unable to unmarshal formatted entry: ", err) + } + + if entry["fields.func"] != "howdy pardner" { + t.Fatalf("fields.func not set to original func field when ReportCaller=true (got '%s')", + entry["fields.func"]) + } + + if entry["func"] != "somefunc" { + t.Fatalf("func not set as expected when ReportCaller=true (got '%s')", + entry["func"]) + } + + SetReportCaller(false) // return to default value +} + +func TestJSONDisableTimestamp(t *testing.T) { + formatter := &JSONFormatter{ + DisableTimestamp: true, + } + + b, err := formatter.Format(WithField("level", "something")) + if err != nil { + t.Fatal("Unable to format entry: ", err) + } + s := string(b) + if strings.Contains(s, FieldKeyTime) { + t.Error("Did not prevent timestamp", s) + } +} + +func TestJSONEnableTimestamp(t *testing.T) { + formatter := &JSONFormatter{} + + b, err := formatter.Format(WithField("level", "something")) + if err != nil { + t.Fatal("Unable to format entry: ", err) + } + s := string(b) + if !strings.Contains(s, FieldKeyTime) { + t.Error("Timestamp not present", s) + } +} diff --git a/logger/logrus/logrus/level_test.go b/logger/logrus/logrus/level_test.go new file mode 100644 index 0000000..6b0fc8b --- /dev/null +++ b/logger/logrus/logrus/level_test.go @@ -0,0 +1,62 @@ +package logrus_test + +import ( + "bytes" + "encoding/json" + "testing" + + "github.com/stack-labs/stack-rpc-plugins/logger/logrus/logrus" + "github.com/stretchr/testify/require" +) + +func TestLevelJsonEncoding(t *testing.T) { + type X struct { + Level logrus.Level + } + + var x X + x.Level = logrus.WarnLevel + var buf bytes.Buffer + enc := json.NewEncoder(&buf) + require.NoError(t, enc.Encode(x)) + dec := json.NewDecoder(&buf) + var y X + require.NoError(t, dec.Decode(&y)) +} + +func TestLevelUnmarshalText(t *testing.T) { + var u logrus.Level + for _, level := range logrus.AllLevels { + t.Run(level.String(), func(t *testing.T) { + require.NoError(t, u.UnmarshalText([]byte(level.String()))) + require.Equal(t, level, u) + }) + } + t.Run("invalid", func(t *testing.T) { + require.Error(t, u.UnmarshalText([]byte("invalid"))) + }) +} + +func TestLevelMarshalText(t *testing.T) { + levelStrings := []string{ + "panic", + "fatal", + "error", + "warning", + "info", + "debug", + "trace", + } + for idx, val := range logrus.AllLevels { + level := val + t.Run(level.String(), func(t *testing.T) { + var cmp logrus.Level + b, err := level.MarshalText() + require.NoError(t, err) + require.Equal(t, levelStrings[idx], string(b)) + err = cmp.UnmarshalText(b) + require.NoError(t, err) + require.Equal(t, level, cmp) + }) + } +} diff --git a/logger/logrus/logrus/logger.go b/logger/logrus/logrus/logger.go new file mode 100644 index 0000000..c0c0b1e --- /dev/null +++ b/logger/logrus/logrus/logger.go @@ -0,0 +1,351 @@ +package logrus + +import ( + "context" + "io" + "os" + "sync" + "sync/atomic" + "time" +) + +type Logger struct { + // The logs are `io.Copy`'d to this in a mutex. It's common to set this to a + // file, or leave it default which is `os.Stderr`. You can also set this to + // something more adventurous, such as logging to Kafka. + Out io.Writer + // Hooks for the logger instance. These allow firing events based on logging + // levels and log entries. For example, to send errors to an error tracking + // service, log to StatsD or dump the core on fatal errors. + Hooks LevelHooks + // All log entries pass through the formatter before logged to Out. The + // included formatters are `TextFormatter` and `JSONFormatter` for which + // TextFormatter is the default. In development (when a TTY is attached) it + // logs with colors, but to a file it wouldn't. You can easily implement your + // own that implements the `Formatter` interface, see the `README` or included + // formatters for examples. + Formatter Formatter + + // Flag for whether to log caller info (off by default) + ReportCaller bool + + // The logging level the logger should log at. This is typically (and defaults + // to) `logrus.Info`, which allows Info(), Warn(), Error() and Fatal() to be + // logged. + Level Level + // Used to sync writing to the log. Locking is enabled by Default + mu MutexWrap + // Reusable empty entry + entryPool sync.Pool + // Function to exit the application, defaults to `os.Exit()` + ExitFunc exitFunc +} + +type exitFunc func(int) + +type MutexWrap struct { + lock sync.Mutex + disabled bool +} + +func (mw *MutexWrap) Lock() { + if !mw.disabled { + mw.lock.Lock() + } +} + +func (mw *MutexWrap) Unlock() { + if !mw.disabled { + mw.lock.Unlock() + } +} + +func (mw *MutexWrap) Disable() { + mw.disabled = true +} + +// Creates a new logger. Configuration should be set by changing `Formatter`, +// `Out` and `Hooks` directly on the default logger instance. You can also just +// instantiate your own: +// +// var log = &Logger{ +// Out: os.Stderr, +// Formatter: new(JSONFormatter), +// Hooks: make(LevelHooks), +// Level: logrus.DebugLevel, +// } +// +// It's recommended to make this a global instance called `log`. +func New() *Logger { + return &Logger{ + Out: os.Stderr, + Formatter: new(TextFormatter), + Hooks: make(LevelHooks), + Level: InfoLevel, + ExitFunc: os.Exit, + ReportCaller: false, + } +} + +func (logger *Logger) newEntry() *Entry { + entry, ok := logger.entryPool.Get().(*Entry) + if ok { + return entry + } + return NewEntry(logger) +} + +func (logger *Logger) releaseEntry(entry *Entry) { + entry.Data = map[string]interface{}{} + logger.entryPool.Put(entry) +} + +// Adds a field to the log entry, note that it doesn't log until you call +// Debug, Print, Info, Warn, Error, Fatal or Panic. It only creates a log entry. +// If you want multiple fields, use `WithFields`. +func (logger *Logger) WithField(key string, value interface{}) *Entry { + entry := logger.newEntry() + defer logger.releaseEntry(entry) + return entry.WithField(key, value) +} + +// Adds a struct of fields to the log entry. All it does is call `WithField` for +// each `Field`. +func (logger *Logger) WithFields(fields Fields) *Entry { + entry := logger.newEntry() + defer logger.releaseEntry(entry) + return entry.WithFields(fields) +} + +// Add an error as single field to the log entry. All it does is call +// `WithError` for the given `error`. +func (logger *Logger) WithError(err error) *Entry { + entry := logger.newEntry() + defer logger.releaseEntry(entry) + return entry.WithError(err) +} + +// Add a context to the log entry. +func (logger *Logger) WithContext(ctx context.Context) *Entry { + entry := logger.newEntry() + defer logger.releaseEntry(entry) + return entry.WithContext(ctx) +} + +// Overrides the time of the log entry. +func (logger *Logger) WithTime(t time.Time) *Entry { + entry := logger.newEntry() + defer logger.releaseEntry(entry) + return entry.WithTime(t) +} + +func (logger *Logger) Logf(level Level, format string, args ...interface{}) { + if logger.IsLevelEnabled(level) { + entry := logger.newEntry() + entry.Logf(level, format, args...) + logger.releaseEntry(entry) + } +} + +func (logger *Logger) Tracef(format string, args ...interface{}) { + logger.Logf(TraceLevel, format, args...) +} + +func (logger *Logger) Debugf(format string, args ...interface{}) { + logger.Logf(DebugLevel, format, args...) +} + +func (logger *Logger) Infof(format string, args ...interface{}) { + logger.Logf(InfoLevel, format, args...) +} + +func (logger *Logger) Printf(format string, args ...interface{}) { + entry := logger.newEntry() + entry.Printf(format, args...) + logger.releaseEntry(entry) +} + +func (logger *Logger) Warnf(format string, args ...interface{}) { + logger.Logf(WarnLevel, format, args...) +} + +func (logger *Logger) Warningf(format string, args ...interface{}) { + logger.Warnf(format, args...) +} + +func (logger *Logger) Errorf(format string, args ...interface{}) { + logger.Logf(ErrorLevel, format, args...) +} + +func (logger *Logger) Fatalf(format string, args ...interface{}) { + logger.Logf(FatalLevel, format, args...) + logger.Exit(1) +} + +func (logger *Logger) Panicf(format string, args ...interface{}) { + logger.Logf(PanicLevel, format, args...) +} + +func (logger *Logger) Log(level Level, args ...interface{}) { + if logger.IsLevelEnabled(level) { + entry := logger.newEntry() + entry.Log(level, args...) + logger.releaseEntry(entry) + } +} + +func (logger *Logger) Trace(args ...interface{}) { + logger.Log(TraceLevel, args...) +} + +func (logger *Logger) Debug(args ...interface{}) { + logger.Log(DebugLevel, args...) +} + +func (logger *Logger) Info(args ...interface{}) { + logger.Log(InfoLevel, args...) +} + +func (logger *Logger) Print(args ...interface{}) { + entry := logger.newEntry() + entry.Print(args...) + logger.releaseEntry(entry) +} + +func (logger *Logger) Warn(args ...interface{}) { + logger.Log(WarnLevel, args...) +} + +func (logger *Logger) Warning(args ...interface{}) { + logger.Warn(args...) +} + +func (logger *Logger) Error(args ...interface{}) { + logger.Log(ErrorLevel, args...) +} + +func (logger *Logger) Fatal(args ...interface{}) { + logger.Log(FatalLevel, args...) + logger.Exit(1) +} + +func (logger *Logger) Panic(args ...interface{}) { + logger.Log(PanicLevel, args...) +} + +func (logger *Logger) Logln(level Level, args ...interface{}) { + if logger.IsLevelEnabled(level) { + entry := logger.newEntry() + entry.Logln(level, args...) + logger.releaseEntry(entry) + } +} + +func (logger *Logger) Traceln(args ...interface{}) { + logger.Logln(TraceLevel, args...) +} + +func (logger *Logger) Debugln(args ...interface{}) { + logger.Logln(DebugLevel, args...) +} + +func (logger *Logger) Infoln(args ...interface{}) { + logger.Logln(InfoLevel, args...) +} + +func (logger *Logger) Println(args ...interface{}) { + entry := logger.newEntry() + entry.Println(args...) + logger.releaseEntry(entry) +} + +func (logger *Logger) Warnln(args ...interface{}) { + logger.Logln(WarnLevel, args...) +} + +func (logger *Logger) Warningln(args ...interface{}) { + logger.Warnln(args...) +} + +func (logger *Logger) Errorln(args ...interface{}) { + logger.Logln(ErrorLevel, args...) +} + +func (logger *Logger) Fatalln(args ...interface{}) { + logger.Logln(FatalLevel, args...) + logger.Exit(1) +} + +func (logger *Logger) Panicln(args ...interface{}) { + logger.Logln(PanicLevel, args...) +} + +func (logger *Logger) Exit(code int) { + runHandlers() + if logger.ExitFunc == nil { + logger.ExitFunc = os.Exit + } + logger.ExitFunc(code) +} + +//When file is opened with appending mode, it's safe to +//write concurrently to a file (within 4k message on Linux). +//In these cases user can choose to disable the lock. +func (logger *Logger) SetNoLock() { + logger.mu.Disable() +} + +func (logger *Logger) level() Level { + return Level(atomic.LoadUint32((*uint32)(&logger.Level))) +} + +// SetLevel sets the logger level. +func (logger *Logger) SetLevel(level Level) { + atomic.StoreUint32((*uint32)(&logger.Level), uint32(level)) +} + +// GetLevel returns the logger level. +func (logger *Logger) GetLevel() Level { + return logger.level() +} + +// AddHook adds a hook to the logger hooks. +func (logger *Logger) AddHook(hook Hook) { + logger.mu.Lock() + defer logger.mu.Unlock() + logger.Hooks.Add(hook) +} + +// IsLevelEnabled checks if the log level of the logger is greater than the level param +func (logger *Logger) IsLevelEnabled(level Level) bool { + return logger.level() >= level +} + +// SetFormatter sets the logger formatter. +func (logger *Logger) SetFormatter(formatter Formatter) { + logger.mu.Lock() + defer logger.mu.Unlock() + logger.Formatter = formatter +} + +// SetOutput sets the logger output. +func (logger *Logger) SetOutput(output io.Writer) { + logger.mu.Lock() + defer logger.mu.Unlock() + logger.Out = output +} + +func (logger *Logger) SetReportCaller(reportCaller bool) { + logger.mu.Lock() + defer logger.mu.Unlock() + logger.ReportCaller = reportCaller +} + +// ReplaceHooks replaces the logger hooks and returns the old ones +func (logger *Logger) ReplaceHooks(hooks LevelHooks) LevelHooks { + logger.mu.Lock() + oldHooks := logger.Hooks + logger.Hooks = hooks + logger.mu.Unlock() + return oldHooks +} diff --git a/logger/logrus/logrus/logger_bench_test.go b/logger/logrus/logrus/logger_bench_test.go new file mode 100644 index 0000000..f0a7684 --- /dev/null +++ b/logger/logrus/logrus/logger_bench_test.go @@ -0,0 +1,85 @@ +package logrus + +import ( + "io/ioutil" + "os" + "testing" +) + +// smallFields is a small size data set for benchmarking +var loggerFields = Fields{ + "foo": "bar", + "baz": "qux", + "one": "two", + "three": "four", +} + +func BenchmarkDummyLogger(b *testing.B) { + nullf, err := os.OpenFile("/dev/null", os.O_WRONLY, 0666) + if err != nil { + b.Fatalf("%v", err) + } + defer nullf.Close() + doLoggerBenchmark(b, nullf, &TextFormatter{DisableColors: true}, smallFields) +} + +func BenchmarkDummyLoggerNoLock(b *testing.B) { + nullf, err := os.OpenFile("/dev/null", os.O_WRONLY|os.O_APPEND, 0666) + if err != nil { + b.Fatalf("%v", err) + } + defer nullf.Close() + doLoggerBenchmarkNoLock(b, nullf, &TextFormatter{DisableColors: true}, smallFields) +} + +func doLoggerBenchmark(b *testing.B, out *os.File, formatter Formatter, fields Fields) { + logger := Logger{ + Out: out, + Level: InfoLevel, + Formatter: formatter, + } + entry := logger.WithFields(fields) + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + entry.Info("aaa") + } + }) +} + +func doLoggerBenchmarkNoLock(b *testing.B, out *os.File, formatter Formatter, fields Fields) { + logger := Logger{ + Out: out, + Level: InfoLevel, + Formatter: formatter, + } + logger.SetNoLock() + entry := logger.WithFields(fields) + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + entry.Info("aaa") + } + }) +} + +func BenchmarkLoggerJSONFormatter(b *testing.B) { + doLoggerBenchmarkWithFormatter(b, &JSONFormatter{}) +} + +func BenchmarkLoggerTextFormatter(b *testing.B) { + doLoggerBenchmarkWithFormatter(b, &TextFormatter{}) +} + +func doLoggerBenchmarkWithFormatter(b *testing.B, f Formatter) { + b.SetParallelism(100) + log := New() + log.Formatter = f + log.Out = ioutil.Discard + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + log. + WithField("foo1", "bar1"). + WithField("foo2", "bar2"). + Info("this is a dummy log") + } + }) +} diff --git a/logger/logrus/logrus/logger_test.go b/logger/logrus/logrus/logger_test.go new file mode 100644 index 0000000..50433e6 --- /dev/null +++ b/logger/logrus/logrus/logger_test.go @@ -0,0 +1,65 @@ +package logrus + +import ( + "bytes" + "encoding/json" + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestFieldValueError(t *testing.T) { + buf := &bytes.Buffer{} + l := &Logger{ + Out: buf, + Formatter: new(JSONFormatter), + Hooks: make(LevelHooks), + Level: DebugLevel, + } + l.WithField("func", func() {}).Info("test") + fmt.Println(buf.String()) + var data map[string]interface{} + json.Unmarshal(buf.Bytes(), &data) + _, ok := data[FieldKeyLogrusError] + require.True(t, ok) +} + +func TestNoFieldValueError(t *testing.T) { + buf := &bytes.Buffer{} + l := &Logger{ + Out: buf, + Formatter: new(JSONFormatter), + Hooks: make(LevelHooks), + Level: DebugLevel, + } + l.WithField("str", "str").Info("test") + fmt.Println(buf.String()) + var data map[string]interface{} + json.Unmarshal(buf.Bytes(), &data) + _, ok := data[FieldKeyLogrusError] + require.False(t, ok) +} + +func TestWarninglnNotEqualToWarning(t *testing.T) { + buf := &bytes.Buffer{} + bufln := &bytes.Buffer{} + + formatter := new(TextFormatter) + formatter.DisableTimestamp = true + formatter.DisableLevelTruncation = true + + l := &Logger{ + Out: buf, + Formatter: formatter, + Hooks: make(LevelHooks), + Level: DebugLevel, + } + l.Warning("hello,", "world") + + l.SetOutput(bufln) + l.Warningln("hello,", "world") + + assert.NotEqual(t, buf.String(), bufln.String(), "Warning() and Wantingln() should not be equal") +} diff --git a/logger/logrus/logrus/logrus.go b/logger/logrus/logrus/logrus.go new file mode 100644 index 0000000..8644761 --- /dev/null +++ b/logger/logrus/logrus/logrus.go @@ -0,0 +1,186 @@ +package logrus + +import ( + "fmt" + "log" + "strings" +) + +// Fields type, used to pass to `WithFields`. +type Fields map[string]interface{} + +// Level type +type Level uint32 + +// Convert the Level to a string. E.g. PanicLevel becomes "panic". +func (level Level) String() string { + if b, err := level.MarshalText(); err == nil { + return string(b) + } else { + return "unknown" + } +} + +// ParseLevel takes a string level and returns the Logrus log level constant. +func ParseLevel(lvl string) (Level, error) { + switch strings.ToLower(lvl) { + case "panic": + return PanicLevel, nil + case "fatal": + return FatalLevel, nil + case "error": + return ErrorLevel, nil + case "warn", "warning": + return WarnLevel, nil + case "info": + return InfoLevel, nil + case "debug": + return DebugLevel, nil + case "trace": + return TraceLevel, nil + } + + var l Level + return l, fmt.Errorf("not a valid logrus Level: %q", lvl) +} + +// UnmarshalText implements encoding.TextUnmarshaler. +func (level *Level) UnmarshalText(text []byte) error { + l, err := ParseLevel(string(text)) + if err != nil { + return err + } + + *level = Level(l) + + return nil +} + +func (level Level) MarshalText() ([]byte, error) { + switch level { + case TraceLevel: + return []byte("trace"), nil + case DebugLevel: + return []byte("debug"), nil + case InfoLevel: + return []byte("info"), nil + case WarnLevel: + return []byte("warning"), nil + case ErrorLevel: + return []byte("error"), nil + case FatalLevel: + return []byte("fatal"), nil + case PanicLevel: + return []byte("panic"), nil + } + + return nil, fmt.Errorf("not a valid logrus level %d", level) +} + +// A constant exposing all logging levels +var AllLevels = []Level{ + PanicLevel, + FatalLevel, + ErrorLevel, + WarnLevel, + InfoLevel, + DebugLevel, + TraceLevel, +} + +// These are the different logging levels. You can set the logging level to log +// on your instance of logger, obtained with `logrus.New()`. +const ( + // PanicLevel level, highest level of severity. Logs and then calls panic with the + // message passed to Debug, Info, ... + PanicLevel Level = iota + // FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the + // logging level is set to Panic. + FatalLevel + // ErrorLevel level. Logs. Used for errors that should definitely be noted. + // Commonly used for hooks to send errors to an error tracking service. + ErrorLevel + // WarnLevel level. Non-critical entries that deserve eyes. + WarnLevel + // InfoLevel level. General operational entries about what's going on inside the + // application. + InfoLevel + // DebugLevel level. Usually only enabled when debugging. Very verbose logging. + DebugLevel + // TraceLevel level. Designates finer-grained informational events than the Debug. + TraceLevel +) + +// Won't compile if StdLogger can't be realized by a log.Logger +var ( + _ StdLogger = &log.Logger{} + _ StdLogger = &Entry{} + _ StdLogger = &Logger{} +) + +// StdLogger is what your logrus-enabled library should take, that way +// it'll accept a stdlib logger and a logrus logger. There's no standard +// interface, this is the closest we get, unfortunately. +type StdLogger interface { + Print(...interface{}) + Printf(string, ...interface{}) + Println(...interface{}) + + Fatal(...interface{}) + Fatalf(string, ...interface{}) + Fatalln(...interface{}) + + Panic(...interface{}) + Panicf(string, ...interface{}) + Panicln(...interface{}) +} + +// The FieldLogger interface generalizes the Entry and Logger types +type FieldLogger interface { + WithField(key string, value interface{}) *Entry + WithFields(fields Fields) *Entry + WithError(err error) *Entry + + Debugf(format string, args ...interface{}) + Infof(format string, args ...interface{}) + Printf(format string, args ...interface{}) + Warnf(format string, args ...interface{}) + Warningf(format string, args ...interface{}) + Errorf(format string, args ...interface{}) + Fatalf(format string, args ...interface{}) + Panicf(format string, args ...interface{}) + + Debug(args ...interface{}) + Info(args ...interface{}) + Print(args ...interface{}) + Warn(args ...interface{}) + Warning(args ...interface{}) + Error(args ...interface{}) + Fatal(args ...interface{}) + Panic(args ...interface{}) + + Debugln(args ...interface{}) + Infoln(args ...interface{}) + Println(args ...interface{}) + Warnln(args ...interface{}) + Warningln(args ...interface{}) + Errorln(args ...interface{}) + Fatalln(args ...interface{}) + Panicln(args ...interface{}) + + // IsDebugEnabled() bool + // IsInfoEnabled() bool + // IsWarnEnabled() bool + // IsErrorEnabled() bool + // IsFatalEnabled() bool + // IsPanicEnabled() bool +} + +// Ext1FieldLogger (the first extension to FieldLogger) is superfluous, it is +// here for consistancy. Do not use. Use Logger or Entry instead. +type Ext1FieldLogger interface { + FieldLogger + Tracef(format string, args ...interface{}) + Trace(args ...interface{}) + Traceln(args ...interface{}) +} diff --git a/logger/logrus/logrus/logrus_test.go b/logger/logrus/logrus/logrus_test.go new file mode 100644 index 0000000..d4d6b3e --- /dev/null +++ b/logger/logrus/logrus/logrus_test.go @@ -0,0 +1,762 @@ +package logrus_test + +import ( + "bytes" + "encoding/json" + "fmt" + "io/ioutil" + "os" + "path/filepath" + "runtime" + "sync" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + . "github.com/stack-labs/stack-rpc-plugins/logger/logrus/logrus" + . "github.com/sirupsen/logrus/internal/testutils" +) + +// TestReportCaller verifies that when ReportCaller is set, the 'func' field +// is added, and when it is unset it is not set or modified +// Verify that functions within the Logrus package aren't considered when +// discovering the caller. +func TestReportCallerWhenConfigured(t *testing.T) { + LogAndAssertJSON(t, func(log *Logger) { + log.ReportCaller = false + log.Print("testNoCaller") + }, func(fields Fields) { + assert.Equal(t, "testNoCaller", fields["msg"]) + assert.Equal(t, "info", fields["level"]) + assert.Equal(t, nil, fields["func"]) + }) + + LogAndAssertJSON(t, func(log *Logger) { + log.ReportCaller = true + log.Print("testWithCaller") + }, func(fields Fields) { + assert.Equal(t, "testWithCaller", fields["msg"]) + assert.Equal(t, "info", fields["level"]) + assert.Equal(t, + "github.com/sirupsen/logrus_test.TestReportCallerWhenConfigured.func3", fields[FieldKeyFunc]) + }) + + LogAndAssertJSON(t, func(log *Logger) { + log.ReportCaller = true + log.Formatter.(*JSONFormatter).CallerPrettyfier = func(f *runtime.Frame) (string, string) { + return "somekindoffunc", "thisisafilename" + } + log.Print("testWithCallerPrettyfier") + }, func(fields Fields) { + assert.Equal(t, "somekindoffunc", fields[FieldKeyFunc]) + assert.Equal(t, "thisisafilename", fields[FieldKeyFile]) + }) + + LogAndAssertText(t, func(log *Logger) { + log.ReportCaller = true + log.Formatter.(*TextFormatter).CallerPrettyfier = func(f *runtime.Frame) (string, string) { + return "somekindoffunc", "thisisafilename" + } + log.Print("testWithCallerPrettyfier") + }, func(fields map[string]string) { + assert.Equal(t, "somekindoffunc", fields[FieldKeyFunc]) + assert.Equal(t, "thisisafilename", fields[FieldKeyFile]) + }) +} + +func logSomething(t *testing.T, message string) Fields { + var buffer bytes.Buffer + var fields Fields + + logger := New() + logger.Out = &buffer + logger.Formatter = new(JSONFormatter) + logger.ReportCaller = true + + entry := logger.WithFields(Fields{ + "foo": "bar", + }) + + entry.Info(message) + + err := json.Unmarshal(buffer.Bytes(), &fields) + assert.Nil(t, err) + + return fields +} + +// TestReportCallerHelperDirect - verify reference when logging from a regular function +func TestReportCallerHelperDirect(t *testing.T) { + fields := logSomething(t, "direct") + + assert.Equal(t, "direct", fields["msg"]) + assert.Equal(t, "info", fields["level"]) + assert.Regexp(t, "github.com/.*/logrus_test.logSomething", fields["func"]) +} + +// TestReportCallerHelperDirect - verify reference when logging from a function called via pointer +func TestReportCallerHelperViaPointer(t *testing.T) { + fptr := logSomething + fields := fptr(t, "via pointer") + + assert.Equal(t, "via pointer", fields["msg"]) + assert.Equal(t, "info", fields["level"]) + assert.Regexp(t, "github.com/.*/logrus_test.logSomething", fields["func"]) +} + +func TestPrint(t *testing.T) { + LogAndAssertJSON(t, func(log *Logger) { + log.Print("test") + }, func(fields Fields) { + assert.Equal(t, "test", fields["msg"]) + assert.Equal(t, "info", fields["level"]) + }) +} + +func TestInfo(t *testing.T) { + LogAndAssertJSON(t, func(log *Logger) { + log.Info("test") + }, func(fields Fields) { + assert.Equal(t, "test", fields["msg"]) + assert.Equal(t, "info", fields["level"]) + }) +} + +func TestWarn(t *testing.T) { + LogAndAssertJSON(t, func(log *Logger) { + log.Warn("test") + }, func(fields Fields) { + assert.Equal(t, "test", fields["msg"]) + assert.Equal(t, "warning", fields["level"]) + }) +} + +func TestLog(t *testing.T) { + LogAndAssertJSON(t, func(log *Logger) { + log.Log(WarnLevel, "test") + }, func(fields Fields) { + assert.Equal(t, "test", fields["msg"]) + assert.Equal(t, "warning", fields["level"]) + }) +} + +func TestInfolnShouldAddSpacesBetweenStrings(t *testing.T) { + LogAndAssertJSON(t, func(log *Logger) { + log.Infoln("test", "test") + }, func(fields Fields) { + assert.Equal(t, "test test", fields["msg"]) + }) +} + +func TestInfolnShouldAddSpacesBetweenStringAndNonstring(t *testing.T) { + LogAndAssertJSON(t, func(log *Logger) { + log.Infoln("test", 10) + }, func(fields Fields) { + assert.Equal(t, "test 10", fields["msg"]) + }) +} + +func TestInfolnShouldAddSpacesBetweenTwoNonStrings(t *testing.T) { + LogAndAssertJSON(t, func(log *Logger) { + log.Infoln(10, 10) + }, func(fields Fields) { + assert.Equal(t, "10 10", fields["msg"]) + }) +} + +func TestInfoShouldAddSpacesBetweenTwoNonStrings(t *testing.T) { + LogAndAssertJSON(t, func(log *Logger) { + log.Infoln(10, 10) + }, func(fields Fields) { + assert.Equal(t, "10 10", fields["msg"]) + }) +} + +func TestInfoShouldNotAddSpacesBetweenStringAndNonstring(t *testing.T) { + LogAndAssertJSON(t, func(log *Logger) { + log.Info("test", 10) + }, func(fields Fields) { + assert.Equal(t, "test10", fields["msg"]) + }) +} + +func TestInfoShouldNotAddSpacesBetweenStrings(t *testing.T) { + LogAndAssertJSON(t, func(log *Logger) { + log.Info("test", "test") + }, func(fields Fields) { + assert.Equal(t, "testtest", fields["msg"]) + }) +} + +func TestWithFieldsShouldAllowAssignments(t *testing.T) { + var buffer bytes.Buffer + var fields Fields + + logger := New() + logger.Out = &buffer + logger.Formatter = new(JSONFormatter) + + localLog := logger.WithFields(Fields{ + "key1": "value1", + }) + + localLog.WithField("key2", "value2").Info("test") + err := json.Unmarshal(buffer.Bytes(), &fields) + assert.Nil(t, err) + + assert.Equal(t, "value2", fields["key2"]) + assert.Equal(t, "value1", fields["key1"]) + + buffer = bytes.Buffer{} + fields = Fields{} + localLog.Info("test") + err = json.Unmarshal(buffer.Bytes(), &fields) + assert.Nil(t, err) + + _, ok := fields["key2"] + assert.Equal(t, false, ok) + assert.Equal(t, "value1", fields["key1"]) +} + +func TestUserSuppliedFieldDoesNotOverwriteDefaults(t *testing.T) { + LogAndAssertJSON(t, func(log *Logger) { + log.WithField("msg", "hello").Info("test") + }, func(fields Fields) { + assert.Equal(t, "test", fields["msg"]) + }) +} + +func TestUserSuppliedMsgFieldHasPrefix(t *testing.T) { + LogAndAssertJSON(t, func(log *Logger) { + log.WithField("msg", "hello").Info("test") + }, func(fields Fields) { + assert.Equal(t, "test", fields["msg"]) + assert.Equal(t, "hello", fields["fields.msg"]) + }) +} + +func TestUserSuppliedTimeFieldHasPrefix(t *testing.T) { + LogAndAssertJSON(t, func(log *Logger) { + log.WithField("time", "hello").Info("test") + }, func(fields Fields) { + assert.Equal(t, "hello", fields["fields.time"]) + }) +} + +func TestUserSuppliedLevelFieldHasPrefix(t *testing.T) { + LogAndAssertJSON(t, func(log *Logger) { + log.WithField("level", 1).Info("test") + }, func(fields Fields) { + assert.Equal(t, "info", fields["level"]) + assert.Equal(t, 1.0, fields["fields.level"]) // JSON has floats only + }) +} + +func TestDefaultFieldsAreNotPrefixed(t *testing.T) { + LogAndAssertText(t, func(log *Logger) { + ll := log.WithField("herp", "derp") + ll.Info("hello") + ll.Info("bye") + }, func(fields map[string]string) { + for _, fieldName := range []string{"fields.level", "fields.time", "fields.msg"} { + if _, ok := fields[fieldName]; ok { + t.Fatalf("should not have prefixed %q: %v", fieldName, fields) + } + } + }) +} + +func TestWithTimeShouldOverrideTime(t *testing.T) { + now := time.Now().Add(24 * time.Hour) + + LogAndAssertJSON(t, func(log *Logger) { + log.WithTime(now).Info("foobar") + }, func(fields Fields) { + assert.Equal(t, fields["time"], now.Format(time.RFC3339)) + }) +} + +func TestWithTimeShouldNotOverrideFields(t *testing.T) { + now := time.Now().Add(24 * time.Hour) + + LogAndAssertJSON(t, func(log *Logger) { + log.WithField("herp", "derp").WithTime(now).Info("blah") + }, func(fields Fields) { + assert.Equal(t, fields["time"], now.Format(time.RFC3339)) + assert.Equal(t, fields["herp"], "derp") + }) +} + +func TestWithFieldShouldNotOverrideTime(t *testing.T) { + now := time.Now().Add(24 * time.Hour) + + LogAndAssertJSON(t, func(log *Logger) { + log.WithTime(now).WithField("herp", "derp").Info("blah") + }, func(fields Fields) { + assert.Equal(t, fields["time"], now.Format(time.RFC3339)) + assert.Equal(t, fields["herp"], "derp") + }) +} + +func TestTimeOverrideMultipleLogs(t *testing.T) { + var buffer bytes.Buffer + var firstFields, secondFields Fields + + logger := New() + logger.Out = &buffer + formatter := new(JSONFormatter) + formatter.TimestampFormat = time.StampMilli + logger.Formatter = formatter + + llog := logger.WithField("herp", "derp") + llog.Info("foo") + + err := json.Unmarshal(buffer.Bytes(), &firstFields) + assert.NoError(t, err, "should have decoded first message") + + buffer.Reset() + + time.Sleep(10 * time.Millisecond) + llog.Info("bar") + + err = json.Unmarshal(buffer.Bytes(), &secondFields) + assert.NoError(t, err, "should have decoded second message") + + assert.NotEqual(t, firstFields["time"], secondFields["time"], "timestamps should not be equal") +} + +func TestDoubleLoggingDoesntPrefixPreviousFields(t *testing.T) { + + var buffer bytes.Buffer + var fields Fields + + logger := New() + logger.Out = &buffer + logger.Formatter = new(JSONFormatter) + + llog := logger.WithField("context", "eating raw fish") + + llog.Info("looks delicious") + + err := json.Unmarshal(buffer.Bytes(), &fields) + assert.NoError(t, err, "should have decoded first message") + assert.Equal(t, len(fields), 4, "should only have msg/time/level/context fields") + assert.Equal(t, fields["msg"], "looks delicious") + assert.Equal(t, fields["context"], "eating raw fish") + + buffer.Reset() + + llog.Warn("omg it is!") + + err = json.Unmarshal(buffer.Bytes(), &fields) + assert.NoError(t, err, "should have decoded second message") + assert.Equal(t, len(fields), 4, "should only have msg/time/level/context fields") + assert.Equal(t, "omg it is!", fields["msg"]) + assert.Equal(t, "eating raw fish", fields["context"]) + assert.Nil(t, fields["fields.msg"], "should not have prefixed previous `msg` entry") + +} + +func TestNestedLoggingReportsCorrectCaller(t *testing.T) { + var buffer bytes.Buffer + var fields Fields + + logger := New() + logger.Out = &buffer + logger.Formatter = new(JSONFormatter) + logger.ReportCaller = true + + llog := logger.WithField("context", "eating raw fish") + + llog.Info("looks delicious") + _, _, line, _ := runtime.Caller(0) + + err := json.Unmarshal(buffer.Bytes(), &fields) + require.NoError(t, err, "should have decoded first message") + assert.Equal(t, 6, len(fields), "should have msg/time/level/func/context fields") + assert.Equal(t, "looks delicious", fields["msg"]) + assert.Equal(t, "eating raw fish", fields["context"]) + assert.Equal(t, + "github.com/sirupsen/logrus_test.TestNestedLoggingReportsCorrectCaller", fields["func"]) + cwd, err := os.Getwd() + require.NoError(t, err) + assert.Equal(t, filepath.ToSlash(fmt.Sprintf("%s/logrus_test.go:%d", cwd, line-1)), filepath.ToSlash(fields["file"].(string))) + + buffer.Reset() + + logger.WithFields(Fields{ + "Clyde": "Stubblefield", + }).WithFields(Fields{ + "Jab'o": "Starks", + }).WithFields(Fields{ + "uri": "https://www.youtube.com/watch?v=V5DTznu-9v0", + }).WithFields(Fields{ + "func": "y drummer", + }).WithFields(Fields{ + "James": "Brown", + }).Print("The hardest workin' man in show business") + _, _, line, _ = runtime.Caller(0) + + err = json.Unmarshal(buffer.Bytes(), &fields) + assert.NoError(t, err, "should have decoded second message") + assert.Equal(t, 11, len(fields), "should have all builtin fields plus foo,bar,baz,...") + assert.Equal(t, "Stubblefield", fields["Clyde"]) + assert.Equal(t, "Starks", fields["Jab'o"]) + assert.Equal(t, "https://www.youtube.com/watch?v=V5DTznu-9v0", fields["uri"]) + assert.Equal(t, "y drummer", fields["fields.func"]) + assert.Equal(t, "Brown", fields["James"]) + assert.Equal(t, "The hardest workin' man in show business", fields["msg"]) + assert.Nil(t, fields["fields.msg"], "should not have prefixed previous `msg` entry") + assert.Equal(t, + "github.com/sirupsen/logrus_test.TestNestedLoggingReportsCorrectCaller", fields["func"]) + require.NoError(t, err) + assert.Equal(t, filepath.ToSlash(fmt.Sprintf("%s/logrus_test.go:%d", cwd, line-1)), filepath.ToSlash(fields["file"].(string))) + + logger.ReportCaller = false // return to default value +} + +func logLoop(iterations int, reportCaller bool) { + var buffer bytes.Buffer + + logger := New() + logger.Out = &buffer + logger.Formatter = new(JSONFormatter) + logger.ReportCaller = reportCaller + + for i := 0; i < iterations; i++ { + logger.Infof("round %d of %d", i, iterations) + } +} + +// Assertions for upper bounds to reporting overhead +func TestCallerReportingOverhead(t *testing.T) { + iterations := 5000 + before := time.Now() + logLoop(iterations, false) + during := time.Now() + logLoop(iterations, true) + after := time.Now() + + elapsedNotReporting := during.Sub(before).Nanoseconds() + elapsedReporting := after.Sub(during).Nanoseconds() + + maxDelta := 1 * time.Second + assert.WithinDuration(t, during, before, maxDelta, + "%d log calls without caller name lookup takes less than %d second(s) (was %d nanoseconds)", + iterations, maxDelta.Seconds(), elapsedNotReporting) + assert.WithinDuration(t, after, during, maxDelta, + "%d log calls without caller name lookup takes less than %d second(s) (was %d nanoseconds)", + iterations, maxDelta.Seconds(), elapsedReporting) +} + +// benchmarks for both with and without caller-function reporting +func BenchmarkWithoutCallerTracing(b *testing.B) { + for i := 0; i < b.N; i++ { + logLoop(1000, false) + } +} + +func BenchmarkWithCallerTracing(b *testing.B) { + for i := 0; i < b.N; i++ { + logLoop(1000, true) + } +} + +func TestConvertLevelToString(t *testing.T) { + assert.Equal(t, "trace", TraceLevel.String()) + assert.Equal(t, "debug", DebugLevel.String()) + assert.Equal(t, "info", InfoLevel.String()) + assert.Equal(t, "warning", WarnLevel.String()) + assert.Equal(t, "error", ErrorLevel.String()) + assert.Equal(t, "fatal", FatalLevel.String()) + assert.Equal(t, "panic", PanicLevel.String()) +} + +func TestParseLevel(t *testing.T) { + l, err := ParseLevel("panic") + assert.Nil(t, err) + assert.Equal(t, PanicLevel, l) + + l, err = ParseLevel("PANIC") + assert.Nil(t, err) + assert.Equal(t, PanicLevel, l) + + l, err = ParseLevel("fatal") + assert.Nil(t, err) + assert.Equal(t, FatalLevel, l) + + l, err = ParseLevel("FATAL") + assert.Nil(t, err) + assert.Equal(t, FatalLevel, l) + + l, err = ParseLevel("error") + assert.Nil(t, err) + assert.Equal(t, ErrorLevel, l) + + l, err = ParseLevel("ERROR") + assert.Nil(t, err) + assert.Equal(t, ErrorLevel, l) + + l, err = ParseLevel("warn") + assert.Nil(t, err) + assert.Equal(t, WarnLevel, l) + + l, err = ParseLevel("WARN") + assert.Nil(t, err) + assert.Equal(t, WarnLevel, l) + + l, err = ParseLevel("warning") + assert.Nil(t, err) + assert.Equal(t, WarnLevel, l) + + l, err = ParseLevel("WARNING") + assert.Nil(t, err) + assert.Equal(t, WarnLevel, l) + + l, err = ParseLevel("info") + assert.Nil(t, err) + assert.Equal(t, InfoLevel, l) + + l, err = ParseLevel("INFO") + assert.Nil(t, err) + assert.Equal(t, InfoLevel, l) + + l, err = ParseLevel("debug") + assert.Nil(t, err) + assert.Equal(t, DebugLevel, l) + + l, err = ParseLevel("DEBUG") + assert.Nil(t, err) + assert.Equal(t, DebugLevel, l) + + l, err = ParseLevel("trace") + assert.Nil(t, err) + assert.Equal(t, TraceLevel, l) + + l, err = ParseLevel("TRACE") + assert.Nil(t, err) + assert.Equal(t, TraceLevel, l) + + l, err = ParseLevel("invalid") + assert.Equal(t, "not a valid logrus Level: \"invalid\"", err.Error()) +} + +func TestLevelString(t *testing.T) { + var loggerlevel Level + loggerlevel = 32000 + + _ = loggerlevel.String() +} + +func TestGetSetLevelRace(t *testing.T) { + wg := sync.WaitGroup{} + for i := 0; i < 100; i++ { + wg.Add(1) + go func(i int) { + defer wg.Done() + if i%2 == 0 { + SetLevel(InfoLevel) + } else { + GetLevel() + } + }(i) + + } + wg.Wait() +} + +func TestLoggingRace(t *testing.T) { + logger := New() + + var wg sync.WaitGroup + wg.Add(100) + + for i := 0; i < 100; i++ { + go func() { + logger.Info("info") + wg.Done() + }() + } + wg.Wait() +} + +func TestLoggingRaceWithHooksOnEntry(t *testing.T) { + logger := New() + hook := new(ModifyHook) + logger.AddHook(hook) + entry := logger.WithField("context", "clue") + + var wg sync.WaitGroup + wg.Add(100) + + for i := 0; i < 100; i++ { + go func() { + entry.Info("info") + wg.Done() + }() + } + wg.Wait() +} + +func TestReplaceHooks(t *testing.T) { + old, cur := &TestHook{}, &TestHook{} + + logger := New() + logger.SetOutput(ioutil.Discard) + logger.AddHook(old) + + hooks := make(LevelHooks) + hooks.Add(cur) + replaced := logger.ReplaceHooks(hooks) + + logger.Info("test") + + assert.Equal(t, old.Fired, false) + assert.Equal(t, cur.Fired, true) + + logger.ReplaceHooks(replaced) + logger.Info("test") + assert.Equal(t, old.Fired, true) +} + +// Compile test +func TestLogrusInterfaces(t *testing.T) { + var buffer bytes.Buffer + // This verifies FieldLogger and Ext1FieldLogger work as designed. + // Please don't use them. Use Logger and Entry directly. + fn := func(xl Ext1FieldLogger) { + var l FieldLogger = xl + b := l.WithField("key", "value") + b.Debug("Test") + } + // test logger + logger := New() + logger.Out = &buffer + fn(logger) + + // test Entry + e := logger.WithField("another", "value") + fn(e) +} + +// Implements io.Writer using channels for synchronization, so we can wait on +// the Entry.Writer goroutine to write in a non-racey way. This does assume that +// there is a single call to Logger.Out for each message. +type channelWriter chan []byte + +func (cw channelWriter) Write(p []byte) (int, error) { + cw <- p + return len(p), nil +} + +func TestEntryWriter(t *testing.T) { + cw := channelWriter(make(chan []byte, 1)) + log := New() + log.Out = cw + log.Formatter = new(JSONFormatter) + log.WithField("foo", "bar").WriterLevel(WarnLevel).Write([]byte("hello\n")) + + bs := <-cw + var fields Fields + err := json.Unmarshal(bs, &fields) + assert.Nil(t, err) + assert.Equal(t, fields["foo"], "bar") + assert.Equal(t, fields["level"], "warning") +} + +func TestLogLevelEnabled(t *testing.T) { + log := New() + log.SetLevel(PanicLevel) + assert.Equal(t, true, log.IsLevelEnabled(PanicLevel)) + assert.Equal(t, false, log.IsLevelEnabled(FatalLevel)) + assert.Equal(t, false, log.IsLevelEnabled(ErrorLevel)) + assert.Equal(t, false, log.IsLevelEnabled(WarnLevel)) + assert.Equal(t, false, log.IsLevelEnabled(InfoLevel)) + assert.Equal(t, false, log.IsLevelEnabled(DebugLevel)) + assert.Equal(t, false, log.IsLevelEnabled(TraceLevel)) + + log.SetLevel(FatalLevel) + assert.Equal(t, true, log.IsLevelEnabled(PanicLevel)) + assert.Equal(t, true, log.IsLevelEnabled(FatalLevel)) + assert.Equal(t, false, log.IsLevelEnabled(ErrorLevel)) + assert.Equal(t, false, log.IsLevelEnabled(WarnLevel)) + assert.Equal(t, false, log.IsLevelEnabled(InfoLevel)) + assert.Equal(t, false, log.IsLevelEnabled(DebugLevel)) + assert.Equal(t, false, log.IsLevelEnabled(TraceLevel)) + + log.SetLevel(ErrorLevel) + assert.Equal(t, true, log.IsLevelEnabled(PanicLevel)) + assert.Equal(t, true, log.IsLevelEnabled(FatalLevel)) + assert.Equal(t, true, log.IsLevelEnabled(ErrorLevel)) + assert.Equal(t, false, log.IsLevelEnabled(WarnLevel)) + assert.Equal(t, false, log.IsLevelEnabled(InfoLevel)) + assert.Equal(t, false, log.IsLevelEnabled(DebugLevel)) + assert.Equal(t, false, log.IsLevelEnabled(TraceLevel)) + + log.SetLevel(WarnLevel) + assert.Equal(t, true, log.IsLevelEnabled(PanicLevel)) + assert.Equal(t, true, log.IsLevelEnabled(FatalLevel)) + assert.Equal(t, true, log.IsLevelEnabled(ErrorLevel)) + assert.Equal(t, true, log.IsLevelEnabled(WarnLevel)) + assert.Equal(t, false, log.IsLevelEnabled(InfoLevel)) + assert.Equal(t, false, log.IsLevelEnabled(DebugLevel)) + assert.Equal(t, false, log.IsLevelEnabled(TraceLevel)) + + log.SetLevel(InfoLevel) + assert.Equal(t, true, log.IsLevelEnabled(PanicLevel)) + assert.Equal(t, true, log.IsLevelEnabled(FatalLevel)) + assert.Equal(t, true, log.IsLevelEnabled(ErrorLevel)) + assert.Equal(t, true, log.IsLevelEnabled(WarnLevel)) + assert.Equal(t, true, log.IsLevelEnabled(InfoLevel)) + assert.Equal(t, false, log.IsLevelEnabled(DebugLevel)) + assert.Equal(t, false, log.IsLevelEnabled(TraceLevel)) + + log.SetLevel(DebugLevel) + assert.Equal(t, true, log.IsLevelEnabled(PanicLevel)) + assert.Equal(t, true, log.IsLevelEnabled(FatalLevel)) + assert.Equal(t, true, log.IsLevelEnabled(ErrorLevel)) + assert.Equal(t, true, log.IsLevelEnabled(WarnLevel)) + assert.Equal(t, true, log.IsLevelEnabled(InfoLevel)) + assert.Equal(t, true, log.IsLevelEnabled(DebugLevel)) + assert.Equal(t, false, log.IsLevelEnabled(TraceLevel)) + + log.SetLevel(TraceLevel) + assert.Equal(t, true, log.IsLevelEnabled(PanicLevel)) + assert.Equal(t, true, log.IsLevelEnabled(FatalLevel)) + assert.Equal(t, true, log.IsLevelEnabled(ErrorLevel)) + assert.Equal(t, true, log.IsLevelEnabled(WarnLevel)) + assert.Equal(t, true, log.IsLevelEnabled(InfoLevel)) + assert.Equal(t, true, log.IsLevelEnabled(DebugLevel)) + assert.Equal(t, true, log.IsLevelEnabled(TraceLevel)) +} + +func TestReportCallerOnTextFormatter(t *testing.T) { + l := New() + + l.Formatter.(*TextFormatter).ForceColors = true + l.Formatter.(*TextFormatter).DisableColors = false + l.WithFields(Fields{"func": "func", "file": "file"}).Info("test") + + l.Formatter.(*TextFormatter).ForceColors = false + l.Formatter.(*TextFormatter).DisableColors = true + l.WithFields(Fields{"func": "func", "file": "file"}).Info("test") +} + +func TestSetReportCallerRace(t *testing.T) { + l := New() + l.Out = ioutil.Discard + l.SetReportCaller(true) + + var wg sync.WaitGroup + wg.Add(100) + + for i := 0; i < 100; i++ { + go func() { + l.Error("Some Error") + wg.Done() + }() + } + wg.Wait() +} diff --git a/logger/logrus/logrus/terminal_check_appengine.go b/logger/logrus/logrus/terminal_check_appengine.go new file mode 100644 index 0000000..2403de9 --- /dev/null +++ b/logger/logrus/logrus/terminal_check_appengine.go @@ -0,0 +1,11 @@ +// +build appengine + +package logrus + +import ( + "io" +) + +func checkIfTerminal(w io.Writer) bool { + return true +} diff --git a/logger/logrus/logrus/terminal_check_bsd.go b/logger/logrus/logrus/terminal_check_bsd.go new file mode 100644 index 0000000..3c4f43f --- /dev/null +++ b/logger/logrus/logrus/terminal_check_bsd.go @@ -0,0 +1,13 @@ +// +build darwin dragonfly freebsd netbsd openbsd + +package logrus + +import "golang.org/x/sys/unix" + +const ioctlReadTermios = unix.TIOCGETA + +func isTerminal(fd int) bool { + _, err := unix.IoctlGetTermios(fd, ioctlReadTermios) + return err == nil +} + diff --git a/logger/logrus/logrus/terminal_check_no_terminal.go b/logger/logrus/logrus/terminal_check_no_terminal.go new file mode 100644 index 0000000..97af92c --- /dev/null +++ b/logger/logrus/logrus/terminal_check_no_terminal.go @@ -0,0 +1,11 @@ +// +build js nacl plan9 + +package logrus + +import ( + "io" +) + +func checkIfTerminal(w io.Writer) bool { + return false +} diff --git a/logger/logrus/logrus/terminal_check_notappengine.go b/logger/logrus/logrus/terminal_check_notappengine.go new file mode 100644 index 0000000..3293fb3 --- /dev/null +++ b/logger/logrus/logrus/terminal_check_notappengine.go @@ -0,0 +1,17 @@ +// +build !appengine,!js,!windows,!nacl,!plan9 + +package logrus + +import ( + "io" + "os" +) + +func checkIfTerminal(w io.Writer) bool { + switch v := w.(type) { + case *os.File: + return isTerminal(int(v.Fd())) + default: + return false + } +} diff --git a/logger/logrus/logrus/terminal_check_solaris.go b/logger/logrus/logrus/terminal_check_solaris.go new file mode 100644 index 0000000..f6710b3 --- /dev/null +++ b/logger/logrus/logrus/terminal_check_solaris.go @@ -0,0 +1,11 @@ +package logrus + +import ( + "golang.org/x/sys/unix" +) + +// IsTerminal returns true if the given file descriptor is a terminal. +func isTerminal(fd int) bool { + _, err := unix.IoctlGetTermio(fd, unix.TCGETA) + return err == nil +} diff --git a/logger/logrus/logrus/terminal_check_unix.go b/logger/logrus/logrus/terminal_check_unix.go new file mode 100644 index 0000000..355dc96 --- /dev/null +++ b/logger/logrus/logrus/terminal_check_unix.go @@ -0,0 +1,13 @@ +// +build linux aix + +package logrus + +import "golang.org/x/sys/unix" + +const ioctlReadTermios = unix.TCGETS + +func isTerminal(fd int) bool { + _, err := unix.IoctlGetTermios(fd, ioctlReadTermios) + return err == nil +} + diff --git a/logger/logrus/logrus/terminal_check_windows.go b/logger/logrus/logrus/terminal_check_windows.go new file mode 100644 index 0000000..572889d --- /dev/null +++ b/logger/logrus/logrus/terminal_check_windows.go @@ -0,0 +1,34 @@ +// +build !appengine,!js,windows + +package logrus + +import ( + "io" + "os" + "syscall" + + sequences "github.com/konsorten/go-windows-terminal-sequences" +) + +func initTerminal(w io.Writer) { + switch v := w.(type) { + case *os.File: + sequences.EnableVirtualTerminalProcessing(syscall.Handle(v.Fd()), true) + } +} + +func checkIfTerminal(w io.Writer) bool { + var ret bool + switch v := w.(type) { + case *os.File: + var mode uint32 + err := syscall.GetConsoleMode(syscall.Handle(v.Fd()), &mode) + ret = (err == nil) + default: + ret = false + } + if ret { + initTerminal(w) + } + return ret +} diff --git a/logger/logrus/logrus/text_formatter.go b/logger/logrus/logrus/text_formatter.go new file mode 100644 index 0000000..39b2ebc --- /dev/null +++ b/logger/logrus/logrus/text_formatter.go @@ -0,0 +1,329 @@ +package logrus + +import ( + "bytes" + "fmt" + "os" + "runtime" + "sort" + "strings" + "sync" + "time" +) + +const ( + red = 31 + yellow = 33 + blue = 36 + gray = 37 +) + +var baseTimestamp time.Time + +func init() { + baseTimestamp = time.Now() +} + +// TextFormatter formats logs into text +type TextFormatter struct { + // Set to true to bypass checking for a TTY before outputting colors. + ForceColors bool + + // Force disabling colors. + DisableColors bool + + // Override coloring based on CLICOLOR and CLICOLOR_FORCE. - https://bixense.com/clicolors/ + EnvironmentOverrideColors bool + + // Disable timestamp logging. useful when output is redirected to logging + // system that already adds timestamps. + DisableTimestamp bool + + // Enable logging the full timestamp when a TTY is attached instead of just + // the time passed since beginning of execution. + FullTimestamp bool + + // TimestampFormat to use for display when a full timestamp is printed + TimestampFormat string + + // The fields are sorted by default for a consistent output. For applications + // that log extremely frequently and don't use the JSON formatter this may not + // be desired. + DisableSorting bool + + // The keys sorting function, when uninitialized it uses sort.Strings. + SortingFunc func([]string) + + // Disables the truncation of the level text to 4 characters. + DisableLevelTruncation bool + + // QuoteEmptyFields will wrap empty fields in quotes if true + QuoteEmptyFields bool + + // WithoutKey prints no key + WithoutKey bool + WithoutQuote bool + + // Whether the logger's out is to a terminal + isTerminal bool + + // FieldMap allows users to customize the names of keys for default fields. + // As an example: + // formatter := &TextFormatter{ + // FieldMap: FieldMap{ + // FieldKeyTime: "@timestamp", + // FieldKeyLevel: "@level", + // FieldKeyMsg: "@message"}} + FieldMap FieldMap + + appendKeyValue func(b *bytes.Buffer, key string, value interface{}) + appendValue func(b *bytes.Buffer, value interface{}) + // CallerPrettyfier can be set by the user to modify the content + // of the function and file keys in the data when ReportCaller is + // activated. If any of the returned value is the empty string the + // corresponding key will be removed from fields. + CallerPrettyfier func(*runtime.Frame) (function string, file string) + + terminalInitOnce sync.Once +} + +func (f *TextFormatter) init(entry *Entry) { + if entry.Logger != nil { + f.isTerminal = checkIfTerminal(entry.Logger.Out) + } + + if f.WithoutKey { + f.appendKeyValue = f.appendKeyValueWithoutKey + } else { + f.appendKeyValue = f.appendKeyValueWithKey + } + + if f.WithoutQuote { + f.appendValue = f.appendValueWithoutQuote + } else { + f.appendValue = f.appendValueWithQuote + } +} + +func (f *TextFormatter) isColored() bool { + isColored := f.ForceColors || (f.isTerminal && (runtime.GOOS != "windows")) + + if f.EnvironmentOverrideColors { + if force, ok := os.LookupEnv("CLICOLOR_FORCE"); ok && force != "0" { + isColored = true + } else if ok && force == "0" { + isColored = false + } else if os.Getenv("CLICOLOR") == "0" { + isColored = false + } + } + + return isColored && !f.DisableColors +} + +// Format renders a single log entry +func (f *TextFormatter) Format(entry *Entry) ([]byte, error) { + data := make(Fields) + for k, v := range entry.Data { + data[k] = v + } + prefixFieldClashes(data, f.FieldMap, entry.HasCaller()) + keys := make([]string, 0, len(data)) + for k := range data { + keys = append(keys, k) + } + + var funcVal, fileVal string + + fixedKeys := make([]string, 0, 4+len(data)) + if !f.DisableTimestamp { + fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyTime)) + } + fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyLevel)) + if entry.Message != "" { + fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyMsg)) + } + if entry.err != "" { + fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyLogrusError)) + } + if entry.HasCaller() { + if f.CallerPrettyfier != nil { + funcVal, fileVal = f.CallerPrettyfier(entry.Caller) + } else { + funcVal = entry.Caller.Function + fileVal = fmt.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line) + } + + if funcVal != "" { + fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyFunc)) + } + if fileVal != "" { + fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyFile)) + } + } + + if !f.DisableSorting { + if f.SortingFunc == nil { + sort.Strings(keys) + fixedKeys = append(fixedKeys, keys...) + } else { + if !f.isColored() { + fixedKeys = append(fixedKeys, keys...) + f.SortingFunc(fixedKeys) + } else { + f.SortingFunc(keys) + } + } + } else { + fixedKeys = append(fixedKeys, keys...) + } + + var b *bytes.Buffer + if entry.Buffer != nil { + b = entry.Buffer + } else { + b = &bytes.Buffer{} + } + + f.terminalInitOnce.Do(func() { f.init(entry) }) + + timestampFormat := f.TimestampFormat + if timestampFormat == "" { + timestampFormat = defaultTimestampFormat + } + if f.isColored() { + f.printColored(b, entry, keys, data, timestampFormat) + } else { + for _, key := range fixedKeys { + var value interface{} + switch { + case key == f.FieldMap.resolve(FieldKeyTime): + value = entry.Time.Format(timestampFormat) + case key == f.FieldMap.resolve(FieldKeyLevel): + value = entry.Level.String() + case key == f.FieldMap.resolve(FieldKeyMsg): + value = entry.Message + case key == f.FieldMap.resolve(FieldKeyLogrusError): + value = entry.err + case key == f.FieldMap.resolve(FieldKeyFunc) && entry.HasCaller(): + value = funcVal + case key == f.FieldMap.resolve(FieldKeyFile) && entry.HasCaller(): + value = fileVal + default: + value = data[key] + } + + f.appendKeyValue(b, key, value) + } + } + + b.WriteByte('\n') + return b.Bytes(), nil +} + +func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []string, data Fields, timestampFormat string) { + var levelColor int + switch entry.Level { + case DebugLevel, TraceLevel: + levelColor = gray + case WarnLevel: + levelColor = yellow + case ErrorLevel, FatalLevel, PanicLevel: + levelColor = red + default: + levelColor = blue + } + + levelText := strings.ToUpper(entry.Level.String()) + if !f.DisableLevelTruncation { + levelText = levelText[0:4] + } + + // Remove a single newline if it already exists in the message to keep + // the behavior of logrus text_formatter the same as the stdlib log package + entry.Message = strings.TrimSuffix(entry.Message, "\n") + + caller := "" + if entry.HasCaller() { + funcVal := fmt.Sprintf("%s()", entry.Caller.Function) + fileVal := fmt.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line) + + if f.CallerPrettyfier != nil { + funcVal, fileVal = f.CallerPrettyfier(entry.Caller) + } + + if fileVal == "" { + caller = funcVal + } else if funcVal == "" { + caller = fileVal + } else { + caller = fileVal + " " + funcVal + } + } + + if f.DisableTimestamp { + fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m%s %-44s ", levelColor, levelText, caller, entry.Message) + } else if !f.FullTimestamp { + fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%04d]%s %-44s ", levelColor, levelText, int(entry.Time.Sub(baseTimestamp)/time.Second), caller, entry.Message) + } else { + fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s]%s %-44s ", levelColor, levelText, entry.Time.Format(timestampFormat), caller, entry.Message) + } + for _, k := range keys { + v := data[k] + fmt.Fprintf(b, " \x1b[%dm%s\x1b[0m=", levelColor, k) + f.appendValue(b, v) + } +} + +func (f *TextFormatter) needsQuoting(text string) bool { + if f.QuoteEmptyFields && len(text) == 0 { + return true + } + for _, ch := range text { + if !((ch >= 'a' && ch <= 'z') || + (ch >= 'A' && ch <= 'Z') || + (ch >= '0' && ch <= '9') || + ch == '-' || ch == '.' || ch == '_' || ch == '/' || ch == '@' || ch == '^' || ch == '+') { + return true + } + } + return false +} + +func (f *TextFormatter) appendKeyValueWithoutKey(b *bytes.Buffer, key string, value interface{}) { + if b.Len() > 0 { + b.WriteByte(' ') + } + f.appendValue(b, value) +} + +func (f *TextFormatter) appendKeyValueWithKey(b *bytes.Buffer, key string, value interface{}) { + if b.Len() > 0 { + b.WriteByte(' ') + } + b.WriteString(key) + b.WriteByte('=') + f.appendValue(b, value) +} + +func (f *TextFormatter) appendValueWithoutQuote(b *bytes.Buffer, value interface{}) { + stringVal, ok := value.(string) + if !ok { + stringVal = fmt.Sprint(value) + } + + b.WriteString(stringVal) +} + +func (f *TextFormatter) appendValueWithQuote(b *bytes.Buffer, value interface{}) { + stringVal, ok := value.(string) + if !ok { + stringVal = fmt.Sprint(value) + } + + if !f.needsQuoting(stringVal) { + b.WriteString(stringVal) + } else { + b.WriteString(fmt.Sprintf("%q", stringVal)) + } +} diff --git a/logger/logrus/logrus/text_formatter_test.go b/logger/logrus/logrus/text_formatter_test.go new file mode 100644 index 0000000..9c5e6f0 --- /dev/null +++ b/logger/logrus/logrus/text_formatter_test.go @@ -0,0 +1,485 @@ +package logrus + +import ( + "bytes" + "errors" + "fmt" + "os" + "runtime" + "sort" + "strings" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestFormatting(t *testing.T) { + tf := &TextFormatter{DisableColors: true} + + testCases := []struct { + value string + expected string + }{ + {`foo`, "time=\"0001-01-01T00:00:00Z\" level=panic test=foo\n"}, + } + + for _, tc := range testCases { + b, _ := tf.Format(WithField("test", tc.value)) + + if string(b) != tc.expected { + t.Errorf("formatting expected for %q (result was %q instead of %q)", tc.value, string(b), tc.expected) + } + } +} + +func TestQuoting(t *testing.T) { + tf := &TextFormatter{DisableColors: true} + + checkQuoting := func(q bool, value interface{}) { + b, _ := tf.Format(WithField("test", value)) + idx := bytes.Index(b, ([]byte)("test=")) + cont := bytes.Contains(b[idx+5:], []byte("\"")) + if cont != q { + if q { + t.Errorf("quoting expected for: %#v", value) + } else { + t.Errorf("quoting not expected for: %#v", value) + } + } + } + + checkQuoting(false, "") + checkQuoting(false, "abcd") + checkQuoting(false, "v1.0") + checkQuoting(false, "1234567890") + checkQuoting(false, "/foobar") + checkQuoting(false, "foo_bar") + checkQuoting(false, "foo@bar") + checkQuoting(false, "foobar^") + checkQuoting(false, "+/-_^@f.oobar") + checkQuoting(true, "foobar$") + checkQuoting(true, "&foobar") + checkQuoting(true, "x y") + checkQuoting(true, "x,y") + checkQuoting(false, errors.New("invalid")) + checkQuoting(true, errors.New("invalid argument")) + + // Test for quoting empty fields. + tf.QuoteEmptyFields = true + checkQuoting(true, "") + checkQuoting(false, "abcd") + checkQuoting(true, errors.New("invalid argument")) +} + +func TestEscaping(t *testing.T) { + tf := &TextFormatter{DisableColors: true} + + testCases := []struct { + value string + expected string + }{ + {`ba"r`, `ba\"r`}, + {`ba'r`, `ba'r`}, + } + + for _, tc := range testCases { + b, _ := tf.Format(WithField("test", tc.value)) + if !bytes.Contains(b, []byte(tc.expected)) { + t.Errorf("escaping expected for %q (result was %q instead of %q)", tc.value, string(b), tc.expected) + } + } +} + +func TestEscaping_Interface(t *testing.T) { + tf := &TextFormatter{DisableColors: true} + + ts := time.Now() + + testCases := []struct { + value interface{} + expected string + }{ + {ts, fmt.Sprintf("\"%s\"", ts.String())}, + {errors.New("error: something went wrong"), "\"error: something went wrong\""}, + } + + for _, tc := range testCases { + b, _ := tf.Format(WithField("test", tc.value)) + if !bytes.Contains(b, []byte(tc.expected)) { + t.Errorf("escaping expected for %q (result was %q instead of %q)", tc.value, string(b), tc.expected) + } + } +} + +func TestTimestampFormat(t *testing.T) { + checkTimeStr := func(format string) { + customFormatter := &TextFormatter{DisableColors: true, TimestampFormat: format} + customStr, _ := customFormatter.Format(WithField("test", "test")) + timeStart := bytes.Index(customStr, ([]byte)("time=")) + timeEnd := bytes.Index(customStr, ([]byte)("level=")) + timeStr := customStr[timeStart+5+len("\"") : timeEnd-1-len("\"")] + if format == "" { + format = time.RFC3339 + } + _, e := time.Parse(format, (string)(timeStr)) + if e != nil { + t.Errorf("time string \"%s\" did not match provided time format \"%s\": %s", timeStr, format, e) + } + } + + checkTimeStr("2006-01-02T15:04:05.000000000Z07:00") + checkTimeStr("Mon Jan _2 15:04:05 2006") + checkTimeStr("") +} + +func TestDisableLevelTruncation(t *testing.T) { + entry := &Entry{ + Time: time.Now(), + Message: "testing", + } + keys := []string{} + timestampFormat := "Mon Jan 2 15:04:05 -0700 MST 2006" + checkDisableTruncation := func(disabled bool, level Level) { + tf := &TextFormatter{DisableLevelTruncation: disabled} + var b bytes.Buffer + entry.Level = level + tf.printColored(&b, entry, keys, nil, timestampFormat) + logLine := (&b).String() + if disabled { + expected := strings.ToUpper(level.String()) + if !strings.Contains(logLine, expected) { + t.Errorf("level string expected to be %s when truncation disabled", expected) + } + } else { + expected := strings.ToUpper(level.String()) + if len(level.String()) > 4 { + if strings.Contains(logLine, expected) { + t.Errorf("level string %s expected to be truncated to %s when truncation is enabled", expected, expected[0:4]) + } + } else { + if !strings.Contains(logLine, expected) { + t.Errorf("level string expected to be %s when truncation is enabled and level string is below truncation threshold", expected) + } + } + } + } + + checkDisableTruncation(true, DebugLevel) + checkDisableTruncation(true, InfoLevel) + checkDisableTruncation(false, ErrorLevel) + checkDisableTruncation(false, InfoLevel) +} + +func TestDisableTimestampWithColoredOutput(t *testing.T) { + tf := &TextFormatter{DisableTimestamp: true, ForceColors: true} + + b, _ := tf.Format(WithField("test", "test")) + if strings.Contains(string(b), "[0000]") { + t.Error("timestamp not expected when DisableTimestamp is true") + } +} + +func TestNewlineBehavior(t *testing.T) { + tf := &TextFormatter{ForceColors: true} + + // Ensure a single new line is removed as per stdlib log + e := NewEntry(StandardLogger()) + e.Message = "test message\n" + b, _ := tf.Format(e) + if bytes.Contains(b, []byte("test message\n")) { + t.Error("first newline at end of Entry.Message resulted in unexpected 2 newlines in output. Expected newline to be removed.") + } + + // Ensure a double new line is reduced to a single new line + e = NewEntry(StandardLogger()) + e.Message = "test message\n\n" + b, _ = tf.Format(e) + if bytes.Contains(b, []byte("test message\n\n")) { + t.Error("Double newline at end of Entry.Message resulted in unexpected 2 newlines in output. Expected single newline") + } + if !bytes.Contains(b, []byte("test message\n")) { + t.Error("Double newline at end of Entry.Message did not result in a single newline after formatting") + } +} + +func TestTextFormatterFieldMap(t *testing.T) { + formatter := &TextFormatter{ + DisableColors: true, + FieldMap: FieldMap{ + FieldKeyMsg: "message", + FieldKeyLevel: "somelevel", + FieldKeyTime: "timeywimey", + }, + } + + entry := &Entry{ + Message: "oh hi", + Level: WarnLevel, + Time: time.Date(1981, time.February, 24, 4, 28, 3, 100, time.UTC), + Data: Fields{ + "field1": "f1", + "message": "messagefield", + "somelevel": "levelfield", + "timeywimey": "timeywimeyfield", + }, + } + + b, err := formatter.Format(entry) + if err != nil { + t.Fatal("Unable to format entry: ", err) + } + + assert.Equal(t, + `timeywimey="1981-02-24T04:28:03Z" `+ + `somelevel=warning `+ + `message="oh hi" `+ + `field1=f1 `+ + `fields.message=messagefield `+ + `fields.somelevel=levelfield `+ + `fields.timeywimey=timeywimeyfield`+"\n", + string(b), + "Formatted output doesn't respect FieldMap") +} + +func TestTextFormatterIsColored(t *testing.T) { + params := []struct { + name string + expectedResult bool + isTerminal bool + disableColor bool + forceColor bool + envColor bool + clicolorIsSet bool + clicolorForceIsSet bool + clicolorVal string + clicolorForceVal string + }{ + // Default values + { + name: "testcase1", + expectedResult: false, + isTerminal: false, + disableColor: false, + forceColor: false, + envColor: false, + clicolorIsSet: false, + clicolorForceIsSet: false, + }, + // Output on terminal + { + name: "testcase2", + expectedResult: true, + isTerminal: true, + disableColor: false, + forceColor: false, + envColor: false, + clicolorIsSet: false, + clicolorForceIsSet: false, + }, + // Output on terminal with color disabled + { + name: "testcase3", + expectedResult: false, + isTerminal: true, + disableColor: true, + forceColor: false, + envColor: false, + clicolorIsSet: false, + clicolorForceIsSet: false, + }, + // Output not on terminal with color disabled + { + name: "testcase4", + expectedResult: false, + isTerminal: false, + disableColor: true, + forceColor: false, + envColor: false, + clicolorIsSet: false, + clicolorForceIsSet: false, + }, + // Output not on terminal with color forced + { + name: "testcase5", + expectedResult: true, + isTerminal: false, + disableColor: false, + forceColor: true, + envColor: false, + clicolorIsSet: false, + clicolorForceIsSet: false, + }, + // Output on terminal with clicolor set to "0" + { + name: "testcase6", + expectedResult: false, + isTerminal: true, + disableColor: false, + forceColor: false, + envColor: true, + clicolorIsSet: true, + clicolorForceIsSet: false, + clicolorVal: "0", + }, + // Output on terminal with clicolor set to "1" + { + name: "testcase7", + expectedResult: true, + isTerminal: true, + disableColor: false, + forceColor: false, + envColor: true, + clicolorIsSet: true, + clicolorForceIsSet: false, + clicolorVal: "1", + }, + // Output not on terminal with clicolor set to "0" + { + name: "testcase8", + expectedResult: false, + isTerminal: false, + disableColor: false, + forceColor: false, + envColor: true, + clicolorIsSet: true, + clicolorForceIsSet: false, + clicolorVal: "0", + }, + // Output not on terminal with clicolor set to "1" + { + name: "testcase9", + expectedResult: false, + isTerminal: false, + disableColor: false, + forceColor: false, + envColor: true, + clicolorIsSet: true, + clicolorForceIsSet: false, + clicolorVal: "1", + }, + // Output not on terminal with clicolor set to "1" and force color + { + name: "testcase10", + expectedResult: true, + isTerminal: false, + disableColor: false, + forceColor: true, + envColor: true, + clicolorIsSet: true, + clicolorForceIsSet: false, + clicolorVal: "1", + }, + // Output not on terminal with clicolor set to "0" and force color + { + name: "testcase11", + expectedResult: false, + isTerminal: false, + disableColor: false, + forceColor: true, + envColor: true, + clicolorIsSet: true, + clicolorForceIsSet: false, + clicolorVal: "0", + }, + // Output not on terminal with clicolor_force set to "1" + { + name: "testcase12", + expectedResult: true, + isTerminal: false, + disableColor: false, + forceColor: false, + envColor: true, + clicolorIsSet: false, + clicolorForceIsSet: true, + clicolorForceVal: "1", + }, + // Output not on terminal with clicolor_force set to "0" + { + name: "testcase13", + expectedResult: false, + isTerminal: false, + disableColor: false, + forceColor: false, + envColor: true, + clicolorIsSet: false, + clicolorForceIsSet: true, + clicolorForceVal: "0", + }, + // Output on terminal with clicolor_force set to "0" + { + name: "testcase14", + expectedResult: false, + isTerminal: true, + disableColor: false, + forceColor: false, + envColor: true, + clicolorIsSet: false, + clicolorForceIsSet: true, + clicolorForceVal: "0", + }, + } + + cleanenv := func() { + os.Unsetenv("CLICOLOR") + os.Unsetenv("CLICOLOR_FORCE") + } + + defer cleanenv() + + for _, val := range params { + t.Run("textformatter_"+val.name, func(subT *testing.T) { + tf := TextFormatter{ + isTerminal: val.isTerminal, + DisableColors: val.disableColor, + ForceColors: val.forceColor, + EnvironmentOverrideColors: val.envColor, + } + cleanenv() + if val.clicolorIsSet { + os.Setenv("CLICOLOR", val.clicolorVal) + } + if val.clicolorForceIsSet { + os.Setenv("CLICOLOR_FORCE", val.clicolorForceVal) + } + res := tf.isColored() + if runtime.GOOS == "windows" && !tf.ForceColors && !val.clicolorForceIsSet { + assert.Equal(subT, false, res) + } else { + assert.Equal(subT, val.expectedResult, res) + } + }) + } +} + +func TestCustomSorting(t *testing.T) { + formatter := &TextFormatter{ + DisableColors: true, + SortingFunc: func(keys []string) { + sort.Slice(keys, func(i, j int) bool { + if keys[j] == "prefix" { + return false + } + if keys[i] == "prefix" { + return true + } + return strings.Compare(keys[i], keys[j]) == -1 + }) + }, + } + + entry := &Entry{ + Message: "Testing custom sort function", + Time: time.Now(), + Level: InfoLevel, + Data: Fields{ + "test": "testvalue", + "prefix": "the application prefix", + "blablabla": "blablabla", + }, + } + b, err := formatter.Format(entry) + require.NoError(t, err) + require.True(t, strings.HasPrefix(string(b), "prefix="), "format output is %q", string(b)) +} diff --git a/logger/logrus/logrus/travis/cross_build.sh b/logger/logrus/logrus/travis/cross_build.sh new file mode 100644 index 0000000..545d8c3 --- /dev/null +++ b/logger/logrus/logrus/travis/cross_build.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +if [[ "$TRAVIS_GO_VERSION" =~ ^1.\12\. ]] && [[ "$TRAVIS_OS_NAME" == "linux" ]]; then + /tmp/gox/gox -build-lib -all +fi diff --git a/logger/logrus/logrus/travis/install.sh b/logger/logrus/logrus/travis/install.sh new file mode 100644 index 0000000..07f4532 --- /dev/null +++ b/logger/logrus/logrus/travis/install.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +set -e + +if [[ "$TRAVIS_GO_VERSION" =~ ^1.\12\. ]] && [[ "$TRAVIS_OS_NAME" == "linux" ]]; then + git clone https://github.com/dgsb/gox.git /tmp/gox + pushd /tmp/gox + git checkout new_master + go build ./ + popd +fi diff --git a/logger/logrus/logrus/writer.go b/logger/logrus/logrus/writer.go new file mode 100644 index 0000000..9e1f751 --- /dev/null +++ b/logger/logrus/logrus/writer.go @@ -0,0 +1,64 @@ +package logrus + +import ( + "bufio" + "io" + "runtime" +) + +func (logger *Logger) Writer() *io.PipeWriter { + return logger.WriterLevel(InfoLevel) +} + +func (logger *Logger) WriterLevel(level Level) *io.PipeWriter { + return NewEntry(logger).WriterLevel(level) +} + +func (entry *Entry) Writer() *io.PipeWriter { + return entry.WriterLevel(InfoLevel) +} + +func (entry *Entry) WriterLevel(level Level) *io.PipeWriter { + reader, writer := io.Pipe() + + var printFunc func(args ...interface{}) + + switch level { + case TraceLevel: + printFunc = entry.Trace + case DebugLevel: + printFunc = entry.Debug + case InfoLevel: + printFunc = entry.Info + case WarnLevel: + printFunc = entry.Warn + case ErrorLevel: + printFunc = entry.Error + case FatalLevel: + printFunc = entry.Fatal + case PanicLevel: + printFunc = entry.Panic + default: + printFunc = entry.Print + } + + go entry.writerScanner(reader, printFunc) + runtime.SetFinalizer(writer, writerFinalizer) + + return writer +} + +func (entry *Entry) writerScanner(reader *io.PipeReader, printFunc func(args ...interface{})) { + scanner := bufio.NewScanner(reader) + for scanner.Scan() { + printFunc(scanner.Text()) + } + if err := scanner.Err(); err != nil { + entry.Errorf("Error while reading from Writer: %s", err) + } + reader.Close() +} + +func writerFinalizer(writer *io.PipeWriter) { + writer.Close() +} diff --git a/logger/logrus/options.go b/logger/logrus/options.go index 7ab8b3f..f3d9b9c 100644 --- a/logger/logrus/options.go +++ b/logger/logrus/options.go @@ -1,7 +1,7 @@ package logrus import ( - "github.com/sirupsen/logrus" + "github.com/stack-labs/stack-rpc-plugins/logger/logrus/logrus" "github.com/stack-labs/stack-rpc/logger" ) @@ -9,8 +9,11 @@ type Options struct { logger.Options Formatter logrus.Formatter // Flag for whether to log caller info (off by default) - ReportCaller bool - SplitLevel bool + ReportCaller bool + SplitLevel bool + WithoutKey bool + WithoutQuote bool + TimestampFormat string // Exit Function to call when FatalLevel log ExitFunc func(int) } @@ -19,6 +22,9 @@ type formatterKey struct{} type splitLevelKey struct{} type reportCallerKey struct{} type exitKey struct{} +type withoutKeyKey struct{} +type withoutQuoteKey struct{} +type timestampFormat struct{} func TextFormatter(formatter *logrus.TextFormatter) logger.Option { return logger.SetOption(formatterKey{}, formatter) @@ -36,8 +42,20 @@ func SplitLevel(s bool) logger.Option { return logger.SetOption(splitLevelKey{}, s) } +func WithoutKey(w bool) logger.Option { + return logger.SetOption(withoutKeyKey{}, w) +} + +func WithoutQuote(w bool) logger.Option { + return logger.SetOption(withoutQuoteKey{}, w) +} + +func TimestampFormat(format string) logger.Option { + return logger.SetOption(timestampFormat{}, format) +} + // warning to use this option. because logrus doest not open CallerDepth option // this will only print this package -func ReportCaller() logger.Option { - return logger.SetOption(reportCallerKey{}, true) +func ReportCaller(r bool) logger.Option { + return logger.SetOption(reportCallerKey{}, r) } diff --git a/logger/logrus/plugin.go b/logger/logrus/plugin.go new file mode 100644 index 0000000..1eef259 --- /dev/null +++ b/logger/logrus/plugin.go @@ -0,0 +1,62 @@ +package logrus + +import ( + "github.com/stack-labs/stack-rpc-plugins/logger/logrus/logrus" + "github.com/stack-labs/stack-rpc/config" + "github.com/stack-labs/stack-rpc/logger" + "github.com/stack-labs/stack-rpc/plugin" + scfg "github.com/stack-labs/stack-rpc/service/config" +) + +var options struct { + Stack struct { + Logger struct { + scfg.Logger + Logrus struct { + SplitLevel bool `sc:"split-level"` + ReportCaller bool `sc:"report-caller"` + Formatter string `sc:"formatter"` + WithoutKey bool `sc:"without-key"` + WithoutQuote bool `sc:"without-quote"` + TimestampFormat string `sc:"timestamp-format"` + } `sc:"slogrus"` + } `sc:"logger"` + } `sc:"stack"` +} + +type logrusLogPlugin struct{} + +func (l *logrusLogPlugin) Name() string { + return "slogrus" +} + +func (l *logrusLogPlugin) Options() []logger.Option { + var opts []logger.Option + lc := options.Stack.Logger.Logrus + opts = append(opts, SplitLevel(lc.SplitLevel)) + opts = append(opts, ReportCaller(lc.ReportCaller)) + opts = append(opts, WithoutKey(lc.WithoutKey)) + opts = append(opts, WithoutQuote(lc.WithoutQuote)) + + if len(lc.TimestampFormat) > 0 { + opts = append(opts, TimestampFormat(lc.TimestampFormat)) + } + + switch lc.Formatter { + case "text": + opts = append(opts, TextFormatter(new(logrus.TextFormatter))) + case "json": + opts = append(opts, JSONFormatter(new(logrus.JSONFormatter))) + } + + return opts +} + +func (l *logrusLogPlugin) New(opts ...logger.Option) logger.Logger { + return NewLogger(opts...) +} + +func init() { + config.RegisterOptions(&options) + plugin.LoggerPlugins["slogrus"] = &logrusLogPlugin{} +} diff --git a/registry/consul/consul.go b/registry/consul/consul.go index 96f6a55..5aa5ea5 100755 --- a/registry/consul/consul.go +++ b/registry/consul/consul.go @@ -13,6 +13,7 @@ import ( consul "github.com/hashicorp/consul/api" hash "github.com/mitchellh/hashstructure" + "github.com/stack-labs/stack-rpc/plugin" "github.com/stack-labs/stack-rpc/registry" mnet "github.com/stack-labs/stack-rpc/util/net" ) @@ -35,6 +36,10 @@ type consulRegistry struct { lastChecked map[string]time.Time } +func init() { + plugin.RegistryPlugins["consul"] = &consulRegistryPlugin{} +} + func getDeregisterTTL(t time.Duration) time.Duration { // splay slightly for the watcher? splay := time.Second * 5 diff --git a/registry/consul/go.mod b/registry/consul/go.mod index 3a9fdb4..5c5a724 100755 --- a/registry/consul/go.mod +++ b/registry/consul/go.mod @@ -2,6 +2,10 @@ module github.com/stack-labs/stack-rpc-plugins/registry/consul go 1.14 +replace ( + github.com/stack-labs/stack-rpc v1.0.0 => ../../../stack-rpc +) + require ( github.com/hashicorp/consul/api v1.3.0 github.com/stack-labs/stack-rpc v1.0.0 diff --git a/registry/consul/plugin.go b/registry/consul/plugin.go new file mode 100644 index 0000000..60c08d4 --- /dev/null +++ b/registry/consul/plugin.go @@ -0,0 +1,17 @@ +package consul + +import "github.com/stack-labs/stack-rpc/registry" + +type consulRegistryPlugin struct{} + +func (c *consulRegistryPlugin) Name() string { + return "consul" +} + +func (c *consulRegistryPlugin) Options() []registry.Option { + return nil +} + +func (c *consulRegistryPlugin) New(opts ...registry.Option) registry.Registry { + return NewRegistry(opts...) +} diff --git a/registry/consul/watcher.go b/registry/consul/watcher.go index 961a3ea..980f956 100755 --- a/registry/consul/watcher.go +++ b/registry/consul/watcher.go @@ -9,6 +9,7 @@ import ( "github.com/hashicorp/consul/api" "github.com/hashicorp/consul/api/watch" "github.com/stack-labs/stack-rpc/registry" + rUtil "github.com/stack-labs/stack-rpc/util/registry" ) type consulWatcher struct { @@ -163,7 +164,7 @@ func (cw *consulWatcher) serviceHandler(idx uint64, data interface{}) { // it's an update rather than creation if len(nodes) > 0 { - delService := registry.CopyService(oldService) + delService := rUtil.CopyService(oldService) delService.Nodes = nodes cw.next <- ®istry.Result{Action: "delete", Service: delService} } diff --git a/registry/zookeeper/go.mod b/registry/zookeeper/go.mod index cb1d277..e37ef22 100644 --- a/registry/zookeeper/go.mod +++ b/registry/zookeeper/go.mod @@ -2,6 +2,10 @@ module github.com/stack-labs/stack-rpc-plugins/registry/zookeeper go 1.14 +replace ( + github.com/stack-labs/stack-rpc v1.0.0 => ../../../stack-rpc +) + require ( github.com/go-zookeeper/zk v1.0.2 github.com/google/uuid v1.1.2 diff --git a/registry/zookeeper/plugin.go b/registry/zookeeper/plugin.go new file mode 100644 index 0000000..f7b76e0 --- /dev/null +++ b/registry/zookeeper/plugin.go @@ -0,0 +1,25 @@ +package zookeeper + +import ( + "github.com/stack-labs/stack-rpc/plugin" + "github.com/stack-labs/stack-rpc/registry" +) + +type zkRegistryPlugin struct { +} + +func (z *zkRegistryPlugin) Name() string { + return "zookeeper" +} + +func (z *zkRegistryPlugin) Options() []registry.Option { + return nil +} + +func (z *zkRegistryPlugin) New(opts ...registry.Option) registry.Registry { + return NewRegistry(opts...) +} + +func init() { + plugin.RegistryPlugins["zookeeper"] = &zkRegistryPlugin{} +} diff --git a/registry/zookeeper/util.go b/registry/zookeeper/util.go index b8c0fac..a333c38 100644 --- a/registry/zookeeper/util.go +++ b/registry/zookeeper/util.go @@ -7,7 +7,6 @@ import ( "time" "github.com/go-zookeeper/zk" - log "github.com/stack-labs/stack-rpc/logger" "github.com/stack-labs/stack-rpc/registry" ) @@ -25,7 +24,6 @@ func nodePath(domain, name, id string) string { service := strings.Replace(name, "/", "-", -1) node := strings.Replace(id, "/", "-", -1) p := path.Join(prefixWithDomain(domain), service, node) - log.Infof("register path: %s", p) return p } diff --git a/service/stackway/README.md b/service/stackway/README.md index 42b6064..3386f4e 100644 --- a/service/stackway/README.md +++ b/service/stackway/README.md @@ -1,5 +1,5 @@ # Stackway ```shell script -$ go run main.go --config=stack_config.yml +$ go run main.go --config=stack.yml ``` diff --git a/service/stackway/api/api.go b/service/stackway/api/api.go index 8aa7464..1fd5f45 100644 --- a/service/stackway/api/api.go +++ b/service/stackway/api/api.go @@ -3,10 +3,11 @@ package api import ( "github.com/stack-labs/stack-rpc" "github.com/stack-labs/stack-rpc/pkg/cli" + "github.com/stack-labs/stack-rpc/service" ) // api stackway options -func Options() (options []stack.Option) { +func Options() (options []service.Option) { flags := []cli.Flag{ cli.StringFlag{ Name: "stackway_name", diff --git a/service/stackway/api/http.go b/service/stackway/api/http.go index b958ad0..dbac85d 100644 --- a/service/stackway/api/http.go +++ b/service/stackway/api/http.go @@ -6,6 +6,10 @@ import ( "github.com/gorilla/mux" "github.com/stack-labs/stack-rpc" + "github.com/stack-labs/stack-rpc-plugins/service/stackway/handler" + "github.com/stack-labs/stack-rpc-plugins/service/stackway/helper" + "github.com/stack-labs/stack-rpc-plugins/service/stackway/plugin" + gwServer "github.com/stack-labs/stack-rpc-plugins/service/stackway/server" ahandler "github.com/stack-labs/stack-rpc/api/handler" aapi "github.com/stack-labs/stack-rpc/api/handler/api" "github.com/stack-labs/stack-rpc/api/handler/event" @@ -23,12 +27,8 @@ import ( "github.com/stack-labs/stack-rpc/api/server/acme" "github.com/stack-labs/stack-rpc/api/server/acme/autocert" httpapi "github.com/stack-labs/stack-rpc/api/server/http" + "github.com/stack-labs/stack-rpc/service" "github.com/stack-labs/stack-rpc/util/log" - - "github.com/stack-labs/stack-rpc-plugins/service/stackway/handler" - "github.com/stack-labs/stack-rpc-plugins/service/stackway/helper" - "github.com/stack-labs/stack-rpc-plugins/service/stackway/plugin" - gwServer "github.com/stack-labs/stack-rpc-plugins/service/stackway/server" ) type config struct { @@ -87,11 +87,11 @@ func newDefaultConfig() *config { } type httpServer struct { - svc stack.Service + svc service.Service api apiServer.Server } -func (s *httpServer) Options() []stack.Option { +func (s *httpServer) Options() []service.Option { opts := Options() opts = append( opts, @@ -307,6 +307,6 @@ func (s *httpServer) Stop() error { return s.api.Stop() } -func NewServer(svc stack.Service) *httpServer { +func NewServer(svc service.Service) *httpServer { return &httpServer{svc: svc} } diff --git a/service/stackway/go.mod b/service/stackway/go.mod index 5789eef..717a9be 100644 --- a/service/stackway/go.mod +++ b/service/stackway/go.mod @@ -3,14 +3,13 @@ module github.com/stack-labs/stack-rpc-plugins/service/stackway go 1.14 replace ( - github.com/coreos/bbolt => go.etcd.io/bbolt v1.3.4 - google.golang.org/grpc => google.golang.org/grpc v1.26.0 + github.com/stack-labs/stack-rpc v1.0.1 => ../../../stack-rpc ) require ( github.com/golang/protobuf v1.4.3 github.com/google/uuid v1.1.1 github.com/gorilla/mux v1.7.4 - github.com/stack-labs/stack-rpc v1.0.0 + github.com/stack-labs/stack-rpc v1.0.1 github.com/stretchr/testify v1.4.0 ) diff --git a/service/stackway/go.sum b/service/stackway/go.sum index 2aafbd0..45ff765 100644 --- a/service/stackway/go.sum +++ b/service/stackway/go.sum @@ -1,8 +1,18 @@ bazil.org/fuse v0.0.0-20160811212531-371fbbdaa898/go.mod h1:Xbm+BRKSBEpa4q4hTSxohYNQpsxXPbPry4JJWOB3LB8= +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.31.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.37.0/go.mod h1:TS1dMSSfndXH133OKGwekG838Om/cQT0BUHV3HcBgoo= cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU= +cloud.google.com/go v0.44.1/go.mod h1:iSa0KzasP4Uvy3f1mN/7PiObzGgflwredwwASm/v6AU= +cloud.google.com/go v0.44.2/go.mod h1:60680Gw3Yr4ikxnPRS/oxxkBccT6SA1yMk63TGekxKY= +cloud.google.com/go v0.45.1/go.mod h1:RpBamKRgapWJb87xiFSdk4g1CME7QZg3uwTez+TSTjc= +cloud.google.com/go v0.46.3/go.mod h1:a6bKKbmY7er1mI7TEI4lsAkts/mkhTSZK8w33B4RAg0= +cloud.google.com/go v0.50.0/go.mod h1:r9sluTvynVuxRIOHXQEHMFffphuXHOMZMycpNR5e6To= +cloud.google.com/go/bigquery v1.0.1/go.mod h1:i/xbL2UlR5RvWAURpBYZTtm/cXjCha9lbfbpx4poX+o= +cloud.google.com/go/datastore v1.0.0/go.mod h1:LXYbyblFSglQ5pkeyhO+Qmw7ukd3C+pD7TKLgZqpHYE= +cloud.google.com/go/pubsub v1.0.1/go.mod h1:R0Gpsv3s54REJCy4fxDixWD93lHJMoZTyQ2kNxGRt3I= +cloud.google.com/go/storage v1.0.0/go.mod h1:IhtSnM/ZTZV8YYJWCY8RULGVqBDmpoyjwiyrjsg+URw= contrib.go.opencensus.io/exporter/ocagent v0.4.12/go.mod h1:450APlNTSR6FrvC3CTRqYosuDstRB9un7SOx2k/9ckA= dmitri.shuralyov.com/app/changes v0.0.0-20180602232624-0a106ad413e3/go.mod h1:Yl+fi1br7+Rr3LqpNJf1/uxUdtRUV+Tnj0o93V2B9MU= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= @@ -58,11 +68,14 @@ github.com/bradfitz/go-smtpd v0.0.0-20170404230938-deb6d6237625/go.mod h1:HYsPBT github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23/go.mod h1:bbYlZJ7hK1yFx9hf58LP0zeX7UjIGs20ufpu3evjr+s= github.com/bwmarrin/discordgo v0.20.1/go.mod h1:O9S4p+ofTFwB02em7jkpkV8M3R0/PUVOwN61zSZ0r4Q= github.com/cenkalti/backoff/v3 v3.0.0/go.mod h1:cIeZDE3IrqwwJl6VUwCN6trj1oXrTS4rc0ij+ULvLYs= +github.com/cenkalti/backoff/v4 v4.0.0/go.mod h1:eEew/i+1Q6OrCDZh3WiXYv3+nJwBASZ8Bog/87DQnVg= github.com/census-instrumentation/opencensus-proto v0.2.0/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cheekybits/genny v1.0.0 h1:uGGa4nei+j20rOSeDeP5Of12XVm7TGUd4dJA9RDitfE= github.com/cheekybits/genny v1.0.0/go.mod h1:+tQajlRqAUrPI7DOSpB0XAqZYtQakVtB7wXkRAgjxjQ= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cloudflare/cloudflare-go v0.10.2/go.mod h1:qhVI5MKwBGhdNU89ZRz2plgYutcJ5PCekLxXn56w6SY= +github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/containerd/cgroups v0.0.0-20190919134610-bf292b21730f/go.mod h1:OApqhQ4XNSNC13gXIwDjhOQxjWa/NxkwZXJ1EvqT0ko= github.com/containerd/console v0.0.0-20180822173158-c12b1e7919c1/go.mod h1:Tj/on1eG8kiEhd0+fhSDzsPAFESxzBBvdyEgyryXffw= github.com/containerd/containerd v1.3.0-beta.2.0.20190828155532-0293cbd26c69/go.mod h1:bC6axHOhabU15QhwfG7w5PipXdVtMXFTttgp+kVtyUA= @@ -85,6 +98,7 @@ github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfc github.com/cpu/goacmedns v0.0.1/go.mod h1:sesf/pNnCYwUevQEQfEwY0Y3DydlQWSGZbaMElOWxok= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -104,7 +118,9 @@ github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5m github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU= github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I= github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o= +github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/exoscale/egoscale v0.18.1/go.mod h1:Z7OOdzzTOz1Q1PjQXumlz9Wn/CddH0zSYdCF3rnBKXE= github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= @@ -120,8 +136,11 @@ github.com/gliderlabs/ssh v0.1.1/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aev github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0= github.com/go-acme/lego/v3 v3.1.0 h1:yanYFoYW8azFkCvJfIk7edWWfjkYkhDxe45ZsxoW4Xk= github.com/go-acme/lego/v3 v3.1.0/go.mod h1:074uqt+JS6plx+c9Xaiz6+L+GBb+7itGtzfcDM2AhEE= +github.com/go-acme/lego/v3 v3.4.0 h1:deB9NkelA+TfjGHVw8J7iKl/rMtffcGMWSMmptvMv0A= +github.com/go-acme/lego/v3 v3.4.0/go.mod h1:xYbLDuxq3Hy4bMUT1t9JIuz6GWIWb3m5X+TeTHYaT7M= github.com/go-cmd/cmd v1.0.5/go.mod h1:y8q8qlK5wQibcw63djSl/ntiHUHXHGdCkPk0j4QeW4s= github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q= +github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-ini/ini v1.44.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= @@ -146,8 +165,10 @@ github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfU github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/groupcache v0.0.0-20191027212112-611e8accdfc9 h1:uHTyIjqVhYRhLbJ8nIiOJHkEZZ+5YoOsAbD3sk82NiE= github.com/golang/groupcache v0.0.0-20191027212112-611e8accdfc9/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:tluoj9z5200jBnyusfRPU2LqT6J+DAorxEvtC7LHB+E= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y= github.com/golang/mock v1.4.0/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc= github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4= @@ -167,6 +188,7 @@ github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c h1:964Od4U6p2jUkFxvCydnIczKteheJEzHRToSGK3Bnlw= github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= +github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= @@ -178,6 +200,7 @@ github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= +github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= @@ -232,6 +255,8 @@ github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCV github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/json-iterator/go v1.1.8 h1:QiWkFLKq0T7mpzwOTu6BzNDbfTE8OLrYhVKYMLF46Ok= github.com/json-iterator/go v1.1.8/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= +github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= @@ -240,6 +265,7 @@ github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvW github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek= +github.com/klauspost/cpuid v1.2.3/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek= github.com/kolo/xmlrpc v0.0.0-20190717152603-07c4ee3fd181/go.mod h1:o03bZfuBwAXHetKXuInt4S7omeXUu62/A845kiycsSQ= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s= @@ -252,6 +278,7 @@ github.com/kr/pty v1.1.3/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/labbsr0x/bindman-dns-webhook v1.0.2/go.mod h1:p6b+VCXIR8NYKpDr8/dg1HKfQoRHCdcsROXKvmoehKA= github.com/labbsr0x/goh v1.0.1/go.mod h1:8K2UhVoaWXcCU7Lxoa2omWnC8gyW8px7/lmO61c027w= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= @@ -279,6 +306,8 @@ github.com/microcosm-cc/bluemonday v1.0.1/go.mod h1:hsXNsILzKxV+sX77C5b8FSuKF00v github.com/miekg/dns v1.1.15/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= github.com/miekg/dns v1.1.22 h1:Jm64b3bO9kP43ddLjL2EY3Io6bmy1qGb9Xxz6TqS6rc= github.com/miekg/dns v1.1.22/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso= +github.com/miekg/dns v1.1.27 h1:aEH/kqUzUxGJ/UHcEKdJY+ugH6WEzsEBBSPa8zuy1aM= +github.com/miekg/dns v1.1.27/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/go-vnc v0.0.0-20150629162542-723ed9867aed/go.mod h1:3rdaFaCv4AyBgu5ALFM0+tSuHrBh6v692nyQe3ikrq0= github.com/mitchellh/hashstructure v1.0.0 h1:ZkRJX1CyOoTkar7p/mLS5TZU4nJ1Rn/F8u9dGS02Q3Y= @@ -308,8 +337,10 @@ github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OS github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms= github.com/neelance/astrewrite v0.0.0-20160511093645-99348263ae86/go.mod h1:kHJEU3ofeGjhHklVoIGuVj85JJwZ6kWPaJwCIxgnFmo= github.com/neelance/sourcemap v0.0.0-20151028013722-8c68805598ab/go.mod h1:Qr6/a/Q4r9LP1IltGz7tA7iOK1WonHEYhu1HRBA7ZiM= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/nlopes/slack v0.6.0/go.mod h1:JzQ9m3PMAqcpeCam7UaHSuBuupz7CmpjehYMayT6YOk= github.com/nrdcg/auroradns v1.0.0/go.mod h1:6JPXKzIRzZzMqtTDgueIhTi6rFf1QvYE/HzqidhOhjw= +github.com/nrdcg/dnspod-go v0.4.0/go.mod h1:vZSoFSFeQVm2gWLMkyX61LZ8HI3BaqtHZWgPTGKr6KQ= github.com/nrdcg/goinwx v0.6.1/go.mod h1:XPiut7enlbEdntAqalBIqcYcTEVhpv/dKWgDCX2SwKQ= github.com/nrdcg/namesilo v0.2.1/go.mod h1:lwMvfQTyYq+BbjJd30ylEG4GPSS6PII0Tia4rRpRiyw= github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78= @@ -346,6 +377,8 @@ github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pkg/errors v0.8.1-0.20171018195549-f15c970de5b7/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v0.8.0/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= @@ -482,6 +515,7 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20190313024323-a1f597ede03a/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190418165655-df01cb2cc480/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190923035154-9ee001bba392/go.mod h1:/lpIB1dKB+9EgE3H3cr1v9wB50oz8l4C4h62xy7jSTY= @@ -494,17 +528,26 @@ golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 h1:pLI5jrR7OSLijeIDcmRxNm golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= +golang.org/x/exp v0.0.0-20190829153037-c13cbed26979/go.mod h1:86+5VVa7VpoJ4kLfm080zCjGlMRFzhUhsZKEZO7MGek= +golang.org/x/exp v0.0.0-20191129062945-2f5052295587/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= golang.org/x/exp v0.0.0-20200331195152-e8c3332aa8e5/go.mod h1:4M0jN8W1tt0AVLNr8HDosyJCDCDuyL9N9+3m7wDWgKw= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= +golang.org/x/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20190909230951-414d861bb4ac/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f/go.mod h1:5qLYkcX4OjUUV8bRuDixDT3tpyyb+LUpUlRWLxfhWrs= +golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE= golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o= golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= +golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY= golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b h1:GgiSbuUyC0BlbUmHQBgFqu32eiRR/CEYdjOjOd4zE6Y= golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= @@ -524,11 +567,13 @@ golang.org/x/net v0.0.0-20190313220215-9f648a60d977/go.mod h1:t9HGtf8HONx5eT2rtn golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190724013045-ca1201d0de80/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190930134127-c5a3c61f89f3/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20191027093000-83d349e8ac1a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200421231249-e086a090c8fd/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/net v0.0.0-20200707034311-ab3426394381 h1:VXak5I6aEWmAXeQjA+QSZzlgNrpq9mjcfDemuexIKsU= @@ -547,6 +592,7 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180622082034-63fc586f45fe/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181029174526-d69651ed3497/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -564,6 +610,8 @@ golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190514135907-3a4b5fb9f71f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190801041406-cbf593c0f2f3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -591,22 +639,33 @@ golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxb golang.org/x/time v0.0.0-20190921001708-c4c64cad1fd0 h1:xQwXv67TxFo9nC1GJFyab5eq/5B590r6RlnL/G8Sz7w= golang.org/x/time v0.0.0-20190921001708-c4c64cad1fd0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20180828015842-6cd1fcedba52/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20181030000716-a0a13e073c7b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +golang.org/x/tools v0.0.0-20190628153133-6cdbf07be9d0/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= golang.org/x/tools v0.0.0-20190729092621-ff9f1409240a/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= +golang.org/x/tools v0.0.0-20190816200558-6889da9d5479/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20190907020128-2ca718005c18/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20190911174233-4f2ddba30aff/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191012152004-8de300cfc20a/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5 h1:hKsoRgsbwY1NafxrwTs+k64bikrLBkAgPir1TNCj3Zs= golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191125144606-a911d9008d1f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/tools v0.0.0-20191216173652-a0e659d51361/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.0.0-20200207183749-b753a1ba74fa h1:5E4dL8+NgFOgjwbTKz+OOEGGhP+ectTmF842l6KjupQ= golang.org/x/tools v0.0.0-20200207183749-b753a1ba74fa/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= @@ -618,12 +677,16 @@ google.golang.org/api v0.0.0-20181030000543-1d582fd0359e/go.mod h1:4mhQ8q/RsB7i+ google.golang.org/api v0.1.0/go.mod h1:UGEZY7KEX120AnNLIHFMKIo4obdJhkp2tPbaPlQx13Y= google.golang.org/api v0.3.1/go.mod h1:6wY9I6uQWHQ8EM57III9mq/AjF+i8G65rmVagqKMtkk= google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE= +google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M= google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg= +google.golang.org/api v0.9.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg= +google.golang.org/api v0.14.0/go.mod h1:iLdEw5Ide6rF15KTC1Kkl0iskquN2gFfn9o9XIsbkAI= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.2.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.3.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/appengine v1.6.1/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= google.golang.org/genproto v0.0.0-20180831171423-11092d34479b/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= google.golang.org/genproto v0.0.0-20181029155118-b69ba1387ce2/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= @@ -633,12 +696,27 @@ google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRn google.golang.org/genproto v0.0.0-20190418145605-e7d98fc518a7/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= google.golang.org/genproto v0.0.0-20190502173448-54afdca5d873/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= +google.golang.org/genproto v0.0.0-20190801165951-fa694d86fc64/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/genproto v0.0.0-20190911173649-1774047e7e51/go.mod h1:IbNlFCBrqXvoKpeg0TB2l7cyZUmoaFKYIwrEpbDKLA8= +google.golang.org/genproto v0.0.0-20191216164720-4f79533eabd1/go.mod h1:n3cpQtvxv34hfy77yVDNjmbRyujviMdxYliBSkLhpCc= google.golang.org/genproto v0.0.0-20200423170343-7949de9c1215/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 h1:+kGHl1aib/qcwaRi1CbqBZ1rk19r85MNUf8HaBghugY= google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= +google.golang.org/grpc v1.14.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw= +google.golang.org/grpc v1.16.0/go.mod h1:0JHn/cJsOMiMfNA9+DeHDlAU7KAAB5GDlYFpa9MZMio= +google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.19.1/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= +google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= +google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= google.golang.org/grpc v1.26.0 h1:2dTRdpdFEEhJYQD8EMLB61nnrzSCTbG38PhqdhvOltg= google.golang.org/grpc v1.26.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +google.golang.org/grpc v1.29.1 h1:EC2SB8S04d2r73uptxphDSUG+kTKVgjRPF+N3xpxRB4= +google.golang.org/grpc v1.29.1/go.mod h1:itym6AZVZYACWQqET3MqgPpjcuV5QH3BxFS3IjizoKk= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= @@ -654,6 +732,7 @@ gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLks gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2/go.mod h1:Xk6kEKp8OKb+X14hQBKWaSkCsqBpgog8nAV2xsGOxlo= @@ -683,6 +762,7 @@ gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw= grpc.go4.org v0.0.0-20170609214715-11d0a25b4919/go.mod h1:77eQGdRu53HpSqPFJFmuJdjuHRquDANNeA4x7B8WQ9o= +honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= @@ -690,6 +770,7 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh honnef.co/go/tools v0.0.1-2019.2.3 h1:3JgtbtFHMiCmsznwGVTUWbgGov+pVqnlf1dEJTNAXeM= honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= k8s.io/kubernetes v1.13.0/go.mod h1:ocZa8+6APFNC2tX1DZASIbocyYT5jHzqFVsY5aoB7Jk= +rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA= sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q= diff --git a/service/stackway/handler/meta.go b/service/stackway/handler/meta.go index 3221d34..946a77e 100644 --- a/service/stackway/handler/meta.go +++ b/service/stackway/handler/meta.go @@ -3,12 +3,11 @@ package handler import ( "net/http" - "github.com/stack-labs/stack-rpc" "github.com/stack-labs/stack-rpc/api/handler" "github.com/stack-labs/stack-rpc/api/handler/event" "github.com/stack-labs/stack-rpc/api/router" + "github.com/stack-labs/stack-rpc/service" "github.com/stack-labs/stack-rpc/util/errors" - // TODO: only import handler package aapi "github.com/stack-labs/stack-rpc/api/handler/api" ahttp "github.com/stack-labs/stack-rpc/api/handler/http" @@ -17,7 +16,7 @@ import ( ) type metaHandler struct { - s stack.Service + s service.Service r router.Router } @@ -59,7 +58,7 @@ func (m *metaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } // Meta is a http.Handler that routes based on endpoint metadata -func Meta(s stack.Service, r router.Router) http.Handler { +func Meta(s service.Service, r router.Router) http.Handler { return &metaHandler{ s: s, r: r, diff --git a/service/stackway/handler/rpc.go b/service/stackway/handler/rpc.go index 9f75be9..65accf4 100644 --- a/service/stackway/handler/rpc.go +++ b/service/stackway/handler/rpc.go @@ -7,11 +7,10 @@ import ( "strings" "time" - "github.com/stack-labs/stack-rpc" + "github.com/stack-labs/stack-rpc-plugins/service/stackway/helper" "github.com/stack-labs/stack-rpc/client" + "github.com/stack-labs/stack-rpc/service" "github.com/stack-labs/stack-rpc/util/errors" - - "github.com/stack-labs/stack-rpc-plugins/service/stackway/helper" ) type rpcRequest struct { @@ -23,10 +22,10 @@ type rpcRequest struct { } type rpcHandler struct { - opts stack.Options + opts service.Options } -func NewRPCHandlerFunc(opts stack.Options) http.HandlerFunc { +func NewRPCHandlerFunc(opts service.Options) http.HandlerFunc { rpc := rpcHandler{opts: opts} return rpc.Handler } diff --git a/service/stackway/hook/hook.go b/service/stackway/hook/hook.go index e084db4..cbc99e8 100644 --- a/service/stackway/hook/hook.go +++ b/service/stackway/hook/hook.go @@ -2,13 +2,13 @@ package hook import ( "github.com/stack-labs/stack-rpc" - "github.com/stack-labs/stack-rpc/util/log" - "github.com/stack-labs/stack-rpc-plugins/service/stackway/api" "github.com/stack-labs/stack-rpc-plugins/service/stackway/plugin" + "github.com/stack-labs/stack-rpc/service" + "github.com/stack-labs/stack-rpc/util/log" ) -func Hook(svc stack.Service) { +func Hook(svc service.Service) { apiServer := api.NewServer(svc) // stackway options diff --git a/service/stackway/stack.yml b/service/stackway/stack.yml new file mode 100644 index 0000000..90b9dd2 --- /dev/null +++ b/service/stackway/stack.yml @@ -0,0 +1,24 @@ +stack: + server: + name: stack.rpc.stackway + version: 1.0.0 + address: :8080 # stackway run as server address + registry: + name: mdns + stackway: + handler: meta + resolver: stack + rpc_path: /rpc + api_path: / + proxy_path: /{service:[a-zA-Z0-9]+} + namespace: stack.rpc.api + header_prefix: X-Stack- + enable_rpc: true + enable_acme: false + enable_tls: false + acme: + provider: autocert + challenge_provider: cloudflare + ca: https://acme-v02.api.letsencrypt.org/directory + hosts: + - "" diff --git a/service/stackway/stack_config.yml b/service/stackway/stack_config.yml deleted file mode 100644 index 834d630..0000000 --- a/service/stackway/stack_config.yml +++ /dev/null @@ -1,26 +0,0 @@ -stack: - server: - name: "stack.rpc.stackway" - version: "1.0.0" - address: :8080 # stackway run as server address - - registry: - name: mdns - - stackway: - handler: "meta" - resolver: "stack" - rpc_path: "/rpc" - api_path: "/" - proxy_path: "/{service:[a-zA-Z0-9]+}" - namespace: "stack.rpc.api" - header_prefix: "X-Stack-" - enable_rpc: true - enable_acme: false - enable_tls: false - acme: - provider: "autocert" - challenge_provider: "cloudflare" - ca: "https://acme-v02.api.letsencrypt.org/directory" - hosts: - - "" diff --git a/service/stackweb/backend/Makefile b/service/stackweb/backend/Makefile new file mode 100644 index 0000000..7caf623 --- /dev/null +++ b/service/stackweb/backend/Makefile @@ -0,0 +1,14 @@ + +GOPATH:=$(shell go env GOPATH) + +.PHONY: build +build: + go build -o platform-web main.go module.go + +.PHONY: test +test: + go test -v ./... -cover + +.PHONY: docker +docker: + docker build . -t user-srv:latest diff --git a/service/stackweb/backend/README.md b/service/stackweb/backend/README.md new file mode 100644 index 0000000..a8915b3 --- /dev/null +++ b/service/stackweb/backend/README.md @@ -0,0 +1,16 @@ +# platform-web backend + +## Overview + +srv is the backend code directory of platform-web. + +## Content + +```text +├── README.md +├── cmd # platform-web command line interface +├── conf # config files dir, if want +├── logs # default log files dir +├── main.go # entrance +├── plugins.go # plugins +``` diff --git a/service/stackweb/backend/go.mod b/service/stackweb/backend/go.mod new file mode 100644 index 0000000..6e6f764 --- /dev/null +++ b/service/stackweb/backend/go.mod @@ -0,0 +1,13 @@ +module github.com/stack-labs/stack-rpc-plugins/service/stackweb + +go 1.14 + +replace ( + github.com/stack-labs/stack-rpc v1.0.1 => ../../../../stack-rpc + github.com/stack-labs/stack-rpc-plugins/logger/logrus v1.0.0 => ../../../../stack-rpc-plugins/logger/logrus +) + +require ( + github.com/stack-labs/stack-rpc v1.0.1 + github.com/stack-labs/stack-rpc-plugins/logger/logrus v1.0.0 +) diff --git a/service/stackweb/backend/internal/config/config.go b/service/stackweb/backend/internal/config/config.go new file mode 100644 index 0000000..5f6bdfb --- /dev/null +++ b/service/stackweb/backend/internal/config/config.go @@ -0,0 +1,11 @@ +package config + +import ( + "github.com/micro/go-config/reader" + "github.com/micro/go-micro/config" +) + +func GetConfig(names ...string) reader.Value { + names2 := append([]string{"micro", "platform_web"}, names...) + return config.Get(names2...) +} diff --git a/service/stackweb/backend/internal/db/db.go b/service/stackweb/backend/internal/db/db.go new file mode 100644 index 0000000..84dddda --- /dev/null +++ b/service/stackweb/backend/internal/db/db.go @@ -0,0 +1,69 @@ +package db + +import ( + "database/sql" + "sync" + + "github.com/micro/cli" +) + +var ( + pgConfig *PGConfig + pgDB *sql.DB + m sync.RWMutex + once sync.Once +) + +type PGConfig struct { + DBName string `json:"dbName"` // The name of the database to connect to + User string `json:"user"` // the user to sign in as + Password string `json:"password"` // The user's password + Host string `json:"host"` // The host to connect to.Values that start with / are for unix domain sockets. (default is localhost) + Port int `json:"port"` // The port to bind to.(default is 5432) + SSLMode string `json:"sslMode"` // Whether or not to use SSL (default is require, this is not the default for libpq) + ConnectTimeout int `json:"connectTimeout"` // Maximum wait for connection, in seconds.Zero or not specified means wait indefinitely. + SSLCert string `json:"sslCert"` // Cert file location. The file must contain PEM encoded data. + SSLKey string `json:"sslKey"` // Key file location.The file must contain PEM encoded data. + SSLRootCert string `json:"sslRootCert"` // The location of the root certificate file.The file must contain PEM encoded data.) + MaxOpenConnection int `json:"maxOpenConnection"` // use the default 0 + MaxIdleConnection int `json:"maxIdleConnection"` // use the default 0 +} + +func Init(ctx *cli.Context) { + m.Lock() + defer m.Unlock() + + pgConfig = newPGConfig() + + if pgConfigFile := ctx.String("pg_config_file"); len(pgConfigFile) > 0 { + if err := config.Get(pgConfigFile).Scan(&pgConfig); err != nil { + panic(err) + } + } + + initDB() +} + +func newPGConfig() *PGConfig { + return &PGConfig{ + DBName: "postgres", + User: "postgres", + Password: "password", + Host: "localhost", + ConnectTimeout: 10, + Port: 5432, + SSLMode: "disable", + } +} + +// initDB 初始化数据库 +func initDB() { + once.Do(func() { + initPG() + }) +} + +// GetPG get postgre db +func GetPG() *sql.DB { + return pgDB +} diff --git a/service/stackweb/backend/internal/db/pg.go b/service/stackweb/backend/internal/db/pg.go new file mode 100644 index 0000000..f09537a --- /dev/null +++ b/service/stackweb/backend/internal/db/pg.go @@ -0,0 +1,54 @@ +package db + +import ( + "database/sql" + "fmt" +) + +func initPG() { + log.Logf("[initPG] init postgreSQL") + + var err error + + pgDB, err = sql.Open("postgres", parseConnectStr()) + if err != nil { + log.Fatal(err) + panic(err) + } + + pgDB.SetMaxOpenConns(pgConfig.MaxOpenConnection) + pgDB.SetMaxIdleConns(pgConfig.MaxIdleConnection) + + if err = pgDB.Ping(); err != nil { + log.Fatal(err) + } + + log.Logf("[initPG] pg connected") +} + +func parseConnectStr() string { + str := fmt.Sprintf("postgres://%s:%s@%s:%d/%s?", pgConfig.User, pgConfig.Password, pgConfig.Host, pgConfig.Port, pgConfig.DBName) + + log.Logf("[initPG] pg connected %s", str) + + str = fmt.Sprintf("%ssslmode=%s", str, pgConfig.SSLMode) + + if pgConfig.SSLMode != "disable" { + + if pgConfig.SSLCert != "" { + str += "&sslcert=" + pgConfig.SSLCert + } + + if pgConfig.SSLKey != "" { + str += "&sslkey=" + pgConfig.SSLKey + } + + if pgConfig.SSLRootCert != "" { + str += "&sslrootcert=" + pgConfig.SSLRootCert + } + } else { + // do something + } + + return str +} diff --git a/service/stackweb/backend/internal/proxy/proxy.go b/service/stackweb/backend/internal/proxy/proxy.go new file mode 100644 index 0000000..646b77a --- /dev/null +++ b/service/stackweb/backend/internal/proxy/proxy.go @@ -0,0 +1,106 @@ +package proxy + +import ( + "io" + "net" + "net/http" + "net/http/httputil" + "regexp" + "strings" +) + +var ( + re = regexp.MustCompile("^[a-zA-Z0-9]+([a-zA-Z0-9-]*[a-zA-Z0-9]*)?$") + // Base path sent to web service. + // This is stripped from the request path + // Allows the web service to define absolute paths + BasePathHeader = "X-Micro-Web-Base-Path" +) + +type Proxy struct { + Default *httputil.ReverseProxy + Director func(r *http.Request) +} + +func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { + if !isWebSocket(r) { + // the usual path + p.Default.ServeHTTP(w, r) + return + } + + // the websocket path + req := new(http.Request) + *req = *r + p.Director(req) + host := req.URL.Host + + 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 +} diff --git a/service/stackweb/backend/internal/tools/number.go b/service/stackweb/backend/internal/tools/number.go new file mode 100644 index 0000000..bebd8d0 --- /dev/null +++ b/service/stackweb/backend/internal/tools/number.go @@ -0,0 +1,10 @@ +package tools + +func Int32ArrayTo64(in []int32) []int64 { + ret := make([]int64, len(in)) + for _, v := range in { + ret = append(ret, int64(v)) + } + + return ret +} diff --git a/service/stackweb/backend/internal/tools/sort.go b/service/stackweb/backend/internal/tools/sort.go new file mode 100644 index 0000000..d724423 --- /dev/null +++ b/service/stackweb/backend/internal/tools/sort.go @@ -0,0 +1,21 @@ +package tools + +import ( + "github.com/micro/go-micro/registry" +) + +type SortedServices struct { + Services []*registry.Service +} + +func (s SortedServices) Len() int { + return len(s.Services) +} + +func (s SortedServices) Less(i, j int) bool { + return s.Services[i].Name < s.Services[j].Name +} + +func (s SortedServices) Swap(i, j int) { + s.Services[i], s.Services[j] = s.Services[j], s.Services[i] +} diff --git a/service/stackweb/backend/internal/tools/time.go b/service/stackweb/backend/internal/tools/time.go new file mode 100644 index 0000000..906c5b4 --- /dev/null +++ b/service/stackweb/backend/internal/tools/time.go @@ -0,0 +1,31 @@ +package tools + +import ( + "github.com/golang/protobuf/ptypes" + "github.com/golang/protobuf/ptypes/timestamp" + "github.com/micro/go-log" + "time" +) + +func TimeFixRange(start, end string, startFixed, endFixed time.Duration) (s, e time.Time) { + s, err := time.Parse(time.RFC3339, start) + if err != nil { + s = time.Now() + log.Log(err) + s = s.Add(startFixed) + } + + e, err = time.Parse(time.RFC3339, end) + if err != nil { + e = time.Now() + log.Log(err) + e = e.Add(endFixed) + } + + return +} + +func PTimestamp(ts *timestamp.Timestamp) (t time.Time) { + t, _ = ptypes.Timestamp(ts) + return t +} diff --git a/service/stackweb/backend/main.go b/service/stackweb/backend/main.go new file mode 100644 index 0000000..fd4c33d --- /dev/null +++ b/service/stackweb/backend/main.go @@ -0,0 +1,78 @@ +package main + +import ( + "fmt" + + "github.com/stack-labs/stack-rpc" + _ "github.com/stack-labs/stack-rpc-plugins/logger/logrus" + "github.com/stack-labs/stack-rpc-plugins/service/stackweb/plugins" + _ "github.com/stack-labs/stack-rpc-plugins/service/stackweb/plugins/basic" + cfg "github.com/stack-labs/stack-rpc/config" + "github.com/stack-labs/stack-rpc/service" + "github.com/stack-labs/stack-rpc/service/web" +) + +type config struct { + Stack struct { + Stackweb struct { + Name string `sc:"name"` + Address string `sc:"address"` + ApiPath string `sc:"api-path"` + RootPath string `sc:"root-path"` + StaticDir string `sc:"static-dir"` + FaviconIco string `sc:"favicon-ico"` + } `sc:"stackweb"` + } `sc:"stack"` +} + +var ( + stackwebConfig config +) + +func init() { + cfg.RegisterOptions(&stackwebConfig) +} + +func main() { + s := stack.NewWebService( + stack.Name("stack.rpc.stackweb"), + stack.Address(":8090"), + stack.WebHandleFuncs(handlers()...), + ) + if err := s.Init(stack.AfterStart(func() error { + return loadPlugins(s.Options()) + })); err != nil { + panic(err) + } + + if err := s.Run(); err != nil { + panic(err) + } +} + +func handlers() []web.HandlerFunc { + handlers := make([]web.HandlerFunc, 0) + for _, m := range plugins.Plugins() { + for k, h := range m.Handlers() { + if h.IsFunc() { + handlers = append(handlers, web.HandlerFunc{ + Route: k, + Func: h.Func, + }) + } + } + } + + return handlers +} + +func loadPlugins(s service.Options) (err error) { + for _, m := range plugins.Plugins() { + err := m.Init(plugins.Service(s)) + if err != nil { + return fmt.Errorf("plugin [%s] init err: %s", m.Name(), err) + } + } + + return nil +} diff --git a/service/stackweb/backend/plugins/basic/api.go b/service/stackweb/backend/plugins/basic/api.go new file mode 100644 index 0000000..08dcd34 --- /dev/null +++ b/service/stackweb/backend/plugins/basic/api.go @@ -0,0 +1,263 @@ +package basic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "sort" + "strconv" + "strings" + + "github.com/stack-labs/stack-rpc-plugins/service/stackweb/plugins/basic/tools" + "github.com/stack-labs/stack-rpc/client/selector" + "github.com/stack-labs/stack-rpc/pkg/metadata" + "github.com/stack-labs/stack-rpc/registry" + "github.com/stack-labs/stack-rpc/service" +) + +type api struct { + service service.Options +} + +type rpcRequest struct { + Service string + Endpoint string + Method string + Address string + URL string + timeout int + Request interface{} +} + +// serviceAPIDetail is the service api detail +type serviceAPIDetail struct { + Name string `json:"name,omitempty"` + Endpoints []*registry.Endpoint `json:"endpoints,omitempty"` +} + +func (a *api) webServices(w http.ResponseWriter, r *http.Request) { + services, err := a.service.Registry.ListServices() + if err != nil { + http.Error(w, "Error occurred:"+err.Error(), 500) + return + } + + webServices := make([]*registry.Service, 0) + for _, s := range services { + for _, webN := range WebNamespacePrefix { + if strings.Index(s.Name, webN) == 0 && len(strings.TrimPrefix(s.Name, webN)) > 0 { + s.Name = strings.Replace(s.Name, webN+".", "", 1) + webServices = append(webServices, s) + } + } + } + + sort.Sort(tools.SortedServices{Services: services}) + + tools.WriteJsonData(w, webServices) + + return +} + +func (a *api) services(w http.ResponseWriter, r *http.Request) { + services, err := a.service.Registry.ListServices() + if err != nil { + http.Error(w, "Error occurred:"+err.Error(), 500) + return + } + + for _, service := range services { + ss, err := a.service.Registry.GetService(service.Name) + if err != nil { + continue + } + if len(ss) == 0 { + continue + } + + for _, s := range ss { + service.Nodes = append(service.Nodes, s.Nodes...) + service.Endpoints = s.Endpoints + } + + } + + sort.Sort(tools.SortedServices{Services: services}) + + tools.WriteJsonData(w, services) + return +} + +func (a *api) microServices(w http.ResponseWriter, r *http.Request) { + services, err := a.service.Registry.ListServices() + if err != nil { + http.Error(w, "Error occurred:"+err.Error(), 500) + return + } + + ret := make([]*registry.Service, 0) + + for _, srv := range services { + temp, err := a.service.Registry.GetService(srv.Name) + if err != nil { + http.Error(w, "Error occurred:"+err.Error(), 500) + return + } + + for _, s := range temp { + for _, n := range s.Nodes { + if n.Metadata["registry"] != "" { + ret = append(ret, s) + break + } + } + } + } + + sort.Sort(tools.SortedServices{Services: ret}) + + tools.WriteJsonData(w, ret) + return +} + +func (a *api) serviceDetails(w http.ResponseWriter, r *http.Request) { + services, err := a.service.Registry.ListServices() + if err != nil { + http.Error(w, "Error occurred:"+err.Error(), 500) + return + } + + sort.Sort(tools.SortedServices{Services: services}) + + serviceDetails := make([]*serviceAPIDetail, 0) + for _, service := range services { + s, err := a.service.Registry.GetService(service.Name) + if err != nil { + continue + } + if len(s) == 0 { + continue + } + + serviceDetails = append(serviceDetails, &serviceAPIDetail{ + Name: service.Name, + Endpoints: s[0].Endpoints, + }) + } + + tools.WriteJsonData(w, serviceDetails) + return +} + +func (a *api) handler(w http.ResponseWriter, r *http.Request) { + serviceName := r.URL.Query().Get("service") + + if len(serviceName) > 0 { + s, err := a.service.Registry.GetService(serviceName) + if err != nil { + http.Error(w, "Error occurred:"+err.Error(), 500) + return + } + + if len(s) == 0 { + tools.WriteError(w, fmt.Errorf("Service Is Not found %s: ", serviceName)) + return + } + + tools.WriteJsonData(w, s) + return + } + + return +} + +func (a *api) apiGatewayServices(w http.ResponseWriter, r *http.Request) { + services, err := a.service.Registry.ListServices() + if err != nil { + http.Error(w, "Error occurred:"+err.Error(), 500) + return + } + + ret := make([]*registry.Service, 0) + for _, service := range services { + _, _ = a.service.Selector.Next(service.Name, func(options *selector.SelectOptions) { + filter := func(services []*registry.Service) []*registry.Service { + for _, s := range services { + for _, gwN := range GatewayNamespaces { + if s.Name == gwN { + ret = append(ret, s) + break + } + } + } + return ret + } + + options.Filters = append(options.Filters, filter) + }) + } + + tools.WriteJsonData(w, ret) + return +} + +func (a *api) rpc(w http.ResponseWriter, r *http.Request) { + defer r.Body.Close() + rpcReq := &rpcRequest{} + + d := json.NewDecoder(r.Body) + d.UseNumber() + + if err := d.Decode(&rpcReq); err != nil { + tools.WriteError(w, fmt.Errorf("rpc decode err %s: ", err)) + return + } + + if len(rpcReq.Endpoint) == 0 { + rpcReq.Endpoint = rpcReq.Method + } + + rpcReq.timeout, _ = strconv.Atoi(r.Header.Get("Timeout")) + rpcReq.URL = r.URL.Path + + a.rpcCall(w, requestToContext(r), rpcReq) +} + +func (a *api) health(w http.ResponseWriter, r *http.Request) { + defer r.Body.Close() + + rpcReq := &rpcRequest{ + Service: r.URL.Query().Get("service"), + Endpoint: "Debug.Health", + Request: "{}", + URL: r.URL.Path, + Address: r.URL.Query().Get("address"), + } + + a.rpcCall(w, requestToContext(r), rpcReq) +} + +func (a *api) stats(w http.ResponseWriter, r *http.Request) { + defer r.Body.Close() + + rpcReq := &rpcRequest{ + Service: r.URL.Query().Get("service"), + Endpoint: "Debug.Stats", + Request: "{}", + URL: r.URL.Path, + Address: r.URL.Query().Get("address"), + } + + a.rpcCall(w, requestToContext(r), rpcReq) + return +} + +func requestToContext(r *http.Request) context.Context { + ctx := context.Background() + md := make(metadata.Metadata) + for k, v := range r.Header { + md[k] = strings.Join(v, ",") + } + return metadata.NewContext(ctx, md) +} diff --git a/service/stackweb/backend/plugins/basic/basic.go b/service/stackweb/backend/plugins/basic/basic.go new file mode 100644 index 0000000..3857acc --- /dev/null +++ b/service/stackweb/backend/plugins/basic/basic.go @@ -0,0 +1,105 @@ +package basic + +import ( + "sync" + + "github.com/stack-labs/stack-rpc-plugins/service/stackweb/plugins" +) + +func init() { + m = &basicModule{ + name: "basic", + path: "/b", + } + + plugins.Register(m) +} + +var ( + m *basicModule + + // Default address to bind to + GatewayNamespaces = []string{"stack.stackway.api"} + WebNamespacePrefix = []string{"stack.web"} +) + +// basicModule includes web, registry, CLI, Stats submodules. +type basicModule struct { + name string + path string + sync.RWMutex + api *api +} + +func (m *basicModule) Name() string { + return m.name +} + +func (m *basicModule) Path() string { + return m.path +} + +func (m *basicModule) Init(opts ...plugins.Option) error { + options := &plugins.Options{} + for _, opt := range opts { + opt(options) + } + + m.api = &api{ + service: options.Service, + } + return nil +} + +func (m *basicModule) Handlers() (mp map[string]*plugins.Handler) { + m.Lock() + defer m.Unlock() + + mp = make(map[string]*plugins.Handler) + mp["/services"] = &plugins.Handler{ + Func: m.api.services, + Method: []string{"GET"}, + } + + mp["/micro-services"] = &plugins.Handler{ + Func: m.api.microServices, + Method: []string{"GET"}, + } + + mp["/service"] = &plugins.Handler{ + Func: m.api.handler, + Method: []string{"GET"}, + } + + mp["/api-gateway-services"] = &plugins.Handler{ + Func: m.api.apiGatewayServices, + Method: []string{"GET"}, + } + + mp["/service-details"] = &plugins.Handler{ + Func: m.api.serviceDetails, + Method: []string{"GET"}, + } + + mp["/stats"] = &plugins.Handler{ + Func: m.api.stats, + Method: []string{"GET"}, + } + + mp["/web-services"] = &plugins.Handler{ + Func: m.api.webServices, + Method: []string{"GET"}, + } + + mp["/rpc"] = &plugins.Handler{ + Func: m.api.rpc, + Method: []string{"POST"}, + } + + mp["/health"] = &plugins.Handler{ + Func: m.api.health, + Method: []string{"GET"}, + } + + return +} diff --git a/service/stackweb/backend/plugins/basic/internal.go b/service/stackweb/backend/plugins/basic/internal.go new file mode 100644 index 0000000..48d88ef --- /dev/null +++ b/service/stackweb/backend/plugins/basic/internal.go @@ -0,0 +1,80 @@ +package basic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "strings" + "time" + + "github.com/stack-labs/stack-rpc-plugins/service/stackweb/plugins/basic/tools" + "github.com/stack-labs/stack-rpc/client" + "github.com/stack-labs/stack-rpc/util/errors" +) + +func (a *api) rpcCall(w http.ResponseWriter, ctx context.Context, rpcReq *rpcRequest) { + if len(rpcReq.Service) == 0 { + tools.WriteError(w, fmt.Errorf("Service Is Not found ")) + return + } + + if len(rpcReq.Endpoint) == 0 { + tools.WriteError(w, fmt.Errorf("Endpoint Is Not found err ")) + return + } + + // decode rpc request param body + if req, ok := rpcReq.Request.(string); ok { + d := json.NewDecoder(strings.NewReader(req)) + d.UseNumber() + + if err := d.Decode(&rpcReq.Request); err != nil { + tools.WriteError(w, fmt.Errorf("error decoding request string err: %s", err)) + return + } + } + + // create request/response + var response json.RawMessage + var err error + req := a.service.Client.NewRequest(rpcReq.Service, rpcReq.Endpoint, rpcReq.Request, client.WithContentType("application/json")) + + var opts []client.CallOption + + // set timeout + if rpcReq.timeout > 0 { + opts = append(opts, client.WithRequestTimeout(time.Duration(rpcReq.timeout)*time.Second)) + } + + // remote call + if len(rpcReq.Address) > 0 { + opts = append(opts, client.WithAddress(rpcReq.Address)) + } + + // remote call + err = a.service.Client.Call(ctx, req, &response, opts...) + if err != nil { + ce := errors.Parse(err.Error()) + switch ce.Code { + case 0: + // assuming it's totally screwed + ce.Code = 500 + ce.Id = "stack.rpc" + ce.Status = http.StatusText(500) + ce.Detail = "error during request: " + ce.Detail + w.WriteHeader(500) + default: + w.WriteHeader(int(ce.Code)) + } + w.Write([]byte(ce.Error())) + return + } + + if strings.Contains(rpcReq.URL, "/v1/rpc") { + w.Header().Set("Content-Type", "application/json") + w.Write(response) + } else { + tools.WriteJsonData(w, response) + } +} diff --git a/service/stackweb/backend/plugins/basic/tools.go b/service/stackweb/backend/plugins/basic/tools.go new file mode 100644 index 0000000..8e1ef80 --- /dev/null +++ b/service/stackweb/backend/plugins/basic/tools.go @@ -0,0 +1,21 @@ +package basic + +import ( + "github.com/stack-labs/stack-rpc/registry" +) + +type SortedServices struct { + Services []*registry.Service +} + +func (s SortedServices) Len() int { + return len(s.Services) +} + +func (s SortedServices) Less(i, j int) bool { + return s.Services[i].Name < s.Services[j].Name +} + +func (s SortedServices) Swap(i, j int) { + s.Services[i], s.Services[j] = s.Services[j], s.Services[i] +} diff --git a/service/stackweb/backend/plugins/basic/tools/json.go b/service/stackweb/backend/plugins/basic/tools/json.go new file mode 100644 index 0000000..eed9464 --- /dev/null +++ b/service/stackweb/backend/plugins/basic/tools/json.go @@ -0,0 +1,40 @@ +package tools + +import ( + "encoding/json" + "net/http" + + "github.com/stack-labs/stack-rpc-plugins/service/stackweb/plugins" +) + +func WriteJsonData(w http.ResponseWriter, data interface{}) { + rsp := &plugins.Rsp{ + Data: data, + Success: true, + } + + b, err := json.Marshal(rsp) + if err != nil { + http.Error(w, "Error occurred:"+err.Error(), 500) + return + } + + w.Header().Set("Content-Type", "application/json") + w.Write(b) +} + +func WriteError(w http.ResponseWriter, err error) { + rsp := &plugins.Rsp{ + Error: err.Error(), + Success: false, + } + + b, err := json.Marshal(rsp) + if err != nil { + http.Error(w, "Error occurred:"+err.Error(), 500) + return + } + + w.Header().Set("Content-Type", "application/json") + w.Write(b) +} diff --git a/service/stackweb/backend/plugins/basic/tools/number.go b/service/stackweb/backend/plugins/basic/tools/number.go new file mode 100644 index 0000000..bebd8d0 --- /dev/null +++ b/service/stackweb/backend/plugins/basic/tools/number.go @@ -0,0 +1,10 @@ +package tools + +func Int32ArrayTo64(in []int32) []int64 { + ret := make([]int64, len(in)) + for _, v := range in { + ret = append(ret, int64(v)) + } + + return ret +} diff --git a/service/stackweb/backend/plugins/basic/tools/sort.go b/service/stackweb/backend/plugins/basic/tools/sort.go new file mode 100644 index 0000000..c02985d --- /dev/null +++ b/service/stackweb/backend/plugins/basic/tools/sort.go @@ -0,0 +1,21 @@ +package tools + +import ( + "github.com/stack-labs/stack-rpc/registry" +) + +type SortedServices struct { + Services []*registry.Service +} + +func (s SortedServices) Len() int { + return len(s.Services) +} + +func (s SortedServices) Less(i, j int) bool { + return s.Services[i].Name < s.Services[j].Name +} + +func (s SortedServices) Swap(i, j int) { + s.Services[i], s.Services[j] = s.Services[j], s.Services[i] +} diff --git a/service/stackweb/backend/plugins/options.go b/service/stackweb/backend/plugins/options.go new file mode 100644 index 0000000..81dacc7 --- /dev/null +++ b/service/stackweb/backend/plugins/options.go @@ -0,0 +1,17 @@ +package plugins + +import ( + "github.com/stack-labs/stack-rpc/service" +) + +type Option func(o *Options) + +type Options struct { + Service service.Options +} + +func Service(s service.Options) Option { + return func(o *Options) { + o.Service = s + } +} diff --git a/service/stackweb/backend/plugins/plugin.go b/service/stackweb/backend/plugins/plugin.go new file mode 100644 index 0000000..d163e84 --- /dev/null +++ b/service/stackweb/backend/plugins/plugin.go @@ -0,0 +1,53 @@ +package plugins + +import ( + "net/http" +) + +// Plugin is the biggest unit of web component. +// a component is a webapp which will be registered on root path by Name function +type Plugin interface { + // Name of module + Name() string + + // Path returns the root path of this module + Path() string + + // Init initializes the module + Init(...Option) error + + // Handlers returns http handler of this module + Handlers() map[string]*Handler +} + +type Handler struct { + Name string + Func func(w http.ResponseWriter, r *http.Request) + Method []string + Hld http.Handler +} + +func (h Handler) IsFunc() bool { + return h.Func != nil +} + +// Rsp is the struct of http api response +type Rsp struct { + Code uint `json:"code,omitempty"` + Success bool `json:"success"` + Data interface{} `json:"data,omitempty"` + Error interface{} `json:"error,omitempty"` +} + +var ( + modules []Plugin +) + +func Register(m Plugin) { + modules = append(modules, m) +} + +// Plugins returns all of the registered modules +func Plugins() []Plugin { + return modules +} diff --git a/service/stackweb/backend/stack.yml b/service/stackweb/backend/stack.yml new file mode 100644 index 0000000..d985cd5 --- /dev/null +++ b/service/stackweb/backend/stack.yml @@ -0,0 +1,18 @@ +stack: + service: + name: stack.rpc.stackweb + web: + root-path: /stack + static: + route: webapp + dir: static + # todo + favicon-ico: + server: + address: :8090 + config: + hierarchy-merge: true + storege: false + logger: + name: console + level: info diff --git a/service/stackweb/frontend/.editorconfig b/service/stackweb/frontend/.editorconfig new file mode 100644 index 0000000..7e3649a --- /dev/null +++ b/service/stackweb/frontend/.editorconfig @@ -0,0 +1,16 @@ +# http://editorconfig.org +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false + +[Makefile] +indent_style = tab diff --git a/service/stackweb/frontend/.eslintignore b/service/stackweb/frontend/.eslintignore new file mode 100644 index 0000000..16116a2 --- /dev/null +++ b/service/stackweb/frontend/.eslintignore @@ -0,0 +1,4 @@ +/lambda/ +/scripts +/config +.history \ No newline at end of file diff --git a/service/stackweb/frontend/.eslintrc.js b/service/stackweb/frontend/.eslintrc.js new file mode 100644 index 0000000..b882c20 --- /dev/null +++ b/service/stackweb/frontend/.eslintrc.js @@ -0,0 +1,8 @@ +module.exports = { + extends: [require.resolve('@umijs/fabric/dist/eslint')], + globals: { + ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION: true, + page: true, + REACT_APP_ENV: true, + }, +}; diff --git a/service/stackweb/frontend/.gitignore b/service/stackweb/frontend/.gitignore new file mode 100644 index 0000000..7fd9f58 --- /dev/null +++ b/service/stackweb/frontend/.gitignore @@ -0,0 +1,40 @@ +# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. + +# dependencies +**/node_modules +# roadhog-api-doc ignore +/src/utils/request-temp.js +_roadhog-api-doc + +# production +/dist +/.vscode + +# misc +.DS_Store +npm-debug.log* +yarn-error.log + +/coverage +.idea +yarn.lock +package-lock.json +*bak +.vscode + +# visual studio code +.history +*.log +functions/* +.temp/** + +# umi +.umi +.umi-production + +# screenshot +screenshot +.firebase +.eslintcache + +build diff --git a/service/stackweb/frontend/.prettierignore b/service/stackweb/frontend/.prettierignore new file mode 100644 index 0000000..5456c4f --- /dev/null +++ b/service/stackweb/frontend/.prettierignore @@ -0,0 +1,22 @@ +**/*.svg +package.json +.umi +.umi-production +/dist +.dockerignore +.DS_Store +.eslintignore +*.png +*.toml +docker +.editorconfig +Dockerfile* +.gitignore +.prettierignore +LICENSE +.eslintcache +*.lock +yarn-error.log +.history +CNAME +/build diff --git a/service/stackweb/frontend/.prettierrc.js b/service/stackweb/frontend/.prettierrc.js new file mode 100644 index 0000000..7b597d7 --- /dev/null +++ b/service/stackweb/frontend/.prettierrc.js @@ -0,0 +1,5 @@ +const fabric = require('@umijs/fabric'); + +module.exports = { + ...fabric.prettier, +}; diff --git a/service/stackweb/frontend/.stylelintrc.js b/service/stackweb/frontend/.stylelintrc.js new file mode 100644 index 0000000..c203078 --- /dev/null +++ b/service/stackweb/frontend/.stylelintrc.js @@ -0,0 +1,5 @@ +const fabric = require('@umijs/fabric'); + +module.exports = { + ...fabric.stylelint, +}; diff --git a/service/stackweb/frontend/README.md b/service/stackweb/frontend/README.md new file mode 100644 index 0000000..6404215 --- /dev/null +++ b/service/stackweb/frontend/README.md @@ -0,0 +1 @@ +# Platform Web UI diff --git a/service/stackweb/frontend/config/config.ts b/service/stackweb/frontend/config/config.ts new file mode 100644 index 0000000..361556c --- /dev/null +++ b/service/stackweb/frontend/config/config.ts @@ -0,0 +1,372 @@ +// https://umijs.org/config/ +import {defineConfig, utils} from 'umi'; +import defaultSettings from './defaultSettings'; +import proxy from './proxy'; +import webpackPlugin from './plugin.config'; + +const {winPath} = utils; // preview.pro.ant.design only do not use in your production ; +// preview.pro.ant.design 专用环境变量,请不要在你的项目中使用它。 + +const {ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION, REACT_APP_ENV, GA_KEY} = process.env; +export default defineConfig({ + hash: true, + antd: {}, + analytics: GA_KEY + ? { + ga: GA_KEY, + } + : false, + dva: { + hmr: true, + }, + locale: { + // default zh-CN + default: 'zh-CN', + // default true, when it is true, will use `navigator.language` overwrite default + antd: true, + baseNavigator: true, + }, + dynamicImport: { + loading: '@/components/PageLoading/index', + }, + targets: { + ie: 11, + }, + // umi routes: https://umijs.org/docs/routing + routes: [ + { + path: '/', + component: '../layouts/BlankLayout', + routes: [ + { + path: '/user', + component: '../layouts/UserLayout', + routes: [ + { + path: '/user', + redirect: '/user/login', + }, + { + name: 'login', + icon: 'smile', + path: '/user/login', + component: './user/login', + }, + { + name: 'register-result', + icon: 'smile', + path: '/user/register-result', + component: './user/register-result', + }, + { + name: 'register', + icon: 'smile', + path: '/user/register', + component: './user/register', + }, + { + component: '404', + }, + ], + }, + { + path: '/', + component: '../layouts/BasicLayout', + Routes: ['src/pages/Authorized'], + authority: ['admin', 'user'], + routes: [ + { + path: '/service', + name: 'service', + icon: 'service', + routes: [ + { + name: 'service-list', + icon: 'smile', + path: '/service/service-list', + component: './service/service-list', + }, + { + name: 'call-service', + icon: 'smile', + path: '/service/call-service', + component: './service/call', + } + ], + }, + { + path: '/dashboard', + name: 'dashboard', + icon: 'dashboard', + routes: [ + { + name: 'analysis', + icon: 'smile', + path: '/dashboard/analysis', + component: './dashboard/analysis', + }, + { + name: 'monitor', + icon: 'smile', + path: '/dashboard/monitor', + component: './dashboard/monitor', + }, + { + name: 'workplace', + icon: 'smile', + path: '/dashboard/workplace', + component: './dashboard/workplace', + }, + ], + }, + { + path: '/form', + icon: 'form', + name: 'form', + routes: [ + { + name: 'basic-form', + icon: 'smile', + path: '/form/basic-form', + component: './form/basic-form', + }, + { + name: 'step-form', + icon: 'smile', + path: '/form/step-form', + component: './form/step-form', + }, + { + name: 'advanced-form', + icon: 'smile', + path: '/form/advanced-form', + component: './form/advanced-form', + }, + ], + }, + { + path: '/list', + icon: 'table', + name: 'list', + routes: [ + { + path: '/list/search', + name: 'search-list', + component: './list/search', + routes: [ + { + path: '/list/search', + redirect: '/list/search/articles', + }, + { + name: 'articles', + icon: 'smile', + path: '/list/search/articles', + component: './list/search/articles', + }, + { + name: 'projects', + icon: 'smile', + path: '/list/search/projects', + component: './list/search/projects', + }, + { + name: 'applications', + icon: 'smile', + path: '/list/search/applications', + component: './list/search/applications', + }, + ], + }, + { + name: 'table-list', + icon: 'smile', + path: '/list/table-list', + component: './list/table-list', + }, + { + name: 'basic-list', + icon: 'smile', + path: '/list/basic-list', + component: './list/basic-list', + }, + { + name: 'card-list', + icon: 'smile', + path: '/list/card-list', + component: './list/card-list', + }, + ], + }, + { + path: '/profile', + name: 'profile', + icon: 'profile', + routes: [ + { + name: 'basic', + icon: 'smile', + path: '/profile/basic', + component: './profile/basic', + }, + { + name: 'advanced', + icon: 'smile', + path: '/profile/advanced', + component: './profile/advanced', + }, + ], + }, + { + name: 'result', + icon: 'CheckCircleOutlined', + path: '/result', + routes: [ + { + name: 'success', + icon: 'smile', + path: '/result/success', + component: './result/success', + }, + { + name: 'fail', + icon: 'smile', + path: '/result/fail', + component: './result/fail', + }, + ], + }, + { + name: 'exception', + icon: 'warning', + path: '/exception', + routes: [ + { + name: '403', + icon: 'smile', + path: '/exception/403', + component: './exception/403', + }, + { + name: '404', + icon: 'smile', + path: '/exception/404', + component: './exception/404', + }, + { + name: '500', + icon: 'smile', + path: '/exception/500', + component: './exception/500', + }, + ], + }, + { + name: 'account', + icon: 'user', + path: '/account', + routes: [ + { + name: 'center', + icon: 'smile', + path: '/account/center', + component: './account/center', + }, + { + name: 'settings', + icon: 'smile', + path: '/account/settings', + component: './account/settings', + }, + ], + }, + { + name: 'editor', + icon: 'highlight', + path: '/editor', + routes: [ + { + name: 'flow', + icon: 'smile', + path: '/editor/flow', + component: './editor/flow', + }, + { + name: 'mind', + icon: 'smile', + path: '/editor/mind', + component: './editor/mind', + }, + { + name: 'koni', + icon: 'smile', + path: '/editor/koni', + component: './editor/koni', + }, + ], + }, + { + path: '/', + redirect: '/dashboard/analysis', + authority: ['admin', 'user'], + }, + { + component: '404', + }, + ], + }, + ], + }, + ], + // Theme for antd: https://ant.design/docs/react/customize-theme-cn + theme: { + // ...darkTheme, + 'primary-color': defaultSettings.primaryColor, + }, + define: { + REACT_APP_ENV: REACT_APP_ENV || false, + ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION: + ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION || '', // preview.pro.ant.design only do not use in your production ; preview.pro.ant.design 专用环境变量,请不要在你的项目中使用它。 + }, + ignoreMomentLocale: true, + lessLoader: { + javascriptEnabled: true, + }, + cssLoader: { + modules: { + getLocalIdent: ( + context: { + resourcePath: string; + }, + _: string, + localName: string, + ) => { + if ( + context.resourcePath.includes('node_modules') || + context.resourcePath.includes('ant.design.pro.less') || + context.resourcePath.includes('global.less') + ) { + return localName; + } + + const match = context.resourcePath.match(/src(.*)/); + + if (match && match[1]) { + const antdProPath = match[1].replace('.less', ''); + const arr = winPath(antdProPath) + .split('/') + .map((a: string) => a.replace(/([A-Z])/g, '-$1')) + .map((a: string) => a.toLowerCase()); + return `antd-pro${arr.join('-')}-${localName}`.replace(/--/g, '-'); + } + + return localName; + }, + }, + }, + manifest: { + basePath: '/', + }, + proxy: proxy[REACT_APP_ENV || 'dev'], + chainWebpack: webpackPlugin, +}); diff --git a/service/stackweb/frontend/config/defaultSettings.ts b/service/stackweb/frontend/config/defaultSettings.ts new file mode 100644 index 0000000..888bcef --- /dev/null +++ b/service/stackweb/frontend/config/defaultSettings.ts @@ -0,0 +1,61 @@ +import { MenuTheme } from 'antd/es/menu/MenuContext'; + +export type ContentWidth = 'Fluid' | 'Fixed'; + +export interface DefaultSettings { + /** + * theme for nav menu + */ + navTheme: MenuTheme; + /** + * primary color of ant design + */ + primaryColor: string; + /** + * nav menu position: `sidemenu` or `topmenu` + */ + layout: 'sidemenu' | 'topmenu'; + /** + * layout of content: `Fluid` or `Fixed`, only works when layout is topmenu + */ + contentWidth: ContentWidth; + /** + * sticky header + */ + fixedHeader: boolean; + /** + * auto hide header + */ + autoHideHeader: boolean; + /** + * sticky siderbar + */ + fixSiderbar: boolean; + menu: { locale: boolean }; + title: string; + pwa: boolean; + // Your custom iconfont Symbol script Url + // eg://at.alicdn.com/t/font_1039637_btcrd5co4w.js + // 注意:如果需要图标多色,Iconfont 图标项目里要进行批量去色处理 + // Usage: https://github.com/ant-design/ant-design-pro/pull/3517 + iconfontUrl: string; + colorWeak: boolean; +} + +export default { + navTheme: 'dark', + // 拂晓蓝 + primaryColor: '#1890ff', + layout: 'sidemenu', + contentWidth: 'Fluid', + fixedHeader: false, + autoHideHeader: false, + fixSiderbar: false, + colorWeak: false, + menu: { + locale: true, + }, + title: 'Platform Web', + pwa: false, + iconfontUrl: '', +} as DefaultSettings; diff --git a/service/stackweb/frontend/config/plugin.config.ts b/service/stackweb/frontend/config/plugin.config.ts new file mode 100644 index 0000000..aff2d47 --- /dev/null +++ b/service/stackweb/frontend/config/plugin.config.ts @@ -0,0 +1,65 @@ +import path from 'path'; + +import * as IWebpackChainConfig from 'webpack-chain'; + +function getModulePackageName(module: { context: string }) { + if (!module.context) return null; + + const nodeModulesPath = path.join(__dirname, '../node_modules/'); + if (module.context.substring(0, nodeModulesPath.length) !== nodeModulesPath) { + return null; + } + + const moduleRelativePath = module.context.substring(nodeModulesPath.length); + const [moduleDirName] = moduleRelativePath.split(path.sep); + let packageName: string | null = moduleDirName; + // handle tree shaking + if (packageName && packageName.match('^_')) { + // eslint-disable-next-line prefer-destructuring + packageName = packageName.match(/^_(@?[^@]+)/)![1]; + } + return packageName; +} + +const webpackPlugin = (config: IWebpackChainConfig) => { + // optimize chunks + config.optimization + // share the same chunks across different modules + .runtimeChunk(false) + .splitChunks({ + chunks: 'async', + name: 'vendors', + maxInitialRequests: Infinity, + minSize: 0, + cacheGroups: { + vendors: { + test: (module: { context: string }) => { + const packageName = getModulePackageName(module) || ''; + if (packageName) { + return [ + 'bizcharts', + 'gg-editor', + 'g6', + '@antv', + 'l7', + 'gg-editor-core', + 'bizcharts-plugin-slider', + ].includes(packageName); + } + return false; + }, + name(module: { context: string }) { + const packageName = getModulePackageName(module); + if (packageName) { + if (['bizcharts', '@antv_data-set'].indexOf(packageName) >= 0) { + return 'viz'; // visualization package + } + } + return 'misc'; + }, + }, + }, + }); +}; + +export default webpackPlugin; diff --git a/service/stackweb/frontend/config/proxy.ts b/service/stackweb/frontend/config/proxy.ts new file mode 100644 index 0000000..50c562b --- /dev/null +++ b/service/stackweb/frontend/config/proxy.ts @@ -0,0 +1,35 @@ +/** + * 在生产环境 代理是无法生效的,所以这里没有生产环境的配置 + * The agent cannot take effect in the production environment + * so there is no configuration of the production environment + * For details, please see + * https://pro.ant.design/docs/deploy + */ +export default { + dev: { + '/api/': { + target: 'https://preview.pro.ant.design', + changeOrigin: true, + pathRewrite: { '^': '' }, + }, + '/stackweb': { + target: 'http://localhost:9082', + changeOrigin: true, + pathRewrite: { '^': '' }, + }, + }, + test: { + '/api/': { + target: 'https://preview.pro.ant.design', + changeOrigin: true, + pathRewrite: { '^': '' }, + }, + }, + pre: { + '/api/': { + target: 'your pre url', + changeOrigin: true, + pathRewrite: { '^': '' }, + }, + }, +}; diff --git a/service/stackweb/frontend/jest.config.js b/service/stackweb/frontend/jest.config.js new file mode 100644 index 0000000..c82d7f1 --- /dev/null +++ b/service/stackweb/frontend/jest.config.js @@ -0,0 +1,8 @@ +module.exports = { + testURL: 'http://localhost:8000', + extraSetupFiles: ['./tests/setupTests.js'], + globals: { + ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION: false, + localStorage: null, + }, +}; diff --git a/service/stackweb/frontend/jsconfig.json b/service/stackweb/frontend/jsconfig.json new file mode 100644 index 0000000..f87334d --- /dev/null +++ b/service/stackweb/frontend/jsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "baseUrl": ".", + "paths": { + "@/*": ["./src/*"] + } + } +} diff --git a/service/stackweb/frontend/mock/listTableList.ts b/service/stackweb/frontend/mock/listTableList.ts new file mode 100644 index 0000000..efc3224 --- /dev/null +++ b/service/stackweb/frontend/mock/listTableList.ts @@ -0,0 +1,153 @@ +// eslint-disable-next-line import/no-extraneous-dependencies +import { Request, Response } from 'express'; +import { parse } from 'url'; +import { TableListItem, TableListParams } from '@/pages/ListTableList/data'; + +// mock tableListDataSource +const genList = (current: number, pageSize: number) => { + const tableListDataSource: TableListItem[] = []; + + for (let i = 0; i < pageSize; i += 1) { + const index = (current - 1) * 10 + i; + tableListDataSource.push({ + key: index, + disabled: i % 6 === 0, + href: 'https://ant.design', + avatar: [ + 'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png', + 'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png', + ][i % 2], + name: `TradeCode ${index}`, + owner: '曲丽丽', + desc: '这是一段描述', + callNo: Math.floor(Math.random() * 1000), + status: Math.floor(Math.random() * 10) % 4, + updatedAt: new Date(), + createdAt: new Date(), + progress: Math.ceil(Math.random() * 100), + }); + } + tableListDataSource.reverse(); + return tableListDataSource; +}; + +let tableListDataSource = genList(1, 100); + +function getRule(req: Request, res: Response, u: string) { + let realUrl = u; + if (!realUrl || Object.prototype.toString.call(realUrl) !== '[object String]') { + realUrl = req.url; + } + const { current = 1, pageSize = 10 } = req.query; + const params = (parse(realUrl, true).query as unknown) as TableListParams; + + let dataSource = [...tableListDataSource].slice((current - 1) * pageSize, current * pageSize); + if (params.sorter) { + const s = params.sorter.split('_'); + dataSource = dataSource.sort((prev, next) => { + if (s[1] === 'descend') { + return next[s[0]] - prev[s[0]]; + } + return prev[s[0]] - next[s[0]]; + }); + } + + if (params.status) { + const status = params.status.split(','); + let filterDataSource: TableListItem[] = []; + status.forEach((s: string) => { + filterDataSource = filterDataSource.concat( + dataSource.filter((item) => { + if (parseInt(`${item.status}`, 10) === parseInt(s.split('')[0], 10)) { + return true; + } + return false; + }), + ); + }); + dataSource = filterDataSource; + } + + if (params.name) { + dataSource = dataSource.filter((data) => data.name.includes(params.name || '')); + } + const result = { + data: dataSource, + total: tableListDataSource.length, + success: true, + pageSize, + current: parseInt(`${params.currentPage}`, 10) || 1, + }; + + return res.json(result); +} + +function postRule(req: Request, res: Response, u: string, b: Request) { + let realUrl = u; + if (!realUrl || Object.prototype.toString.call(realUrl) !== '[object String]') { + realUrl = req.url; + } + + const body = (b && b.body) || req.body; + const { method, name, desc, key } = body; + + switch (method) { + /* eslint no-case-declarations:0 */ + case 'delete': + tableListDataSource = tableListDataSource.filter((item) => key.indexOf(item.key) === -1); + break; + case 'post': + (() => { + const i = Math.ceil(Math.random() * 10000); + const newRule = { + key: tableListDataSource.length, + href: 'https://ant.design', + avatar: [ + 'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png', + 'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png', + ][i % 2], + name, + owner: '曲丽丽', + desc, + callNo: Math.floor(Math.random() * 1000), + status: Math.floor(Math.random() * 10) % 2, + updatedAt: new Date(), + createdAt: new Date(), + progress: Math.ceil(Math.random() * 100), + }; + tableListDataSource.unshift(newRule); + return res.json(newRule); + })(); + return; + + case 'update': + (() => { + let newRule = {}; + tableListDataSource = tableListDataSource.map((item) => { + if (item.key === key) { + newRule = { ...item, desc, name }; + return { ...item, desc, name }; + } + return item; + }); + return res.json(newRule); + })(); + return; + default: + break; + } + + const result = { + list: tableListDataSource, + pagination: { + total: tableListDataSource.length, + }, + }; + + res.json(result); +} + +export default { + 'GET /api/rule': getRule, + 'POST /api/rule': postRule, +}; diff --git a/service/stackweb/frontend/mock/notices.ts b/service/stackweb/frontend/mock/notices.ts new file mode 100644 index 0000000..b9e3bf2 --- /dev/null +++ b/service/stackweb/frontend/mock/notices.ts @@ -0,0 +1,105 @@ +import { Request, Response } from 'express'; + +const getNotices = (req: Request, res: Response) => { + res.json([ + { + id: '000000001', + avatar: 'https://gw.alipayobjects.com/zos/rmsportal/ThXAXghbEsBCCSDihZxY.png', + title: '你收到了 14 份新周报', + datetime: '2017-08-09', + type: 'notification', + }, + { + id: '000000002', + avatar: 'https://gw.alipayobjects.com/zos/rmsportal/OKJXDXrmkNshAMvwtvhu.png', + title: '你推荐的 曲妮妮 已通过第三轮面试', + datetime: '2017-08-08', + type: 'notification', + }, + { + id: '000000003', + avatar: 'https://gw.alipayobjects.com/zos/rmsportal/kISTdvpyTAhtGxpovNWd.png', + title: '这种模板可以区分多种通知类型', + datetime: '2017-08-07', + read: true, + type: 'notification', + }, + { + id: '000000004', + avatar: 'https://gw.alipayobjects.com/zos/rmsportal/GvqBnKhFgObvnSGkDsje.png', + title: '左侧图标用于区分不同的类型', + datetime: '2017-08-07', + type: 'notification', + }, + { + id: '000000005', + avatar: 'https://gw.alipayobjects.com/zos/rmsportal/ThXAXghbEsBCCSDihZxY.png', + title: '内容不要超过两行字,超出时自动截断', + datetime: '2017-08-07', + type: 'notification', + }, + { + id: '000000006', + avatar: 'https://gw.alipayobjects.com/zos/rmsportal/fcHMVNCjPOsbUGdEduuv.jpeg', + title: '曲丽丽 评论了你', + description: '描述信息描述信息描述信息', + datetime: '2017-08-07', + type: 'message', + clickClose: true, + }, + { + id: '000000007', + avatar: 'https://gw.alipayobjects.com/zos/rmsportal/fcHMVNCjPOsbUGdEduuv.jpeg', + title: '朱偏右 回复了你', + description: '这种模板用于提醒谁与你发生了互动,左侧放『谁』的头像', + datetime: '2017-08-07', + type: 'message', + clickClose: true, + }, + { + id: '000000008', + avatar: 'https://gw.alipayobjects.com/zos/rmsportal/fcHMVNCjPOsbUGdEduuv.jpeg', + title: '标题', + description: '这种模板用于提醒谁与你发生了互动,左侧放『谁』的头像', + datetime: '2017-08-07', + type: 'message', + clickClose: true, + }, + { + id: '000000009', + title: '任务名称', + description: '任务需要在 2017-01-12 20:00 前启动', + extra: '未开始', + status: 'todo', + type: 'event', + }, + { + id: '000000010', + title: '第三方紧急代码变更', + description: '冠霖提交于 2017-01-06,需在 2017-01-07 前完成代码变更任务', + extra: '马上到期', + status: 'urgent', + type: 'event', + }, + { + id: '000000011', + title: '信息安全考试', + description: '指派竹尔于 2017-01-09 前完成更新并发布', + extra: '已耗时 8 天', + status: 'doing', + type: 'event', + }, + { + id: '000000012', + title: 'ABCD 版本发布', + description: '冠霖提交于 2017-01-06,需在 2017-01-07 前完成代码变更任务', + extra: '进行中', + status: 'processing', + type: 'event', + }, + ]); +}; + +export default { + 'GET /api/notices': getNotices, +}; diff --git a/service/stackweb/frontend/mock/route.ts b/service/stackweb/frontend/mock/route.ts new file mode 100644 index 0000000..418d10f --- /dev/null +++ b/service/stackweb/frontend/mock/route.ts @@ -0,0 +1,5 @@ +export default { + '/api/auth_routes': { + '/form/advanced-form': { authority: ['admin', 'user'] }, + }, +}; diff --git a/service/stackweb/frontend/mock/user.ts b/service/stackweb/frontend/mock/user.ts new file mode 100644 index 0000000..24fa3f7 --- /dev/null +++ b/service/stackweb/frontend/mock/user.ts @@ -0,0 +1,154 @@ +import { Request, Response } from 'express'; + +function getFakeCaptcha(req: Request, res: Response) { + return res.json('captcha-xxx'); +} +// 代码中会兼容本地 service mock 以及部署站点的静态数据 +export default { + // 支持值为 Object 和 Array + 'GET /api/currentUser': { + name: 'Serati Ma', + avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png', + userid: '00000001', + email: 'antdesign@alipay.com', + signature: '海纳百川,有容乃大', + title: '交互专家', + group: '蚂蚁金服-某某某事业群-某某平台部-某某技术部-UED', + tags: [ + { + key: '0', + label: '很有想法的', + }, + { + key: '1', + label: '专注设计', + }, + { + key: '2', + label: '辣~', + }, + { + key: '3', + label: '大长腿', + }, + { + key: '4', + label: '川妹子', + }, + { + key: '5', + label: '海纳百川', + }, + ], + notifyCount: 12, + unreadCount: 11, + country: 'China', + geographic: { + province: { + label: '浙江省', + key: '330000', + }, + city: { + label: '杭州市', + key: '330100', + }, + }, + address: '西湖区工专路 77 号', + phone: '0752-268888888', + }, + // GET POST 可省略 + 'GET /api/users': [ + { + key: '1', + name: 'John Brown', + age: 32, + address: 'New York No. 1 Lake Park', + }, + { + key: '2', + name: 'Jim Green', + age: 42, + address: 'London No. 1 Lake Park', + }, + { + key: '3', + name: 'Joe Black', + age: 32, + address: 'Sidney No. 1 Lake Park', + }, + ], + 'POST /api/login/account': (req: Request, res: Response) => { + const { password, userName, type } = req.body; + if (password === 'ant.design' && userName === 'admin') { + res.send({ + status: 'ok', + type, + currentAuthority: 'admin', + }); + return; + } + if (password === 'ant.design' && userName === 'user') { + res.send({ + status: 'ok', + type, + currentAuthority: 'user', + }); + return; + } + if (type === 'mobile') { + res.send({ + status: 'ok', + type, + currentAuthority: 'admin', + }); + return; + } + + res.send({ + status: 'error', + type, + currentAuthority: 'guest', + }); + }, + 'POST /api/register': (req: Request, res: Response) => { + res.send({ status: 'ok', currentAuthority: 'user' }); + }, + 'GET /api/500': (req: Request, res: Response) => { + res.status(500).send({ + timestamp: 1513932555104, + status: 500, + error: 'error', + message: 'error', + path: '/base/category/list', + }); + }, + 'GET /api/404': (req: Request, res: Response) => { + res.status(404).send({ + timestamp: 1513932643431, + status: 404, + error: 'Not Found', + message: 'No message available', + path: '/base/category/list/2121212', + }); + }, + 'GET /api/403': (req: Request, res: Response) => { + res.status(403).send({ + timestamp: 1513932555104, + status: 403, + error: 'Unauthorized', + message: 'Unauthorized', + path: '/base/category/list', + }); + }, + 'GET /api/401': (req: Request, res: Response) => { + res.status(401).send({ + timestamp: 1513932555104, + status: 401, + error: 'Unauthorized', + message: 'Unauthorized', + path: '/base/category/list', + }); + }, + + 'GET /api/login/captcha': getFakeCaptcha, +}; diff --git a/service/stackweb/frontend/package.json b/service/stackweb/frontend/package.json new file mode 100644 index 0000000..7948caf --- /dev/null +++ b/service/stackweb/frontend/package.json @@ -0,0 +1,129 @@ +{ + "name": "platform-web", + "version": "1.0.0", + "private": true, + "description": "Go-Micro 治理一站式解决方案", + "scripts": { + "analyze": "cross-env ANALYZE=1 umi build", + "build": "umi build", + "deploy": "npm run site && npm run gh-pages", + "dev": "npm run start:dev", + "fetch:blocks": "pro fetch-blocks --branch=umi@3 && npm run prettier", + "gh-pages": "cp CNAME ./dist/ && gh-pages -d dist", + "i18n-remove": "pro i18n-remove --locale=zh-CN --write", + "lint": "umi g tmp && npm run lint:js && npm run lint:style && npm run lint:prettier", + "lint-staged": "lint-staged", + "lint-staged:js": "eslint --ext .js,.jsx,.ts,.tsx ", + "lint:fix": "eslint --fix --cache --ext .js,.jsx,.ts,.tsx --format=pretty ./src && npm run lint:style", + "lint:js": "eslint --cache --ext .js,.jsx,.ts,.tsx --format=pretty ./src", + "lint:prettier": "prettier --check \"**/*\" --end-of-line auto", + "lint:style": "stylelint --fix \"src/**/*.less\" --syntax less", + "prettier": "prettier -c --write \"**/*\"", + "start": "umi dev", + "start:dev": "cross-env REACT_APP_ENV=dev MOCK=none umi dev", + "start:no-mock": "cross-env MOCK=none umi dev", + "start:no-ui": "cross-env UMI_UI=none umi dev", + "start:pre": "cross-env REACT_APP_ENV=pre umi dev", + "start:test": "cross-env REACT_APP_ENV=test MOCK=none umi dev", + "test": "umi test", + "test:all": "node ./tests/run-tests.js", + "test:component": "umi test ./src/components", + "tsc": "tsc" + }, + "husky": { + "hooks": { + "pre-commit": "npm run lint-staged" + } + }, + "lint-staged": { + "**/*.less": "stylelint --syntax less", + "**/*.{js,jsx,ts,tsx}": "npm run lint-staged:js", + "**/*.{js,jsx,tsx,ts,less,md,json}": [ + "prettier --write" + ] + }, + "browserslist": [ + "> 1%", + "last 2 versions", + "not ie <= 10" + ], + "dependencies": { + "@ant-design/icons": "^4.0.0", + "@ant-design/pro-layout": "^5.0.8", + "@ant-design/pro-table": "^2.1.11", + "@antv/data-set": "^0.11.0", + "@antv/l7": "^2.0.0", + "@antv/l7-maps": "^2.0.0", + "@types/lodash.debounce": "^4.0.6", + "@types/lodash.isequal": "^4.5.5", + "antd": "^4.0.0", + "bizcharts": "^3.5.3-beta.0", + "bizcharts-plugin-slider": "^2.1.1-beta.1", + "classnames": "^2.2.6", + "gg-editor": "^2.0.2", + "lodash": "^4.17.11", + "lodash-decorators": "^6.0.0", + "lodash.debounce": "^4.0.8", + "lodash.isequal": "^4.5.0", + "mockjs": "^1.0.1-beta3", + "moment": "^2.24.0", + "numeral": "^2.0.6", + "nzh": "^1.0.3", + "omit.js": "^1.0.2", + "path-to-regexp": "2.4.0", + "prop-types": "^15.5.10", + "qs": "^6.9.0", + "react": "^16.8.6", + "react-dom": "^16.8.6", + "react-fittext": "^1.0.0", + "react-helmet-async": "^1.0.4", + "react-router": "^4.3.1", + "umi": "^3.0.14", + "umi-request": "^1.0.8", + "use-merge-value": "^1.0.1" + }, + "devDependencies": { + "@ant-design/pro-cli": "^1.0.18", + "@types/classnames": "^2.2.7", + "@types/express": "^4.17.0", + "@types/history": "^4.7.2", + "@types/jest": "^25.1.0", + "@types/lodash": "^4.14.144", + "@types/qs": "^6.5.3", + "@types/react": "^16.9.17", + "@types/react-dom": "^16.8.4", + "@types/react-helmet": "^5.0.13", + "@umijs/fabric": "^2.0.5", + "@umijs/plugin-blocks": "^2.0.5", + "@umijs/preset-ant-design-pro": "^1.0.1", + "@umijs/preset-react": "^1.4.8", + "@umijs/preset-ui": "^2.0.9", + "chalk": "^3.0.0", + "cross-env": "^7.0.0", + "cross-port-killer": "^1.1.1", + "enzyme": "^3.11.0", + "eslint": "^6.8.0", + "express": "^4.17.1", + "gh-pages": "^2.0.1", + "husky": "^4.0.7", + "jsdom-global": "^3.0.2", + "lint-staged": "^10.0.0", + "mockjs": "^1.0.1-beta3", + "prettier": "^2.0.1", + "pro-download": "1.0.1", + "stylelint": "^13.0.0" + }, + "optionalDependencies": { + "puppeteer": "^2.0.0" + }, + "engines": { + "node": ">=10.0.0" + }, + "checkFiles": [ + "src/**/*.js*", + "src/**/*.ts*", + "src/**/*.less", + "config/**/*.js*", + "scripts/**/*.js" + ] +} diff --git a/service/stackweb/frontend/public/CNAME b/service/stackweb/frontend/public/CNAME new file mode 100644 index 0000000..30c2d4d --- /dev/null +++ b/service/stackweb/frontend/public/CNAME @@ -0,0 +1 @@ +preview.pro.ant.design \ No newline at end of file diff --git a/service/stackweb/frontend/public/favicon.png b/service/stackweb/frontend/public/favicon.png new file mode 100644 index 0000000..ece59ce Binary files /dev/null and b/service/stackweb/frontend/public/favicon.png differ diff --git a/service/stackweb/frontend/public/home_bg.png b/service/stackweb/frontend/public/home_bg.png new file mode 100644 index 0000000..7c92a4b Binary files /dev/null and b/service/stackweb/frontend/public/home_bg.png differ diff --git a/service/stackweb/frontend/public/icons/icon-128x128.png b/service/stackweb/frontend/public/icons/icon-128x128.png new file mode 100644 index 0000000..48d0e23 Binary files /dev/null and b/service/stackweb/frontend/public/icons/icon-128x128.png differ diff --git a/service/stackweb/frontend/public/icons/icon-192x192.png b/service/stackweb/frontend/public/icons/icon-192x192.png new file mode 100644 index 0000000..938e9b5 Binary files /dev/null and b/service/stackweb/frontend/public/icons/icon-192x192.png differ diff --git a/service/stackweb/frontend/public/icons/icon-512x512.png b/service/stackweb/frontend/public/icons/icon-512x512.png new file mode 100644 index 0000000..21fc108 Binary files /dev/null and b/service/stackweb/frontend/public/icons/icon-512x512.png differ diff --git a/service/stackweb/frontend/public/logo.svg b/service/stackweb/frontend/public/logo.svg new file mode 100644 index 0000000..90a8f2b --- /dev/null +++ b/service/stackweb/frontend/public/logo.svg @@ -0,0 +1,55 @@ + +image/svg+xmlChina + \ No newline at end of file diff --git a/service/stackweb/frontend/public/pw.svg b/service/stackweb/frontend/public/pw.svg new file mode 100644 index 0000000..5739fa7 --- /dev/null +++ b/service/stackweb/frontend/public/pw.svg @@ -0,0 +1,10 @@ + + + background + + + + Layer 1 + Platform-Web + + \ No newline at end of file diff --git a/service/stackweb/frontend/src/assets/logo-white.svg b/service/stackweb/frontend/src/assets/logo-white.svg new file mode 100644 index 0000000..4b4a0ea --- /dev/null +++ b/service/stackweb/frontend/src/assets/logo-white.svg @@ -0,0 +1,3 @@ + + + diff --git a/service/stackweb/frontend/src/assets/logo.svg b/service/stackweb/frontend/src/assets/logo.svg new file mode 100644 index 0000000..90a8f2b --- /dev/null +++ b/service/stackweb/frontend/src/assets/logo.svg @@ -0,0 +1,55 @@ + +image/svg+xmlChina + \ No newline at end of file diff --git a/service/stackweb/frontend/src/components/Authorized/Authorized.tsx b/service/stackweb/frontend/src/components/Authorized/Authorized.tsx new file mode 100644 index 0000000..d91b7b5 --- /dev/null +++ b/service/stackweb/frontend/src/components/Authorized/Authorized.tsx @@ -0,0 +1,35 @@ +import React from 'react'; +import { Result } from 'antd'; +import check, { IAuthorityType } from './CheckPermissions'; + +import AuthorizedRoute from './AuthorizedRoute'; +import Secured from './Secured'; + +interface AuthorizedProps { + authority: IAuthorityType; + noMatch?: React.ReactNode; +} + +type IAuthorizedType = React.FunctionComponent & { + Secured: typeof Secured; + check: typeof check; + AuthorizedRoute: typeof AuthorizedRoute; +}; + +const Authorized: React.FunctionComponent = ({ + children, + authority, + noMatch = ( + + ), +}) => { + const childrenRender: React.ReactNode = typeof children === 'undefined' ? null : children; + const dom = check(authority, childrenRender, noMatch); + return <>{dom}; +}; + +export default Authorized as IAuthorizedType; diff --git a/service/stackweb/frontend/src/components/Authorized/AuthorizedRoute.tsx b/service/stackweb/frontend/src/components/Authorized/AuthorizedRoute.tsx new file mode 100644 index 0000000..7743eae --- /dev/null +++ b/service/stackweb/frontend/src/components/Authorized/AuthorizedRoute.tsx @@ -0,0 +1,33 @@ +import { Redirect, Route } from 'umi'; + +import React from 'react'; +import Authorized from './Authorized'; +import { IAuthorityType } from './CheckPermissions'; + +interface AuthorizedRoutePops { + currentAuthority: string; + component: React.ComponentClass; + render: (props: any) => React.ReactNode; + redirectPath: string; + authority: IAuthorityType; +} + +const AuthorizedRoute: React.SFC = ({ + component: Component, + render, + authority, + redirectPath, + ...rest +}) => ( + } />} + > + (Component ? : render(props))} + /> + +); + +export default AuthorizedRoute; diff --git a/service/stackweb/frontend/src/components/Authorized/CheckPermissions.tsx b/service/stackweb/frontend/src/components/Authorized/CheckPermissions.tsx new file mode 100644 index 0000000..ff35f60 --- /dev/null +++ b/service/stackweb/frontend/src/components/Authorized/CheckPermissions.tsx @@ -0,0 +1,83 @@ +import React from 'react'; +import { CURRENT } from './renderAuthorize'; +// eslint-disable-next-line import/no-cycle +import PromiseRender from './PromiseRender'; + +export type IAuthorityType = + | undefined + | string + | string[] + | Promise + | ((currentAuthority: string | string[]) => IAuthorityType); + +/** + * 通用权限检查方法 + * Common check permissions method + * @param { 权限判定 | Permission judgment } authority + * @param { 你的权限 | Your permission description } currentAuthority + * @param { 通过的组件 | Passing components } target + * @param { 未通过的组件 | no pass components } Exception + */ +const checkPermissions = ( + authority: IAuthorityType, + currentAuthority: string | string[], + target: T, + Exception: K, +): T | K | React.ReactNode => { + // 没有判定权限.默认查看所有 + // Retirement authority, return target; + if (!authority) { + return target; + } + // 数组处理 + if (Array.isArray(authority)) { + if (Array.isArray(currentAuthority)) { + if (currentAuthority.some((item) => authority.includes(item))) { + return target; + } + } else if (authority.includes(currentAuthority)) { + return target; + } + return Exception; + } + // string 处理 + if (typeof authority === 'string') { + if (Array.isArray(currentAuthority)) { + if (currentAuthority.some((item) => authority === item)) { + return target; + } + } else if (authority === currentAuthority) { + return target; + } + return Exception; + } + // Promise 处理 + if (authority instanceof Promise) { + return ok={target} error={Exception} promise={authority} />; + } + // Function 处理 + if (typeof authority === 'function') { + try { + const bool = authority(currentAuthority); + // 函数执行后返回值是 Promise + if (bool instanceof Promise) { + return ok={target} error={Exception} promise={bool} />; + } + if (bool) { + return target; + } + return Exception; + } catch (error) { + throw error; + } + } + throw new Error('unsupported parameters'); +}; + +export { checkPermissions }; + +function check(authority: IAuthorityType, target: T, Exception: K): T | K | React.ReactNode { + return checkPermissions(authority, CURRENT, target, Exception); +} + +export default check; diff --git a/service/stackweb/frontend/src/components/Authorized/PromiseRender.tsx b/service/stackweb/frontend/src/components/Authorized/PromiseRender.tsx new file mode 100644 index 0000000..25f2597 --- /dev/null +++ b/service/stackweb/frontend/src/components/Authorized/PromiseRender.tsx @@ -0,0 +1,93 @@ +import React from 'react'; +import { Spin } from 'antd'; +import isEqual from 'lodash/isEqual'; +import { isComponentClass } from './Secured'; +// eslint-disable-next-line import/no-cycle + +interface PromiseRenderProps { + ok: T; + error: K; + promise: Promise; +} + +interface PromiseRenderState { + component: React.ComponentClass | React.FunctionComponent; +} + +export default class PromiseRender extends React.Component< + PromiseRenderProps, + PromiseRenderState +> { + state: PromiseRenderState = { + component: () => null, + }; + + componentDidMount() { + this.setRenderComponent(this.props); + } + + shouldComponentUpdate = (nextProps: PromiseRenderProps, nextState: PromiseRenderState) => { + const { component } = this.state; + if (!isEqual(nextProps, this.props)) { + this.setRenderComponent(nextProps); + } + if (nextState.component !== component) return true; + return false; + }; + + // set render Component : ok or error + setRenderComponent(props: PromiseRenderProps) { + const ok = this.checkIsInstantiation(props.ok); + const error = this.checkIsInstantiation(props.error); + props.promise + .then(() => { + this.setState({ + component: ok, + }); + return true; + }) + .catch(() => { + this.setState({ + component: error, + }); + }); + } + + // Determine whether the incoming component has been instantiated + // AuthorizedRoute is already instantiated + // Authorized render is already instantiated, children is no instantiated + // Secured is not instantiated + checkIsInstantiation = ( + target: React.ReactNode | React.ComponentClass, + ): React.FunctionComponent => { + if (isComponentClass(target)) { + const Target = target as React.ComponentClass; + return (props: any) => ; + } + if (React.isValidElement(target)) { + return (props: any) => React.cloneElement(target, props); + } + return () => target as React.ReactNode & null; + }; + + render() { + const { component: Component } = this.state; + const { ok, error, promise, ...rest } = this.props; + + return Component ? ( + + ) : ( +
+ +
+ ); + } +} diff --git a/service/stackweb/frontend/src/components/Authorized/Secured.tsx b/service/stackweb/frontend/src/components/Authorized/Secured.tsx new file mode 100644 index 0000000..0bdbbe4 --- /dev/null +++ b/service/stackweb/frontend/src/components/Authorized/Secured.tsx @@ -0,0 +1,66 @@ +import React from 'react'; +import CheckPermissions from './CheckPermissions'; + +/** + * 默认不能访问任何页面 + * default is "NULL" + */ +const Exception403 = () => 403; + +export const isComponentClass = (component: React.ComponentClass | React.ReactNode): boolean => { + if (!component) return false; + const proto = Object.getPrototypeOf(component); + if (proto === React.Component || proto === Function.prototype) return true; + return isComponentClass(proto); +}; + +// Determine whether the incoming component has been instantiated +// AuthorizedRoute is already instantiated +// Authorized render is already instantiated, children is no instantiated +// Secured is not instantiated +const checkIsInstantiation = (target: React.ComponentClass | React.ReactNode) => { + if (isComponentClass(target)) { + const Target = target as React.ComponentClass; + return (props: any) => ; + } + if (React.isValidElement(target)) { + return (props: any) => React.cloneElement(target, props); + } + return () => target; +}; + +/** + * 用于判断是否拥有权限访问此 view 权限 + * authority 支持传入 string, () => boolean | Promise + * e.g. 'user' 只有 user 用户能访问 + * e.g. 'user,admin' user 和 admin 都能访问 + * e.g. ()=>boolean 返回true能访问,返回false不能访问 + * e.g. Promise then 能访问 catch不能访问 + * e.g. authority support incoming string, () => boolean | Promise + * e.g. 'user' only user user can access + * e.g. 'user, admin' user and admin can access + * e.g. () => boolean true to be able to visit, return false can not be accessed + * e.g. Promise then can not access the visit to catch + * @param {string | function | Promise} authority + * @param {ReactNode} error 非必需参数 + */ +const authorize = (authority: string, error?: React.ReactNode) => { + /** + * conversion into a class + * 防止传入字符串时找不到staticContext造成报错 + * String parameters can cause staticContext not found error + */ + let classError: boolean | React.FunctionComponent = false; + if (error) { + classError = (() => error) as React.FunctionComponent; + } + if (!authority) { + throw new Error('authority is required'); + } + return function decideAuthority(target: React.ComponentClass | React.ReactNode) { + const component = CheckPermissions(authority, target, classError || Exception403); + return checkIsInstantiation(component); + }; +}; + +export default authorize; diff --git a/service/stackweb/frontend/src/components/Authorized/index.tsx b/service/stackweb/frontend/src/components/Authorized/index.tsx new file mode 100644 index 0000000..6703a46 --- /dev/null +++ b/service/stackweb/frontend/src/components/Authorized/index.tsx @@ -0,0 +1,11 @@ +import Authorized from './Authorized'; +import Secured from './Secured'; +import check from './CheckPermissions'; +import renderAuthorize from './renderAuthorize'; + +Authorized.Secured = Secured; +Authorized.check = check; + +const RenderAuthorize = renderAuthorize(Authorized); + +export default RenderAuthorize; diff --git a/service/stackweb/frontend/src/components/Authorized/renderAuthorize.ts b/service/stackweb/frontend/src/components/Authorized/renderAuthorize.ts new file mode 100644 index 0000000..df00875 --- /dev/null +++ b/service/stackweb/frontend/src/components/Authorized/renderAuthorize.ts @@ -0,0 +1,30 @@ +/* eslint-disable eslint-comments/disable-enable-pair */ +/* eslint-disable import/no-mutable-exports */ +let CURRENT: string | string[] = 'NULL'; + +type CurrentAuthorityType = string | string[] | (() => typeof CURRENT); +/** + * use authority or getAuthority + * @param {string|()=>String} currentAuthority + */ +const renderAuthorize = (Authorized: T): ((currentAuthority: CurrentAuthorityType) => T) => ( + currentAuthority: CurrentAuthorityType, +): T => { + if (currentAuthority) { + if (typeof currentAuthority === 'function') { + CURRENT = currentAuthority(); + } + if ( + Object.prototype.toString.call(currentAuthority) === '[object String]' || + Array.isArray(currentAuthority) + ) { + CURRENT = currentAuthority as string[]; + } + } else { + CURRENT = 'NULL'; + } + return Authorized; +}; + +export { CURRENT }; +export default (Authorized: T) => renderAuthorize(Authorized); diff --git a/service/stackweb/frontend/src/components/Forms/index.js b/service/stackweb/frontend/src/components/Forms/index.js new file mode 100644 index 0000000..7353ac8 --- /dev/null +++ b/service/stackweb/frontend/src/components/Forms/index.js @@ -0,0 +1,120 @@ +import React from 'react' +import { Form, Input, Select, Button } from 'antd' +import { FormInstance } from 'antd/lib/form'; + +const FormItem = Form.Item +const {TextArea } = Input + + +class Forms extends React.Component{ + + + constructor(props){ + super(props) + this.state = { + options:props.options, + validateMessage:null + } + this.ref = React.createRef(); + + } + componentWillReceiveProps(nextProps) { + if(nextProps.options!=this.state.options){ + + this.setState({ + options:this.state.options + }) + } + + } + + getFieldsValue=()=>{ + return this.ref.current.getFieldsValue() + } + + setFieldsValue=(name,value)=>{ + this.ref.current.setFieldsValue(name, value); + } + + validateFields=(callback)=>{ + //console.log(this.state.validateMessage) + //所有方法的验证状态都是异步 + setTimeout(()=>{ + if(this.state.validateMessage){ + callback && callback(this.state.validateMessage) + } + },100) + + } + + onFinish = (val)=>this.setState({validateMessage:val}) + onFinishFailed = (error)=>this.setState({validateMessage:error}) + + + + + render(){ + const {options, formOpt } = this.props + function creatFormItem(item,index){ + let {formType,name,label,rules,...otherProps} = item + switch (formType) { + case 'input' : + return ( + + + + ) + break; + case 'textarea' : + return ( + + + + + + + + ); +} + +export default connect( + ({ + loading, + callService, + }: { + loading: { effects: { [key: string]: boolean } }; + callService: CallState; + }) => ({ + callService, + loading: loading.effects['call/fetch'], + }), +)(Call); diff --git a/service/stackweb/frontend/src/pages/service/call/model.ts b/service/stackweb/frontend/src/pages/service/call/model.ts new file mode 100644 index 0000000..4abfd93 --- /dev/null +++ b/service/stackweb/frontend/src/pages/service/call/model.ts @@ -0,0 +1,80 @@ +import { Effect, Reducer, Subscription } from 'umi'; +import {Endpoint, Node, Service} from './data.d'; +import {callService, queryServices} from './service'; + +export interface CallState { + services: Service[]; + nodes: Node[]; + endpoints: Endpoint[]; + callResult: any +} + +export interface ModelType { + namespace: string; + state: CallState; + subscriptions: { setup: Subscription }; + effects: { + fetch: Effect; + callServicer: Effect; + }; + reducers: { + setState: Reducer; + }; +} + +const Model: ModelType = { + namespace: 'callService', + + state: { + services: [], + nodes: [], + endpoints: [], + callResult:{} + }, + + subscriptions: { + setup ({ dispatch, history }):void { + history.listen((location:any) => { + if(location.pathname === '/service/call-service'){ + dispatch({ + type: 'fetch', + payload: {}, + }); + } + }) + }, + }, + + effects: { + * fetch({payload}, {call, put}) { + const response = yield call(queryServices, payload); + const data = Array.isArray(response.data) ? response.data : []; + + const services: Service[] = data + + yield put({ + type: 'queryServices', + payload: {services}, + }); + }, + * callServicer({payload}, {call, put}) { + const response = yield call(callService, payload); + yield put({ + type: 'callService', + payload: {callResult:response.data}, + }); + }, + + }, + + reducers: { + setState(state,{payload}){ + return { + ...state, + ...payload + } + } + }, +}; + +export default Model; diff --git a/service/stackweb/frontend/src/pages/service/call/service.ts b/service/stackweb/frontend/src/pages/service/call/service.ts new file mode 100644 index 0000000..ed3c23b --- /dev/null +++ b/service/stackweb/frontend/src/pages/service/call/service.ts @@ -0,0 +1,24 @@ +import request from 'umi-request'; +import {Pagination, CallParam} from './data.d'; + +export async function queryServices(params: Pagination) { + const data = request('/platform/api/v1/b/services', { + method: 'POST', + data: { + ...params, + }, + }); + + return data; +} + +export async function callService(params: CallParam) { + const data = request('/platform/api/v1/b/rpc', { + method: 'POST', + data: { + ...params, + }, + }); + + return data; +} diff --git a/service/stackweb/frontend/src/pages/service/service-list/_mock.ts b/service/stackweb/frontend/src/pages/service/service-list/_mock.ts new file mode 100644 index 0000000..c5a3d56 --- /dev/null +++ b/service/stackweb/frontend/src/pages/service/service-list/_mock.ts @@ -0,0 +1,147 @@ +// eslint-disable-next-line import/no-extraneous-dependencies +import { Request, Response } from 'express'; +import { parse } from 'url'; +import { TableListItem, TableListParams } from './data.d'; + +// mock tableListDataSource +let tableListDataSource: TableListItem[] = []; + +for (let i = 0; i < 10; i += 1) { + tableListDataSource.push({ + key: i, + disabled: i % 6 === 0, + href: 'https://ant.design', + avatar: [ + 'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png', + 'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png', + ][i % 2], + name: `TradeCode ${i}`, + title: `一个任务名称 ${i}`, + owner: '曲丽丽', + desc: '这是一段描述', + callNo: Math.floor(Math.random() * 1000), + status: Math.floor(Math.random() * 10) % 4, + updatedAt: new Date(`2017-07-${Math.floor(i / 2) + 1}`), + createdAt: new Date(`2017-07-${Math.floor(i / 2) + 1}`), + progress: Math.ceil(Math.random() * 100), + }); +} + +function getRule(req: Request, res: Response, u: string) { + let url = u; + if (!url || Object.prototype.toString.call(url) !== '[object String]') { + // eslint-disable-next-line prefer-destructuring + url = req.url; + } + + const params = (parse(url, true).query as unknown) as TableListParams; + + let dataSource = tableListDataSource; + + if (params.sorter) { + const s = params.sorter.split('_'); + dataSource = dataSource.sort((prev, next) => { + if (s[1] === 'descend') { + return next[s[0]] - prev[s[0]]; + } + return prev[s[0]] - next[s[0]]; + }); + } + + if (params.status) { + const status = params.status.split(','); + let filterDataSource: TableListItem[] = []; + status.forEach((s: string) => { + filterDataSource = filterDataSource.concat( + dataSource.filter((item) => { + if (parseInt(`${item.status}`, 10) === parseInt(s.split('')[0], 10)) { + return true; + } + return false; + }), + ); + }); + dataSource = filterDataSource; + } + + if (params.name) { + dataSource = dataSource.filter((data) => data.name.includes(params.name || '')); + } + + let pageSize = 10; + if (params.pageSize) { + pageSize = parseInt(`${params.pageSize}`, 0); + } + + const result = { + data: dataSource, + total: dataSource.length, + success: true, + pageSize, + current: parseInt(`${params.currentPage}`, 10) || 1, + }; + + return res.json(result); +} + +function postRule(req: Request, res: Response, u: string, b: Request) { + let url = u; + if (!url || Object.prototype.toString.call(url) !== '[object String]') { + // eslint-disable-next-line prefer-destructuring + url = req.url; + } + + const body = (b && b.body) || req.body; + const { method, name, desc, key } = body; + + switch (method) { + /* eslint no-case-declarations:0 */ + case 'delete': + tableListDataSource = tableListDataSource.filter((item) => key.indexOf(item.key) === -1); + break; + case 'post': + const i = Math.ceil(Math.random() * 10000); + tableListDataSource.unshift({ + key: i, + href: 'https://ant.design', + avatar: [ + 'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png', + 'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png', + ][i % 2], + name: `TradeCode ${i}`, + title: `一个任务名称 ${i}`, + owner: '曲丽丽', + desc, + callNo: Math.floor(Math.random() * 1000), + status: Math.floor(Math.random() * 10) % 2, + updatedAt: new Date(), + createdAt: new Date(), + progress: Math.ceil(Math.random() * 100), + }); + break; + case 'update': + tableListDataSource = tableListDataSource.map((item) => { + if (item.key === key) { + return { ...item, desc, name }; + } + return item; + }); + break; + default: + break; + } + + const result = { + list: tableListDataSource, + pagination: { + total: tableListDataSource.length, + }, + }; + + return res.json(result); +} + +export default { + 'GET /api/rule': getRule, + 'POST /api/rule': postRule, +}; diff --git a/service/stackweb/frontend/src/pages/service/service-list/data.d.ts b/service/stackweb/frontend/src/pages/service/service-list/data.d.ts new file mode 100644 index 0000000..7744cb5 --- /dev/null +++ b/service/stackweb/frontend/src/pages/service/service-list/data.d.ts @@ -0,0 +1,27 @@ +export interface Service { + name: string; + version: string; + metadata: Map; + endpoints: Endpoint[]; + nodes: Node[]; +} + +export interface Endpoint {} + +export interface Node { + id: string; + address: string; + metadata: Map; +} + +export interface Pagination { + service: Service; + total: number; + pageSize: number; + current: number; +} + +export interface PageData { + data: Service[]; + pagination: Partial; +} diff --git a/service/stackweb/frontend/src/pages/service/service-list/index.tsx b/service/stackweb/frontend/src/pages/service/service-list/index.tsx new file mode 100644 index 0000000..252080c --- /dev/null +++ b/service/stackweb/frontend/src/pages/service/service-list/index.tsx @@ -0,0 +1,128 @@ +import React, { FC, useEffect } from 'react'; +import { Input, Button, Layout, Table, Space } from 'antd'; +import { PageHeaderWrapper } from '@ant-design/pro-layout'; +import { SearchOutlined } from '@ant-design/icons'; +import { connect, Dispatch } from 'umi'; +import { Service, Node } from './data.d'; +import { ServicesState } from '@/pages/service/service-list/model'; + +const { Header, Content } = Layout; + +interface ServicesProps { + dispatch: Dispatch; + searchServices: ServicesState; + loading: boolean; +} + +const Services: FC = ({ dispatch, searchServices: { list, filters }, loading }) => { + const onSearch = () => { + dispatch({ + type: 'searchServices/fetch', + payload: { + serviceStr: filters.service, + nodeStr: filters.node, + }, + }); + }; + + useEffect(() => { + onSearch(); + }, []); + + const appendCallURL = (service: string, address: string) => { + let url: string = `/service/call-service?service=${service}`; + if (address !== '') { + url += `&address=${address}`; + } + + return url; + }; + + const onServiceChange = (e: any) => { + const obj = filters; + obj.service = e.target.value; + }; + + const onNodeChange = (e: any) => { + const obj = filters; + obj.node = e.target.value; + }; + + const expandedRowRender = (row: any) => { + const columns = [ + { title: 'ID', dataIndex: 'id', key: 'id' }, + { + title: 'address', + dataIndex: 'address', + key: 'address', + }, + { + title: 'metadata', + dataIndex: 'metadata', + key: 'metadata', + }, + { + title: 'Action', + render: (node: Node) => { + const url = appendCallURL(node.id, node.address); + return Call; + }, + }, + ]; + + return ; + }; + + const columns = [ + { title: 'Name', dataIndex: 'name', key: 'name' }, + { title: 'Version', dataIndex: 'version', key: 'version' }, + { + title: 'Action', + render: (row: Service) => { + const url = appendCallURL(row.name, ''); + return Call; + }, + }, + ]; + + return ( +
+ +
+ + + + + +
+ + + rowKey={(row: Service) => { + return row.name; + }} + loading={list.length === 0 ? loading : false} + columns={columns} + expandable={{ expandedRowRender }} + dataSource={list} + pagination={false} + /> + +
+
+ ); +}; + +export default connect( + ({ + searchServices, + loading, + }: { + searchServices: ServicesState; + loading: { models: { [key: string]: boolean } }; + }) => ({ + searchServices, + loading: loading.models.listAndsearchAndarticles, + }), +)(Services); diff --git a/service/stackweb/frontend/src/pages/service/service-list/model.ts b/service/stackweb/frontend/src/pages/service/service-list/model.ts new file mode 100644 index 0000000..938bff1 --- /dev/null +++ b/service/stackweb/frontend/src/pages/service/service-list/model.ts @@ -0,0 +1,111 @@ +import {Effect, Reducer} from 'umi'; +import {Node, Service} from './data.d'; +import {queryServices} from './service'; + +export interface ServicesState { + list: Service[]; + filters: FiltersState; +} + +export interface FiltersState { + service: string; + node: string; +} + +export interface ModelType { + namespace: string; + state: ServicesState; + effects: { + fetch: Effect; + }; + reducers: { + queryList: Reducer; + }; +} + +const filterNode = (filter: string, nodes: Node[]) => { + const nodesTemp: Node[] = []; + if (filter != null) { + nodes.forEach((n: Node) => { + if ( + n.id.indexOf(filter) > 0 || + n.address.indexOf(filter) > 0 || + JSON.stringify(n.metadata).indexOf(filter) > 0 + ) { + nodesTemp.push(n); + } + }); + + return nodesTemp; + } + + return nodes; +}; + +const filterService = (filter: string, services: Service[]) => { + const servicesTemp: Service[] = []; + if (filter != null && filter !== '') { + services.forEach((item: Service) => { + if (item.name.indexOf(filter) > 0) { + servicesTemp.push(item); + } + }); + + return servicesTemp; + } + + return services; +}; + +const emptyArray = (arr: any[]) => { + while (arr.length > 0) { + arr.pop(); + } +}; + +// @ts-ignore +// @ts-ignore +const Model: ModelType = { + namespace: 'searchServices', + + state: { + list: [], + filters: { + service: "", + node: "", + }, + }, + + effects: { + * fetch({payload}, {call, put}) { + const response = yield call(queryServices, payload); + const data = Array.isArray(response.data) ? response.data : []; + // filter locally + const {serviceStr, nodeStr} = payload; + const services: Service[] = filterService(serviceStr, data); + + if (nodeStr != null && nodeStr !== '') { + services.forEach((service: Service) => { + emptyArray(service.nodes); + service.nodes.push(...filterNode(nodeStr, service.nodes)); + }); + } + + yield put({ + type: 'queryList', + payload: services, + }); + } + }, + + reducers: { + queryList(state, action) { + return { + ...(state as ServicesState), + list: action.payload, + }; + }, + }, +}; + +export default Model; diff --git a/service/stackweb/frontend/src/pages/service/service-list/service.ts b/service/stackweb/frontend/src/pages/service/service-list/service.ts new file mode 100644 index 0000000..067a919 --- /dev/null +++ b/service/stackweb/frontend/src/pages/service/service-list/service.ts @@ -0,0 +1,13 @@ +import request from 'umi-request'; +import { Pagination } from './data.d'; + +export async function queryServices(params: Pagination) { + const data = request('/platform/api/v1/b/services', { + method: 'POST', + data: { + ...params, + }, + }); + + return data; +} diff --git a/service/stackweb/frontend/src/pages/user/login/_mock.ts b/service/stackweb/frontend/src/pages/user/login/_mock.ts new file mode 100644 index 0000000..a34c87f --- /dev/null +++ b/service/stackweb/frontend/src/pages/user/login/_mock.ts @@ -0,0 +1,42 @@ +// eslint-disable-next-line import/no-extraneous-dependencies +import { Request, Response } from 'express'; + +function getFakeCaptcha(req: Request, res: Response) { + return res.json('captcha-xxx'); +} + +export default { + 'POST /api/login/account': (req: Request, res: Response) => { + const { password, userName, type } = req.body; + if (password === 'ant.design' && userName === 'admin') { + res.send({ + status: 'ok', + type, + currentAuthority: 'admin', + }); + return; + } + if (password === 'ant.design' && userName === 'user') { + res.send({ + status: 'ok', + type, + currentAuthority: 'user', + }); + return; + } + if (type === 'mobile') { + res.send({ + status: 'ok', + type, + currentAuthority: 'admin', + }); + return; + } + res.send({ + status: 'error', + type, + currentAuthority: 'guest', + }); + }, + 'GET /api/login/captcha': getFakeCaptcha, +}; diff --git a/service/stackweb/frontend/src/pages/user/login/components/Login/LoginContext.tsx b/service/stackweb/frontend/src/pages/user/login/components/Login/LoginContext.tsx new file mode 100644 index 0000000..ae571e0 --- /dev/null +++ b/service/stackweb/frontend/src/pages/user/login/components/Login/LoginContext.tsx @@ -0,0 +1,13 @@ +import { createContext } from 'react'; + +export interface LoginContextProps { + tabUtil?: { + addTab: (id: string) => void; + removeTab: (id: string) => void; + }; + updateActive?: (activeItem: { [key: string]: string } | string) => void; +} + +const LoginContext: React.Context = createContext({}); + +export default LoginContext; diff --git a/service/stackweb/frontend/src/pages/user/login/components/Login/LoginItem.tsx b/service/stackweb/frontend/src/pages/user/login/components/Login/LoginItem.tsx new file mode 100644 index 0000000..28f51d9 --- /dev/null +++ b/service/stackweb/frontend/src/pages/user/login/components/Login/LoginItem.tsx @@ -0,0 +1,169 @@ +import { Button, Col, Input, Row, Form, message } from 'antd'; +import React, { useState, useCallback, useEffect } from 'react'; + +import omit from 'omit.js'; +import { FormItemProps } from 'antd/es/form/FormItem'; +import ItemMap from './map'; +import LoginContext, { LoginContextProps } from './LoginContext'; +import styles from './index.less'; +import { getFakeCaptcha } from '../../service'; + +export type WrappedLoginItemProps = LoginItemProps; +export type LoginItemKeyType = keyof typeof ItemMap; +export interface LoginItemType { + UserName: React.FC; + Password: React.FC; + Mobile: React.FC; + Captcha: React.FC; +} + +export interface LoginItemProps extends Partial { + name?: string; + style?: React.CSSProperties; + placeholder?: string; + buttonText?: React.ReactNode; + countDown?: number; + getCaptchaButtonText?: string; + getCaptchaSecondText?: string; + updateActive?: LoginContextProps['updateActive']; + type?: string; + defaultValue?: string; + customProps?: { [key: string]: unknown }; + onChange?: (e: React.ChangeEvent) => void; + tabUtil?: LoginContextProps['tabUtil']; +} + +const FormItem = Form.Item; + +const getFormItemOptions = ({ + onChange, + defaultValue, + customProps = {}, + rules, +}: LoginItemProps) => { + const options: { + rules?: LoginItemProps['rules']; + onChange?: LoginItemProps['onChange']; + initialValue?: LoginItemProps['defaultValue']; + } = { + rules: rules || (customProps.rules as LoginItemProps['rules']), + }; + if (onChange) { + options.onChange = onChange; + } + if (defaultValue) { + options.initialValue = defaultValue; + } + return options; +}; + +const LoginItem: React.FC = (props) => { + const [count, setCount] = useState(props.countDown || 0); + const [timing, setTiming] = useState(false); + // 这么写是为了防止restProps中 带入 onChange, defaultValue, rules props tabUtil + const { + onChange, + customProps, + defaultValue, + rules, + name, + getCaptchaButtonText, + getCaptchaSecondText, + updateActive, + type, + tabUtil, + ...restProps + } = props; + + const onGetCaptcha = useCallback(async (mobile: string) => { + const result = await getFakeCaptcha(mobile); + if (result === false) { + return; + } + message.success('获取验证码成功!验证码为:1234'); + setTiming(true); + }, []); + + useEffect(() => { + let interval: number = 0; + const { countDown } = props; + if (timing) { + interval = window.setInterval(() => { + setCount((preSecond) => { + if (preSecond <= 1) { + setTiming(false); + clearInterval(interval); + // 重置秒数 + return countDown || 60; + } + return preSecond - 1; + }); + }, 1000); + } + return () => clearInterval(interval); + }, [timing]); + if (!name) { + return null; + } + // get getFieldDecorator props + const options = getFormItemOptions(props); + const otherProps = restProps || {}; + + if (type === 'Captcha') { + const inputProps = omit(otherProps, ['onGetCaptcha', 'countDown']); + + return ( + + {({ getFieldValue }) => ( + + + + + + + + + + + )} + + ); + } + return ( + + + + ); +}; + +const LoginItems: Partial = {}; + +Object.keys(ItemMap).forEach((key) => { + const item = ItemMap[key]; + LoginItems[key] = (props: LoginItemProps) => ( + + {(context) => ( + + )} + + ); +}); + +export default LoginItems as LoginItemType; diff --git a/service/stackweb/frontend/src/pages/user/login/components/Login/LoginSubmit.tsx b/service/stackweb/frontend/src/pages/user/login/components/Login/LoginSubmit.tsx new file mode 100644 index 0000000..280fb0f --- /dev/null +++ b/service/stackweb/frontend/src/pages/user/login/components/Login/LoginSubmit.tsx @@ -0,0 +1,23 @@ +import { Button, Form } from 'antd'; + +import { ButtonProps } from 'antd/es/button'; +import React from 'react'; +import classNames from 'classnames'; +import styles from './index.less'; + +const FormItem = Form.Item; + +interface LoginSubmitProps extends ButtonProps { + className?: string; +} + +const LoginSubmit: React.FC = ({ className, ...rest }) => { + const clsString = classNames(styles.submit, className); + return ( + + + + + + + +); + +const RegisterResult: React.FC = ({ location }) => ( + + + + } + subTitle={formatMessage({ id: 'userandregister-result.register-result.activation-email' })} + extra={actions} + /> +); + +export default RegisterResult; diff --git a/service/stackweb/frontend/src/pages/user/register-result/locales/en-US.ts b/service/stackweb/frontend/src/pages/user/register-result/locales/en-US.ts new file mode 100644 index 0000000..ed75e97 --- /dev/null +++ b/service/stackweb/frontend/src/pages/user/register-result/locales/en-US.ts @@ -0,0 +1,23 @@ +export default { + 'userandregister-result.login.userName': 'userName', + 'userandregister-result.login.password': 'password', + 'userandregister-result.login.message-invalid-credentials': + 'Invalid username or password(admin/ant.design)', + 'userandregister-result.login.message-invalid-verification-code': 'Invalid verification code', + 'userandregister-result.login.tab-login-credentials': 'Credentials', + 'userandregister-result.login.tab-login-mobile': 'Mobile number', + 'userandregister-result.login.remember-me': 'Remember me', + 'userandregister-result.login.forgot-password': 'Forgot your password?', + 'userandregister-result.login.sign-in-with': 'Sign in with', + 'userandregister-result.login.signup': 'Sign up', + 'userandregister-result.login.login': 'Login', + 'userandregister-result.register.register': 'Register', + 'userandregister-result.register.get-verification-code': 'Get code', + 'userandregister-result.register.sign-in': 'Already have an account?', + 'userandregister-result.register-result.msg': 'Account:registered at {email}', + 'userandregister-result.register-result.activation-email': + 'The activation email has been sent to your email address and is valid for 24 hours. Please log in to the email in time and click on the link in the email to activate the account.', + 'userandregister-result.register-result.back-home': 'Back to home', + 'userandregister-result.register-result.view-mailbox': 'View mailbox', + 'userandregister-result.navBar.lang': 'Languages', +}; diff --git a/service/stackweb/frontend/src/pages/user/register-result/locales/zh-CN.ts b/service/stackweb/frontend/src/pages/user/register-result/locales/zh-CN.ts new file mode 100644 index 0000000..eb77363 --- /dev/null +++ b/service/stackweb/frontend/src/pages/user/register-result/locales/zh-CN.ts @@ -0,0 +1,22 @@ +export default { + 'userandregister-result.login.userName': '用户名', + 'userandregister-result.login.password': '密码', + 'userandregister-result.login.message-invalid-credentials': '账户或密码错误(admin/ant.design)', + 'userandregister-result.login.message-invalid-verification-code': '验证码错误', + 'userandregister-result.login.tab-login-credentials': '账户密码登录', + 'userandregister-result.login.tab-login-mobile': '手机号登录', + 'userandregister-result.login.remember-me': '自动登录', + 'userandregister-result.login.forgot-password': '忘记密码', + 'userandregister-result.login.sign-in-with': '其他登录方式', + 'userandregister-result.login.signup': '注册账户', + 'userandregister-result.login.login': '登录', + 'userandregister-result.register.register': '注册', + 'userandregister-result.register.get-verification-code': '获取验证码', + 'userandregister-result.register.sign-in': '使用已有账户登录', + 'userandregister-result.register-result.msg': '你的账户:{email} 注册成功', + 'userandregister-result.register-result.activation-email': + '激活邮件已发送到你的邮箱中,邮件有效期为24小时。请及时登录邮箱,点击邮件中的链接激活帐户。', + 'userandregister-result.register-result.back-home': '返回首页', + 'userandregister-result.register-result.view-mailbox': '查看邮箱', + 'userandregister-result.navBar.lang': '语言', +}; diff --git a/service/stackweb/frontend/src/pages/user/register-result/locales/zh-TW.ts b/service/stackweb/frontend/src/pages/user/register-result/locales/zh-TW.ts new file mode 100644 index 0000000..2a2c502 --- /dev/null +++ b/service/stackweb/frontend/src/pages/user/register-result/locales/zh-TW.ts @@ -0,0 +1,22 @@ +export default { + 'userandregister-result.login.userName': '賬戶', + 'userandregister-result.login.password': '密碼', + 'userandregister-result.login.message-invalid-credentials': '賬戶或密碼錯誤(admin/ant.design)', + 'userandregister-result.login.message-invalid-verification-code': '驗證碼錯誤', + 'userandregister-result.login.tab-login-credentials': '賬戶密碼登錄', + 'userandregister-result.login.tab-login-mobile': '手機號登錄', + 'userandregister-result.login.remember-me': '自動登錄', + 'userandregister-result.login.forgot-password': '忘記密碼', + 'userandregister-result.login.sign-in-with': '其他登錄方式', + 'userandregister-result.login.signup': '註冊賬戶', + 'userandregister-result.login.login': '登錄', + 'userandregister-result.register.register': '註冊', + 'userandregister-result.register.get-verification-code': '獲取驗證碼', + 'userandregister-result.register.sign-in': '使用已有賬戶登錄', + 'userandregister-result.register-result.msg': '妳的賬戶:{email} 註冊成功', + 'userandregister-result.register-result.activation-email': + '激活郵件已發送到妳的郵箱中,郵件有效期為24小時。請及時登錄郵箱,點擊郵件中的鏈接激活帳戶。', + 'userandregister-result.register-result.back-home': '返回首頁', + 'userandregister-result.register-result.view-mailbox': '查看郵箱', + 'userandregister-result.navBar.lang': '語言', +}; diff --git a/service/stackweb/frontend/src/pages/user/register-result/style.less b/service/stackweb/frontend/src/pages/user/register-result/style.less new file mode 100644 index 0000000..dc1b890 --- /dev/null +++ b/service/stackweb/frontend/src/pages/user/register-result/style.less @@ -0,0 +1,23 @@ +.registerResult { + width: 800px; + min-height: 400px; + margin: auto; + padding: 80px; + background: none; + :global { + .anticon { + font-size: 64px; + } + } + .title { + margin-top: 32px; + font-size: 20px; + line-height: 28px; + } + .actions { + margin-top: 40px; + a + a { + margin-left: 8px; + } + } +} diff --git a/service/stackweb/frontend/src/pages/user/register/_mock.ts b/service/stackweb/frontend/src/pages/user/register/_mock.ts new file mode 100644 index 0000000..1519ba8 --- /dev/null +++ b/service/stackweb/frontend/src/pages/user/register/_mock.ts @@ -0,0 +1,8 @@ +// eslint-disable-next-line import/no-extraneous-dependencies +import { Request, Response } from 'express'; + +export default { + 'POST /api/register': (_: Request, res: Response) => { + res.send({ status: 'ok', currentAuthority: 'user' }); + }, +}; diff --git a/service/stackweb/frontend/src/pages/user/register/index.tsx b/service/stackweb/frontend/src/pages/user/register/index.tsx new file mode 100644 index 0000000..aedac4e --- /dev/null +++ b/service/stackweb/frontend/src/pages/user/register/index.tsx @@ -0,0 +1,332 @@ +import { Form, Button, Col, Input, Popover, Progress, Row, Select, message } from 'antd'; +import React, { FC, useState, useEffect } from 'react'; +import { Link, connect, router, FormattedMessage, formatMessage, Dispatch } from 'umi'; + +import { StateType } from './model'; +import styles from './style.less'; + +const FormItem = Form.Item; +const { Option } = Select; +const InputGroup = Input.Group; + +const passwordStatusMap = { + ok: ( +
+ +
+ ), + pass: ( +
+ +
+ ), + poor: ( +
+ +
+ ), +}; + +const passwordProgressMap: { + ok: 'success'; + pass: 'normal'; + poor: 'exception'; +} = { + ok: 'success', + pass: 'normal', + poor: 'exception', +}; + +interface RegisterProps { + dispatch: Dispatch; + userAndregister: StateType; + submitting: boolean; +} + +export interface UserRegisterParams { + mail: string; + password: string; + confirm: string; + mobile: string; + captcha: string; + prefix: string; +} + +const Register: FC = ({ submitting, dispatch, userAndregister }) => { + const [count, setcount]: [number, any] = useState(0); + const [visible, setvisible]: [boolean, any] = useState(false); + const [prefix, setprefix]: [string, any] = useState('86'); + const [popover, setpopover]: [boolean, any] = useState(false); + const confirmDirty = false; + let interval: number | undefined; + const [form] = Form.useForm(); + useEffect(() => { + if (!userAndregister) { + return; + } + const account = form.getFieldValue('mail'); + if (userAndregister.status === 'ok') { + message.success('注册成功!'); + router.push({ + pathname: '/user/register-result', + state: { + account, + }, + }); + } + }, [userAndregister]); + useEffect( + () => () => { + clearInterval(interval); + }, + [], + ); + const onGetCaptcha = () => { + let counts = 59; + setcount(counts); + interval = window.setInterval(() => { + counts -= 1; + setcount(counts); + if (counts === 0) { + clearInterval(interval); + } + }, 1000); + }; + const getPasswordStatus = () => { + const value = form.getFieldValue('password'); + if (value && value.length > 9) { + return 'ok'; + } + if (value && value.length > 5) { + return 'pass'; + } + return 'poor'; + }; + const onFinish = (values: { [key: string]: any }) => { + dispatch({ + type: 'userAndregister/submit', + payload: { + ...values, + prefix, + }, + }); + }; + const checkConfirm = (_: any, value: string) => { + const promise = Promise; + if (value && value !== form.getFieldValue('password')) { + return promise.reject(formatMessage({ id: 'userandregister.password.twice' })); + } + return promise.resolve(); + }; + const checkPassword = (_: any, value: string) => { + const promise = Promise; + // 没有值的情况 + if (!value) { + setvisible(!!value); + return promise.reject(formatMessage({ id: 'userandregister.password.required' })); + } + // 有值的情况 + if (!visible) { + setvisible(!!value); + } + setpopover(!popover); + if (value.length < 6) { + return promise.reject(''); + } + if (value && confirmDirty) { + form.validateFields(['confirm']); + } + return promise.resolve(); + }; + const changePrefix = (value: string) => { + setprefix(value); + }; + const renderPasswordProgress = () => { + const value = form.getFieldValue('password'); + const passwordStatus = getPasswordStatus(); + return value && value.length ? ( +
+ 100 ? 100 : value.length * 10} + showInfo={false} + /> +
+ ) : null; + }; + + return ( +
+

+ +

+
+ + + + { + if (node && node.parentNode) { + return node.parentNode as HTMLElement; + } + return node; + }} + content={ + visible && ( +
+ {passwordStatusMap[getPasswordStatus()]} + {renderPasswordProgress()} +
+ +
+
+ ) + } + overlayStyle={{ width: 240 }} + placement="right" + visible={visible} + > + 0 && + styles.password + } + rules={[ + { + validator: checkPassword, + }, + ]} + > + + +
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + ); +}; +export default connect( + ({ + userAndregister, + loading, + }: { + userAndregister: StateType; + loading: { + effects: { + [key: string]: boolean; + }; + }; + }) => ({ + userAndregister, + submitting: loading.effects['userAndregister/submit'], + }), +)(Register); diff --git a/service/stackweb/frontend/src/pages/user/register/locales/en-US.ts b/service/stackweb/frontend/src/pages/user/register/locales/en-US.ts new file mode 100644 index 0000000..c01fa43 --- /dev/null +++ b/service/stackweb/frontend/src/pages/user/register/locales/en-US.ts @@ -0,0 +1,78 @@ +export default { + 'userandregister.login.userName': 'userName', + 'userandregister.login.password': 'password', + 'userandregister.login.message-invalid-credentials': + 'Invalid username or password(admin/ant.design)', + 'userandregister.login.message-invalid-verification-code': 'Invalid verification code', + 'userandregister.login.tab-login-credentials': 'Credentials', + 'userandregister.login.tab-login-mobile': 'Mobile number', + 'userandregister.login.remember-me': 'Remember me', + 'userandregister.login.forgot-password': 'Forgot your password?', + 'userandregister.login.sign-in-with': 'Sign in with', + 'userandregister.login.signup': 'Sign up', + 'userandregister.login.login': 'Login', + 'userandregister.register.register': 'Register', + 'userandregister.register.get-verification-code': 'Get code', + 'userandregister.register.sign-in': 'Already have an account?', + 'userandregister.register-result.msg': 'Account:registered at {email}', + 'userandregister.register-result.activation-email': + 'The activation email has been sent to your email address and is valid for 24 hours. Please log in to the email in time and click on the link in the email to activate the account.', + 'userandregister.register-result.back-home': 'Back to home', + 'userandregister.register-result.view-mailbox': 'View mailbox', + 'userandregister.email.required': 'Please enter your email!', + 'userandregister.email.wrong-format': 'The email address is in the wrong format!', + 'userandregister.userName.required': 'Please enter your userName!', + 'userandregister.password.required': 'Please enter your password!', + 'userandregister.password.twice': 'The passwords entered twice do not match!', + 'userandregister.strength.msg': + "Please enter at least 6 characters and don't use passwords that are easy to guess.", + 'userandregister.strength.strong': 'Strength: strong', + 'userandregister.strength.medium': 'Strength: medium', + 'userandregister.strength.short': 'Strength: too short', + 'userandregister.confirm-password.required': 'Please confirm your password!', + 'userandregister.phone-number.required': 'Please enter your phone number!', + 'userandregister.phone-number.wrong-format': 'Malformed phone number!', + 'userandregister.verification-code.required': 'Please enter the verification code!', + 'userandregister.title.required': 'Please enter a title', + 'userandregister.date.required': 'Please select the start and end date', + 'userandregister.goal.required': 'Please enter a description of the goal', + 'userandregister.standard.required': 'Please enter a metric', + 'userandregister.form.get-captcha': 'Get Captcha', + 'userandregister.captcha.second': 'sec', + 'userandregister.form.optional': ' (optional) ', + 'userandregister.form.submit': 'Submit', + 'userandregister.form.save': 'Save', + 'userandregister.email.placeholder': 'Email', + 'userandregister.password.placeholder': 'Password', + 'userandregister.confirm-password.placeholder': 'Confirm password', + 'userandregister.phone-number.placeholder': 'Phone number', + 'userandregister.verification-code.placeholder': 'Verification code', + 'userandregister.title.label': 'Title', + 'userandregister.title.placeholder': 'Give the target a name', + 'userandregister.date.label': 'Start and end date', + 'userandregister.placeholder.start': 'Start date', + 'userandregister.placeholder.end': 'End date', + 'userandregister.goal.label': 'Goal description', + 'userandregister.goal.placeholder': 'Please enter your work goals', + 'userandregister.standard.label': 'Metrics', + 'userandregister.standard.placeholder': 'Please enter a metric', + 'userandregister.client.label': 'Client', + 'userandregister.label.tooltip': 'Target service object', + 'userandregister.client.placeholder': + 'Please describe your customer service, internal customers directly @ Name / job number', + 'userandregister.invites.label': 'Inviting critics', + 'userandregister.invites.placeholder': + 'Please direct @ Name / job number, you can invite up to 5 people', + 'userandregister.weight.label': 'Weight', + 'userandregister.weight.placeholder': 'Please enter weight', + 'userandregister.public.label': 'Target disclosure', + 'userandregister.label.help': 'Customers and invitees are shared by default', + 'userandregister.radio.public': 'Public', + 'userandregister.radio.partially-public': 'Partially public', + 'userandregister.radio.private': 'Private', + 'userandregister.publicUsers.placeholder': 'Open to', + 'userandregister.option.A': 'Colleague A', + 'userandregister.option.B': 'Colleague B', + 'userandregister.option.C': 'Colleague C', + 'userandregister.navBar.lang': 'Languages', +}; diff --git a/service/stackweb/frontend/src/pages/user/register/locales/zh-CN.ts b/service/stackweb/frontend/src/pages/user/register/locales/zh-CN.ts new file mode 100644 index 0000000..0e0e4b0 --- /dev/null +++ b/service/stackweb/frontend/src/pages/user/register/locales/zh-CN.ts @@ -0,0 +1,74 @@ +export default { + 'userandregister.login.userName': '用户名', + 'userandregister.login.password': '密码', + 'userandregister.login.message-invalid-credentials': '账户或密码错误(admin/ant.design)', + 'userandregister.login.message-invalid-verification-code': '验证码错误', + 'userandregister.login.tab-login-credentials': '账户密码登录', + 'userandregister.login.tab-login-mobile': '手机号登录', + 'userandregister.login.remember-me': '自动登录', + 'userandregister.login.forgot-password': '忘记密码', + 'userandregister.login.sign-in-with': '其他登录方式', + 'userandregister.login.signup': '注册账户', + 'userandregister.login.login': '登录', + 'userandregister.register.register': '注册', + 'userandregister.register.get-verification-code': '获取验证码', + 'userandregister.register.sign-in': '使用已有账户登录', + 'userandregister.register-result.msg': '你的账户:{email} 注册成功', + 'userandregister.register-result.activation-email': + '激活邮件已发送到你的邮箱中,邮件有效期为24小时。请及时登录邮箱,点击邮件中的链接激活帐户。', + 'userandregister.register-result.back-home': '返回首页', + 'userandregister.register-result.view-mailbox': '查看邮箱', + 'userandregister.email.required': '请输入邮箱地址!', + 'userandregister.email.wrong-format': '邮箱地址格式错误!', + 'userandregister.userName.required': '请输入用户名!', + 'userandregister.password.required': '请输入密码!', + 'userandregister.password.twice': '两次输入的密码不匹配!', + 'userandregister.strength.msg': '请至少输入 6 个字符。请不要使用容易被猜到的密码。', + 'userandregister.strength.strong': '强度:强', + 'userandregister.strength.medium': '强度:中', + 'userandregister.strength.short': '强度:太短', + 'userandregister.confirm-password.required': '请确认密码!', + 'userandregister.phone-number.required': '请输入手机号!', + 'userandregister.phone-number.wrong-format': '手机号格式错误!', + 'userandregister.verification-code.required': '请输入验证码!', + 'userandregister.title.required': '请输入标题', + 'userandregister.date.required': '请选择起止日期', + 'userandregister.goal.required': '请输入目标描述', + 'userandregister.standard.required': '请输入衡量标准', + 'userandregister.form.get-captcha': '获取验证码', + 'userandregister.captcha.second': '秒', + 'userandregister.form.optional': '(选填)', + 'userandregister.form.submit': '提交', + 'userandregister.form.save': '保存', + 'userandregister.email.placeholder': '邮箱', + 'userandregister.password.placeholder': '至少6位密码,区分大小写', + 'userandregister.confirm-password.placeholder': '确认密码', + 'userandregister.phone-number.placeholder': '手机号', + 'userandregister.verification-code.placeholder': '验证码', + 'userandregister.title.label': '标题', + 'userandregister.title.placeholder': '给目标起个名字', + 'userandregister.date.label': '起止日期', + 'userandregister.placeholder.start': '开始日期', + 'userandregister.placeholder.end': '结束日期', + 'userandregister.goal.label': '目标描述', + 'userandregister.goal.placeholder': '请输入你的阶段性工作目标', + 'userandregister.standard.label': '衡量标准', + 'userandregister.standard.placeholder': '请输入衡量标准', + 'userandregister.client.label': '客户', + 'userandregister.label.tooltip': '目标的服务对象', + 'userandregister.client.placeholder': '请描述你服务的客户,内部客户直接 @姓名/工号', + 'userandregister.invites.label': '邀评人', + 'userandregister.invites.placeholder': '请直接 @姓名/工号,最多可邀请 5 人', + 'userandregister.weight.label': '权重', + 'userandregister.weight.placeholder': '请输入', + 'userandregister.public.label': '目标公开', + 'userandregister.label.help': '客户、邀评人默认被分享', + 'userandregister.radio.public': '公开', + 'userandregister.radio.partially-public': '部分公开', + 'userandregister.radio.private': '不公开', + 'userandregister.publicUsers.placeholder': '公开给', + 'userandregister.option.A': '同事甲', + 'userandregister.option.B': '同事乙', + 'userandregister.option.C': '同事丙', + 'userandregister.navBar.lang': '语言', +}; diff --git a/service/stackweb/frontend/src/pages/user/register/locales/zh-TW.ts b/service/stackweb/frontend/src/pages/user/register/locales/zh-TW.ts new file mode 100644 index 0000000..54094ca --- /dev/null +++ b/service/stackweb/frontend/src/pages/user/register/locales/zh-TW.ts @@ -0,0 +1,74 @@ +export default { + 'userandregister.login.userName': '賬戶', + 'userandregister.login.password': '密碼', + 'userandregister.login.message-invalid-credentials': '賬戶或密碼錯誤(admin/ant.design)', + 'userandregister.login.message-invalid-verification-code': '驗證碼錯誤', + 'userandregister.login.tab-login-credentials': '賬戶密碼登錄', + 'userandregister.login.tab-login-mobile': '手機號登錄', + 'userandregister.login.remember-me': '自動登錄', + 'userandregister.login.forgot-password': '忘記密碼', + 'userandregister.login.sign-in-with': '其他登錄方式', + 'userandregister.login.signup': '註冊賬戶', + 'userandregister.login.login': '登錄', + 'userandregister.register.register': '註冊', + 'userandregister.register.get-verification-code': '獲取驗證碼', + 'userandregister.register.sign-in': '使用已有賬戶登錄', + 'userandregister.register-result.msg': '妳的賬戶:{email} 註冊成功', + 'userandregister.register-result.activation-email': + '激活郵件已發送到妳的郵箱中,郵件有效期為24小時。請及時登錄郵箱,點擊郵件中的鏈接激活帳戶。', + 'userandregister.register-result.back-home': '返回首頁', + 'userandregister.register-result.view-mailbox': '查看郵箱', + 'userandregister.email.required': '請輸入郵箱地址!', + 'userandregister.email.wrong-format': '郵箱地址格式錯誤!', + 'userandregister.userName.required': '請輸入賬戶!', + 'userandregister.password.required': '請輸入密碼!', + 'userandregister.password.twice': '兩次輸入的密碼不匹配!', + 'userandregister.strength.msg': '請至少輸入 6 個字符。請不要使用容易被猜到的密碼。', + 'userandregister.strength.strong': '強度:強', + 'userandregister.strength.medium': '強度:中', + 'userandregister.strength.short': '強度:太短', + 'userandregister.confirm-password.required': '請確認密碼!', + 'userandregister.phone-number.required': '請輸入手機號!', + 'userandregister.phone-number.wrong-format': '手機號格式錯誤!', + 'userandregister.verification-code.required': '請輸入驗證碼!', + 'userandregister.title.required': '請輸入標題', + 'userandregister.date.required': '請選擇起止日期', + 'userandregister.goal.required': '請輸入目標描述', + 'userandregister.standard.required': '請輸入衡量標淮', + 'userandregister.form.get-captcha': '獲取驗證碼', + 'userandregister.captcha.second': '秒', + 'userandregister.form.optional': '(選填)', + 'userandregister.form.submit': '提交', + 'userandregister.form.save': '保存', + 'userandregister.email.placeholder': '郵箱', + 'userandregister.password.placeholder': '至少6位密碼,區分大小寫', + 'userandregister.confirm-password.placeholder': '確認密碼', + 'userandregister.phone-number.placeholder': '手機號', + 'userandregister.verification-code.placeholder': '驗證碼', + 'userandregister.title.label': '標題', + 'userandregister.title.placeholder': '給目標起個名字', + 'userandregister.date.label': '起止日期', + 'userandregister.placeholder.start': '開始日期', + 'userandregister.placeholder.end': '結束日期', + 'userandregister.goal.label': '目標描述', + 'userandregister.goal.placeholder': '請輸入妳的階段性工作目標', + 'userandregister.standard.label': '衡量標淮', + 'userandregister.standard.placeholder': '請輸入衡量標淮', + 'userandregister.client.label': '客戶', + 'userandregister.label.tooltip': '目標的服務對象', + 'userandregister.client.placeholder': '請描述妳服務的客戶,內部客戶直接 @姓名/工號', + 'userandregister.invites.label': '邀評人', + 'userandregister.invites.placeholder': '請直接 @姓名/工號,最多可邀請 5 人', + 'userandregister.weight.label': '權重', + 'userandregister.weight.placeholder': '請輸入', + 'userandregister.public.label': '目標公開', + 'userandregister.label.help': '客戶、邀評人默認被分享', + 'userandregister.radio.public': '公開', + 'userandregister.radio.partially-public': '部分公開', + 'userandregister.radio.private': '不公開', + 'userandregister.publicUsers.placeholder': '公開給', + 'userandregister.option.A': '同事甲', + 'userandregister.option.B': '同事乙', + 'userandregister.option.C': '同事丙', + 'userandregister.navBar.lang': '語言', +}; diff --git a/service/stackweb/frontend/src/pages/user/register/model.ts b/service/stackweb/frontend/src/pages/user/register/model.ts new file mode 100644 index 0000000..3fe5592 --- /dev/null +++ b/service/stackweb/frontend/src/pages/user/register/model.ts @@ -0,0 +1,48 @@ +import { Effect, Reducer } from 'umi'; + +import { fakeRegister } from './service'; + +export interface StateType { + status?: 'ok' | 'error'; + currentAuthority?: 'user' | 'guest' | 'admin'; +} + +export interface ModelType { + namespace: string; + state: StateType; + effects: { + submit: Effect; + }; + reducers: { + registerHandle: Reducer; + }; +} + +const Model: ModelType = { + namespace: 'userAndregister', + + state: { + status: undefined, + }, + + effects: { + *submit({ payload }, { call, put }) { + const response = yield call(fakeRegister, payload); + yield put({ + type: 'registerHandle', + payload: response, + }); + }, + }, + + reducers: { + registerHandle(state, { payload }) { + return { + ...state, + status: payload.status, + }; + }, + }, +}; + +export default Model; diff --git a/service/stackweb/frontend/src/pages/user/register/service.ts b/service/stackweb/frontend/src/pages/user/register/service.ts new file mode 100644 index 0000000..2f8d011 --- /dev/null +++ b/service/stackweb/frontend/src/pages/user/register/service.ts @@ -0,0 +1,9 @@ +import request from 'umi-request'; +import { UserRegisterParams } from './index'; + +export async function fakeRegister(params: UserRegisterParams) { + return request('/api/register', { + method: 'POST', + data: params, + }); +} diff --git a/service/stackweb/frontend/src/pages/user/register/style.less b/service/stackweb/frontend/src/pages/user/register/style.less new file mode 100644 index 0000000..e13868f --- /dev/null +++ b/service/stackweb/frontend/src/pages/user/register/style.less @@ -0,0 +1,60 @@ +@import '~antd/es/style/themes/default.less'; + +.main { + width: 368px; + margin: 0 auto; + + h3 { + margin-bottom: 20px; + font-size: 16px; + } + + .password { + margin-bottom: 24px; + :global { + .ant-form-item-explain { + display: none; + } + } + } + + .getCaptcha { + display: block; + width: 100%; + } + + .submit { + width: 50%; + } + + .login { + float: right; + line-height: @btn-height-lg; + } +} + +.success, +.warning, +.error { + transition: color 0.3s; +} + +.success { + color: @success-color; +} + +.warning { + color: @warning-color; +} + +.error { + color: @error-color; +} + +.progress-pass > .progress { + :global { + .ant-progress-bg { + background-color: @warning-color; + } + } +} diff --git a/service/stackweb/frontend/src/service-worker.js b/service/stackweb/frontend/src/service-worker.js new file mode 100644 index 0000000..03b3d51 --- /dev/null +++ b/service/stackweb/frontend/src/service-worker.js @@ -0,0 +1,70 @@ +/* eslint-disable eslint-comments/disable-enable-pair */ +/* eslint-disable no-restricted-globals */ +/* eslint-disable no-underscore-dangle */ +/* globals workbox */ +workbox.core.setCacheNameDetails({ + prefix: 'antd-pro', + suffix: 'v1', +}); +// Control all opened tabs ASAP +workbox.clientsClaim(); + +/** + * Use precaching list generated by workbox in build process. + * https://developers.google.com/web/tools/workbox/reference-docs/latest/workbox.precaching + */ +workbox.precaching.precacheAndRoute(self.__precacheManifest || []); + +/** + * Register a navigation route. + * https://developers.google.com/web/tools/workbox/modules/workbox-routing#how_to_register_a_navigation_route + */ +workbox.routing.registerNavigationRoute('/index.html'); + +/** + * Use runtime cache: + * https://developers.google.com/web/tools/workbox/reference-docs/latest/workbox.routing#.registerRoute + * + * Workbox provides all common caching strategies including CacheFirst, NetworkFirst etc. + * https://developers.google.com/web/tools/workbox/reference-docs/latest/workbox.strategies + */ + +/** + * Handle API requests + */ +workbox.routing.registerRoute(/\/api\//, workbox.strategies.networkFirst()); + +/** + * Handle third party requests + */ +workbox.routing.registerRoute( + /^https:\/\/gw\.alipayobjects\.com\//, + workbox.strategies.networkFirst(), +); +workbox.routing.registerRoute( + /^https:\/\/cdnjs\.cloudflare\.com\//, + workbox.strategies.networkFirst(), +); +workbox.routing.registerRoute(/\/color.less/, workbox.strategies.networkFirst()); + +/** + * Response to client after skipping waiting with MessageChannel + */ +addEventListener('message', (event) => { + const replyPort = event.ports[0]; + const message = event.data; + if (replyPort && message && message.type === 'skip-waiting') { + event.waitUntil( + self.skipWaiting().then( + () => + replyPort.postMessage({ + error: null, + }), + (error) => + replyPort.postMessage({ + error, + }), + ), + ); + } +}); diff --git a/service/stackweb/frontend/src/services/login.ts b/service/stackweb/frontend/src/services/login.ts new file mode 100644 index 0000000..5c694df --- /dev/null +++ b/service/stackweb/frontend/src/services/login.ts @@ -0,0 +1,19 @@ +import request from '@/utils/request'; + +export interface LoginParamsType { + userName: string; + password: string; + mobile: string; + captcha: string; +} + +export async function fakeAccountLogin(params: LoginParamsType) { + return request('/api/login/account', { + method: 'POST', + data: params, + }); +} + +export async function getFakeCaptcha(mobile: string) { + return request(`/api/login/captcha?mobile=${mobile}`); +} diff --git a/service/stackweb/frontend/src/services/user.ts b/service/stackweb/frontend/src/services/user.ts new file mode 100644 index 0000000..1988721 --- /dev/null +++ b/service/stackweb/frontend/src/services/user.ts @@ -0,0 +1,13 @@ +import request from '@/utils/request'; + +export async function query(): Promise { + return request('/api/users'); +} + +export async function queryCurrent(): Promise { + return request('/api/currentUser'); +} + +export async function queryNotices(): Promise { + return request('/api/notices'); +} diff --git a/service/stackweb/frontend/src/typings.d.ts b/service/stackweb/frontend/src/typings.d.ts new file mode 100644 index 0000000..eb1d955 --- /dev/null +++ b/service/stackweb/frontend/src/typings.d.ts @@ -0,0 +1,38 @@ +declare module 'slash2'; +declare module '*.css'; +declare module '*.less'; +declare module '*.scss'; +declare module '*.sass'; +declare module '*.svg'; +declare module '*.png'; +declare module '*.jpg'; +declare module '*.jpeg'; +declare module '*.gif'; +declare module '*.bmp'; +declare module '*.tiff'; +declare module 'omit.js'; + +// google analytics interface +interface GAFieldsObject { + eventCategory: string; + eventAction: string; + eventLabel?: string; + eventValue?: number; + nonInteraction?: boolean; +} +interface Window { + ga: ( + command: 'send', + hitType: 'event' | 'pageview', + fieldsObject: GAFieldsObject | string, + ) => void; + reloadAuthorized: () => void; +} + +declare let ga: Function; + +// preview.pro.ant.design only do not use in your production ; +// preview.pro.ant.design 专用环境变量,请不要在你的项目中使用它。 +declare let ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION: 'site' | undefined; + +declare const REACT_APP_ENV: 'test' | 'dev' | 'pre' | false; diff --git a/service/stackweb/frontend/src/utils/Authorized.ts b/service/stackweb/frontend/src/utils/Authorized.ts new file mode 100644 index 0000000..5c78964 --- /dev/null +++ b/service/stackweb/frontend/src/utils/Authorized.ts @@ -0,0 +1,19 @@ +import RenderAuthorize from '@/components/Authorized'; +import { getAuthority } from './authority'; +/* eslint-disable eslint-comments/disable-enable-pair */ +/* eslint-disable import/no-mutable-exports */ +let Authorized = RenderAuthorize(getAuthority()); + +// Reload the rights component +const reloadAuthorized = (): void => { + Authorized = RenderAuthorize(getAuthority()); +}; + +/** + * hard code + * block need it。 + */ +window.reloadAuthorized = reloadAuthorized; + +export { reloadAuthorized }; +export default Authorized; diff --git a/service/stackweb/frontend/src/utils/authority.ts b/service/stackweb/frontend/src/utils/authority.ts new file mode 100644 index 0000000..d99659d --- /dev/null +++ b/service/stackweb/frontend/src/utils/authority.ts @@ -0,0 +1,32 @@ +import { reloadAuthorized } from './Authorized'; + +// use localStorage to store the authority info, which might be sent from server in actual project. +export function getAuthority(str?: string): string | string[] { + const authorityString = + typeof str === 'undefined' && localStorage ? localStorage.getItem('antd-pro-authority') : str; + // authorityString could be admin, "admin", ["admin"] + let authority; + try { + if (authorityString) { + authority = JSON.parse(authorityString); + } + } catch (e) { + authority = authorityString; + } + if (typeof authority === 'string') { + return [authority]; + } + // preview.pro.ant.design only do not use in your production. + // preview.pro.ant.design 专用环境变量,请不要在你的项目中使用它。 + if (!authority && ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site') { + return ['admin']; + } + return authority; +} + +export function setAuthority(authority: string | string[]): void { + const proAuthority = typeof authority === 'string' ? [authority] : authority; + localStorage.setItem('antd-pro-authority', JSON.stringify(proAuthority)); + // auto reload + reloadAuthorized(); +} diff --git a/service/stackweb/frontend/src/utils/request.ts b/service/stackweb/frontend/src/utils/request.ts new file mode 100644 index 0000000..270dfad --- /dev/null +++ b/service/stackweb/frontend/src/utils/request.ts @@ -0,0 +1,56 @@ +/** + * request 网络请求工具 + * 更详细的 api 文档: https://github.com/umijs/umi-request + */ +import { extend } from 'umi-request'; +import { notification } from 'antd'; + +const codeMessage = { + 200: '服务器成功返回请求的数据。', + 201: '新建或修改数据成功。', + 202: '一个请求已经进入后台排队(异步任务)。', + 204: '删除数据成功。', + 400: '发出的请求有错误,服务器没有进行新建或修改数据的操作。', + 401: '用户没有权限(令牌、用户名、密码错误)。', + 403: '用户得到授权,但是访问是被禁止的。', + 404: '发出的请求针对的是不存在的记录,服务器没有进行操作。', + 406: '请求的格式不可得。', + 410: '请求的资源被永久删除,且不会再得到的。', + 422: '当创建一个对象时,发生一个验证错误。', + 500: '服务器发生错误,请检查服务器。', + 502: '网关错误。', + 503: '服务不可用,服务器暂时过载或维护。', + 504: '网关超时。', +}; + +/** + * 异常处理程序 + */ +const errorHandler = (error: { response: Response }): Response => { + const { response } = error; + if (response && response.status) { + const errorText = codeMessage[response.status] || response.statusText; + const { status, url } = response; + + notification.error({ + message: `请求错误 ${status}: ${url}`, + description: errorText, + }); + } else if (!response) { + notification.error({ + description: '您的网络发生异常,无法连接服务器', + message: '网络异常', + }); + } + return response; +}; + +/** + * 配置request请求时的默认参数 + */ +const request = extend({ + errorHandler, // 默认错误处理 + credentials: 'include', // 默认请求是否带上cookie +}); + +export default request; diff --git a/service/stackweb/frontend/src/utils/utils.less b/service/stackweb/frontend/src/utils/utils.less new file mode 100644 index 0000000..e9dfd5c --- /dev/null +++ b/service/stackweb/frontend/src/utils/utils.less @@ -0,0 +1,16 @@ +// mixins for clearfix +// ------------------------ +.clearfix() { + zoom: 1; + &::before, + &::after { + display: table; + content: ' '; + } + &::after { + clear: both; + height: 0; + font-size: 0; + visibility: hidden; + } +} diff --git a/service/stackweb/frontend/src/utils/utils.test.ts b/service/stackweb/frontend/src/utils/utils.test.ts new file mode 100644 index 0000000..ea84819 --- /dev/null +++ b/service/stackweb/frontend/src/utils/utils.test.ts @@ -0,0 +1,76 @@ +import { isUrl, getRouteAuthority } from './utils'; + +describe('isUrl tests', (): void => { + it('should return false for invalid and corner case inputs', (): void => { + expect(isUrl([] as any)).toBeFalsy(); + expect(isUrl({} as any)).toBeFalsy(); + expect(isUrl(false as any)).toBeFalsy(); + expect(isUrl(true as any)).toBeFalsy(); + expect(isUrl(NaN as any)).toBeFalsy(); + expect(isUrl(null as any)).toBeFalsy(); + expect(isUrl(undefined as any)).toBeFalsy(); + expect(isUrl('')).toBeFalsy(); + }); + + it('should return false for invalid URLs', (): void => { + expect(isUrl('foo')).toBeFalsy(); + expect(isUrl('bar')).toBeFalsy(); + expect(isUrl('bar/test')).toBeFalsy(); + expect(isUrl('http:/example.com/')).toBeFalsy(); + expect(isUrl('ttp://example.com/')).toBeFalsy(); + }); + + it('should return true for valid URLs', (): void => { + expect(isUrl('http://example.com/')).toBeTruthy(); + expect(isUrl('https://example.com/')).toBeTruthy(); + expect(isUrl('http://example.com/test/123')).toBeTruthy(); + expect(isUrl('https://example.com/test/123')).toBeTruthy(); + expect(isUrl('http://example.com/test/123?foo=bar')).toBeTruthy(); + expect(isUrl('https://example.com/test/123?foo=bar')).toBeTruthy(); + expect(isUrl('http://www.example.com/')).toBeTruthy(); + expect(isUrl('https://www.example.com/')).toBeTruthy(); + expect(isUrl('http://www.example.com/test/123')).toBeTruthy(); + expect(isUrl('https://www.example.com/test/123')).toBeTruthy(); + expect(isUrl('http://www.example.com/test/123?foo=bar')).toBeTruthy(); + expect(isUrl('https://www.example.com/test/123?foo=bar')).toBeTruthy(); + }); +}); + +describe('getRouteAuthority tests', () => { + it('should return authority for each route', (): void => { + const routes = [ + { path: '/user', name: 'user', authority: ['user'], exact: true }, + { path: '/admin', name: 'admin', authority: ['admin'], exact: true }, + ]; + expect(getRouteAuthority('/user', routes)).toEqual(['user']); + expect(getRouteAuthority('/admin', routes)).toEqual(['admin']); + }); + + it('should return inherited authority for unconfigured route', (): void => { + const routes = [ + { path: '/nested', authority: ['admin', 'user'], exact: true }, + { path: '/nested/user', name: 'user', exact: true }, + ]; + expect(getRouteAuthority('/nested/user', routes)).toEqual(['admin', 'user']); + }); + + it('should return authority for configured route', (): void => { + const routes = [ + { path: '/nested', authority: ['admin', 'user'], exact: true }, + { path: '/nested/user', name: 'user', authority: ['user'], exact: true }, + { path: '/nested/admin', name: 'admin', authority: ['admin'], exact: true }, + ]; + expect(getRouteAuthority('/nested/user', routes)).toEqual(['user']); + expect(getRouteAuthority('/nested/admin', routes)).toEqual(['admin']); + }); + + it('should return authority for substring route', (): void => { + const routes = [ + { path: '/nested', authority: ['user', 'users'], exact: true }, + { path: '/nested/users', name: 'users', authority: ['users'], exact: true }, + { path: '/nested/user', name: 'user', authority: ['user'], exact: true }, + ]; + expect(getRouteAuthority('/nested/user', routes)).toEqual(['user']); + expect(getRouteAuthority('/nested/users', routes)).toEqual(['users']); + }); +}); diff --git a/service/stackweb/frontend/src/utils/utils.ts b/service/stackweb/frontend/src/utils/utils.ts new file mode 100644 index 0000000..e0afc31 --- /dev/null +++ b/service/stackweb/frontend/src/utils/utils.ts @@ -0,0 +1,65 @@ +import { parse } from 'querystring'; +import pathRegexp from 'path-to-regexp'; +import { Route } from '@/models/connect'; + +/* eslint no-useless-escape:0 import/prefer-default-export:0 */ +const reg = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/; + +export const isUrl = (path: string): boolean => reg.test(path); + +export const isAntDesignPro = (): boolean => { + if (ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site') { + return true; + } + return window.location.hostname === 'preview.pro.ant.design'; +}; + +// 给官方演示站点用,用于关闭真实开发环境不需要使用的特性 +export const isAntDesignProOrDev = (): boolean => { + const { NODE_ENV } = process.env; + if (NODE_ENV === 'development') { + return true; + } + return isAntDesignPro(); +}; + +export const getPageQuery = () => parse(window.location.href.split('?')[1]); + +/** + * props.route.routes + * @param router [{}] + * @param pathname string + */ +export const getAuthorityFromRouter = ( + router: T[] = [], + pathname: string, +): T | undefined => { + const authority = router.find( + ({ routes, path = '/' }) => + (path && pathRegexp(path).exec(pathname)) || + (routes && getAuthorityFromRouter(routes, pathname)), + ); + if (authority) return authority; + return undefined; +}; + +export const getRouteAuthority = (path: string, routeData: Route[]) => { + let authorities: string[] | string | undefined; + routeData.forEach((route) => { + // match prefix + if (pathRegexp(`${route.path}/(.*)`).test(`${path}/`)) { + if (route.authority) { + authorities = route.authority; + } + // exact match + if (route.path === path) { + authorities = route.authority || authorities; + } + // get children authority recursively + if (route.routes) { + authorities = getRouteAuthority(path, route.routes) || authorities; + } + } + }); + return authorities; +}; diff --git a/service/stackweb/frontend/test.md b/service/stackweb/frontend/test.md new file mode 100644 index 0000000..32a5326 --- /dev/null +++ b/service/stackweb/frontend/test.md @@ -0,0 +1,30 @@ +# 在线数据查询数据分类 + +数据主要有两大类:持久性与周期性 + +## 持久性数据 + +- 配置类(用户模型配置) + +## 周期性数据 + +- 用户标签 + +- + +## 存储 + +目前万象服务存储的主要有如下数据: + +活动数据 + - + +## 使用总结 + +- MySQL: 存储元数据 + - 配置类,如模型与模型 +- Redis: + - 用户缓存,如IMEI与标签映射,IMEI与是否黑白名单映射 +- ES: + - 用户统计数据:如用户基础信息{IMEI, SSOID, 所有标签} + diff --git a/service/stackweb/frontend/tests/run-tests.js b/service/stackweb/frontend/tests/run-tests.js new file mode 100644 index 0000000..3aa080d --- /dev/null +++ b/service/stackweb/frontend/tests/run-tests.js @@ -0,0 +1,52 @@ +/* eslint-disable eslint-comments/disable-enable-pair */ +/* eslint-disable @typescript-eslint/no-var-requires */ +/* eslint-disable eslint-comments/no-unlimited-disable */ +const { spawn } = require('child_process'); +// eslint-disable-next-line import/no-extraneous-dependencies +const { kill } = require('cross-port-killer'); + +const env = Object.create(process.env); +env.BROWSER = 'none'; +env.TEST = true; +env.UMI_UI = 'none'; +env.PROGRESS = 'none'; +// flag to prevent multiple test +let once = false; + +const startServer = spawn(/^win/.test(process.platform) ? 'npm.cmd' : 'npm', ['start'], { + env, +}); + +startServer.stderr.on('data', (data) => { + // eslint-disable-next-line + console.log(data.toString()); +}); + +startServer.on('exit', () => { + kill(process.env.PORT || 8000); +}); + +console.log('Starting development server for e2e tests...'); +startServer.stdout.on('data', (data) => { + console.log(data.toString()); + // hack code , wait umi + if ( + (!once && data.toString().indexOf('Compiled successfully') >= 0) || + data.toString().indexOf('Theme generated successfully') >= 0 + ) { + // eslint-disable-next-line + once = true; + console.log('Development server is started, ready to run tests.'); + const testCmd = spawn( + /^win/.test(process.platform) ? 'npm.cmd' : 'npm', + ['test', '--', '--maxWorkers=1', '--runInBand'], + { + stdio: 'inherit', + }, + ); + testCmd.on('exit', (code) => { + startServer.kill(); + process.exit(code); + }); + } +}); diff --git a/service/stackweb/frontend/tests/setupTests.js b/service/stackweb/frontend/tests/setupTests.js new file mode 100644 index 0000000..30e7dd1 --- /dev/null +++ b/service/stackweb/frontend/tests/setupTests.js @@ -0,0 +1,22 @@ +import 'jsdom-global/register'; + +// browserMocks.js +const localStorageMock = (() => { + let store = {}; + + return { + getItem(key) { + return store[key] || null; + }, + setItem(key, value) { + store[key] = value.toString(); + }, + clear() { + store = {}; + }, + }; +})(); + +Object.defineProperty(window, 'localStorage', { + value: localStorageMock, +}); diff --git a/service/stackweb/frontend/tsconfig.json b/service/stackweb/frontend/tsconfig.json new file mode 100644 index 0000000..d50aa7d --- /dev/null +++ b/service/stackweb/frontend/tsconfig.json @@ -0,0 +1,26 @@ +{ + "compilerOptions": { + "outDir": "build/dist", + "module": "esnext", + "target": "esnext", + "lib": ["esnext", "dom"], + "sourceMap": true, + "baseUrl": ".", + "jsx": "react", + "allowSyntheticDefaultImports": true, + "moduleResolution": "node", + "forceConsistentCasingInFileNames": true, + "noImplicitReturns": true, + "suppressImplicitAnyIndexErrors": true, + "noUnusedLocals": true, + "allowJs": true, + "skipLibCheck": true, + "experimentalDecorators": true, + "strict": true, + "paths": { + "@/*": ["./src/*"], + "@@/*": ["./src/.umi/*"] + } + }, + "exclude": ["node_modules", "build", "dist", "scripts", "src/.umi/*", "webpack", "jest"] +}