This repository has been archived by the owner on Oct 15, 2019. It is now read-only.
forked from seven5/seven5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.go
93 lines (83 loc) · 2.96 KB
/
deploy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package seven5
import (
"fmt"
"os"
"strconv"
"strings"
)
const (
REDIRECT_HOST_TEST = "http://localhost:%d"
HEROKU_HOST = "https://%s.herokuapp.com"
)
//HerokuDeploy is an implementation of DeploymentEnvironment that understands
//about Heroku and reads its configuration from environment variables.
type HerokuDeploy struct {
herokuName string
appName string
}
//NewHerokuDeploy returns a new HerokuDeploy object that implements DeploymentEnvironment.
//The first parameter is the _host_ name on heroku, typically like damp-sierra-7161.
//The second parameter is the application's name locally, like myproject.
func NewHerokuDeploy(herokuName string, appName string) *HerokuDeploy {
result := &HerokuDeploy{
herokuName: herokuName,
appName: appName,
}
return result
}
//GetQbsStore returns a Qbs store suitable for use with tihs application. The
//implementation uses GetDSNOrDie which ends up looking for the environment
//variable DATABASE_URL which must be set or we panic.
func (self *HerokuDeploy) GetQbsStore() *QbsStore {
dsn := GetDSNOrDie()
return NewQbsStoreFromDSN(dsn)
}
//IsTest returns true if the environment variable localname_TEST is set to
//a value that's not "".
func (self *HerokuDeploy) IsTest() bool {
t := os.Getenv(strings.ToUpper(self.appName) + "_TEST")
return t != ""
}
//Port reads the value of the environment variable PORT to get the value to return here. It
//will panic if the environment variable is not set or it's not a number.
func (self *HerokuDeploy) Port() int {
p := os.Getenv("PORT")
if p == "" {
panic("PORT not defined")
}
i, err := strconv.Atoi(p)
if err != nil {
panic(err)
}
return i
}
//RedirectHost is needed in cases where you are using oauth because this must sent to the
//"other side" of the handshake without any extra knowlege.
func (self *HerokuDeploy) RedirectHost() string {
if self.IsTest() {
return fmt.Sprintf(REDIRECT_HOST_TEST, self.Port())
}
return fmt.Sprintf(HEROKU_HOST, self.herokuName)
}
// Url returns the string that points to the application itself. Note that
// this will not have a / on the end.
func (self *HerokuDeploy) Url() string {
return "http://" + self.RedirectHost()
}
//DeploymentEnvironment encodes information that cannot be obtained from the source code but can only
//be determined from "outside" the application. This is here to provide a
//a vague hope of supporting deployments that are not based on environment
//variables.
type DeploymentEnvironment interface {
//GetQbsStore returns the correct flavor of QbsStore for the deployment
//environment, taking into account possible test settings.
GetQbsStore() *QbsStore
//IsTest returns true if this application is running on
//a local system, not deployed to remote system.
IsTest() bool
//Returns the port number for this application.
Port() int
//RedirectHost is needed in cases where you are using oauth because this must sent to the
//"other side" of the handshake without any extra knowlege.
RedirectHost() string
}