This repository was archived by the owner on Dec 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
90 lines (80 loc) · 2.24 KB
/
main.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
package main
import (
"html/template"
"log"
"net/http"
"os"
"contrib.go.opencensus.io/exporter/stackdriver"
"contrib.go.opencensus.io/exporter/stackdriver/propagation"
"go.opencensus.io/plugin/ochttp"
"go.opencensus.io/trace"
)
var templates *template.Template
func main() {
var err error
templates, err = template.ParseFiles(
"templates/index.shtml",
"templates/header.shtml",
"templates/menu.shtml",
"templates/footer.shtml",
"templates/contact.shtml",
"templates/dev-info.shtml",
"templates/documentation.shtml",
"templates/download.shtml",
"templates/faq.shtml",
"templates/features.shtml",
"templates/related.shtml",
)
if err != nil {
log.Fatalln("template.ParseFiles:", err)
}
// set up tracing
exporter, err := stackdriver.NewExporter(stackdriver.Options{
ProjectID: os.Getenv("GOOGLE_CLOUD_PROJECT"),
})
if err != nil {
log.Fatalln(err)
}
trace.RegisterExporter(exporter)
trace.ApplyConfig(trace.Config{
DefaultSampler: trace.AlwaysSample(),
})
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("Defaulting to port %s", port)
}
handlers := []struct {
Prefix, Template, Title string
}{
{"/", "index.shtml", "Start page"},
{"/index.shtml", "index.shtml", "Start page"},
{"/contact.shtml", "contact.shtml", "Contact"},
{"/dev-info.shtml", "dev-info.shtml", "Development Information"},
{"/documentation.shtml", "documentation.shtml", "Documentation"},
{"/download.shtml", "download.shtml", "Download"},
{"/faq.shtml", "faq.shtml", "Frequently asked questions"},
{"/features.shtml", "features.shtml", "Features"},
{"/related.shtml", "related.shtml", "Related sites"},
}
for _, hndl := range handlers {
http.Handle(hndl.Prefix, &ochttp.Handler{
Propagation: &propagation.HTTPFormat{},
Handler: templateHandler{hndl.Template, hndl.Title},
IsPublicEndpoint: true,
})
}
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatalln("http.ListenAndServe:", err)
}
}
type templateHandler struct {
Name string
Title string
}
func (h templateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err := templates.ExecuteTemplate(w, h.Name, h); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}