/
eAddress.go
149 lines (124 loc) · 3.87 KB
/
eAddress.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"fmt"
"os"
"net/http"
"html/template"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
)
const (
MongoDBHosts = "dsxxxxxx.mongolab.com:xxxxxx"
AuthDatabase = "xxxxxx"
AuthUserName = "xxxxxxx"
AuthPassword = "xxxxxxxx"
TestDatabase = "xxxxxxxxxxx"
)
type AddressData struct{
name string `bson:"Name"`
Email string `bson:"Email"`
}
func main() {
http.HandleFunc("/", root)
http.HandleFunc("/display", display)
fmt.Println("listening...")
err := http.ListenAndServe(GetPort(), nil)
if err != nil {
panic(err)
}
}
// Get the Port from the environment
func GetPort() string {
var port = os.Getenv("PORT")
// Set a default port if there is nothing in the environment
if port == "" {
port = "8080"
fmt.Println("INFO: No PORT environment variable detected, defaulting to " + port)
}
return ":" + port
}
func root(res http.ResponseWriter, req *http.Request) {
fmt.Fprint(res, rootForm)
}
const rootForm=
`<!doctype html>
<html>
<head>
<style>
body {background-color:blue}
h1 {color:orange}
h2 {color: yellow}
p {color: white}
legend {color: yellow}
</style>
<title>TITLE - Fetch Some Data</title>
</head>
<body>
<h1>Description - A simple app to fetch data from a cloud based document database</h1>
<h2>Form Demo to retrieve e.mail address</h2>
<form>
<fieldset>
<legend>Enter the search criteria here, e.g. "Maxine":</legend>
<p>
<label>Name</label>
<form action="/display" method="post" accept-charset="utf-8" class="pure-form">
<input type="text" name="name" placeholder="name" />
<input type="submit" value=".. and query database!" formaction="/display"/>
</form>
</p>
</fielsset>
</form>
</body>
</html>`
var displayTemplate = template.Must(template.New("display").Parse(displayTemplateHTML))
func display(w http.ResponseWriter, r *http.Request) {
mongoDBDialInfo := &mgo.DialInfo{
Addrs: []string{MongoDBHosts},
Database: AuthDatabase,
Username: AuthUserName,
Password: AuthPassword,
}
mongoSession, err := mgo.DialWithInfo(mongoDBDialInfo)
if err != nil {
fmt.Println("error1")
}
mongoSession.SetMode(mgo.Monotonic, true)
collection := mongoSession.DB(TestDatabase).C("AddressData")
var result AddressData
err = collection.Find(bson.M{"Name": r.FormValue("name")}).One(&result)
if result.Email != "" {
errn := displayTemplate.Execute(w, "The email id you wanted is: " + result.Email)
if errn != nil {
http.Error(w, errn.Error(), http.StatusInternalServerError)
}
} else {
displayTemplate.Execute(w, "Sorry... The email id you wanted does not exist.")
}
}
const displayTemplateHTML =
`<!doctype html>
<html>
<head>
<style>
body {background-color:blue}
h1 {color:orange}
h2 {color: yellow}
p {color: white}
legend {color: yellow}
</style>
<title>TITLE - Fetch Some Data</title>
</head>
<body>
<h1>Description - A simple app to fetch data from a cloud based document database</h1>
<h2>Form Demo to retrieve e.mail address</h2>
<form>
<fieldset>
<legend>Enter the search criteria here, e.g. "Maxine":</legend>
<p>
<p><b>{{html .}}</b></p>
<p><a href="/">Start again!</a></p>
</p>
</fielsset>
</form>
</body>
</html>`