Skip to content

Commit 8e4238b

Browse files
committedAug 13, 2015
イベント通知機能
1 parent 054a797 commit 8e4238b

10 files changed

+154
-3
lines changed
 

‎.node-version

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v0.12.7

‎node/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

‎node/.python-version

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2.7.10

‎node/files/index.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<html>
2+
<head>Realtime Example</head>
3+
<body>
4+
<h1>Realtime Example</h1>
5+
<script src="/socket.io/socket.io.js"></script>
6+
<script>
7+
var socket = io();
8+
</script>
9+
</body>
10+
</html>

‎node/index.js

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
var app = require('express')();
2+
var http = require('http').Server(app);
3+
var io = require('socket.io')(http);
4+
var request = require('request');
5+
var bodyParser = require('body-parser');
6+
7+
app.use(bodyParser());
8+
9+
app.get('/realtime/', function(req, res){
10+
res.sendFile(__dirname + '/files/index.html');
11+
});
12+
13+
app.post('/events/', function(req, res){
14+
if(req.body){
15+
io.emit("event", req.body);
16+
}
17+
res.status(201);
18+
res.send("{}");
19+
});
20+
21+
io.on("connection", function(socket){
22+
// Authentication Check
23+
var headers = socket.handshake.headers;
24+
request({
25+
url:"http://localhost:8000/api/auth/",
26+
json:true,
27+
headers:{
28+
"Cookie":headers.cookie,
29+
"Accept":"application/json"
30+
}
31+
},function (error, response, body){
32+
if (!error && response.statusCode == 200 && body.is_staff) {
33+
allowed = true;
34+
}else{
35+
socket.disconnect();
36+
}
37+
});
38+
39+
40+
41+
42+
});
43+
44+
45+
http.listen(8001, function(){
46+
console.log('listening on *:8001');
47+
});
48+
49+
50+
51+
52+
53+
// Proxy Server for Development
54+
var http = require('http');
55+
var httpProxy = require('http-proxy');
56+
var url = require('url');
57+
var proxy = httpProxy.createProxyServer({});
58+
59+
var server = http.createServer(function (req, res) {
60+
61+
var hostname = req.headers.host.split(":")[0];
62+
var pathname = url.parse(req.url).pathname;
63+
64+
if(pathname.indexOf("/socket.io/") === 0 || pathname.indexOf("/realtime/") === 0 ){
65+
proxy.web(req, res, {
66+
target: "http://localhost:8001"
67+
});
68+
return;
69+
}
70+
71+
proxy.web(req, res, {
72+
target: "http://localhost:8000"
73+
});
74+
});
75+
76+
server.on('upgrade', function (req, socket, head) {
77+
console.log("upgrade");
78+
proxy.ws(req, socket, head, {
79+
target: {
80+
host: 'localhost',
81+
port: 8001
82+
}
83+
});
84+
});
85+
86+
server.listen(8080, function(){
87+
console.log('listening on *:8080');
88+
});

‎node/package.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "lepus-node",
3+
"version": "1.0.0",
4+
"description": "LapusはRESTfulなCTFサーバーです.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git@git.spica.bz:nomuken/lepus.git"
12+
},
13+
"author": "",
14+
"license": "ISC",
15+
"devDependencies": {
16+
"body-parser": "^1.13.3",
17+
"express": "^4.10.2",
18+
"http-proxy": "^1.11.1",
19+
"request": "^2.60.0",
20+
"socket.io": "^1.3.6"
21+
}
22+
}

‎package.pip

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ Markdown
33
django-filter
44
djangorestframework
55
django-debug-toolbar
6+
requests

‎src/lepus/middleware.py

+2
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ def process_request(self, request):
1111

1212
if user.is_authenticated() and ip:
1313
UserConnection.update(user, ip)
14+
15+
from lepus.signals import *

‎src/lepus/serializers.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ def update(self, instance, validated_data):
6363
class UserSerializer(serializers.ModelSerializer):
6464
class Meta:
6565
model = models.User
66-
fields = ("id", "username", "password", "team", "points", "last_score_time", "team_name", "team_password")
67-
read_only_fields = ("id", "team", "points", "last_score_time")
66+
fields = ("id", "username", "password", "team", "points", "last_score_time", "team_name", "team_password", "is_staff")
67+
read_only_fields = ("id", "team", "points", "last_score_time", "is_staff")
6868
extra_kwargs = {'password': {'write_only': True}}
6969

7070
team_name = serializers.CharField(write_only=True, allow_null=False, error_messages={"require":"チーム名は必須です"})
@@ -188,4 +188,3 @@ class NoticeSerializer(serializers.ModelSerializer):
188188
class Meta:
189189
model = models.Notice
190190
fields = ('id', 'title', 'body', 'created_at', 'updated_at')
191-

‎src/lepus/signals.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# encoding=utf-8
2+
import requests
3+
import json
4+
from django.core.signals import request_finished
5+
from django.dispatch import Signal, receiver
6+
from django.db.models.signals import post_save
7+
from lepus.models import Answer
8+
9+
def send_realtime_event(data):
10+
headers = {'Content-type': 'application/json', 'Accept': 'application/json'}
11+
response = requests.post("http://localhost:8001/events/", data=json.dumps(data), headers=headers)
12+
print(data)
13+
14+
@receiver(post_save, sender=Answer)
15+
def on_answer_sent(sender, **kwargs):
16+
if kwargs["created"]:
17+
answer = kwargs["instance"]
18+
data = {
19+
"type":"answer",
20+
"user":answer.user.id,
21+
"team":answer.team.id,
22+
"answer":answer.answer,
23+
"flag":answer.flag.id if answer.flag else None,
24+
"is_correct":answer.is_correct
25+
}
26+
send_realtime_event(data)

0 commit comments

Comments
 (0)
Please sign in to comment.