-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
51 lines (43 loc) · 1.35 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
package main
import (
"log"
"net/http"
"os"
"contrib.go.opencensus.io/exporter/stackdriver"
"github.com/gomodule/redigo/redis"
"github.com/sinmetal/gcpmetadata"
"go.opencensus.io/plugin/ochttp"
"go.opencensus.io/trace"
)
var redisPool *redis.Pool
func main() {
project, err := gcpmetadata.GetProjectID()
if err != nil {
log.Printf("ProjectID not found")
}
if project != "" {
exporter, err := stackdriver.NewExporter(stackdriver.Options{
ProjectID: project,
})
if err != nil {
panic(err)
}
trace.RegisterExporter(exporter)
trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
}
// redisが存在する時だけ動かす
if setupRedis() {
http.Handle("/increment", ochttp.WithRouteTag(func() http.Handler { return http.HandlerFunc(incrementHandler) }(), "/gp"))
}
http.Handle("/task/add", ochttp.WithRouteTag(func() http.Handler { return http.HandlerFunc(addTaskHandler) }(), "/gp"))
http.Handle("/task/process", ochttp.WithRouteTag(func() http.Handler { return http.HandlerFunc(processTaskHandler) }(), "/gp"))
http.Handle("/admin/hello", ochttp.WithRouteTag(func() http.Handler { return http.HandlerFunc(adminHandler) }(), "/gp"))
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Printf("Listening on port %s", port)
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatal(err)
}
}