|
@@ -0,0 +1,74 @@
|
|
|
+package url
|
|
|
+
|
|
|
+import (
|
|
|
+ "net/url"
|
|
|
+ "os"
|
|
|
+)
|
|
|
+
|
|
|
+type (
|
|
|
+ Options struct {
|
|
|
+ Schema string
|
|
|
+ Host string
|
|
|
+ Path string // encoded query values, without '?'
|
|
|
+ Fragment string // fragment for references, without '#'
|
|
|
+ Params map[string]string
|
|
|
+ }
|
|
|
+ Option func(o *Options)
|
|
|
+)
|
|
|
+
|
|
|
+func newOptions() *Options {
|
|
|
+ return &Options{
|
|
|
+ Schema: "https",
|
|
|
+ Host: os.Getenv("DOMAIN_NAME"),
|
|
|
+ Params: make(map[string]string),
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func WithSchema(schema string) Option {
|
|
|
+ return func(o *Options) {
|
|
|
+ o.Schema = schema
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func WithFragment(fragment string) Option {
|
|
|
+ return func(o *Options) {
|
|
|
+ o.Fragment = fragment
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func WithHost(host string) Option {
|
|
|
+ return func(o *Options) {
|
|
|
+ o.Host = host
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func WithPath(path string) Option {
|
|
|
+ return func(o *Options) {
|
|
|
+ o.Path = path
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func WithParams(ps map[string]string) Option {
|
|
|
+ return func(o *Options) {
|
|
|
+ o.Params = ps
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func Create(opts ...Option) string {
|
|
|
+ o := newOptions()
|
|
|
+ for _, cb := range opts {
|
|
|
+ cb(o)
|
|
|
+ }
|
|
|
+ qs := url.Values{}
|
|
|
+ for k, v := range o.Params {
|
|
|
+ qs.Set(k, v)
|
|
|
+ }
|
|
|
+ uri := url.URL{
|
|
|
+ Scheme: o.Schema,
|
|
|
+ Host: o.Host,
|
|
|
+ Path: o.Path,
|
|
|
+ ForceQuery: true,
|
|
|
+ RawQuery: qs.Encode(),
|
|
|
+ }
|
|
|
+ return uri.String()
|
|
|
+}
|