Browse Source

优化代码

fancl 2 years ago
parent
commit
228cae524a
2 changed files with 21 additions and 14 deletions
  1. 4 2
      gateway/cli/client.go
  2. 17 12
      helper/url/url.go

+ 4 - 2
gateway/cli/client.go

@@ -6,11 +6,13 @@ import (
 	"encoding/json"
 	"errors"
 	"fmt"
+	"git.nspix.com/golang/micro/helper/net/ip"
 	byte2 "git.nspix.com/golang/micro/helper/pool/byte"
 	"git.nspix.com/golang/micro/helper/unsafestr"
 	"github.com/peterh/liner"
 	"net"
 	"os"
+	"runtime"
 	"strings"
 	"time"
 )
@@ -62,8 +64,8 @@ func (client *Client) rdyLoop(c chan error) {
 				client.stdout("\u001B[0G")
 				client.appName = vs["name"]
 				client.stdout(fmt.Sprintf("Welcome to the %s monitor.", vs["name"]), EOL)
-				client.stdout(fmt.Sprintf("Server Version: %s, connection id is %s", vs["version"], vs["cid"]), EOL)
-				client.stdout(fmt.Sprintf("Last login: %s from %s", vs["login_at"], vs["client_addr"]), EOL)
+				client.stdout(fmt.Sprintf("Server Version: %s, os: %s, ip: %s", vs["version"], runtime.GOOS, ip.InternalIP()), EOL)
+				client.stdout(fmt.Sprintf("Last login: %s from %s@%s", vs["login_at"], vs["client_addr"], vs["cid"]), EOL)
 				client.stdout("Type 'help;' for help. Type 'cls' to clear the current input statement.", EOL)
 				select {
 				case client.readyChan <- struct{}{}:

+ 17 - 12
helper/url/url.go

@@ -8,9 +8,11 @@ import (
 )
 
 const (
-	AuthFormField     = "access_token"
-	AuthMethodField   = "auth_method"
-	AuthMethodGateway = "Sugo"
+	TokenFormLabel  = "access_token"
+	MethodFormLabel = "auth_method"
+	AuthGateway     = "Sugo"
+
+	DomainNameEnvVariable = "DOMAIN_NAME"
 )
 
 type (
@@ -21,7 +23,7 @@ type (
 		Fragment    string // fragment for references, without '#'
 		Params      map[string]string
 		EnableAuth  bool   //enable auth
-		AuthAppName string //auth app name
+		AuthName    string //auth app name
 		AuthUserID  string
 		AuthExpired time.Duration
 	}
@@ -31,7 +33,7 @@ type (
 func newOptions() *Options {
 	return &Options{
 		Schema: "https",
-		Host:   os.Getenv("DOMAIN_NAME"),
+		Host:   os.Getenv(DomainNameEnvVariable),
 		Params: make(map[string]string),
 	}
 }
@@ -45,7 +47,7 @@ func WithSchema(schema string) Option {
 func WithAuth(app string, uid string, expired time.Duration) Option {
 	return func(o *Options) {
 		o.EnableAuth = true
-		o.AuthAppName = app
+		o.AuthName = app
 		o.AuthUserID = uid
 		o.AuthExpired = expired
 	}
@@ -75,7 +77,7 @@ func WithParams(ps map[string]string) Option {
 	}
 }
 
-func Create(opts ...Option) string {
+func Build(opts ...Option) *url.URL {
 	o := newOptions()
 	for _, cb := range opts {
 		cb(o)
@@ -85,16 +87,19 @@ func Create(opts ...Option) string {
 		qs.Set(k, v)
 	}
 	if o.EnableAuth {
-		if token, err := tokenize.Encode(o.AuthAppName, o.AuthUserID, time.Now().Add(o.AuthExpired)); err == nil {
-			qs.Set(AuthFormField, token)
-			qs.Set(AuthMethodField, AuthMethodGateway)
+		if token, err := tokenize.Encode(o.AuthName, o.AuthUserID, time.Now().Add(o.AuthExpired)); err == nil {
+			qs.Set(TokenFormLabel, token)
+			qs.Set(MethodFormLabel, AuthGateway)
 		}
 	}
-	uri := url.URL{
+	return &url.URL{
 		Scheme:   o.Schema,
 		Host:     o.Host,
 		Path:     o.Path,
 		RawQuery: qs.Encode(),
 	}
-	return uri.String()
+}
+
+func Create(opts ...Option) string {
+	return Build(opts...).String()
 }