Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tv_panel
Manage
Activity
Members
Plan
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container registry
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hermod
tv_panel
Commits
4092e8b0
Commit
4092e8b0
authored
Apr 14, 2018
by
Ayc0
Browse files
Options
Downloads
Patches
Plain Diff
Serve front from back
parent
e3b50384
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
server/index.js
+27
-68
27 additions, 68 deletions
server/index.js
server/socket.js
+75
-0
75 additions, 0 deletions
server/socket.js
with
102 additions
and
68 deletions
server/index.js
+
27
−
68
View file @
4092e8b0
const
fetch
=
require
(
'
node-fetch
'
);
const
http
=
require
(
'
http
'
);
const
fs
=
require
(
'
fs
'
);
const
path
=
require
(
'
path
'
);
const
path
=
require
(
'
path
'
);
const
socketIO
=
require
(
'
socket.io
'
);
const
{
port
}
=
require
(
'
./config
'
);
const
socket
=
require
(
'
./socket
'
);
const
{
const
indexPath
=
path
.
resolve
(
__dirname
,
'
../front/build/index.html
'
);
uuid
,
fontSize
,
rowHeight
,
port
,
api
,
}
=
require
(
'
./config
'
);
const
io
=
require
(
'
socket.io
'
)(
port
||
3000
);
const
server
=
(
req
,
res
)
=>
{
const
{
createSignedJWT
,
interval
}
=
require
(
'
./utils
'
);
let
url
=
req
.
url
.
replace
(
/
(
^
\/
|
\/
$
)
/g
,
''
).
trim
();
if
(
url
===
''
)
{
const
{
spawn
}
=
require
(
'
child_process
'
);
url
=
'
index.html
'
;
const
dummyResponse
=
require
(
'
./dummyResponse.json
'
);
const
useDummy
=
false
;
let
version
;
const
checkVersion
=
(
newVersion
)
=>
{
console
.
log
(
version
!==
newVersion
?
'
Update code
'
:
''
);
if
(
version
&&
version
!==
newVersion
)
{
const
updateServer
=
spawn
(
path
.
resolve
(
__dirname
,
'
../scripts/update_server.sh
'
));
updateServer
.
stdout
.
on
(
'
data
'
,
(
data
)
=>
{
process
.
stdout
.
write
(
data
);
});
}
}
version
=
newVersion
;
console
.
log
(
url
);
};
const
askedPath
=
path
.
resolve
(
__dirname
,
`../front/build/
${
url
}
`
);
const
filePath
=
fs
.
existsSync
(
askedPath
)
?
askedPath
:
indexPath
;
const
doScreenApiRequest
=
(
socket
,
userid
=
null
)
=>
{
fs
.
readFile
(
filePath
,
(
err
,
data
)
=>
{
const
query
=
userid
?
`?userid=
${
userid
}
`
:
''
;
if
(
err
)
{
return
fetch
(
`
${
api
.
url
}
/
${
api
.
version
}
/screen/
${
uuid
}${
query
}
`
,
{
res
.
writeHead
(
500
);
headers
:
{
res
.
end
(
'
Error loading index.html
'
);
Autorization
:
`Bearer
${
createSignedJWT
()}
`
,
return
;
},
})
.
then
(
rawRes
=>
rawRes
.
json
())
.
then
((
res
)
=>
{
checkVersion
(
res
.
version
);
socket
.
emit
(
'
panel_data
'
,
res
);
return
res
.
ttl
;
})
.
catch
(
console
.
log
);
};
const
setChrono
=
(
socket
)
=>
{
if
(
useDummy
)
{
return
interval
(()
=>
socket
.
emit
(
'
panel_data
'
,
dummyResponse
),
10000
);
}
}
return
interval
(()
=>
doScreenApiRequest
(
socket
),
10000
);
};
io
.
of
(
'
/
'
).
on
(
'
connection
'
,
(
socket
)
=>
{
socket
.
emit
(
'
config
'
,
{
fontSize
,
rowHeight
});
const
chrono
=
setChrono
(
socket
);
res
.
writeHead
(
200
);
chrono
.
startNow
();
res
.
end
(
data
);
// Respond to date message with the date
socket
.
on
(
'
date
'
,
()
=>
{
socket
.
emit
(
'
date
'
,
{
date
:
Date
.
now
()
});
});
socket
.
on
(
'
disconnect
'
,
()
=>
{
chrono
.
stop
();
});
});
};
const
badgeChild
=
spawn
(
path
.
resolve
(
__dirname
,
'
../scripts/nfc-poll-wrapper.sh
'
));
const
app
=
http
.
createServer
(
server
);
app
.
listen
(
port
||
3000
);
badgeChild
.
stdout
.
on
(
'
data
'
,
(
data0
)
=>
{
const
io
=
socketIO
(
app
);
const
userid
=
data0
.
toString
().
trim
();
console
.
log
(
`child stdout:
${
userid
}
`
);
doScreenApiRequest
(
socket
,
userid
).
then
(
ttl
=>
chrono
.
restart
(
ttl
));
io
.
of
(
'
/
'
).
on
(
'
connection
'
,
socket
);
});
});
This diff is collapsed.
Click to expand it.
server/socket.js
0 → 100644
+
75
−
0
View file @
4092e8b0
const
fetch
=
require
(
'
node-fetch
'
);
const
path
=
require
(
'
path
'
);
const
{
uuid
,
fontSize
,
rowHeight
,
api
,
}
=
require
(
'
./config
'
);
const
{
spawn
}
=
require
(
'
child_process
'
);
const
dummyResponse
=
require
(
'
./dummyResponse.json
'
);
const
{
createSignedJWT
,
interval
}
=
require
(
'
./utils
'
);
const
useDummy
=
false
;
let
version
;
const
checkVersion
=
(
newVersion
)
=>
{
console
.
log
(
version
!==
newVersion
?
'
Update code
'
:
''
);
if
(
version
&&
version
!==
newVersion
)
{
const
updateServer
=
spawn
(
path
.
resolve
(
__dirname
,
'
../scripts/update_server.sh
'
));
updateServer
.
stdout
.
on
(
'
data
'
,
(
data
)
=>
{
process
.
stdout
.
write
(
data
);
});
}
version
=
newVersion
;
};
const
doScreenApiRequest
=
(
socket
,
userid
=
null
)
=>
{
const
query
=
userid
?
`?userid=
${
userid
}
`
:
''
;
return
fetch
(
`
${
api
.
url
}
/
${
api
.
version
}
/screen/
${
uuid
}${
query
}
`
,
{
headers
:
{
Autorization
:
`Bearer
${
createSignedJWT
()}
`
,
},
})
.
then
(
rawRes
=>
rawRes
.
json
())
.
then
(
async
(
res
)
=>
{
await
checkVersion
(
res
.
version
);
socket
.
emit
(
'
panel_data
'
,
res
);
return
res
.
ttl
;
})
.
catch
(
console
.
log
);
};
const
setChrono
=
(
socket
)
=>
{
if
(
useDummy
)
{
return
interval
(()
=>
socket
.
emit
(
'
panel_data
'
,
dummyResponse
),
10000
);
}
return
interval
(()
=>
doScreenApiRequest
(
socket
),
10000
);
};
module
.
exports
=
(
socket
)
=>
{
socket
.
emit
(
'
config
'
,
{
fontSize
,
rowHeight
});
const
chrono
=
setChrono
(
socket
);
chrono
.
startNow
();
// Respond to date message with the date
socket
.
on
(
'
date
'
,
()
=>
{
socket
.
emit
(
'
date
'
,
{
date
:
Date
.
now
()
});
});
socket
.
on
(
'
disconnect
'
,
()
=>
{
chrono
.
stop
();
});
const
badgeChild
=
spawn
(
path
.
resolve
(
__dirname
,
'
../scripts/nfc-poll-wrapper.sh
'
));
badgeChild
.
stdout
.
on
(
'
data
'
,
(
data0
)
=>
{
const
userid
=
data0
.
toString
().
trim
();
console
.
log
(
`child stdout:
${
userid
}
`
);
doScreenApiRequest
(
socket
,
userid
).
then
(
ttl
=>
chrono
.
restart
(
ttl
));
});
};
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment