first commit
parent
eaaea7a27d
commit
65868e0578
|
@ -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");
|
||||
});
|
||||
|
|
@ -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,
|
||||
};
|
||||
|
||||
|
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue