Authored by 姜枫

add login

... ... @@ -11,7 +11,7 @@ import mount from 'koa-mount';
import serve from 'koa-static';
import body from './middleware/yoho-koa-body';
import convert from 'koa-convert';
import session from 'koa-session';
import Socket from 'socket.io';
import config from './config/config';
... ... @@ -26,7 +26,9 @@ const server = http.createServer(app.callback());
const io = new Socket(server);
ws.init(io);
app.keys = ['yoho-node-ci secret'];
app.use(serve(__dirname + '/public'));
app.use(convert(session({}, app)));
app.use(convert(body({
multipart: true,
queryString: {
... ...
'use strict';
import Router from 'koa-router';
import * as models from '../../models';
let r = new Router();
r.get('/init', async(ctx, next) => {
let clean = Object.keys(models).map(k => {
let db = models[k];
return db.remove({});
});
await Promise.all(clean);
let seeds = [];
Object.keys(models).forEach(k => {
let db = models[k];
let ss = db._seeds();
ss.forEach(s => {
seeds.push(db.insert(s));
});
});
await Promise.all(seeds);
ctx.body = 'success';
});
r.get('/test', async(ctx, next) => {
console.log('test');
ctx.body = await ctx.render('test', {
name: 'yoho'
});
});
export default r;
\ No newline at end of file
'use strict';
import Router from 'koa-router';
let r = new Router();
const login = {
loginPage: async (ctx, next) => {
await ctx.render('login', { layout: '' });
},
auth: async (ctx, next) => {
let username = ctx.request.body.username;
let password = ctx.request.body.password;
if (username === 'yoho' && password === 'yoho9646') {
ctx.session = {
user: 'yoho'
};
ctx.redirect('/projects');
ctx.status = 301;
} else {
ctx.flash = { error: '账户密码错误' };
await ctx.render('login', { layout: '', message: ctx.flash.error });
}
},
logout: (ctx, next) => {
ctx.session = null;
ctx.redirect('/');
ctx.status = 301;
}
};
r.get('/login', login.loginPage);
r.get('/logout', login.logout);
r.post('/auth', login.auth);
export default r;
\ No newline at end of file
... ...
... ... @@ -5,6 +5,8 @@ import moment from 'moment';
import Build from '../../ci/build';
import Deploy from '../../ci/deploy';
import ws from '../../../lib/ws';
import {
Building,
Project,
... ...
... ... @@ -7,6 +7,8 @@ import {
} from '../../models';
const r = new Router;
const envs = {
... ...
... ... @@ -7,6 +7,7 @@
import path from 'path';
import Koa from 'koa';
import convert from 'koa-convert';
import hbs from '../../middleware/yoho-koa-hbs';
import helpers from '../../lib/helpers';
import routers from './routers'
... ...
... ... @@ -3,17 +3,29 @@ import Router from 'koa-router';
import index from './actions/index';
import projects from './actions/projects';
import servers from './actions/servers';
import init from './actions/init';
import login from './actions/login';
const noAuth = new Router();
const base = new Router();
export default function(app) {
export default function (app) {
noAuth.use('', login.routes(), login.allowedMethods());
app.use(noAuth.routes(), noAuth.allowedMethods());
app.use(async (ctx, next) => {
if (ctx.session && ctx.session.user) {
await next();
} else {
ctx.redirect('/login');
}
});
base.use('/projects', projects.routes(), projects.allowedMethods());
base.use('/servers', servers.routes(), servers.allowedMethods());
base.use('', index.routes(), index.allowedMethods());
base.use('', init.routes(), init.allowedMethods());
app.use(base.routes(), base.allowedMethods());
}
\ No newline at end of file
... ...
... ... @@ -20,7 +20,7 @@
<div class="panel-heading">
<div class="pull-right">
<a class="btn btn-info btn-rounded mr5 log-btn"><i class="fa fa-eye"></i> 查看构建日志</a>
<a class="btn btn-warning btn-rounded mr5 rollback-btn"><i class="fa fa-reply"></i> 回滚</a>
{{!--<a class="btn btn-warning btn-rounded mr5 rollback-btn"><i class="fa fa-reply"></i> 回滚</a>--}}
<a class="btn btn-success btn-rounded mr20 build-btn"><i class="glyphicon glyphicon-plus"></i> 新增构建</a>
<a href="" class="tooltips panel-minimize"><i class="fa fa-minus"></i></a>
</div>
... ...
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<title>{{title}}</title>
<link href="css/style.default.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="js/html5shiv.js"></script>
<script src="js/respond.min.js"></script>
<![endif]-->
</head>
<body class="signin">
<section>
<div class="panel panel-signin">
<div class="panel-body">
<div class="logo text-center">
<img src="images/logo.png" alt="yoho!" >
</div>
<br />
<p class="text-center">{{message}}</p>
<div class="mb30"></div>
<form action="/auth" method="post">
<div class="input-group mb15">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input type="text" class="form-control" placeholder="Username" name="username">
</div><!-- input-group -->
<div class="input-group mb15">
<span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
<input type="password" class="form-control" placeholder="Password" name="password">
</div><!-- input-group -->
<div class="clearfix">
{{!--<div class="pull-left">
<div class="ckbox ckbox-primary mt10">
<input type="checkbox" id="rememberMe" value="1">
<label for="rememberMe">Remember Me</label>
</div>
</div>--}}
<div class="pull-right">
<button type="submit" class="btn btn-success">Sign In <i class="fa fa-angle-right ml5"></i></button>
</div>
</div>
</form>
</div><!-- panel-body -->
</div><!-- panel -->
</section>
<script src="js/jquery-1.11.1.min.js"></script>
<script src="js/jquery-migrate-1.2.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/modernizr.min.js"></script>
<script src="js/pace.min.js"></script>
<script src="js/retina.min.js"></script>
<script src="js/jquery.cookies.js"></script>
<script src="js/custom.js"></script>
</body>
</html>
... ...
... ... @@ -16,7 +16,7 @@
<div class="pull-right">
<form class="form form-search" action="search-results.html">
{{!--<form class="form form-search" action="search-results.html">
<input type="search" class="form-control" placeholder="Search" />
</form>
... ... @@ -131,20 +131,20 @@
</div>
<!-- dropdown-menu -->
</div>
<!-- btn-group -->
<!-- btn-group -->--}}
<div class="btn-group btn-group-option">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
<i class="fa fa-caret-down"></i>
<div class="btn-group">
<button id="logout" type="button" class="btn btn-default">
<i class="glyphicon glyphicon-log-out"></i> 退出
</button>
<ul class="dropdown-menu pull-right" role="menu">
{{!--<ul class="dropdown-menu pull-right" role="menu">
<li><a href="#"><i class="glyphicon glyphicon-user"></i> My Profile</a></li>
<li><a href="#"><i class="glyphicon glyphicon-star"></i> Activity Log</a></li>
<li><a href="#"><i class="glyphicon glyphicon-cog"></i> Account Settings</a></li>
<li><a href="#"><i class="glyphicon glyphicon-question-sign"></i> Help</a></li>
<li class="divider"></li>
<li><a href="#"><i class="glyphicon glyphicon-log-out"></i>Sign Out</a></li>
</ul>
</ul>--}}
</div>
<!-- btn-group -->
... ...
... ... @@ -16,7 +16,6 @@
<li><a href="/projects"><i class="glyphicon glyphicon-th"></i> <span>Projects</span></a></li>
<li class="parent"><a href=""><i class="fa fa-gears"></i> <span>Settings</span></a>
<ul class="children">
<li><a href="/test">Test</a></li>
<li><a href="/servers/setting">Servers</a></li>
</ul>
</li>
... ...
... ... @@ -9,14 +9,21 @@ class Ws {
init(io) {
this.io = io;
this.io.on('connection', this.onConn);
this.io.of('/building/log', this.onBuildingLogConn);
}
broadcast(path, msg) {
this.io.emit(path, msg);
}
onConn(ws) {
onConn(socket) {
console.log(socket.conn.id);
}
onBuildingLogConn(socket) {
socket.on('message', (msg) => {
console.log(msg);
});
}
}
... ...
... ... @@ -32,6 +32,7 @@
"author": "jiangfeng <jeff.jiang@yoho.cn>",
"license": "ISC",
"dependencies": {
"co": "^4.6.0",
"co-body": "^4.2.0",
"formidable": "^1.0.17",
"fs-promise": "^0.5.0",
... ... @@ -42,6 +43,7 @@
"koa-convert": "^1.2.0",
"koa-mount": "^2.0.0",
"koa-router": "^7.0.1",
"koa-session": "^3.3.1",
"koa-static": "^3.0.0",
"lodash": "^4.13.1",
"moment": "^2.13.0",
... ...
... ... @@ -98,6 +98,10 @@ jQuery(document).ready(function() {
}
}
jQuery('#logout').click(function(){
location.href = '/logout';
});
});
... ...