Skip to content
This repository has been archived by the owner on Nov 14, 2018. It is now read-only.

Commit

Permalink
picoappz
Browse files Browse the repository at this point in the history
  • Loading branch information
danmayer committed Jul 17, 2013
1 parent 04d685f commit 1356544
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 0 deletions.
1 change: 1 addition & 0 deletions .rvmrc
@@ -0,0 +1 @@
rvm use ruby-1.9.3-p392@picoappz --create
17 changes: 17 additions & 0 deletions Rakefile
@@ -0,0 +1,17 @@
require "rubygems"
require 'rake'
require 'rake/testtask'
require_relative 'lib/app'

task :default => :test

desc "run tests"
task :test do
# just run tests, nothing fancy
Dir["test/**/*.rb"].sort.each { |test| load test }
end

desc "Build files and upload to s3"
task :build do
PicoAppz.new.build
end
9 changes: 9 additions & 0 deletions app/views/index.html.erb
@@ -0,0 +1,9 @@
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title><%= title %></title>
</head>
<body>
<h1>Welboooooooooom one</h1>
<p>Now hosted on Amazon S3!</p>
</body>
</html>
41 changes: 41 additions & 0 deletions lib/app.rb
@@ -0,0 +1,41 @@
require 'fog'
require 'erubis'
require './lib/server-files.rb'
include ServerFiles

class PicoAppz

def initialize
end

def build
render_templates
upload_to_s3
end

private

def render_templates
puts 'building'
Dir["./app/views/**/*.erb"].each do |file|
puts "processing #{file}"
template = File.read(file)
rendered_file = Erubis::Eruby.new(template).result({:title => "picoappz"})
output_file = file.gsub(/\.erb/,'').gsub(/\/app\/views/,"/public")
File.open(output_file, 'w') {|f| f.write(rendered_file) }
end
end

def upload_to_s3
puts 'uploading'
Dir["./public/**/*.*"].each do |file|
unless File.directory?(file)
mimetype = `file -Ib #{file}`.gsub(/\n/,"")
filename = file.gsub(/\.\/public\//,'')
puts "uploading #{file} to #{filename}"
write_file(filename, File.read(file), :content_type => mimetype)
end
end
end

end
50 changes: 50 additions & 0 deletions lib/server-files.rb
@@ -0,0 +1,50 @@
module ServerFiles

# TODO how to allow a server to write files without exposing the shared secrets...
# Thinking a write ONLY ec2 key PER server
# the other thought was this always posts back to the requesting server which writes the file and knows where the data came from
# so these boxes have no sensative data on them
def connection
@connection ||= Fog::Storage.new(
:provider => 'AWS',
:aws_access_key_id => ENV['AMAZON_ACCESS_KEY_ID'],
:aws_secret_access_key => ENV['AMAZON_SECRET_ACCESS_KEY'])
end

def get_file(filename)
begin
file = directory.files.get(filename)
file.body
rescue
''
end
end

def write_commits(project_key, after_commit, commit_key, push)
commits_data = get_file(project_key)
@commits = JSON.parse(commits_data) rescue {}
@commits[after_commit] = {:uri => commit_key, :push => push }
write_file(project_key, @commits.to_json)
end

def write_file(filename, body, options = {})
file_options = {
:key => filename,
:body => body,
:public => true
}
if options[:content_type]
file_options[:content_type] = options[:content_type]
end
file = directory.files.new(file_options)
file.save
end

def directory
directory = connection.directories.create(
:key => "www.picoappz.com",
:public => true
)
end

end
9 changes: 9 additions & 0 deletions public/404.html
@@ -0,0 +1,9 @@
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Picoappz Error</title>
</head>
<body>
<h1>Picoappz Had a Error</h1>
<p>We are sorry, did you hit the right url, if so and you found a error let me know!</p>
</body>
</html>
9 changes: 9 additions & 0 deletions public/index.html
@@ -0,0 +1,9 @@
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>picoappz</title>
</head>
<body>
<h1>Welboooooooooom one</h1>
<p>Now hosted on Amazon S3!</p>
</body>
</html>

0 comments on commit 1356544

Please sign in to comment.