-
Notifications
You must be signed in to change notification settings - Fork 200
/
app.go
64 lines (52 loc) · 1.55 KB
/
app.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
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"os"
"github.com/Microsoft/ApplicationInsights-Go/appinsights"
)
type PageVars struct {
Message string
Language string
}
func main() {
client := appinsights.NewTelemetryClient(os.Getenv("APPINSIGHTS_INSTRUMENTATIONKEY"))
request := appinsights.NewRequestTelemetry("GET", "https://myapp.azurewebsites.net/", 1, "Success")
client.Track(request)
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("css"))))
http.Handle("/img/", http.StripPrefix("/img/", http.FileServer(http.Dir("img"))))
http.Handle("/fonts/", http.StripPrefix("/fonts/", http.FileServer(http.Dir("fonts"))))
http.HandleFunc("/", Home)
log.Fatal(http.ListenAndServe(getPort(), nil))
}
// Increment function increments the entered value by 1
func Increment(x int) (result int) {
return x + 1
}
func getPort() string {
p := os.Getenv("HTTP_PLATFORM_PORT")
if p != "" {
return ":" + p
}
return ":80"
}
func render(w http.ResponseWriter, tmpl string, pageVars PageVars) {
tmpl = fmt.Sprintf("views/%s", tmpl)
t, err := template.ParseFiles(tmpl)
if err != nil { // if there is an error
log.Print("template parsing error: ", err) // log it
}
err = t.Execute(w, pageVars) //execute the template and pass in the variables to fill the gaps
if err != nil { // if there is an error
log.Print("template executing error: ", err) //log it
}
}
func Home(w http.ResponseWriter, req *http.Request) {
pageVars := PageVars{
Message: "Success!",
Language: "Go Lang",
}
render(w, "index.html", pageVars)
}