Last active
December 28, 2021 18:30
[Infobip Tech Blog]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var builder = WebApplication.CreateBuilder(args); | |
builder.Services.AddSingleton<ILiteDatabase, LiteDatabase>(_ => new LiteDatabase("shorten-service.db")); | |
var app = builder.Build(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
global using HashidsNet; | |
global using LiteDB; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var builder = WebApplication.CreateBuilder(args); | |
var app = builder.Build(); | |
app.MapGet("/", () => "Hello World!"); | |
app.Run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
app = Flask(__name__) | |
@app.route("/") | |
def hello_world(): | |
return "<p>Hello, World!</p>" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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