-
Notifications
You must be signed in to change notification settings - Fork 534
/
Copy pathapp.go
78 lines (65 loc) · 1.96 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"os"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// PageVars : These values are used by consuming web pages.
type PageVars struct {
Message string
Language string
}
var requestsCounter = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "requests_counter_total",
Help: "Total Requests Made.",
},
)
func init() {
// Metrics have to be registered to be exposed:
prometheus.MustRegister(requestsCounter)
}
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)
http.Handle("/metrics", promhttp.Handler())
port := getPort()
log.Printf("listening on port %s", port)
log.Fatal(http.ListenAndServe(port, nil))
}
func getPort() string {
p := os.Getenv("HTTP_PLATFORM_PORT")
if p != "" {
return ":" + p
}
return ":8080"
}
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) {
requestsCounter.Inc()
pageVars := PageVars{
Message: "Success!",
Language: "Go Lang",
}
render(w, "index.html", pageVars)
log.Print("page rendering complete")
}