Skip to content
This repository has been archived by the owner on Jul 31, 2024. It is now read-only.

Commit

Permalink
Merge pull request #30 from rogpeppe/032-httprelation-requirer
Browse files Browse the repository at this point in the history
charmbits/httprelation: add Requirer
  • Loading branch information
rogpeppe committed Dec 17, 2014
2 parents e53f928 + c0aeb03 commit 23d5024
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
40 changes: 40 additions & 0 deletions charmbits/httprelation/requirer.go
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
}
12 changes: 12 additions & 0 deletions charmbits/httpservice/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ func (svc *Service) setContext(ctxt *hook.Context) error {
return nil
}

// HTTPPort returns the currently configured HTTP port
// for the service.
func (svc *Service) HTTPPort() int {
return svc.http.HTTPPort()
}

// HTTPSPort returns the currently configured HTTPS port
// for the service.
func (svc *Service) HTTPSPort() int {
return svc.http.HTTPSPort()
}

func (svc *Service) changed() error {
httpPort := svc.http.HTTPPort()
httpsPort := svc.http.HTTPSPort()
Expand Down

0 comments on commit 23d5024

Please sign in to comment.