Last active
July 31, 2018 08:59
-
-
Save duangsuse/a7a96e81679572b0a8f6de252b085960 to your computer and use it in GitHub Desktop.
给 GeekApk 续命
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "json" | |
require "kemal" | |
# Matches GET "http://host:port/" | |
get "/" do | |
"Hello World!" | |
end | |
secs = 0 | |
msgs = Array(String).new | |
post "/p1s" do |env| | |
env.response.headers["Access-Control-Allow-Origin"] = "*" | |
secs += 1 | |
secs.to_s | |
end | |
post "/comment" do |env| | |
env.response.headers["Access-Control-Allow-Origin"] = "*" | |
msgs << if env.request.query_params.has_key?("content") | |
env.request.query_params["content"] | |
else | |
"GeekApk 万岁 🧀" | |
end | |
end | |
get "/comment/list" do |env| | |
env.response.headers["Access-Control-Allow-Origin"] = "*" | |
env.response.content_type = "application/json" | |
{list: msgs}.to_json | |
end | |
delete "/comment/:nth" do |env| | |
env.response.headers["Access-Control-Allow-Origin"] = "*" | |
if list.has_key?(env.query_params["nth"]) | |
list.delete_at(env.query_params["nth"]) | |
end | |
end | |
if ENV.has_key?("KEMAL_PORT") | |
Kemal.run(ENV["KEMAL_PORT"].to_i) | |
else | |
Kemal.run | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<!-- Copyright(C) 2018 duangsuse | |
love_geekapk.html is a standalone web fortend tool giving +1s to geekapk | |
Licensed under LGPLv3 --> | |
<html lang="zh-cn" dir="ltr"> | |
<head> | |
<!-- Basic tags --> | |
<title>给 GeekApk 续一秒</title> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0,maximum-scale=1.0, user-scalable=no"> | |
<!-- OpenGraph & SEO --> | |
<meta name="description" content="死去多时的 GeekApk 需要续命重启,你会给她续命吗?"> | |
<link rel="icon" href="https://geekapk.org/bitmap/symbolic/geekapk-128.png"> | |
<meta property="og:site_name" content="GeekApk"> | |
<meta property="og:title" content="+1s! GeekApk!"> | |
<meta property="og:description" content="死去多时的 GeekApk 需要续命重启,你会给她续命吗?"> | |
<!-- Useful tags --> | |
<meta name="theme-color" content="#009688"> | |
<meta content="index,follow" name="robots"> | |
<meta content="telphone=no, email=no" name="format-detection"> | |
<meta name="format-detection" content="telephone=no"> | |
<meta name="renderer" content="webkit"> | |
<meta http-equiv="Cache-Control" content="no-siteapp"> | |
<!-- MDUI <3 duangsuse --> | |
<link rel="stylesheet" href="https://cdnjs.loli.net/ajax/libs/mdui/0.4.1/css/mdui.min.css"> | |
<script src="https://cdnjs.loli.net/ajax/libs/mdui/0.4.1/js/mdui.min.js"></script> | |
<!-- Small intialize script --> | |
<script> | |
var $$ = mdui.JQ; | |
host = 'https://api.geekapk.org' | |
function getQueryParams(qs) { | |
qs = qs.split('+').join(' '); | |
var params = {}, | |
tokens, | |
re = /[?&]?([^=]+)=([^&]*)/g; | |
while (tokens = re.exec(qs)) { | |
params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]); | |
} | |
return params; | |
} | |
function tfmt(date) { | |
return date.getFullYear() + " 年 " + (date.getMonth() + 1) + " 月 " + date.getDate() + " " + date.getHours() + | |
":" + date.getMinutes() + ":" + date.getSeconds() | |
} | |
// should use dark style from 19:00 to 6:00 | |
function shouldApplyDarkStyle() { | |
var conf = localStorage['night'] | |
if (conf != null) { | |
if (conf == false.toString()) | |
return false; | |
else return true; | |
} | |
var dh = new Date(); | |
var nightBegin = localStorage['nightBegin'] || 19 | |
var nightEnd = localStorage['nightEnd'] || 6 | |
return dh.getHours() >= nightBegin && dh.getHours() <= 24 || dh.getHours() <= nightEnd | |
} | |
MDUI_DARK_CLASS = 'mdui-theme-layout-dark' | |
// call this to apply dark style automatically | |
function autodark() { | |
var body = mdui.JQ('body')[0] | |
var cls = body.classList | |
if (shouldApplyDarkStyle()) | |
cls.add(MDUI_DARK_CLASS) | |
else | |
cls.remove(MDUI_DARK_CLASS) | |
} | |
template = '<li class="mdui-list-item mdui-ripple"><a class="mdui-typo">%t</a></li>' | |
// Ruby dafa is good | |
puts = console.log | |
</script> | |
</head> | |
<!-- AppBar --> | |
<body class="mdui-theme-primary-teal mdui-theme-accent-pink"> | |
<div mdui-headroom="" class="mdui-appbar"> | |
<div class="mdui-toolbar mdui-color-theme"> | |
<!-- Title --> | |
<a id="title" class="mdui-text-color-white mdui-typo-title" mdui-tooltip="{content: '+1s! GeekApk!'}">GeekApk 续命平台</a> | |
<div class="mdui-toolbar-spacer"></div> | |
<!-- Action buttons --> | |
<a id="speak" class="mdui-btn mdui-btn-icon" mdui-tooltip="{content: '讲话'}"> | |
<i class="mdui-text-color-white mdui-icon material-icons">comment</i> | |
</a> | |
<a id="plus1s" class="mdui-btn mdui-btn-icon" mdui-tooltip="{content: '续命'}"> | |
<i class="mdui-text-color-white mdui-icon material-icons">airline_seat_flat</i> | |
</a> | |
</div> | |
</div> | |
<script> | |
function refresh() { | |
$$.ajax({ | |
method: 'GET', | |
url: host + '/comment/list', | |
success: (b) => { | |
var tmp = ''; | |
for (var i of JSON.parse(b)['list']) { | |
tmp += template.replace('%t', i.toString().replace("script", "em").replace( | |
"SCRIPT", | |
"bold")); | |
} | |
$$('#list')[0].innerHTML = tmp; | |
} | |
}); | |
} | |
function delete_nth(nth) { | |
$$.ajax({ | |
method: 'DELETE', | |
url: host + '/comment/' + nth, | |
success: (b) => mdui.snackbar("已删除", { timeout: 2000 })}); | |
} | |
$$('#speak').on('click', () => { | |
mdui.prompt("您要在续命大会上说什么", (v) => { | |
$$.ajax({ | |
method: 'POST', | |
url: host + '/comment?' + $$.param({ | |
content: v | |
}), | |
success: (b) => { | |
mdui.snackbar("成功发言!", { | |
timeout: 300 | |
}); | |
} | |
}); | |
}, (v, d) => d.close(), { | |
confirmOnEnter: true | |
}); | |
}); | |
$$('#plus1s').on('click', () => { | |
$$.ajax({ | |
method: 'POST', | |
url: host + '/p1s', | |
success: (b) => { | |
mdui.snackbar("成功续命一秒! life:" + b, { | |
timeout: 80 | |
}); | |
} | |
}); | |
}); | |
$$('#title').on('click', () => refresh()); | |
autodark(); | |
</script> | |
<div> | |
<ul id="list" class="mdui-list"> | |
<!-- List --> | |
</ul> | |
</div> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Description=Add 1 sec for geekapk | |
After=network.target | |
[Service] | |
WorkingDirectory=/root/ | |
ExecStart=/root/api | |
Restart=always | |
RestartSec=2 | |
Environment=KEMAL_ENV=production | |
Environment=KEMAL_PORT=80 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: api | |
version: 0.1.1 | |
dependencies: | |
kemal: | |
github: kemalcr/kemal |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment