Skip to content

Commit 7d7ba8b

Browse files
committed
initial commit
0 parents  commit 7d7ba8b

40 files changed

+1024
-0
lines changed

.gitignore

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
.trails
2+
.tmp
3+
4+
# Local
5+
config/local.js
6+
7+
# Logs
8+
logs
9+
*.log
10+
11+
# Runtime data
12+
pids
13+
*.pid
14+
*.seed
15+
16+
# Directory for instrumented libs generated by jscoverage/JSCover
17+
lib-cov
18+
19+
# Coverage directory used by tools like istanbul
20+
coverage
21+
22+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
23+
.grunt
24+
25+
# node-waf configuration
26+
.lock-wscript
27+
28+
# Compiled binary addons (http://nodejs.org/api/addons.html)
29+
build/Release
30+
31+
# Dependency directory
32+
node_modules
33+
34+
# Documentation folder
35+
docs
36+
37+
# Miscellaneous
38+
*~
39+
*#
40+
.DS_STORE
41+
.netbeans
42+
nbproject
43+
.idea
44+
.node_history
45+
.tmp
46+
*.sw*
47+
package-lock.json

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# <%= answers['app:name'] %>
2+
3+
[![NPM version][npm-image]][npm-url]
4+
[![Build status][ci-image]][ci-url]
5+
[![Dependency Status][daviddm-image]][daviddm-url]
6+
[![Code Climate][codeclimate-image]][codeclimate-url]
7+
8+
<%= answers['app:desc'] %>
9+
10+
## License
11+
MIT
12+
13+
[npm-image]: https://img.shields.io/npm/v/<%= answers['app:name'] %>.svg?style=flat-square
14+
[npm-url]: https://npmjs.org/package/<%= answers['app:name'] %>
15+
[ci-image]: https://img.shields.io/travis/trailsjs/<%= answers['app:name'] %>/master.svg?style=flat-square
16+
[ci-url]: https://travis-ci.org/trailsjs/<%= answers['app:name'] %>
17+
[daviddm-image]: http://img.shields.io/david/trailsjs/<%= answers['app:name'] %>.svg?style=flat-square
18+
[daviddm-url]: https://david-dm.org/trailsjs/<%= answers['app:name'] %>
19+
[codeclimate-image]: https://img.shields.io/codeclimate/github/trailsjs/<%= answers['app:name'] %>.svg?style=flat-square
20+
[codeclimate-url]: https://codeclimate.com/github/trailsjs/<%= answers['app:name'] %>

api/controllers/DefaultController.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict'
2+
3+
const Controller = require('trails/controller')
4+
5+
/**
6+
* @module DefaultController
7+
*
8+
* @description Default Controller included with a new Trails app
9+
* @see {@link http://trailsjs.io/doc/api/controllers}
10+
* @this TrailsApp
11+
*/
12+
module.exports = class DefaultController extends Controller {
13+
14+
/**
15+
* Return some info about this application
16+
*/
17+
info(req, res) {
18+
res.status(200).json(this.app.services.DefaultService.getApplicationInfo())
19+
}
20+
}

api/controllers/SocketController.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict'
2+
3+
const Controller = require('trails/controller')
4+
const jwt = require('jsonwebtoken')
5+
6+
/**
7+
* @module SocketController
8+
* @description socket controller.
9+
*/
10+
module.exports = class SocketController extends Controller {
11+
12+
home(req, res) {
13+
return res.sendFile('login.html', {root: './public'});
14+
}
15+
16+
login(req,res){
17+
18+
let model = req.body
19+
if(!model || ! model.username) return res.send('Invalid credential')
20+
21+
model.name = model.username
22+
let token = jwt.sign(model, 'jsbot', { expiresIn: 60*60*5 })
23+
24+
// res.cookie('sock_token', token, { expires: new Date(Date.now() + 900000) })
25+
res.json({ token })
26+
}
27+
28+
chat(req,res){
29+
return res.sendFile('chat.html', {root: './public'});
30+
}
31+
32+
group(req,res){
33+
return res.sendFile('group.html', {root: './public'});
34+
}
35+
}
36+

api/controllers/ViewController.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict'
2+
3+
const Controller = require('trails/controller')
4+
5+
module.exports = class ViewController extends Controller {
6+
helloWorld(req, res) {
7+
res.status(200).send('Hello Trails.js !')
8+
}
9+
}

api/controllers/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict'
2+
3+
exports.DefaultController = require('./DefaultController')
4+
exports.ViewController = require('./ViewController')
5+
exports.SocketController = require('./SocketController')

api/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict'
2+
3+
exports.controllers = require('./controllers')
4+
exports.models = require('./models')
5+
exports.policies = require('./policies')
6+
exports.services = require('./services')

api/models/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
'use strict'

api/policies/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
'use strict'

api/services/DefaultService.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict'
2+
3+
const Service = require('trails/service')
4+
5+
/**
6+
* @module DefaultService
7+
*
8+
* @description Default Service included with a new Trails app
9+
* @see {@link http://trailsjs.io/doc/api/services}
10+
* @this TrailsApp
11+
*/
12+
module.exports = class DefaultService extends Service {
13+
14+
/**
15+
* Return some info about this application
16+
*/
17+
getApplicationInfo() {
18+
const trailpacks = []
19+
Object.keys(this.app.packs).forEach(packName => {
20+
if (packName != 'inspect') {
21+
const pack = this.app.packs[packName]
22+
trailpacks.push({
23+
name: pack.name,
24+
version: pack.pkg.version
25+
})
26+
}
27+
})
28+
return {
29+
app: this.app.pkg.version,
30+
node: process.version,
31+
libs: process.versions,
32+
trailpacks: trailpacks
33+
}
34+
}
35+
}

api/services/SocketService.js

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
'use strict'
2+
3+
const Service = require('trails/service')
4+
const socketioJwt = require('socketio-jwt')
5+
6+
/**
7+
* @module SocketService
8+
* @description socket service
9+
*/
10+
module.exports = class SocketService extends Service {
11+
12+
constructor(app){
13+
14+
super(app)
15+
this.io = require('socket.io')
16+
}
17+
18+
socketInit(http){
19+
20+
this.io = this.io(http)
21+
22+
// On socket client connection
23+
this.io.sockets
24+
.on('connection', socketioJwt.authorize({
25+
secret: 'jsbot',
26+
timeout: 15000,
27+
}))
28+
.on('authenticated', (socket)=> {
29+
30+
// console.log(socket.id+' connected');
31+
// console.log(socket.decoded_token)
32+
33+
//HANDLERS
34+
/**
35+
* send all online friends list to all connected socket
36+
*/
37+
const getOnlineFriends = ()=> {
38+
39+
let data = Object.keys(this.io.sockets.sockets)
40+
this.io.emit('allOnlineFriends', { data })
41+
}
42+
/**
43+
* Send a message to particular socket
44+
* @param data
45+
*/
46+
const onMessageSubmit = (data)=> {
47+
this.io.to(data.id).emit('message', { user: socket.decoded_token, message: data.message });
48+
}
49+
/**
50+
* Subscribe to join a room
51+
* @param room
52+
*/
53+
const onSubscribe = (room)=> { socket.join(room); }
54+
/**
55+
* Unsubscribe to leave a room
56+
* @param room
57+
*/
58+
const onUnSubscribe = (room)=> { socket.leave(room); }
59+
/**
60+
* Broadcast a message in room
61+
* @param data
62+
*/
63+
const onBroadcastToRoom = (data)=> {
64+
socket.to(data.room).emit('message', { user: socket.decoded_token, message: data.message });
65+
}
66+
/**
67+
* Socket client disconnection
68+
*/
69+
const onDisconnect = ()=> {
70+
console.log(socket.id+' disconnected');
71+
getOnlineFriends();
72+
}
73+
74+
//LISTENERS
75+
getOnlineFriends()
76+
socket.on('messageSubmit', onMessageSubmit)
77+
socket.on('subscribe', onSubscribe)
78+
socket.on('unsubscribe', onUnSubscribe)
79+
socket.on('broadcast', onBroadcastToRoom)
80+
socket.on('disconnect', onDisconnect)
81+
});
82+
}
83+
}
84+

api/services/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
'use strict'
2+
3+
exports.DefaultService = require('./DefaultService')
4+
exports.SocketService = require('./SocketService')

config/database.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Database Configuration
3+
* (app.config.database)
4+
*
5+
* Configure the ORM layer, connections, etc.
6+
*
7+
* @see {@link http://trailsjs.io/doc/config/database}
8+
*/
9+
module.exports = {
10+
11+
/**
12+
* Define the database stores. A store is typically a single database.
13+
*
14+
* Use the SQLite3 by default for development purposes.
15+
*
16+
* Set production connection info in config/env/production.js
17+
*/
18+
stores: {
19+
20+
/**
21+
* Define a store called "local" which uses SQLite3 to persist data.
22+
*/
23+
/*
24+
sqlitedev: {
25+
database: 'dev',
26+
storage: './.tmp/dev.sqlite',
27+
host: '127.0.0.1',
28+
dialect: 'sqlite'
29+
}
30+
*/
31+
},
32+
33+
models: {
34+
defaultStore: 'sqlitedev',
35+
migrate: 'alter'
36+
}
37+
}

config/env/development.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict'
2+
3+
module.exports = {}

config/env/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict'
2+
3+
exports.development = require('./development')
4+
exports.staging = require('./staging')
5+
exports.production = require('./production')
6+
exports.testing = require('./testing')

config/env/production.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict'
2+
3+
const winston = require('winston')
4+
5+
module.exports = {
6+
7+
trailpack: {
8+
disabled: [
9+
'repl'
10+
]
11+
},
12+
13+
log: {
14+
logger: new winston.Logger({
15+
level: 'info',
16+
exitOnError: false,
17+
transports: [
18+
new winston.transports.Console({
19+
timestamp: true
20+
}),
21+
new winston.transports.File({
22+
name: 'info-file',
23+
level: 'info',
24+
filename: 'trails-info.log',
25+
timestamp: true
26+
}),
27+
new winston.transports.File({
28+
name: 'error-file',
29+
level: 'error',
30+
filename: 'trails-error.log',
31+
timestamp: true
32+
})
33+
]
34+
})
35+
}
36+
37+
}

config/env/staging.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict'
2+
3+
module.exports = {}

config/env/testing.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
3+
module.exports = {
4+
5+
trailpack: {
6+
disabled: [
7+
'repl'
8+
]
9+
}
10+
}

0 commit comments

Comments
 (0)