-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.ru
82 lines (79 loc) · 2.13 KB
/
config.ru
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#vim: set filetype=ruby:
unless defined? Bundler
require 'rubygems'
require 'bundler'
end
Bundler.require
require 'active_record'
#simple save/load
class Map < ActiveRecord::Base
end
module MapRepo
extend self
def get_map(name)
map = Map.find_by_name name
if !map then
map = Map.create :name => name, :value=> File.read('map.json.js')
end
map.value
end
def save_map(name,value)
map = Map.find_by_name name
map = Map.create :name => name unless map
map.value = value
map.save!
end
end
configure :development do
require 'sqlite3'
MY_DB_NAME = "development"
MY_DB = SQLite3::Database.new(MY_DB_NAME)
# get active record set up
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => MY_DB_NAME)
if !Map.table_exists?
ActiveRecord::Base.connection.create_table(:maps) do |t|
t.column :name, :string
t.column :value, :string, :limit => 100000
end
end
end
configure :production do
dbconfig = YAML.load(File.read('config/database.yml'))
ActiveRecord::Base.establish_connection dbconfig['production']
if !Map.table_exists?
ActiveRecord::Base.connection.create_table(:maps) do |t|
t.column :name, :string
t.column :value, :string, :limit => 100000
end
end
end
class CivServer < Sinatra::Base
set :public, "."
helpers do
def map_file
return "resources/maps/#{params[:name]}.js"
end
end
post '/save/:name' do |name|
puts "saving #{name}"
if development? && File.exists?(map_file)
puts "saving to file"
File.open(map_file,"w") do |file|
file.puts params[:map]
end
else
puts "saving to db"
MapRepo.save_map name,params[:map]
end
end
get '/load/:name' do |name|
File.read(map_file) rescue MapRepo.get_map(name)
end
get '/' do
File.read('index.html')
end
get '/editor' do
File.read('index.html').gsub('GameApp.js','EditorApp.js')
end
end
run CivServer