Class: FiveApples::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/fiveapples.rb

Constant Summary collapse

CONFIGDIRNAME =
'fiveapples'.freeze
DBFILENAME =
'fiveapples.db3'.freeze
CONFIGRU =
'config5.ru'.freeze
MEDIADIR =
'media'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Server

Returns a new instance of Server.



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
# File 'lib/fiveapples.rb', line 30

def initialize(options={})
  options ||= {}
  @role = options[:role]
# from libdir we readonly. --> can be a system (install) dir       
  @libdir = __dir__.dup
  
# apphome needs to be user-writeable !
  @apphome = if ( @role == :test )
    File.expand_path('testdb', @libdir)
  else
    gconfigdir = File.expand_path('.config', Dir.home)
    File.expand_path(CONFIGDIRNAME, gconfigdir)
  end
  
  $LOAD_PATH.unshift(@libdir) unless $LOAD_PATH.include?(@libdir)
  
  @systemdbname = File.join(@libdir, DBFILENAME)
  @homedbname = File.expand_path(DBFILENAME, @apphome)
  @media_root = File.expand_path(MEDIADIR, @apphome)
  @configru = File.expand_path(CONFIGRU, @libdir)
  @one_way = options[:one_way]
  @use_cdn = options[:use_cdn]
  
  # in testmode we dont log per default,
  # in non testmode we log per default,
  #  but this can be overriden with option
  @log = ( options[:log] || ( @role != :test ) )
  
  @resources = @use_cdn ? "https://openui5.hana.ondemand.com/#{OpenUI5::VERSION}/resources/sap-ui-core.js"  : '../resources/sap-ui-core.js'

  @ui5d = @one_way ? 'ui5_1way' : 'ui5'
  @port = 9494
  @host = if ( ( @role == :test ) && ENV['selenium_remote_url'] )
    FiveApples.test_host_ip()
  else
    'localhost'
  end
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



20
21
22
# File 'lib/fiveapples.rb', line 20

def db
  @db
end

#hostObject (readonly)

Returns the value of attribute host.



25
26
27
# File 'lib/fiveapples.rb', line 25

def host
  @host
end

#logObject

Returns the value of attribute log.



28
29
30
# File 'lib/fiveapples.rb', line 28

def log
  @log
end

#media_rootObject (readonly)

Returns the value of attribute media_root.



27
28
29
# File 'lib/fiveapples.rb', line 27

def media_root
  @media_root
end

#one_wayObject (readonly)

Returns the value of attribute one_way.



21
22
23
# File 'lib/fiveapples.rb', line 21

def one_way
  @one_way
end

#portObject (readonly)

Returns the value of attribute port.



26
27
28
# File 'lib/fiveapples.rb', line 26

def port
  @port
end

#resourcesObject (readonly)

Returns the value of attribute resources.



23
24
25
# File 'lib/fiveapples.rb', line 23

def resources
  @resources
end

#roleObject (readonly)

Returns the value of attribute role.



24
25
26
# File 'lib/fiveapples.rb', line 24

def role
  @role
end

#ui5dObject (readonly)

Returns the value of attribute ui5d.



22
23
24
# File 'lib/fiveapples.rb', line 22

def ui5d
  @ui5d
end

Instance Method Details

#db_breeder_table_photo_idObject



92
93
94
95
96
97
98
99
100
101
# File 'lib/fiveapples.rb', line 92

def db_breeder_table_photo_id
  return if @db.schema(:breeder).find{|col| col[0] == :photo_id }
  
  @db.alter_table :breeder do
    add_column :photo_id, Integer
    add_foreign_key [:photo_id], :photo, key: :id, 
                    name: :breeder_photo_id_fkey
  end
  
end

#db_create_photo_tableObject



83
84
85
86
87
88
89
90
# File 'lib/fiveapples.rb', line 83

def db_create_photo_table
  @db.create_table :photo do
    primary_key :id
    column :name, :text
    Integer :cultivar_id
    column :content_type, :text
  end 
end

#get_bindingObject

needed for ERB



119
120
121
# File 'lib/fiveapples.rb', line 119

def get_binding
  binding
end

#serveObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/fiveapples.rb', line 123

def serve
  
  puts "Fiveapples version #{FiveApples::VERSION} on #{@host} config #{@configru}"
  begin
  # this requires safrano ~> 0.3.5
    puts "Safrano version #{Safrano::VERSION}"
  rescue
  end
  
  Dir.chdir @libdir do
   
#       this is equivalent to  'rackup config.ru' but in same process  
    ARGV.clear
    ARGV << @configru
    Rack::Server.start 
  
  end

end

#setup_db_in_homedirObject



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/fiveapples.rb', line 69

def setup_db_in_homedir
  FileUtils.mkdir_p(@apphome) unless Dir.exist?(@apphome)
  if ( @role == :test )
    # in test-mode always start with a fresh DB copy
    FileUtils.cp(@systemdbname, @homedbname)
    
    # and reset the media directory 
    media_photo_dir = File.absolute_path('Photo', @media_root)
    FileUtils.remove_entry_secure(media_photo_dir) if File.exists?(media_photo_dir)
  else
    FileUtils.cp(@systemdbname, @homedbname) unless File.exist?(@homedbname)
  end
end

#startdbObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/fiveapples.rb', line 103

def startdb
  setup_db_in_homedir
  
  @db = Sequel::Model.db = Sequel.sqlite(@homedbname)
# data model for media entities table and relations      
  unless @db.table_exists? :photo
    db_create_photo_table
  end
  
  db_breeder_table_photo_id
  
  Kernel.at_exit { Sequel::Model.db.disconnect if Sequel::Model.db }
  
end