|
@@ -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()
|
|
|
}
|