-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
68 lines (59 loc) · 2.2 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
package main
import (
"context"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"gophercon-2023-demo/client"
bskyImpl "gophercon-2023-demo/lambda"
"log"
"net/http"
"strings"
)
var (
blueskyHandle = "golangkorea.bsky.social"
blueskyAppkey = "DEMO"
)
func main() {
lambda.Start(handleRequest)
}
func handleRequest(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
log.Printf("Request: %+v\n", request)
authHeader := request.Headers["Authorization"]
authParts := strings.Split(authHeader, " ")
if len(authParts) != 2 || authParts[0] != "Bearer" || authParts[1] != blueskyAppkey {
return events.APIGatewayProxyResponse{StatusCode: http.StatusUnauthorized}, nil
}
client, err := client.Dial(ctx, client.ServerBskySocial)
if err != nil {
return events.APIGatewayProxyResponse{StatusCode: http.StatusInternalServerError}, nil
}
log.Printf("Logging in with handle: %s, appkey: %s\n", blueskyHandle, blueskyAppkey)
err = client.Login(ctx, blueskyHandle, blueskyAppkey)
if err != nil {
return events.APIGatewayProxyResponse{StatusCode: http.StatusInternalServerError}, nil
}
handle := request.PathParameters["handle"]
if handle == "" {
handle = request.PathParameters["proxy"]
}
switch {
case strings.HasPrefix(request.Path, "/profile"):
return bskyImpl.GetProfile(ctx, client, handle)
case strings.HasPrefix(request.Path, "/avatar"):
return bskyImpl.GetAvatar(ctx, client, handle)
case strings.HasPrefix(request.Path, "/banner"):
return bskyImpl.GetBanner(ctx, client, handle)
case strings.HasPrefix(request.Path, "/followers/full"):
return bskyImpl.GetFollowersFull(ctx, client, handle)
case strings.HasPrefix(request.Path, "/following/full"):
return bskyImpl.GetFollowingFull(ctx, client, handle)
case strings.HasPrefix(request.Path, "/followers/short"):
return bskyImpl.GetFollowersShort(ctx, client, handle)
case strings.HasPrefix(request.Path, "/following/short"):
return bskyImpl.GetFollowingShort(ctx, client, handle)
case strings.HasPrefix(request.Path, "/blob"):
return bskyImpl.GetBlob(ctx, client, request)
default:
return events.APIGatewayProxyResponse{StatusCode: http.StatusNotFound}, nil
}
}