V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
ygweric
V2EX  ›  问与答

短域名生成器 前后端全套代码 NestJS + TypeScript + React 开源

  •  
  •   ygweric · 2020-07-21 08:03:19 +08:00 · 1870 次点击
    这是一个创建于 1367 天前的主题,其中的信息可能已经有所发展或是发生改变。

    https://github.com/ygweric/short-url 欢迎 star 鼓励

    短连接生成系统

    Powered by NestJS + React

    后端 short-url-server

    技术栈

    NestJs + TypeScript + TypeORM + Sqlite

    Run it

    npm i
    npm start
    

    will run server in open http://localhost:3000/

    Test

    npm run test
    npm run test:cov
    

    测试覆盖率

    --------------------|---------|----------|---------|---------|-------------------
    File                | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
    --------------------|---------|----------|---------|---------|-------------------
    All files           |   96.25 |    78.57 |     100 |   95.52 |                   
     common             |     100 |      100 |     100 |     100 |                   
      config.ts         |     100 |      100 |     100 |     100 |                   
      errorCode.ts      |     100 |      100 |     100 |     100 |                   
      stringUtil.ts     |     100 |      100 |     100 |     100 |                   
     controllers        |      96 |    83.33 |     100 |   94.74 |                   
      app.controller.ts |      96 |    83.33 |     100 |   94.74 | 16                
     entities           |     100 |      100 |     100 |     100 |                   
      url.ts            |     100 |      100 |     100 |     100 |                   
     services           |    93.1 |    66.67 |     100 |   91.67 |                   
      app.service.ts    |    93.1 |    66.67 |     100 |   91.67 | 14,32             
    --------------------|---------|----------|---------|---------|-------------------
    

    短连接生成机制

    采用数据库 sum 自增的方式, 将 sum 总数转变为 62 进制的字符串

    对于不超过 8 位的短链接,最多可以存放 62^8 ≈ 218,340,105,584,896 个链接

    数据库

    export class Url {
    	@PrimaryColumn('varchar', { nullable: false })
        public shortUrl: string;
    
    	@Column("text", { nullable: false})
    	public longUrl: string;
    }
    

    结构框架

    ── src
    │   ├── app.module.ts
    │   ├── common 通用模块
    │   │   ├── __tests__
    │   │   │   └── stringUtil.spec.ts
    │   │   ├── config.ts
    │   │   ├── errorCode.ts
    │   │   └── stringUtil.ts
    │   ├── controllers 路由
    │   │   ├── __tests__
    │   │   │   └── app.controller.spec.ts
    │   │   └── app.controller.ts
    │   ├── entities ORM 实例
    │   │   └── url.ts
    │   ├── index.ts
    │   ├── main.ts
    │   ├── middlewares
    │   ├── migration
    │   └── services 服务层,负责 entity 和 controller 的通信
    │       ├── __tests__
    │       │   └── app.service.spec.ts
    │       └── app.service.ts
    

    TODO

    • 在 10 进制到 62 进制的转换中, 对于 32 位的系统,js 会出现溢出,需用 BigNumber 来操作
    • 增加输入合法性的验证

    前端 short-url-web

    技术栈

    React + TypeScript + Axios + AntD

    TODO

    • 本来想配置 less 的,报错了, 先发一版再说,回头改

    Run it

    npm i
    npm start
    

    open http://localhost:8080/

    10 条回复    2020-07-21 09:20:01 +08:00
    opengps
        1
    opengps  
       2020-07-21 08:22:31 +08:00
    短连接生成机制
    采用数据库 sum 自增的方式, 将 sum 总数转变为 62 进制的字符串

    短链接生成最好不要用这种规律性的,否则可以被爬取
    shc
        2
    shc  
       2020-07-21 08:37:10 +08:00
    支持一波~

    不过我没系统学过算法和编程这些,想请问一下,“ 将 sum 总数转变为 62 进制的字符串”,为什么是 62 进制,不是 61/64 进制呢,这里有什么讲究吗?
    optional
        3
    optional  
       2020-07-21 08:38:23 +08:00
    把 base62 改成 hashids 就看不出规律了。
    optional
        4
    optional  
       2020-07-21 08:38:55 +08:00
    @shc 62 = 26+26+10 常见字符编码
    PHPJit
        5
    PHPJit  
       2020-07-21 08:47:23 +08:00
    搞一个 docker 啊
    ygweric
        6
    ygweric  
    OP
       2020-07-21 09:01:32 +08:00
    @shc 64 进制包括数字,字幕大小写, 还有+~,移除了这些特殊字符, 就是 62 进制了, 后续可以扩展写其他合法字符
    ygweric
        7
    ygweric  
    OP
       2020-07-21 09:02:45 +08:00
    @optional 说得好, 临时起意想打个 init 版本的 NestJS 工程,就拿短连接开刀了, 以后会慢慢优化
    ygweric
        8
    ygweric  
    OP
       2020-07-21 09:03:14 +08:00
    @opengps 被爬取这点暂未考虑, 会考虑记性 hash 或其他方式,谢谢
    ygweric
        9
    ygweric  
    OP
       2020-07-21 09:04:16 +08:00
    @PHPJit 算是写代码玩的, 暂时没打算上线,毕竟这个业务已经烂大街了
    shc
        10
    shc  
       2020-07-21 09:20:01 +08:00
    @optional @ygweric 了解了,谢谢解答
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1099 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 22:59 · PVG 06:59 · LAX 15:59 · JFK 18:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.