Skip to content

Instantly share code, notes, and snippets.

@talbergs

talbergs/all.cr Secret

Created July 14, 2018 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save talbergs/c7c5c5b7dd56552363350358f5ba23a2 to your computer and use it in GitHub Desktop.
Save talbergs/c7c5c5b7dd56552363350358f5ba23a2 to your computer and use it in GitHub Desktop.
require "http/server"
require "json"
CONF_FILE = "./conf.json"
class Conf
JSON.mapping(app_name: String)
end
def update_conf
app_conf = get_conf
end
def get_conf : Conf
conf_io = File.open(CONF_FILE)
Conf.from_json(conf_io)
end
app_conf = get_conf
server = HTTP::Server.new do |context|
puts app_conf.app_name
context.response.print app_conf.app_name
end
def poll(interval : Time::Span, file_path : String, &block)
old_mtime = File.info(file_path).modification_time
loop do
sleep interval
current_mtime = File.info(file_path).modification_time
if old_mtime != current_mtime
old_mtime = current_mtime
block.call
end
end
end
spawn do
poll 1.seconds, "./conf.json", &->update_conf
end
server.bind_tcp 8080
server.listen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment