-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
executable file
·133 lines (111 loc) · 3.16 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"context"
"log"
"net/http"
"os"
"github.com/gorilla/mux"
"github.com/urfave/negroni"
"cloud.google.com/go/errorreporting"
"contrib.go.opencensus.io/exporter/stackdriver"
"contrib.go.opencensus.io/exporter/stackdriver/propagation"
"go.opencensus.io/plugin/ochttp"
"go.opencensus.io/trace"
)
func main() {
exporter, err := stackdriver.NewExporter(stackdriver.Options{ProjectID: gae_project()})
if err != nil {
log.Fatal(err)
}
trace.RegisterExporter(exporter)
defer exporter.Flush()
trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
r := mux.NewRouter()
// Frontend pages.
r.NotFoundHandler = r.NewRoute().BuildOnly().HandlerFunc(defaultHandler).GetHandler()
//-Frontend
// API
api_base := mux.NewRouter()
r.PathPrefix("/pdf/api/v1").Handler(negroni.New(
negroni.Wrap(api_base),
))
api_sub := api_base.PathPrefix("/pdf/api/v1").Subrouter()
api_sub.NotFoundHandler = r.NewRoute().BuildOnly().HandlerFunc(defaultHandler).GetHandler()
// PDF Merge
api_sub.HandleFunc("/pdf-merge", pdfMergeGETHandler).Methods("GET")
api_sub.HandleFunc("/pdf-merge", pdfMergePOSTHandler).Methods("POST")
//--
// PDF Merge from GCS
api_sub.HandleFunc("/pdf-merge-from-gcs", pdfMergeGCSGETHandler).Methods("GET")
api_sub.HandleFunc("/pdf-merge-from-gcs", pdfMergeGCSPOSTHandler).Methods("POST")
//--
//-API
//---
// Error reporting...
//---
errorClient, err := errorreporting.NewClient(context.Background(), gae_project(), errorreporting.Config{
ServiceName: gae_service(),
ServiceVersion: gae_version(),
OnError: func(err error) {
log.Printf("Could not log error: %v", err)
},
})
if err != nil {
log.Fatalf("%s", err)
}
defer errorClient.Close()
recover := negroni.NewRecovery()
recover.PrintStack = true
recover.LogStack = false
recover.PanicHandlerFunc = func(info *negroni.PanicInformation) {
errorClient.Report(errorreporting.Entry{
Req: info.Request,
Error: info.RecoveredPanic.(error),
Stack: info.Stack,
})
}
recover.Formatter = &CustomPanicFormatter{}
// Handle all HTTP requests with our router.
http.Handle("/", negroni.New(
recover,
negroni.Wrap(&ochttp.Handler{
Propagation: &propagation.HTTPFormat{},
Handler: r,
} ),
))
// [START setting_port]
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("Defaulting to port %s", port)
}
log.Printf("Listening on port %s", port)
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatal(err)
}
// [END setting_port]
}
func defaultHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Permission Denied", 403)
}
type CustomPanicFormatter struct{}
func (t *CustomPanicFormatter) FormatPanicError(w http.ResponseWriter, r *http.Request, infos *negroni.PanicInformation) {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
func gae_project() string {
return os.Getenv("GOOGLE_CLOUD_PROJECT")
}
func gae_service() string {
if svc := os.Getenv("GAE_SERVICE"); svc != "" {
return svc
} else {
return os.Getenv("K_SERVICE")
}
}
func gae_version() string {
if ver := os.Getenv("GAE_VERSION"); ver != "" {
return ver
} else {
return os.Getenv("K_REVISION")
}
}