-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgetcapital.go
115 lines (97 loc) · 2.95 KB
/
getcapital.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
package main
import (
"fmt"
"html/template"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"log"
"net/http"
"os"
)
type CountryCapital struct {
Country string
Capital string
}
func main() {
http.HandleFunc("/", root)
http.HandleFunc("/upper", upper)
fmt.Println("listening...")
err := http.ListenAndServe(GetPort(), nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func root(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, rootForm)
}
const rootForm = `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/upper.css">
<title>Country Capital</title>
</head>
<body>
<h1>Country Capital</h1>
<p>The Country Capital Service will accept a name of a country from you and return you that country's capital. Have fun!</p>
<form action="/upper" method="post" accept-charset="utf-8">
<input type="text" name="str" value="Type a string..." id="str">
<input type="submit" value=".. and fetch capital!">
</form>
</body>
</html>
`
var upperTemplate = template.Must(template.New("upper").Parse(upperTemplateHTML))
func upper(w http.ResponseWriter, r *http.Request) {
// In the open command window set the following for Heroku:
// heroku config:set MONGOHQ_URL=mongodb://IndianGuru:[email protected]:10080/godata
uri := os.Getenv("MONGOHQ_URL")
if uri == "" {
fmt.Println("no connection string provided")
os.Exit(1)
}
sess, err := mgo.Dial(uri)
if err != nil {
fmt.Printf("Can't connect to mongo, go error %v\n", err)
os.Exit(1)
}
defer sess.Close()
sess.SetSafe(&mgo.Safe{})
collection := sess.DB("godata").C("capital")
result := CountryCapital{}
strEntered := r.FormValue("str")
err = collection.Find(bson.M{"country": strEntered}).One(&result)
if err != nil {
log.Fatal("Find: ", err)
}
err2 := upperTemplate.Execute(w, result.Capital)
if err2 != nil {
http.Error(w, err2.Error(), http.StatusInternalServerError)
}
}
const upperTemplateHTML = `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/upper.css">
<title>String Upper Results</title>
</head>
<body>
<h1>Country Capital</h1>
<p>The Capital of the Country that you had entered is:</p>
<pre>{{html .}}</pre>
</body>
</html>
`
// Get the Port from the environment so we can run on Heroku
func GetPort() string {
var port = os.Getenv("PORT")
// Set a default port if there is nothing in the environment
if port == "" {
port = "4747"
fmt.Println("INFO: No PORT environment variable detected, defaulting to " + port)
}
return ":" + port
}