“SuperShortLink” .NET open source super short chain system – quickly realize the conversion and monitoring of long and short chains


After reading this article, you can gain

  • Learn about the open source short chain project SuperShortLink
  • Learn the principles and methods of converting long chains to short chains, short chains to long chains, and short chain access statistics
  • Master multiple access methods of other internal projects

Article directory

  • 1. Super Short Link
    • 1. Source address
    • 2. Function introduction
    • 3. Architecture design document
  • 2. Build and run the project
    • 1. Run the project
    • 2. Configuration update
  • 3. Project access
    • 1. Access via API extended class library (recommended)
    • 2. Access through the Core extended class library

1. SuperShortLink

This is a short chain generation and monitoring system based on .NET open source. It includes online generation of short chains, short chain jumps to long chains, support for short chain visits and web monitoring pages, which can help us generate short chains more easily, Monitor the short chain!

1. Source URL

  • https://github.com/Bryan-Cyf/SuperShortLink

2. Function introduction

  • Backend and Web management interface
  • Support custom short chain length
  • Support online short chain generation and jump long chain
  • Support real-time statistical short chain visits
  • Support multiple persistence Method: MySQL/PostgreSQL/SqlServer (2012 and above)
  • Fool configuration, open out of the box

3. Architecture Design Document

  • The architecture design and source code sharing of the billion-level short URL generator of “making wheels”

2. Build and run the project

1. Run the project

Step 1: Open the project
Open SuperShortLink.sln via VisualStudio

Step 2: Configure the database

  • Optional: MySQL/PostgreSQL/SqlServer (2012 and above)
  • Update the connection string in the appsetting.json file
"ShortLink": {<!-- -->
    "Secrect": "vZCN8VhSge13UQrYjBTwKulWqsIOAocL0DkmRdxPMJf5tiHbn72z69aXpGyFE4",// randomly scrambled Base62 encoding
    "CodeLength": 6, //short chain length
    "DbType": "PostgreSQL", //DatabaseType:MySQL/PostgreSQL/SqlServer (only supports SQL Server2012 and above)
    "ConnectionString": "Server=127.0.0.1;Port=5432;User Id=uid;Password=pwd;Database=test_db;",//Database connection string
    "LoginAcount": "admin", //login account
    "LoginPassword": "123456" //login password
}

Step 3: Execute the SQL statement for creating a table in the database SQL statement for creating a table

Step 4: Run the project

  • Log in to the management background: {domain name}/home/index
  • Default login account password: admin 123456

2. Configuration update

  • Modify account password: update LoginAcount and LoginPassword of appsetting.json
  • Modify the random secret key: You can directly run the test case to randomly generate a new secret key: ShortLinkTest/Generate_Key_Be_Valid, and then update the Secret of appsetting.json

3. Project access

1. Access via API extension class library (recommended)

The API class library is based on HTTP requests, which is suitable for opening the interface to other platforms/system calls, and shields the application from request details such as Token, timestamp, and application code.

Step 1: Install the package, install the package through Nuget

Install-Package Pandora.ShortLink.Api

Step 2: Configure the Startup startup class

public class Startup
{<!-- -->
    //...
    
    public void ConfigureServices(IServiceCollection services)
    {<!-- -->
        //configuration
        services.AddShortLinkApi(option =>
        {<!-- -->
            option.ApiDomain = "Short chain service domain name";
            option.AppSecret = "Application Secret";
            option.AppCode = "Application Code";
        });
    }
}

Step 3: Use the IShortLinkApiService service interface

[Route("api/[controller]/[Action]")]
public class ShortLinkController : Controller
{<!-- -->
    private readonly IShortLinkApiService _apiService;
    public ShortLinkController(IShortLinkApiService apiService)
    {<!-- -->
        _apiService = apiService;
    }

    /// <summary>
    /// Parse and generate a short URL
    /// </summary>
    /// <param name="url">Long link</param>
    /// <returns></returns>
    [HttpPost]
    public async Task<string> Generate(string url)
    {<!-- -->
        var short_url = await _apiService. GenerateAsync(url);
        return short_url;
    }
}

2. Access through the Core extended class library

The Core class library is directly connected to the database and is suitable for internal platform/system calls without authorization verification

Step 1: Install the package, install the package through Nuget

Install-Package Pandora.ShortLink.Core

Step 2: Configure the Startup startup class

public class Startup
{<!-- -->
    //...
    
    public void ConfigureServices(IServiceCollection services)
    {<!-- -->
        //configuration
        services.AddShortLink(option =>
        {<!-- -->
            option.ConnectionString = "Database Connection";
            option.DbType = "Database Type";//Optional: DatabaseType.PostgreSQL/MySQL/SqlServer
            option.Secrect = "Scrambled Base62 encoding",
            option.CodeLength = "Short chain length";
        });
    }
}

Step 3: Use the IShortLinkService service interface

[Route("api/[controller]/[Action]")]
public class ShortLinkController : Controller
{<!-- -->
    private readonly IShortLinkService _shortLinkService;
    public ShortLinkController(IShortLinkService shortLinkService)
    {<!-- -->
        _shortLinkService = shortLinkService;
    }

    /// <summary>
    /// Parse and generate a short URL
    /// </summary>
    /// <param name="url">Long link</param>
    /// <returns></returns>
    [HttpPost]
    public async Task<string> Generate(string url)
    {<!-- -->
        var short_url = await _shortLinkService. GenerateAsync(url);
        return short_url;
    }
}