diff --git a/app/main.js b/app/main.js new file mode 100644 index 0000000..a6ad12f --- /dev/null +++ b/app/main.js @@ -0,0 +1,54 @@ +const http = require("http"); +const url = require("url"); +const { ServerModel } = require('./model/ServerModel'); +const { SearchModel } = require('./model/SearchModel'); +const { SuccessResult, ErrorResult } = require('./model/ResultModel'); + +const { QQSource } = require('./source/QQSource'); + + +// *********************************************************************** +// 注册源 +const searcher = new SearchModel(); +searcher.registerSourceModel(new QQSource()); + + +// *********************************************************************** +// 添加API接口 +const _server = new ServerModel(); +_server.get("/api/source", (req, res, query) => { + // 罗列都有哪些源 + return new SuccessResult(searcher.getSourceList()); +}); + +_server.get("/api/search", (req, res, query) => { + //从源去进行搜索 + let result = searcher.searchMusic(query.source_name, query.keywords); + return new SuccessResult(result); +}) + + +// *********************************************************************** +// 运行服务 +const server = http.createServer((req, res) => { + let method = req.method; + let reqUrl = req.url; + let reqApi = reqUrl.split('?')[0]; + console.log(method); + let result; + if(method == "GET") { + let query = url.parse(reqUrl, true).query; + result = _server.trigger(method, reqApi, req, res, query, null); + }else if(method == "POST") { + let query = url.parse(reqUrl, true).query; + let data = {}; + result = _server.trigger(method, reqApi, req, res, query, data); + } + res.writeHead(200, {'Content-type': 'text/plain;charset=utf-8'}); + res.end(JSON.stringify(result)); +}); + +server.listen(5000, () => { + console.log("server running at prot 5000"); +}); + diff --git a/app/model/ResultModel.js b/app/model/ResultModel.js new file mode 100644 index 0000000..6575125 --- /dev/null +++ b/app/model/ResultModel.js @@ -0,0 +1,39 @@ +class ResultModel { + constructor(data, message) { + if (typeof data === "string") { + this.message = data; + data = null; + message = null; + } + if (data) { + this.data = data; + } + + if (message) { + this.message = message; + } + } + } + + // 成功模型 + class SuccessResult extends ResultModel { + constructor(data, message) { + super(data, message); + this.errno = 0; + } + } + + // 失败模型 + class ErrorResult extends ResultModel { + constructor(data, message) { + super(data, message); + this.errno = -1; + } + } + + module.exports = { + SuccessResult, + ErrorResult, + }; + + \ No newline at end of file diff --git a/app/model/SearchModel.js b/app/model/SearchModel.js new file mode 100644 index 0000000..56623d1 --- /dev/null +++ b/app/model/SearchModel.js @@ -0,0 +1,25 @@ +class SearchModel { + constructor() { + this.source_models = {}; + } + + registerSourceModel(source_model) { + this.source_models[source_model.getName()] = source_model; + } + + getSourceList() { + return Object.keys(this.source_models); + } + + searchMusic(source_name, keywords) { + if(this.source_models.hasOwnProperty(source_name)) { + return this.source_models[source_name].search(keywords); + }else { + return "无效的搜索源"; + } + } +} + +module.exports = { + SearchModel +} \ No newline at end of file diff --git a/app/model/ServerModel.js b/app/model/ServerModel.js new file mode 100644 index 0000000..f09ac6b --- /dev/null +++ b/app/model/ServerModel.js @@ -0,0 +1,33 @@ +const exp = require("constants"); + +class ServerModel { + constructor() { + this.gets = {}; + this.posts = {}; + } + + get(url, callback) { + this.gets[url] = callback; + } + + post(url, callback) { + this.posts[url] = callback; + } + + trigger(method, url, req, res, query, data) { + if(method == 'GET') { + if(this.gets.hasOwnProperty(url)) { + return this.gets[url](req, res, query); + } + }else if(method == 'POST') { + if(this.posts.hasOwnProperty(url)) { + return this.posts[url](req, res, query, data); + } + } + return "无效的请求"; + } +} + +module.exports = { + ServerModel +} \ No newline at end of file diff --git a/app/model/SourceModel.js b/app/model/SourceModel.js new file mode 100644 index 0000000..aab0803 --- /dev/null +++ b/app/model/SourceModel.js @@ -0,0 +1,22 @@ +class SourceModel { + constructor() { + this.name = ""; + } + + getName() { + return this.name; + } + + search(keywords) { + return { + source_name: "", + name: "", + music_url: "", + lyric_url: "", + } + } +} + +module.exports = { + SourceModel +} \ No newline at end of file diff --git a/app/source/QQSource.js b/app/source/QQSource.js new file mode 100644 index 0000000..304066c --- /dev/null +++ b/app/source/QQSource.js @@ -0,0 +1,21 @@ +const { SourceModel } = require('../model/SourceModel'); + +class QQSource extends SourceModel { + constructor() { + super(); + this.name = "QQ"; + } + + search(keywords) { + return { + source_name: this.getName(), + name: keywords, + music_url: "http://123g", + lyric_url: "http://245", + } + } +} + +module.exports = { + QQSource +} \ No newline at end of file