forked from GoogleCloudPlatform/golang-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
103 lines (85 loc) · 2.67 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
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
// Copyright 2016 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
// Sample endpoints demonstrates a Cloud Endpoints API.
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"github.com/gorilla/mux"
"google.golang.org/appengine"
)
func main() {
r := mux.NewRouter()
r.Path("/echo").Methods("POST").
HandlerFunc(echoHandler)
r.Path("/auth/info/googlejwt").Methods("GET").
HandlerFunc(authInfoHandler)
r.Path("/auth/info/googleidtoken").Methods("GET").
HandlerFunc(authInfoHandler)
r.Path("/auth/info/firebase").Methods("GET", "OPTIONS").
Handler(corsHandler(authInfoHandler))
r.Path("/auth/info/auth0").Methods("GET").
HandlerFunc(authInfoHandler)
http.Handle("/", r)
appengine.Main()
}
// echoHandler reads a JSON object from the body, and writes it back out.
func echoHandler(w http.ResponseWriter, r *http.Request) {
var msg interface{}
if err := json.NewDecoder(r.Body).Decode(&msg); err != nil {
if _, ok := err.(*json.SyntaxError); ok {
errorf(w, http.StatusBadRequest, "Body was not valid JSON: %v", err)
return
}
errorf(w, http.StatusInternalServerError, "Could not get body: %v", err)
return
}
b, err := json.Marshal(msg)
if err != nil {
errorf(w, http.StatusInternalServerError, "Could not marshal JSON: %v", err)
return
}
w.Write(b)
}
// corsHandler wraps a HTTP handler and applies the appropriate responses for Cross-Origin Resource Sharing.
type corsHandler http.HandlerFunc
func (h corsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
if r.Method == "OPTIONS" {
w.Header().Set("Access-Control-Allow-Headers", "Authorization")
return
}
h(w, r)
}
// authInfoHandler reads authentication info provided by the Endpoints proxy.
func authInfoHandler(w http.ResponseWriter, r *http.Request) {
encodedInfo := r.Header.Get("X-Endpoint-API-UserInfo")
if encodedInfo == "" {
w.Write([]byte(`{"id": "anonymous"}`))
return
}
b, err := base64.StdEncoding.DecodeString(encodedInfo)
if err != nil {
errorf(w, http.StatusInternalServerError, "Could not decode auth info: %v", err)
return
}
w.Write(b)
}
// errorf writes a swagger-compliant error response.
func errorf(w http.ResponseWriter, code int, format string, a ...interface{}) {
var out struct {
Code int `json:"code"`
Message string `json:"message"`
}
out.Code = code
out.Message = fmt.Sprintf(format, a...)
b, err := json.Marshal(out)
if err != nil {
http.Error(w, `{"code": 500, "message": "Could not format JSON for original message."}`, 500)
return
}
http.Error(w, string(b), code)
}