-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws.rb
95 lines (78 loc) · 2.06 KB
/
ws.rb
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
83
84
85
86
87
88
89
90
91
92
93
require 'csv'
require 'yaml'
require 'sinatra'
set :bind, '0.0.0.0'
set :port, 5678
config = YAML.load_file(ARGV.first)
types_file = 'types.yaml';
parents_file = 'parents.yaml';
data = {
name: nil,
type: nil,
parent: nil,
comment: nil,
}
types = if File.exist?(types_file) then
YAML.load_file(types_file).compact
else
[]
end
parents = if File.exist?(parents_file) then
YAML.load_file(parents_file).compact
else
[]
end
csv = CSV.open("warehouse.csv", ?a)
trap(:INT) {
csv.close
Sinatra::Base.quit!
}
get "/main.js" do
content_type "text/javascript"
erb "main.js".to_sym, locals: {
lang: config[:client][:lang],
select_word: config[:client][:select_word],
autostart: config[:client][:autostart],
form_names: config[:client][:form_names],
commands: config[:client][:commands]
}
end
get ?/ do
erb "index.htm".to_sym, locals: {
autostart: config[:client][:autostart],
types: types, parents: parents,
type: data[:type].to_s, parent: data[:parent].to_s, comment: data[:comment].to_s
}
end
post ?/ do
p params
name = params["name"]
type = params["type"]
parent = params["parent"]
comment = params["comment"]
if params.key? "save" then
csv.flush
else
if not type.nil? and not types.include?(type)then
types << type
File.write(types_file, types.to_yaml)
end
if not parent.nil? and not parents.include?(parent) then
parents << parent
File.write(parents_file, parents.to_yaml)
end
if type == config[:server][:type_storage] then
parents << name
File.write(parents_file, parents.to_yaml)
end
csv.puts [name, type, parent, comment] unless name.to_s.empty?
data[:type] = type||data[:type]
data[:parent] = parent||data[:parent]
data[:comment] = comment||data[:comment]
end
erb "index.htm".to_sym, locals: {
autostart: config[:client][:autostart],
types: types, parents: parents,
type: data[:type].to_s, parent: data[:parent].to_s, comment: data[:comment].to_s
}
end