This repository has been archived by the owner on May 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
99 lines (80 loc) · 3.36 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
package main
import (
"encoding/json"
"fmt"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/vimcolorschemes/search/internal/bucket"
"github.com/vimcolorschemes/search/internal/database"
"github.com/vimcolorschemes/search/internal/dotenv"
"github.com/vimcolorschemes/search/internal/repository"
req "github.com/vimcolorschemes/search/internal/request"
)
const (
Store = "store"
Search = "search"
)
var Headers = map[string]string{"Access-Control-Allow-Origin": "*"}
type StoreRequestBody struct {
Key string
}
func handle(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
switch request.HTTPMethod {
case "POST":
return store(request), nil
case "GET":
return search(request), nil
default:
body := req.BuildErrorBody(fmt.Sprintf("HTTP method not supported: %s", request.HTTPMethod))
return events.APIGatewayProxyResponse{Body: body, StatusCode: 400, Headers: Headers}, nil
}
}
func store(request events.APIGatewayProxyRequest) events.APIGatewayProxyResponse {
var requestBody StoreRequestBody
if err := json.Unmarshal([]byte(request.Body), &requestBody); err != nil {
body := req.BuildErrorBody("error trying to parse request body:", err.Error())
return events.APIGatewayProxyResponse{Body: body, StatusCode: 500, Headers: Headers}
}
bucketName, exists := dotenv.Get("AWS_S3_BUCKET")
if !exists {
body := req.BuildErrorBody("AWS_S3_BUCKET env variable is missing")
return events.APIGatewayProxyResponse{Body: body, StatusCode: 500, Headers: Headers}
}
fileContent, err := bucket.Get(bucketName, requestBody.Key)
if err != nil {
body := req.BuildErrorBody("error fetching search index file from s3:", err.Error())
return events.APIGatewayProxyResponse{Body: body, StatusCode: 500, Headers: Headers}
}
var searchIndex []repository.Repository
if err := json.Unmarshal([]byte(fileContent), &searchIndex); err != nil {
body := req.BuildErrorBody("error parsing search index content as JSON:", err.Error())
return events.APIGatewayProxyResponse{Body: body, StatusCode: 500, Headers: Headers}
}
if err := database.Store(searchIndex); err != nil {
body := req.BuildErrorBody("error trying to store the search index:", err.Error())
return events.APIGatewayProxyResponse{Body: body, StatusCode: 500, Headers: Headers}
}
return events.APIGatewayProxyResponse{StatusCode: 200, Headers: Headers}
}
func search(request events.APIGatewayProxyRequest) events.APIGatewayProxyResponse {
parameters, err := req.ParseSearchParameters(request)
if err != nil {
body := req.BuildErrorBody("error parsing search parameters:", err.Error())
return events.APIGatewayProxyResponse{Body: body, StatusCode: 400, Headers: Headers}
}
repositories, total, err := database.Search(parameters)
if err != nil {
body := req.BuildErrorBody("error storing search index:", err.Error())
return events.APIGatewayProxyResponse{Body: body, StatusCode: 500, Headers: Headers}
}
result := map[string]interface{}{"repositories": repositories, "totalCount": total}
payload, err := json.Marshal(result)
if err != nil {
body := req.BuildErrorBody("error encoding search result to JSON:", err.Error())
return events.APIGatewayProxyResponse{Body: body, StatusCode: 500, Headers: Headers}
}
return events.APIGatewayProxyResponse{Body: string(payload), StatusCode: 200, Headers: Headers}
}
func main() {
lambda.Start(handle)
}