-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathfunction.go
More file actions
148 lines (133 loc) · 3.51 KB
/
function.go
File metadata and controls
148 lines (133 loc) · 3.51 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// Misc examples
// Author: kkraune
package vespasamples
import (
"cloud.google.com/go/storage"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
"html"
"io"
"net/http"
"os"
"time"
)
func init() {
functions.HTTP("helloVespaWorld", helloVespaWorld)
functions.HTTP("getPage", getPageSimple)
functions.HTTP("storePage", storePage)
}
func helloVespaWorld(w http.ResponseWriter, r *http.Request) {
var d struct {
Name string `json:"name"`
}
if err := json.NewDecoder(r.Body).Decode(&d); err != nil {
fmt.Fprint(w, "Hello, World!")
return
}
if d.Name == "" {
fmt.Fprint(w, "Hello, World!")
return
}
fmt.Fprintf(w, "Hello, %s!", html.EscapeString(d.Name))
}
func getPageSimple(w http.ResponseWriter, r *http.Request) {
url, _, _ := getParams(w, r)
if url == "" {
fmt.Fprint(w, "Input error - url missing")
return
}
response, err := http.Get(url)
if err != nil {
w.WriteHeader(http.StatusServiceUnavailable)
fmt.Fprintf(w, "Request error: %v", err)
return
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Body read error: %v", err)
return
}
fmt.Fprint(w, string(body))
}
func storePage(w http.ResponseWriter, r *http.Request) {
url, bucket, object := getParams(w, r)
if url == "" || bucket == "" || object == "" {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, "Input error - url or bucket or object missing")
return
}
bytes := getPageTLS(w, url)
err := writeDocument(bucket, object, bytes)
if err != nil {
fmt.Fprintf(w, "Store error: %v", err)
return
}
}
func getPageTLS(w http.ResponseWriter, url string) []byte {
// The credentials are stored as Secrets in Google Cloud, exposed as environment variables
certPem := []byte(os.Getenv("SEC_CERT"))
keyPem := []byte(os.Getenv("SEC_KEY"))
cert, err := tls.X509KeyPair(certPem, keyPem)
if err != nil {
fmt.Fprintf(w, "Cert error: %v", err)
return nil
}
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
Certificates: []tls.Certificate{cert},
InsecureSkipVerify: true,
},
},
}
response, err := client.Get(url)
if err != nil {
w.WriteHeader(http.StatusServiceUnavailable)
fmt.Fprintf(w, "Request error: %v", err)
return nil
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Body read error: %v", err)
return nil
}
return body
}
func writeDocument(bucket, object string, doc []byte) error {
ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
return fmt.Errorf("storage.NewClient: %v", err)
}
defer client.Close()
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
defer cancel()
obj := client.Bucket(bucket).Object(object)
wr := obj.NewWriter(ctx)
defer wr.Close()
if _, err := wr.Write(doc); err != nil {
return fmt.Errorf("Write error: %v", err)
}
return nil
}
func getParams(w http.ResponseWriter, r *http.Request) (string, string, string) {
var d struct {
Url string `json:"url"`
Bucket string `json:"bucket"`
Object string `json:"object"`
}
if err := json.NewDecoder(r.Body).Decode(&d); err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "JSON parse error: %v", err)
return "", "", ""
}
return d.Url, d.Bucket, d.Object
}