Skip to content

Commit

Permalink
feat: first demo
Browse files Browse the repository at this point in the history
  • Loading branch information
gaojie committed Sep 23, 2022
0 parents commit ffc8316
Show file tree
Hide file tree
Showing 22 changed files with 493 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .autod.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

module.exports = {
write: true,
prefix: '^',
plugin: 'autod-egg',
test: [
'test',
'benchmark',
],
dep: [
'egg',
'egg-scripts',
],
devdep: [
'egg-ci',
'egg-bin',
'egg-mock',
'autod',
'autod-egg',
'eslint',
'eslint-config-egg',
],
exclude: [
'./test/fixtures',
'./dist',
],
};

1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage
4 changes: 4 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "eslint-config-egg",
"root": true
}
46 changes: 46 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
push:
branches:
- main
- master
pull_request:
branches:
- main
- master
schedule:
- cron: '0 2 * * *'

jobs:
build:
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
node-version: [16]
os: [ubuntu-latest, windows-latest, macos-latest]

steps:
- name: Checkout Git Source
uses: actions/checkout@v2

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- name: Install Dependencies
run: npm i

- name: Continuous Integration
run: npm run ci

- name: Code Coverage
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
logs/
npm-debug.log
yarn-error.log
node_modules/
package-lock.json
yarn.lock
coverage/
.idea/
run/
.DS_Store
*.sw*
*.un~
typings/
.nyc_output/
database/config.json
config/config.local.js
config/config.prod.js
9 changes: 9 additions & 0 deletions .sequelizerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";

const path = require("path");

module.exports = {
config: path.join(__dirname, "database/config.json"),
"migrations-path": path.join(__dirname, "database/migrations"),
"seeders-path": path.join(__dirname, "database/seeders"),
};
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# 用于小程序后端的简单应用(eggjs开发)



## 数据库连接

/config/config.local.js
```js
"use strict";

exports.sequelize = {
dialect: "mysql",
host: "0.0.0.0",
port: 3306,
database: "name",
username: "user",
password: "password",
};
```

/config/config.prod.js
```js
"use strict";

exports.sequelize = {
dialect: "mysql",
host: "0.0.0.0",
port: 3306,
database: "name",
username: "user",
password: "password",
};
```

/database/cofig.json

`用于migration连接数据库`
```json
{
"development": {
"dialect": "mysql",
"host": "0.0.0.0",
"port": 3306,
"database": "name",
"username": "user",
"password": "password"
}
}
```


### Development

```bash
$ npm i
$ npm run dev
$ open http://localhost:7001/
```

### Deploy

```bash
$ npm start
$ npm stop
```


[egg]: https://eggjs.org
28 changes: 28 additions & 0 deletions app/controller/eps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"use strict";

const Controller = require("egg").Controller;

function toInt(str) {
if (typeof str === "number") return str;
if (!str) return str;
return parseInt(str, 10) || 0;
}

class EpController extends Controller {
async index() {
const ctx = this.ctx;
const query = {
limit: toInt(ctx.query.limit),
offset: toInt(ctx.query.offset),
};
ctx.body = await ctx.model.Ep.findAll(query);
}

async newEps() {
const ctx = this.ctx;

ctx.body = await ctx.model.NewEp.findAll({ limit: 1 });
}
}

module.exports = EpController;
12 changes: 12 additions & 0 deletions app/controller/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"use strict";

const Controller = require("egg").Controller;

class HomeController extends Controller {
async index() {
const { ctx } = this;
ctx.body = "hi there";
}
}

module.exports = HomeController;
30 changes: 30 additions & 0 deletions app/controller/tracks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"use strict";

const Controller = require("egg").Controller;

function toInt(str) {
if (typeof str === "number") return str;
if (!str) return str;
return parseInt(str, 10) || 0;
}

class TrackController extends Controller {
async show() {
const ctx = this.ctx;
const { id } = this.ctx.params;
const { limit = 20, current = 1 } = this.ctx.query;
const _limit = toInt(limit);

// 页数从1开始
const _current = toInt(current);
const offset = (_current - 1) * _limit;

ctx.body = await ctx.model.Track.findAndCountAll({
where: { ep_Id: id },
limit: _limit,
offset,
});
}
}

module.exports = TrackController;
21 changes: 21 additions & 0 deletions app/model/ep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"use strict";

module.exports = (app) => {
const { STRING, INTEGER, DATE } = app.Sequelize;

const Ep = app.model.define("ep", {
id: {
type: INTEGER,
primaryKey: true,
autoIncrement: true,
},
_id: { type: STRING, field: "_id" },
name: STRING(250),
img: STRING(250),
sort: INTEGER,
created_at: DATE,
updated_at: DATE,
});

return Ep;
};
19 changes: 19 additions & 0 deletions app/model/newEp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"use strict";

module.exports = (app) => {
const { STRING, DATE, INTEGER, BOOLEAN } = app.Sequelize;

const NewEp = app.model.define("newEp", {
id: {
type: INTEGER,
primaryKey: true,
autoIncrement: true,
},
hideAudio: BOOLEAN,
newEpIds: STRING(300),
createdAt: DATE,
updatedAt: DATE,
});

return NewEp;
};
23 changes: 23 additions & 0 deletions app/model/track.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"use strict";

module.exports = (app) => {
const { STRING, INTEGER, DATE } = app.Sequelize;

const Track = app.model.define("track", {
id: {
type: INTEGER,
primaryKey: true,
autoIncrement: true,
},
_id: { type: STRING, field: "_id" },
title: STRING(250),
epId: STRING(250),
length: STRING(250),
src: STRING(250),
sort: INTEGER,
created_at: DATE,
updated_at: DATE,
});

return Track;
};
14 changes: 14 additions & 0 deletions app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict";

/**
* @param {Egg.Application} app - egg application
*/
module.exports = (app) => {
const { router, controller } = app;
router.get("/", controller.home.index);

router.get("/eps", controller.eps.index);
router.get("/newEps", controller.eps.newEps);

router.get("/tracks/:id", controller.tracks.show);
};
30 changes: 30 additions & 0 deletions config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* eslint valid-jsdoc: "off" */

"use strict";

/**
* @param {Egg.EggAppInfo} appInfo app info
*/
module.exports = (appInfo) => {
/**
* built-in config
* @type {Egg.EggAppConfig}
**/
const config = {};

// use for cookie sign key, should change to your own and keep security
config.keys = appInfo.name + "_1663737137179_1189";

// add your middleware config here
config.middleware = [];

// add your user config here
const userConfig = {
// myAppName: 'egg',
};

return {
...config,
...userConfig,
};
};
7 changes: 7 additions & 0 deletions config/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";

/** @type Egg.EggPlugin */
exports.sequelize = {
enable: true,
package: "egg-sequelize",
};
20 changes: 20 additions & 0 deletions database/migrations/20220921074526-init-eps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use strict";

module.exports = {
async up(queryInterface, Sequelize) {
const { INTEGER, DATE, STRING } = Sequelize;
await queryInterface.createTable("eps", {
id: { type: INTEGER, primaryKey: true, autoIncrement: true },
_id: STRING(250),
name: STRING(250),
img: STRING(250),
sort: INTEGER,
created_at: DATE,
updated_at: DATE,
});
},

async down(queryInterface, Sequelize) {
await queryInterface.dropTable("eps");
},
};
Loading

0 comments on commit ffc8316

Please sign in to comment.