Code Monkey home page Code Monkey logo

short's Introduction

短链接项目

搭建项目的骨架

  1. 建库建表

新建发号器

CREATE TABLE `sequence` (
    `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `stub` varchar(1) NOT NULL,
    `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `idx_uniq_stub` (`stub`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT = '序号表';

新建长链接短链接映射表

CREATE TABLE `short_url_map` (
    `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
    `create_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `create_by` VARCHAR(64) NOT NULL DEFAULT '' COMMENT '创建者',
    `is_del` tinyint UNSIGNED NOT NULL DEFAULT '0' COMMENT '是否删除:0正常1删除',
    `lurl` varchar(2048) DEFAULT NULL COMMENT '长链接',
    `md5` char(32) DEFAULT NULL COMMENT '⻓链接MD5',
    `surl` varchar(11) DEFAULT NULL COMMENT '短链接',
    PRIMARY KEY (`id`),
    INDEX(`is_del`),
    UNIQUE(`md5`),
    UNIQUE(`surl`)
)ENGINE=INNODB DEFAULT CHARSET=utf8mb4 COMMENT = '⻓短链映射表';
  1. 搭建go-zero框架的骨架

2.1 编写api文件,使用goctl命令生成代码

type ConvertRequest {
    LongUrl string `json:"longUrl"`
}
type ConvertResponse {
    ShortUrl string `json:"shortUrl"`
}
type ShowReqeust {
    ShortUrl string `json:"shortUrl"`
}
type ShowResponse {
    LongUrl string `json:"longUrl"`
}
service shortener-api {
    @handler ConvertHandler
    post /convert (ConvertRequest) returns (ConvertResponse)

    @handler ShowHandler
    get /:shortUrl(ShowReqeust) returns (ShowResponse)
}

2.2 根据api文件生成go代码

goctl api go -api shortener.api -dir .
  1. 根据数据表生成model层代码
goctl model mysql datasource -url="root:123456@tcp(127.0.0.1:3306)/db3" -table="short_url_map" -dir="./model"
goctl model mysql datasource -url="root:123456@tcp(127.0.0.1:3306)/db3" -table="sequence" -dir="./model"
  1. 下载项目依赖
go mod tidy
  1. 启动项目
go run .

看到如下输出,说明项目启动成功

Starting server at 0.0.0.0:8888...
  1. 修改配置结构和配置文件 注意:两边要对齐

参数校验

  1. go-zero 使用validator进行参数校验 下载validator
go get github.com/go-playground/validator/v10

添加缓存

有两种方式

  1. 自己使用redis去存储
  2. 使用go-zero自带的缓存 本项目使用第二种方案 重新生成model层代码
goctl model mysql datasource -url="root:123456@tcp(127.0.0.1:3306)/db3" -table="short_url_map" -dir="./model" -c

short's People

Contributors

yakultgo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.