| 1 |
{ |
| 2 |
// A mixin on top of an InstanceBasedService that turns it into an HTTP and/or HTTPs service. |
| 3 |
// This is only really suitable for SingleInstance services because it does not configure load |
| 4 |
// balancing. For that, use HttpService3. |
| 5 |
HttpSingleInstance: { |
| 6 |
local service = self, |
| 7 |
|
| 8 |
// You must have either an httpPort or an httpsPort or both. |
| 9 |
assert service.httpPort != null || service.httpsPort != null, |
| 10 |
|
| 11 |
// HTTPs requires SSL certificates which need DNS. |
| 12 |
assert service.httpsPort == null || service.dnsZone != null : 'HTTPs requires DNS.', |
| 13 |
|
| 14 |
// Set this to null for an https only service. However that is not compatible with |
| 15 |
// health checking so may not be a good idea. |
| 16 |
httpPort:: 80, |
| 17 |
|
| 18 |
// Enable HTTPs here. Use importstr if your certificate and keys are files on disk. |
| 19 |
httpsPort:: null, |
| 20 |
sslCertificate:: error 'sslCertificate field was required but not present.', |
| 21 |
sslCertificateKey:: error 'sslCertificateKey field was required but not present.', |
| 22 |
|
| 23 |
// Ensure the firewall permits traffic to the ports. |
| 24 |
fwTcpPorts+: (if self.httpPort != null then [self.httpPort] else []) |
| 25 |
+ (if self.httpsPort != null then [self.httpsPort] else []), |
| 26 |
|
| 27 |
// A useful way to determine the URL where this service can be accessed. |
| 28 |
httpEndpoint:: |
| 29 |
if service.httpPort == 80 then |
| 30 |
'http://%s' % [service.addressRef] |
| 31 |
else |
| 32 |
'http://%s:%d' % [service.addressRef, service.httpPort], |
| 33 |
|
| 34 |
// A useful way to determine the URL where this service can be accessed. |
| 35 |
httpsEndpoint:: |
| 36 |
if service.httpsPort == 443 then |
| 37 |
'https://%s.%s' % [service.fullName, service.dnsZone.dnsName] |
| 38 |
else |
| 39 |
'https://%s.%s:%d' % [service.fullName, service.dnsZone.dnsName, service.httpsPort], |
| 40 |
|
| 41 |
outputs: { |
| 42 |
[if service.httpPort != null then service.prefixName('http-endpoint')]: |
| 43 |
service.httpEndpoint, |
| 44 |
[if service.httpsPort != null then service.prefixName('https-endpoint')]: |
| 45 |
service.httpsEndpoint, |
| 46 |
}, |
| 47 |
|
| 48 |
// Adding the http-server and https-server tags makes the instances compatible with GCE's |
| 49 |
// default firewall configuration, if that is being used. |
| 50 |
Instance+: { |
| 51 |
tags+: |
| 52 |
(if service.httpPort != null then ['http-server'] else []) |
| 53 |
+ (if service.httpsPort != null then ['https-server'] else []), |
| 54 |
}, |
| 55 |
}, |
| 56 |
|
| 57 |
|
| 58 |
// A mixin on top of a cluster3 service that turns it into an HTTP service. |
| 59 |
HttpService3: self.HttpSingleInstance { |
| 60 |
// Ensure the correct ports are forwarded in. |
| 61 |
lbTcpPorts+: (if self.httpPort != null then [self.httpPort] else []) |
| 62 |
+ (if self.httpsPort != null then [self.httpsPort] else []), |
| 63 |
|
| 64 |
// Prefer http port for health checking unless only https is available. |
| 65 |
httpHealthCheckPort: if self.httpPort != null then self.httpPort else self.httpsPort, |
| 66 |
}, |
| 67 |
|
| 68 |
} |