Esempio n. 1
0
 public ShortUrl(string resourceId, string originalUrl)
 {
     this.dynamoItem = new ShortUrlItem
     {
         resourceId  = resourceId,
         originalUrl = originalUrl,
         ttl         = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds() + 120
     };
 }
Esempio n. 2
0
        public async Task <APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context)
        {
            //string tableName = System.Environment.GetEnvironmentVariable("DYNAMO_TABLE");
            LambdaLogger.Log("EVENT: " + JsonConvert.SerializeObject(apigProxyEvent));
            try
            {
                string resourceId = null;
                int    idLength   = 1;
                if (apigProxyEvent.Path == "/shortUrl")
                {
                    resourceId = createId(idLength);
                    LambdaLogger.Log("Test environment for set");

                    while (true)
                    {
                        resourceId = createId(idLength);
                        LambdaLogger.Log("Generated id " + resourceId);
                        ShortUrlItem item = await table.get(resourceId);

                        if (item == null)
                        {
                            ApiUrlRequestBody requestBody = JsonConvert.DeserializeObject <ApiUrlRequestBody>(apigProxyEvent.Body);
                            await table.save(new ShortUrl(resourceId, requestBody.originalUrl).dynamoItem);

                            break;
                        }
                        LambdaLogger.Log("ITEM: " + item.ToString() + " ID: " + item.resourceId);
                        idLength++;
                    }
                    return(CreateBasicResponse(new ApiBody
                    {
                        resourceId = resourceId,
                        completeUrl = createCompleteUrl(resourceId)
                    }));
                }
                else
                {
                    LambdaLogger.Log("Test environment for get");
                    ShortUrlItem item = await table.get(apigProxyEvent.PathParameters["resourceId"]);

                    LambdaLogger.Log("ITEM: " + item.ToString() + " ID: " + item.resourceId);

                    resourceId = item.resourceId;
                    if (item.ttl < new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds())
                    {
                        throw new Exception("Expired url");
                    }

                    return(CreateRedirectResponse(item.originalUrl));
                }
            } catch (Exception e)
            {
                return(CreateErrorResponse());
            }
        }
Esempio n. 3
0
        public async Task <bool> save(ShortUrlItem urlItem)
        {
            await context.SaveAsync(urlItem);

            return(true);
        }