Skip to content

Commit

Permalink
allow server creation directly through module
Browse files Browse the repository at this point in the history
  • Loading branch information
ujjwalguptaofficial committed Mar 1, 2020
1 parent ec342e8 commit c8238c4
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 44 deletions.
5 changes: 4 additions & 1 deletion src/fort_global.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ require "./providers/index"

module Shivneri
class FortGlobal


@@app_name = "fort"
@@should_parse_cookie = true
@@walls = [] of Proc(RequestHandler, Wall)
Expand Down Expand Up @@ -72,7 +74,8 @@ module Shivneri
return @@logger
end

macro FortGlobal.is_env_production
macro FortGlobal
is_env_production
return @@is_env_production
end
end
1 change: 0 additions & 1 deletion src/helpers/index.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ require "./remove_last_slash"
require "./route_parser"
require "./parse_cookie"
require "./parse_content_type"
# require "./json_result"
require "./get_class_name"
require "./async"
require "./get_view_from_file"
Expand Down
10 changes: 0 additions & 10 deletions src/helpers/json_result.cr

This file was deleted.

74 changes: 53 additions & 21 deletions src/model/fort.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,62 @@ module Shivneri
include GENERIC
include STRUCT

# include Enums

if (ENV.has_key?("CRYSTAL_ENV") == false)
ENV["CRYSTAL_ENV"] = "development"
end

def self.port
return Fort.instance.port
end

def self.add_wall(wall)
# @walls.push(wall)
end

def self.walls=(walls)
walls.each do |wall|
add_wall(wall)
end
end

def self.open(option : AppOption = AppOption.new)
Fort.instance.create(option)
end

def self.open(option : AppOption = AppOption.new, on_success : Proc(Nil) = ->{})
Fort.instance.create(option, on_success)
end

def self.open(option : AppOption = AppOption.new, on_success : Nil = nil)
Fort.instance.create(option)
end

# def self.open(option : AppOption = AppOption.new)
# Fort.instance.create(option, ->{})
# end

def self.close
Fort.instance.destroy
end

def self.routes=(routes)
routes.each do |route|
Fort.instance.register_controller(route[:controller], route[:path])
end
end

def self.port=(port : Int32)
Fort.instance.port = port
end

class Fort
property port
@@instance = Fort.new

def self.instance
return @@instance
end

# @server = nil;
@error_handler : MODEL::ErrorHandler.class = ErrorHandler
@walls = [] of Wall.class
Expand All @@ -33,22 +81,6 @@ module Shivneri
@routes.push({controller_name: controller.name, path: path})
end

def routes=(routes)
routes.each do |route|
register_controller(route[:controller], route[:path])
end
end

def add_wall(wall)
@walls.push(wall)
end

def walls=(walls)
walls.each do |wall|
add_wall(wall)
end
end

def initialize
@server = HTTP::Server.new do |context|
RequestHandler.new(context.request, context.response).handle
Expand Down Expand Up @@ -200,9 +232,9 @@ module Shivneri
FortGlobal.folders = option.folders
end

def create(option : AppOption = AppOption.new, on_success = nil)
create(option)
end
# def create(option : AppOption = AppOption.new, on_success = nil)
# create(option)
# end

def create(option : AppOption = AppOption.new, on_success : Proc(Nil) = ->{})
add_shield
Expand Down
22 changes: 11 additions & 11 deletions tests/general/src/general.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ require "./walls/wall_without_outgoing"

# TODO: Put your code here

class App < Fort
def intialize
end
end
# class App < Fort
# def intialize
# end
# end

def init_app(on_success = nil)
app = App.new
routes = [{
# app = App.new
Shivneri.routes = [{
controller: DefaultController,
path: "/*",
}, {
Expand All @@ -46,8 +46,8 @@ def init_app(on_success = nil)
# routes.each do |route|
# app.register_controller(route[:controller], route[:path])
# end
app.routes = routes
app.walls = [WallWithoutOutgoing]
# Shivneri.routes = routes
Shivneri.walls = [WallWithoutOutgoing]
app_option = AppOption.new
path_of_static_folder = File.join(Dir.current, "static")
app_option.folders = [{
Expand All @@ -57,9 +57,9 @@ def init_app(on_success = nil)
path_alias: "/",
path: path_of_static_folder,
}]
ENV["APP_URL"] = "http://localhost:#{app.port}"
app.create(app_option, on_success)
return app
ENV["APP_URL"] = "http://localhost:#{Shivneri.port}"
Shivneri.open(app_option, on_success)
return Shivneri
end

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

0 comments on commit c8238c4

Please sign in to comment.