Skip to content

Commit

Permalink
Add simple web-based chat app
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Apr 21, 2018
1 parent 566e6de commit 23a9344
Show file tree
Hide file tree
Showing 34 changed files with 744 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/utopia/.bowerrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"directory": "lib/components",
"public": "public/_components"
}
9 changes: 9 additions & 0 deletions examples/utopia/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Development specific:
.rspec_status
.tags*

# Temporary data should not be added to the repository:
tmp/

# This file should only ever exist on production, and may contain sensitive information:
config/environment.yaml
4 changes: 4 additions & 0 deletions examples/utopia/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--format documentation
--backtrace
--warnings
--require spec_helper
35 changes: 35 additions & 0 deletions examples/utopia/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

source "https://rubygems.org"

gem "utopia", "~> 2.3.0"
# gem "utopia-gallery"
# gem "utopia-analytics"

gem "rake"
gem "bundler"

gem 'async-websocket'

gem "rack-freeze", "~> 1.2"

group :development do
# For `rake server`:
gem "guard-falcon", require: false
gem 'guard-rspec', require: false

# For `rake console`:
gem "pry"
gem "rack-test"

# For `rspec` testing:
gem "rspec"
gem "simplecov"

# For testing:
gem 'async-rspec'
end

group :production do
# Used for passenger-config to restart server after deployment:
gem "passenger"
end
29 changes: 29 additions & 0 deletions examples/utopia/Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

group :development do
guard :falcon, port: 9292 do
watch('Gemfile.lock')
watch('config.ru')
watch(%r{^config|lib|pages/.*})

notification :off
end
end

group :test do
guard :rspec, cmd: 'rspec' do
# Notifications can get a bit tedious:
# notification :off

# Re-run specs if they are changed:
watch(%r{^spec/.+_spec\.rb$})
watch('spec/spec_helper.rb') {'spec'}

# Run relevent specs if files in `lib/` or `pages/` are changed:
watch(%r{^lib/(.+)\.rb$}) {|match| "spec/lib/#{match[1]}_spec.rb" }
watch(%r{^pages/(.+)\.(rb|xnode)$}) {|match| "spec/pages/#{match[1]}_spec.rb"}
watch(%r{^pages/(.+)controller\.rb$}) {|match| Dir.glob("spec/pages/#{match[1]}*_spec.rb")}

# If any files in pages changes, ensure the website still works:
watch(%r{^pages/.*}) {'spec/website_spec.rb'}
end
end
16 changes: 16 additions & 0 deletions examples/utopia/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Example WebSocket Chat Server

This is a simple chat client/server implementation with specs.

## Starting Development Server

To start the development server, simply execute

> rake
Generating transient session key for development...
20:57:36 - INFO - Starting Falcon HTTP server on localhost:9292
20:57:36 - INFO - Guard::RSpec is running
20:57:36 - INFO - Guard is now watching at '...'
[1] guard(main)>

Then browse http://localhost:9292 (or as specified) to see your new site.
8 changes: 8 additions & 0 deletions examples/utopia/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

require 'pathname'
SITE_ROOT = Pathname.new(__dir__).realpath

# Load all rake tasks:
import(*Dir.glob('tasks/**/*.rake'))

task :default => :development
47 changes: 47 additions & 0 deletions examples/utopia/config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env rackup

require_relative 'config/environment'

require 'rack/freeze'

if RACK_ENV == :production
# Handle exceptions in production with a error page and send an email notification:
use Utopia::Exceptions::Handler
use Utopia::Exceptions::Mailer
else
# We want to propate exceptions up when running tests:
use Rack::ShowExceptions unless RACK_ENV == :test

# Serve the public directory in a similar way to the web server:
use Utopia::Static, root: 'public'
end

use Rack::Sendfile

use Utopia::ContentLength

use Utopia::Redirection::Rewrite,
'/' => '/client/index'

use Utopia::Redirection::DirectoryIndex

use Utopia::Redirection::Errors,
404 => '/errors/file-not-found'

use Utopia::Localization,
:default_locale => 'en',
:locales => ['en', 'de', 'ja', 'zh']

require 'utopia/session'
use Utopia::Session,
:expires_after => 3600 * 24,
:secret => ENV['UTOPIA_SESSION_SECRET']

use Utopia::Controller

use Utopia::Static

# Serve dynamic content
use Utopia::Content

run lambda { |env| [404, {}, []] }
7 changes: 7 additions & 0 deletions examples/utopia/config/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Utopia Config

This directory contains `environment.rb` which is used to initialize the running environment for tasks and servers.

## Setting Environment Variables

If you wish to set environment variables on a per-deployment basis, you can do so by creating an `config/environment.yaml` and populating it with key-value pairs.
8 changes: 8 additions & 0 deletions examples/utopia/config/environment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

require 'bundler/setup'
Bundler.setup

require 'utopia/setup'
Utopia.setup

RACK_ENV = ENV.fetch('RACK_ENV', :development).to_sym unless defined? RACK_ENV
1 change: 1 addition & 0 deletions examples/utopia/lib/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
You can add additional code for your application in this directory, and require it directly from the config.ru.
2 changes: 2 additions & 0 deletions examples/utopia/pages/_heading.xnode
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?r document.attributes[:title] ||= content ?>
<h1><utopia:content/></h1>
30 changes: 30 additions & 0 deletions examples/utopia/pages/_page.xnode
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html>
<head>
<?r response.cache! ?>

<?r if title = self[:title] ?>
<title>#{title.gsub(/<.*?>/, "")} - Utopia</title>
<?r else ?>
<title>Utopia</title>
<?r end ?>

<base href="#{first.node.uri_path}"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>

<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous" />

<link rel="icon" type="image/png" href="/_static/icon.png" />
<link rel="stylesheet" href="/_static/site.css" type="text/css" media="screen" />
</head>

<body class="#{attributes[:class]}">
<header>
<img src="/_static/utopia.svg" />
</header>

<div id="page">
<utopia:content/>
</div>
</body>
</html>
28 changes: 28 additions & 0 deletions examples/utopia/pages/client/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

var url = new URL('/server/connect', window.location.href);
url.protocol = url.protocol.replace('http', 'ws');

console.log("Connecting to server", url);
var server = new WebSocket(url.href);
console.log("Connected to", server);

server.onopen = function(event) {
chat.onkeypress = function(event) {
if (event.keyCode == 13) {
server.send(JSON.stringify({text: chat.value}));

chat.value = "";
}
}
};

server.onmessage = function(event) {
console.log("Got message", event);

var message = JSON.parse(event.data);

var pre = document.createElement('pre');
pre.innerText = message.text;

response.appendChild(pre);
};
Empty file.
8 changes: 8 additions & 0 deletions examples/utopia/pages/client/index.xnode
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<content:page>
<content:heading>Client</content:heading>

<script src="client.js?#{rand}"></script>
<section id="response" style="border: 1px;"></section>

<input id="chat" />
</content:page>
5 changes: 5 additions & 0 deletions examples/utopia/pages/errors/exception.xnode
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<content:page>
<content:heading>Exception</content:heading>

<p>It seems like something didn't quite work out as expected!</p>
</content:page>
5 changes: 5 additions & 0 deletions examples/utopia/pages/errors/file-not-found.xnode
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<content:page>
<content:heading>File Not Found</content:heading>

<p>The file you requested is unfortunately not available at this time!</p>
</content:page>
2 changes: 2 additions & 0 deletions examples/utopia/pages/links.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
errors:
display: false
21 changes: 21 additions & 0 deletions examples/utopia/pages/server/controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

prepend Actions

require 'async/websocket/server'

$connections = []

on 'connect' do |request|
Async::WebSocket::Server.open(request.env) do |connection|
$connections << connection

while message = connection.next_message
$connections.each do |connection|
puts "Server sending message: #{message.inspect}"
connection.send_message(message)
end
end
end

succeed!
end
Binary file added examples/utopia/public/_static/icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 23a9344

Please sign in to comment.