github.com/blend/go-sdk@v1.20220411.3/webutil/url.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package webutil 9 10 import ( 11 "net/url" 12 "strings" 13 ) 14 15 // MustParseURL parses a url and panics if there is an error. 16 func MustParseURL(rawURL string) *url.URL { 17 u, err := url.Parse(rawURL) 18 if err != nil { 19 panic(err) 20 } 21 return u 22 } 23 24 // URLWithScheme returns a copy url with a given scheme. 25 func URLWithScheme(u *url.URL, scheme string) *url.URL { 26 copy := *u 27 copy.Scheme = scheme 28 return © 29 } 30 31 // URLWithHost returns a copy url with a given host. 32 func URLWithHost(u *url.URL, host string) *url.URL { 33 copy := *u 34 copy.Host = host 35 return © 36 } 37 38 // URLWithPort returns a copy url with a given pprt attached to the host. 39 func URLWithPort(u *url.URL, port string) *url.URL { 40 copy := *u 41 host := copy.Host 42 if strings.Contains(host, ":") { 43 parts := strings.SplitN(host, ":", 2) 44 copy.Host = parts[0] + ":" + port 45 } else { 46 copy.Host = host + ":" + port 47 } 48 return © 49 } 50 51 // URLWithPath returns a copy url with a given path. 52 func URLWithPath(u *url.URL, path string) *url.URL { 53 copy := *u 54 copy.Path = path 55 return © 56 } 57 58 // URLWithRawQuery returns a copy url with a given raw query. 59 func URLWithRawQuery(u *url.URL, rawQuery string) *url.URL { 60 copy := *u 61 copy.RawQuery = rawQuery 62 return © 63 } 64 65 // URLWithQuery returns a copy url with a given raw query. 66 func URLWithQuery(u *url.URL, key, value string) *url.URL { 67 copy := *u 68 queryValues := copy.Query() 69 queryValues.Add(key, value) 70 copy.RawQuery = queryValues.Encode() 71 return © 72 }