-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
174 lines (147 loc) · 3.64 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strconv"
"time"
"github.com/asaskevich/govalidator"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/dyatlov/go-htmlinfo/htmlinfo"
"github.com/namsral/microdata"
"willnorris.com/go/microformats"
)
var (
body []byte
data interface{}
emptyAGW *events.APIGatewayProxyResponse
err error
isMock *bool
isOpenGraph bool
isOEmbed bool
isMicrodata bool
isMicroformats2 bool
resp *http.Response
statusCode int
u string
)
// The API Gateway handler
func Handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
statusCode = int(200)
emptyAGW = new(events.APIGatewayProxyResponse)
cacheFrom := time.Now().Format(http.TimeFormat)
cacheUntil := time.Now().AddDate(1, 0, 0).Format(http.TimeFormat)
if request.QueryStringParameters["url"] == "" {
return *emptyAGW, errors.New("The 'url' query string parameter is required.")
}
u = request.QueryStringParameters["url"]
if !govalidator.IsURL(u) {
return *emptyAGW, errors.New("This is not a valid URL.")
}
resp, err = http.Get(u)
if err != nil {
log.Fatal(err)
}
defer func() {
err = resp.Body.Close()
if err != nil {
log.Fatal(err)
}
}()
if isOpenGraph, err = strconv.ParseBool(os.Getenv("META_OPENGRAPH")); err != nil {
isOpenGraph = false
}
if isOEmbed, err = strconv.ParseBool(os.Getenv("META_OEMBED")); err != nil {
isOEmbed = false
}
if isMicrodata, err = strconv.ParseBool(os.Getenv("META_MICRODATA")); err != nil {
isMicrodata = false
}
if isMicroformats2, err = strconv.ParseBool(os.Getenv("META_MICROFORMATS2")); err != nil {
isMicroformats2 = false
}
if isOpenGraph || isOEmbed {
info := htmlinfo.NewHTMLInfo()
err = info.Parse(resp.Body, &u, nil)
if err != nil {
log.Fatal(err)
}
if isOpenGraph {
data = info.OGInfo
} else if isOEmbed {
data = info.GenerateOembedFor(u)
} else if isMicrodata {
data = info
}
body, err = json.MarshalIndent(data, "", " ")
if err != nil {
log.Fatal(err)
}
} else if isMicrodata {
var info *microdata.Microdata
info, err = microdata.ParseURL(u)
if err != nil {
log.Fatal(err)
}
body, err = json.MarshalIndent(info, "", " ")
if err != nil {
log.Fatal(err)
}
} else if isMicroformats2 {
var URL *url.URL
URL, err = url.Parse(u)
if err != nil {
log.Fatal(err)
}
info := microformats.Parse(resp.Body, URL)
body, err = json.MarshalIndent(info, "", " ")
if err != nil {
log.Fatal(err)
}
}
// HTTP response as JSON
return events.APIGatewayProxyResponse{
Headers: map[string]string{
"Content-Type": "application/json; charset=utf-8",
"Last-Modified": cacheFrom,
"Expires": cacheUntil,
},
Body: string(body),
StatusCode: statusCode,
}, nil
}
// The core function
func main() {
isMock = flag.Bool("mock", false, "Read from the local `mock.json` file instead of an API Gateway request.")
flag.Parse()
if *isMock {
// read json from file
inputJSON, jsonErr := ioutil.ReadFile("./mock.json")
if jsonErr != nil {
fmt.Println(jsonErr.Error())
os.Exit(1)
}
// de-serialize into Go object
var inputEvent events.APIGatewayProxyRequest
if err = json.Unmarshal(inputJSON, &inputEvent); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
var response events.APIGatewayProxyResponse
response, err = Handler(inputEvent)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
fmt.Println(response)
} else {
lambda.Start(Handler)
}
}