|
| 1 | +package dynamodb |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "time" |
| 9 | + |
| 10 | + "log/slog" |
| 11 | + |
| 12 | + "github.com/aws/aws-lambda-go/events" |
| 13 | + "github.com/johnwmail/nclip/internal/storage" |
| 14 | +) |
| 15 | + |
| 16 | +// Use shared Paste struct from storage |
| 17 | + |
| 18 | +var ( |
| 19 | + dynamoStorage storage.Storage |
| 20 | +) |
| 21 | + |
| 22 | +func InitDynamoStorage() { |
| 23 | + tableName := os.Getenv("NCLIP_DYNAMODB_TABLE") |
| 24 | + logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelInfo})) |
| 25 | + s, err := storage.NewDynamoDBStorage(tableName, logger) |
| 26 | + if err != nil { |
| 27 | + panic(fmt.Sprintf("unable to initialize DynamoDB storage: %v", err)) |
| 28 | + } |
| 29 | + dynamoStorage = s |
| 30 | +} |
| 31 | + |
| 32 | +func Handler(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { |
| 33 | + switch req.HTTPMethod { |
| 34 | + case "POST": |
| 35 | + return createPaste(ctx, req) |
| 36 | + case "GET": |
| 37 | + return getPaste(ctx, req) |
| 38 | + default: |
| 39 | + return events.APIGatewayProxyResponse{StatusCode: 405}, nil |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func createPaste(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { |
| 44 | + var paste storage.Paste |
| 45 | + if err := json.Unmarshal([]byte(req.Body), &paste); err != nil { |
| 46 | + return events.APIGatewayProxyResponse{ |
| 47 | + StatusCode: 400, |
| 48 | + Body: "Invalid request", |
| 49 | + Headers: map[string]string{"Content-Type": "text/plain"}, |
| 50 | + }, nil |
| 51 | + } |
| 52 | + |
| 53 | + // Validate required fields |
| 54 | + if paste.ID == "" { |
| 55 | + return events.APIGatewayProxyResponse{ |
| 56 | + StatusCode: 400, |
| 57 | + Body: "Missing paste ID", |
| 58 | + Headers: map[string]string{"Content-Type": "text/plain"}, |
| 59 | + }, nil |
| 60 | + } |
| 61 | + |
| 62 | + paste.CreatedAt = time.Now() |
| 63 | + if paste.ExpiresAt == nil { |
| 64 | + expires := paste.CreatedAt.Add(30 * 24 * time.Hour) |
| 65 | + paste.ExpiresAt = &expires |
| 66 | + } |
| 67 | + |
| 68 | + if err := dynamoStorage.Store(&paste); err != nil { |
| 69 | + return events.APIGatewayProxyResponse{ |
| 70 | + StatusCode: 500, |
| 71 | + Body: err.Error(), |
| 72 | + Headers: map[string]string{"Content-Type": "text/plain"}, |
| 73 | + }, nil |
| 74 | + } |
| 75 | + respBody, _ := json.Marshal(map[string]string{"id": paste.ID}) |
| 76 | + return events.APIGatewayProxyResponse{ |
| 77 | + StatusCode: 201, |
| 78 | + Body: string(respBody), |
| 79 | + Headers: map[string]string{"Content-Type": "application/json"}, |
| 80 | + }, nil |
| 81 | +} |
| 82 | + |
| 83 | +func getPaste(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { |
| 84 | + id := req.PathParameters["id"] |
| 85 | + if id == "" { |
| 86 | + return events.APIGatewayProxyResponse{ |
| 87 | + StatusCode: 400, |
| 88 | + Body: "Missing paste ID", |
| 89 | + Headers: map[string]string{"Content-Type": "text/plain"}, |
| 90 | + }, nil |
| 91 | + } |
| 92 | + |
| 93 | + paste, err := dynamoStorage.Get(id) |
| 94 | + if err != nil { |
| 95 | + return events.APIGatewayProxyResponse{ |
| 96 | + StatusCode: 404, |
| 97 | + Body: "Paste not found", |
| 98 | + Headers: map[string]string{"Content-Type": "text/plain"}, |
| 99 | + }, nil |
| 100 | + } |
| 101 | + |
| 102 | + // Check expiration |
| 103 | + if paste.ExpiresAt != nil && time.Now().After(*paste.ExpiresAt) { |
| 104 | + return events.APIGatewayProxyResponse{ |
| 105 | + StatusCode: 410, |
| 106 | + Body: "Paste has expired", |
| 107 | + Headers: map[string]string{"Content-Type": "text/plain"}, |
| 108 | + }, nil |
| 109 | + } |
| 110 | + |
| 111 | + respBody, _ := json.Marshal(paste) |
| 112 | + return events.APIGatewayProxyResponse{ |
| 113 | + StatusCode: 200, |
| 114 | + Body: string(respBody), |
| 115 | + Headers: map[string]string{"Content-Type": "application/json"}, |
| 116 | + }, nil |
| 117 | +} |
| 118 | + |
| 119 | +// ...existing code... |
0 commit comments