This repository has been archived by the owner on Jul 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from rogpeppe/032-httprelation-requirer
charmbits/httprelation: add Requirer
- Loading branch information
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package httprelation | ||
|
||
import ( | ||
"net" | ||
"strings" | ||
|
||
"github.com/juju/gocharm/charmbits/simplerelation" | ||
"github.com/juju/gocharm/hook" | ||
) | ||
|
||
// Requirer represents the requirer of an http relation. | ||
type Requirer struct { | ||
req simplerelation.Requirer | ||
} | ||
|
||
// Register registers everything necessary on r for | ||
// running the provider side of an http relation with | ||
// the given relation name. | ||
func (req *Requirer) Register(r *hook.Registry, relationName string) { | ||
req.req.Register(r, relationName, "http") | ||
} | ||
|
||
// URLs returns the URLs of all the provider units. | ||
func (req *Requirer) URLs() []string { | ||
return req.req.Strings(attrsToURL) | ||
} | ||
|
||
func attrsToURL(attrs map[string]string) (string, error) { | ||
host := attrs["hostname"] | ||
if host == "" { | ||
return "", nil | ||
} | ||
port := attrs["port"] | ||
if port == "" { | ||
port = "80" | ||
} | ||
hostPort := net.JoinHostPort(host, port) | ||
hostPort = strings.TrimSuffix(hostPort, ":80") | ||
return "http://" + hostPort, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters