Skip to content

Instantly share code, notes, and snippets.

@dlozina
Last active December 28, 2021 18:30
[Infobip Tech Blog]
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton<ILiteDatabase, LiteDatabase>(_ => new LiteDatabase("shorten-service.db"));
var app = builder.Build();
global using HashidsNet;
global using LiteDB;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.Run();
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "<p>Hello, World!</p>"
// <auto-generated/>
global using global::Microsoft.AspNetCore.Builder;
global using global::Microsoft.AspNetCore.Hosting;
global using global::Microsoft.AspNetCore.Http;
global using global::Microsoft.AspNetCore.Routing;
global using global::Microsoft.Extensions.Configuration;
global using global::Microsoft.Extensions.DependencyInjection;
global using global::Microsoft.Extensions.Hosting;
global using global::Microsoft.Extensions.Logging;
global using global::System;
global using global::System.Collections.Generic;
global using global::System.IO;
global using global::System.Linq;
global using global::System.Net.Http;
global using global::System.Net.Http.Json;
global using global::System.Threading;
global using global::System.Threading.Tasks;
app.MapPost("/shorten", (Url url, ILiteDatabase _context) =>
{
var db = _context.GetCollection<Url>(BsonAutoId.Int32);
var id = db.Insert(url);
return Results.Created("ShortURL: ", _hashIds.Encode(id));
});
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton<ILiteDatabase, LiteDatabase>(_ => new LiteDatabase("shorten-service.db"));
var app = builder.Build();
Hashids _hashIds = new Hashids("This is my shortener", 6);
app.MapPost("/shorten", (Url url, ILiteDatabase _context) =>
{
var db = _context.GetCollection<Url>(BsonAutoId.Int32);
var id = db.Insert(url);
return Results.Created("ShortURL: ", _hashIds.Encode(id));
});
app.MapGet("/{shortUrl}", (string shortUrl, ILiteDatabase _context) =>
{
var id = _hashIds.Decode(shortUrl);
var tempId = id[0];
var db = _context.GetCollection<Url>();
var entry = db.Query().Where(x => x.Id.Equals(tempId)).ToList().FirstOrDefault();
if (entry != null) return Results.Ok(entry.longUrl);
return Results.NoContent();
});
app.Run("http://localhost:4000");
public record Url(int Id, string longUrl);
public record Url(int Id, string longUrl);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment