Skip to content

Commit c8238c4

Browse files
allow server creation directly through module
1 parent ec342e8 commit c8238c4

File tree

5 files changed

+68
-44
lines changed

5 files changed

+68
-44
lines changed

src/fort_global.cr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ require "./providers/index"
22

33
module Shivneri
44
class FortGlobal
5+
6+
57
@@app_name = "fort"
68
@@should_parse_cookie = true
79
@@walls = [] of Proc(RequestHandler, Wall)
@@ -72,7 +74,8 @@ module Shivneri
7274
return @@logger
7375
end
7476

75-
macro FortGlobal.is_env_production
77+
macro FortGlobal
78+
is_env_production
7679
return @@is_env_production
7780
end
7881
end

src/helpers/index.cr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ require "./remove_last_slash"
22
require "./route_parser"
33
require "./parse_cookie"
44
require "./parse_content_type"
5-
# require "./json_result"
65
require "./get_class_name"
76
require "./async"
87
require "./get_view_from_file"

src/helpers/json_result.cr

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/model/fort.cr

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,62 @@ module Shivneri
1313
include GENERIC
1414
include STRUCT
1515

16-
# include Enums
17-
1816
if (ENV.has_key?("CRYSTAL_ENV") == false)
1917
ENV["CRYSTAL_ENV"] = "development"
2018
end
2119

20+
def self.port
21+
return Fort.instance.port
22+
end
23+
24+
def self.add_wall(wall)
25+
# @walls.push(wall)
26+
end
27+
28+
def self.walls=(walls)
29+
walls.each do |wall|
30+
add_wall(wall)
31+
end
32+
end
33+
34+
def self.open(option : AppOption = AppOption.new)
35+
Fort.instance.create(option)
36+
end
37+
38+
def self.open(option : AppOption = AppOption.new, on_success : Proc(Nil) = ->{})
39+
Fort.instance.create(option, on_success)
40+
end
41+
42+
def self.open(option : AppOption = AppOption.new, on_success : Nil = nil)
43+
Fort.instance.create(option)
44+
end
45+
46+
# def self.open(option : AppOption = AppOption.new)
47+
# Fort.instance.create(option, ->{})
48+
# end
49+
50+
def self.close
51+
Fort.instance.destroy
52+
end
53+
54+
def self.routes=(routes)
55+
routes.each do |route|
56+
Fort.instance.register_controller(route[:controller], route[:path])
57+
end
58+
end
59+
60+
def self.port=(port : Int32)
61+
Fort.instance.port = port
62+
end
63+
2264
class Fort
2365
property port
66+
@@instance = Fort.new
67+
68+
def self.instance
69+
return @@instance
70+
end
71+
2472
# @server = nil;
2573
@error_handler : MODEL::ErrorHandler.class = ErrorHandler
2674
@walls = [] of Wall.class
@@ -33,22 +81,6 @@ module Shivneri
3381
@routes.push({controller_name: controller.name, path: path})
3482
end
3583

36-
def routes=(routes)
37-
routes.each do |route|
38-
register_controller(route[:controller], route[:path])
39-
end
40-
end
41-
42-
def add_wall(wall)
43-
@walls.push(wall)
44-
end
45-
46-
def walls=(walls)
47-
walls.each do |wall|
48-
add_wall(wall)
49-
end
50-
end
51-
5284
def initialize
5385
@server = HTTP::Server.new do |context|
5486
RequestHandler.new(context.request, context.response).handle
@@ -200,9 +232,9 @@ module Shivneri
200232
FortGlobal.folders = option.folders
201233
end
202234

203-
def create(option : AppOption = AppOption.new, on_success = nil)
204-
create(option)
205-
end
235+
# def create(option : AppOption = AppOption.new, on_success = nil)
236+
# create(option)
237+
# end
206238

207239
def create(option : AppOption = AppOption.new, on_success : Proc(Nil) = ->{})
208240
add_shield

tests/general/src/general.cr

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ require "./walls/wall_without_outgoing"
1414

1515
# TODO: Put your code here
1616

17-
class App < Fort
18-
def intialize
19-
end
20-
end
17+
# class App < Fort
18+
# def intialize
19+
# end
20+
# end
2121

2222
def init_app(on_success = nil)
23-
app = App.new
24-
routes = [{
23+
# app = App.new
24+
Shivneri.routes = [{
2525
controller: DefaultController,
2626
path: "/*",
2727
}, {
@@ -46,8 +46,8 @@ def init_app(on_success = nil)
4646
# routes.each do |route|
4747
# app.register_controller(route[:controller], route[:path])
4848
# end
49-
app.routes = routes
50-
app.walls = [WallWithoutOutgoing]
49+
# Shivneri.routes = routes
50+
Shivneri.walls = [WallWithoutOutgoing]
5151
app_option = AppOption.new
5252
path_of_static_folder = File.join(Dir.current, "static")
5353
app_option.folders = [{
@@ -57,9 +57,9 @@ def init_app(on_success = nil)
5757
path_alias: "/",
5858
path: path_of_static_folder,
5959
}]
60-
ENV["APP_URL"] = "http://localhost:#{app.port}"
61-
app.create(app_option, on_success)
62-
return app
60+
ENV["APP_URL"] = "http://localhost:#{Shivneri.port}"
61+
Shivneri.open(app_option, on_success)
62+
return Shivneri
6363
end
6464

6565
if (ENV["CRYSTAL_ENV"]? != "TEST")

0 commit comments

Comments
 (0)