thonatos / shorturl-egg

shorturl powered by egg
https://swz.li
24 stars 3 forks source link
egg shorturl

shorturl

shorturl powered by egg.js

QuickStart

see egg docs for more detail.

Development

$ npm install
$ npm run dev
$ open http://localhost:7001

Deploy

Use EGG_SERVER_ENV=prod to enable prod mode

$ EGG_SERVER_ENV=prod npm start

npm scripts

docker-compose

cd docs/docker-compose
docker-compose up

see docs/docker-compose for more detail.

Config

Database

// mysql
DROP TABLE IF EXISTS  `url`;
CREATE TABLE `url` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `url` varchar(512) NOT NULL,
  `created` datetime DEFAULT CURRENT_TIMESTAMP,
  `hash` varchar(512) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=200 DEFAULT CHARSET=utf8mb4;
// elasticsearch

// geoip configure
PUT _ingest/pipeline/geoip
{
  "description" : "Add geoip info",
  "processors" : [
    {
      "geoip" : {
        "field" : "ip"
      }
    }
  ]
}

// mappings
PUT shorturl
{
  "mappings": {
    "view": {
      "_all": {
        "enabled": false
      },
      "properties": {
        "ip": {
          "type": "ip"
        },
        "date": {
          "type": "date"
        }
      }
    }
  }
}

Application

'use strict';

exports.site = {
  domain: 'https://swz.li/',
};

exports.mysql = {
  client: {
    host: 'mysql',
    port: '3306',
    user: 'root',
    password: 'mysql',
    database: 'shorturl',
  },
};

exports.redis = {
  client: {
    port: 6379,
    host: 'redis',
    password: null,
    db: 0,
  },
};

exports.elasticsearch = {
  host: 'elasticsearch:9200',
};

Api

// request body
{
    "url":"http://www.baidu.com"
}

// response
{
  "url": "http://www.baidu.com",
  "hash": "QioWY",
  "shorturl": "http://localhost:7001/QioWY"
}
{
  "id": 2,
  "url": "http://www.baidu.com",
  "created": "2017-07-24T03:42:06.000Z"
}
[
  {
    "id": 2,
    "url": "http://www.baidu.com",
    "created": "2017-07-24T03:42:06.000Z"
  },
  {
    "id": 1,
    "url": "http://www.baidu.com/?test=3",
    "created": "2017-07-23T06:56:48.000Z"
  }
]