-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.go
66 lines (55 loc) · 1.6 KB
/
handler.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
package main
import (
"fmt"
"net/http"
"net/url"
"os"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/eawsy/aws-lambda-go-net/service/lambda/runtime/net"
"github.com/eawsy/aws-lambda-go-net/service/lambda/runtime/net/apigatewayproxy"
)
const invalidBucketError = "S3_BUCKET environment variable is not set"
const invalidURLError = "'%s' is not a valid URL."
const defaultProtocol = "http://"
var Handle apigatewayproxy.Handler
var S3Handle *s3.S3
func init() {
ln := net.Listen()
Handle = apigatewayproxy.New(ln, nil).Handle
go http.Serve(ln, http.HandlerFunc(handle))
S3Handle = initS3()
}
func handle(w http.ResponseWriter, r *http.Request) {
longURL := r.URL.Query().Get("url")
hash := getHash(longURL)
resp := Payload{LongURL: longURL}
bucketS3, ok := os.LookupEnv("S3_BUCKET")
if !ok {
resp.Header = http.StatusBadRequest
resp.Message = invalidBucketError
returnResponse(&resp, w, r)
return
}
if !isValidProtocol(longURL) {
//default to HTTP protocol.
longURL = defaultProtocol + longURL
}
if _, err := url.ParseRequestURI(longURL); err != nil {
resp.Header = http.StatusBadRequest
resp.Message = fmt.Sprintf(invalidURLError, longURL)
returnResponse(&resp, w, r)
return
}
redirResponse, err := createRedirObject(S3Handle, longURL, bucketS3, hash)
if err != nil {
resp.Header = http.StatusInternalServerError
resp.Message = err.Error()
returnResponse(&resp, w, r)
return
}
resp.Header = http.StatusCreated
resp.ShortURL = redirResponse.ShortURL
resp.Message = fmt.Sprintf("URL '%s' created for '%s'", resp.ShortURL, resp.LongURL)
returnResponse(&resp, w, r)
return
}