Skip to content

Commit

Permalink
Merge adc5ac1 into 7ce8803
Browse files Browse the repository at this point in the history
  • Loading branch information
acoshift committed Aug 22, 2018
2 parents 7ce8803 + adc5ac1 commit adc8aa1
Show file tree
Hide file tree
Showing 70 changed files with 15,751 additions and 9,753 deletions.
13 changes: 4 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/acoshift/flash v1.0.0
github.com/acoshift/go-firebase-admin v0.0.6
github.com/acoshift/header v0.0.0-20180524134832-fbb24d14e0c3
github.com/acoshift/hime v0.9.0
github.com/acoshift/hime v0.9.1
github.com/acoshift/methodmux v1.1.1
github.com/acoshift/middleware v0.4.1
github.com/acoshift/paginate v1.1.1
Expand All @@ -22,30 +22,25 @@ require (
github.com/disintegration/imaging v1.5.0
github.com/dustin/go-humanize v0.0.0-20180713052910-9f541cc9db5d
github.com/go-redis/redis v6.13.2+incompatible
github.com/golang/protobuf v1.1.0 // indirect
github.com/golang/mock v1.1.1 // indirect
github.com/google/pprof v0.0.0-20180818232756-e98137b3ea71 // indirect
github.com/googleapis/gax-go v2.0.0+incompatible // indirect
github.com/hpcloud/tail v1.0.0 // indirect
github.com/lib/pq v0.0.0-20180523175426-90697d60dd84
github.com/microcosm-cc/bluemonday v1.0.1
github.com/onsi/ginkgo v1.6.0
github.com/onsi/gomega v1.4.1
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday v2.0.0+incompatible
github.com/satori/go.uuid v1.2.1-0.20180103174451-36e9d2ebbde5
github.com/shurcooL/sanitized_anchor_name v0.0.0-20170918181015-86672fcb3f95 // indirect
github.com/stretchr/objx v0.1.1 // indirect
github.com/stretchr/testify v1.2.2
github.com/tdewolff/test v0.0.0-20171106182207-265427085153 // indirect
go.opencensus.io v0.15.0 // indirect
golang.org/x/build v0.0.0-20180822011048-8dcad780e313 // indirect
golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81 // indirect
golang.org/x/net v0.0.0-20180821023952-922f4815f713 // indirect
golang.org/x/oauth2 v0.0.0-20180724155351-3d292e4d0cdc // indirect
golang.org/x/sys v0.0.0-20180816055513-1c9583448a9c // indirect
golang.org/x/text v0.3.0 // indirect
google.golang.org/api v0.0.0-20180818000503-e21acd801f91
google.golang.org/appengine v1.1.0 // indirect
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8 // indirect
google.golang.org/grpc v1.14.0 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/fsnotify.v1 v1.4.7 // indirect
Expand Down
86 changes: 81 additions & 5 deletions go.sum

Large diffs are not rendered by default.

37 changes: 25 additions & 12 deletions internal/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,36 @@ import (
"net/http"
"runtime/debug"

"cloud.google.com/go/errorreporting"
"github.com/acoshift/header"
"github.com/acoshift/middleware"

"github.com/acoshift/acourse/context/appctx"
)

// ErrorRecovery recoveries error
func ErrorRecovery(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
log.Println(err)
debug.PrintStack()
}
}()
h.ServeHTTP(w, r)
})
// ErrorLogger logs error and send error page back to response
func ErrorLogger(errClient *errorreporting.Client) middleware.Middleware {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
log.Println(err)
debug.PrintStack()

if errClient != nil {
nerr, _ := err.(error)
errClient.Report(errorreporting.Entry{
Error: nerr,
Req: r,
Stack: debug.Stack(),
})
}
}
}()
h.ServeHTTP(w, r)
})
}
}

// SetHeaders sets default headers
Expand Down
20 changes: 17 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
_ "image/png"
"log"
"net/http"
"os"
"time"

"cloud.google.com/go/errorreporting"
"cloud.google.com/go/profiler"
"cloud.google.com/go/storage"
"github.com/acoshift/configfile"
Expand Down Expand Up @@ -53,8 +55,20 @@ func main() {

ctx := context.Background()

// init profiler
profiler.Start(profiler.Config{Service: config.StringDefault("profiler_service", "acourse")})
serviceName := config.StringDefault("service", "acourse")

runningProjectID := config.StringDefault("running_project", os.Getenv("GOOGLE_CLOUD_PROJECT"))

// init profiler, ignore error
profiler.Start(profiler.Config{Service: serviceName, ProjectID: runningProjectID})

// init error reporting, ignore error
errClient, _ := errorreporting.NewClient(ctx, runningProjectID, errorreporting.Config{
ServiceName: serviceName,
OnError: func(err error) {
log.Printf("could not log error: %v", err)
},
})

firApp, err := firebase.InitializeApp(ctx, firebase.AppOptions{
ProjectID: config.String("project_id"),
Expand Down Expand Up @@ -198,7 +212,7 @@ func main() {
)(m))

h := middleware.Chain(
internal.ErrorRecovery,
internal.ErrorLogger(errClient),
internal.SetHeaders,
middleware.CSRF(middleware.CSRFConfig{
Origins: []string{baseURL},
Expand Down
50 changes: 50 additions & 0 deletions vendor/cloud.google.com/go/errorreporting/apiv1beta1/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit adc8aa1

Please sign in to comment.