-
Notifications
You must be signed in to change notification settings - Fork 8
/
micropub.go
73 lines (68 loc) · 2.08 KB
/
micropub.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
package main
import (
"errors"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
func handler(req events.APIGatewayProxyRequest) (*events.APIGatewayProxyResponse, error) {
// a handler for GET requests, used for troubleshooting
if req.HTTPMethod == "GET" {
if q, ok := req.PathParameters["q"]; ok && q == "syndicate-to" {
return &events.APIGatewayProxyResponse{
StatusCode: 200,
Headers: map[string]string{"Content-type": "application/json"},
Body: "[]",
}, nil
}
return &events.APIGatewayProxyResponse{
StatusCode: 200,
Headers: map[string]string{"Content-type": "application/json"},
Body: "{}",
}, nil
}
// check if the request is a post
if req.HTTPMethod != "POST" {
return &events.APIGatewayProxyResponse{
StatusCode: 405,
Body: "The HTTP method is not allowed, make a POST request",
}, errors.New("HTTP method is not valid")
}
// check the content-type
contentType, err := GetContentType(req.Headers)
if err != nil {
return &events.APIGatewayProxyResponse{
StatusCode: 400,
Body: err.Error(),
}, err
}
entry, err := CreateEntry(contentType, req.Body)
if entry == nil {
return &events.APIGatewayProxyResponse{
StatusCode: 400,
Body: "There was an error creating the entry",
}, err
}
if CheckAuthorization(entry, req.Headers) {
location, err := WriteEntry(entry)
if err != nil {
return &events.APIGatewayProxyResponse{
StatusCode: 400,
Body: "There was an error committing the entry to the repository",
}, errors.New("Error committing the entry to the repository")
} else {
return &events.APIGatewayProxyResponse{
StatusCode: 202,
Headers: map[string]string{"Location": location},
}, nil
}
} else {
return &events.APIGatewayProxyResponse{
StatusCode: 403,
Body: "Forbidden, there was a problem with the provided access token",
}, errors.New("The provided access token does not grant permission")
}
}
func main() {
// Make the handler available for Remote Procedure Call by AWS Lambda
lambda.Start(handler)
}