aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/guff/api-content-type.cr2
-rw-r--r--src/guff/handler.cr2
-rw-r--r--src/guff/handlers.cr38
-rw-r--r--src/guff/model.cr21
-rw-r--r--src/guff/models.cr36
-rw-r--r--src/guff/run.cr6
-rw-r--r--src/guff/server.cr20
7 files changed, 76 insertions, 49 deletions
diff --git a/src/guff/api-content-type.cr b/src/guff/api-content-type.cr
index 9b6c74a..d6e1f83 100644
--- a/src/guff/api-content-type.cr
+++ b/src/guff/api-content-type.cr
@@ -6,7 +6,7 @@ module Guff
}
private def get_content_type
- CONTENT_TYPES[@model.config["environment"]]
+ CONTENT_TYPES[@models.config["environment"]]
end
end
end
diff --git a/src/guff/handler.cr b/src/guff/handler.cr
index 125e7e7..ce225eb 100644
--- a/src/guff/handler.cr
+++ b/src/guff/handler.cr
@@ -4,7 +4,7 @@ module Guff
class Handler < ::HTTP::Handler
getter :model
- def initialize(@model : Model)
+ def initialize(@models : Models)
end
def call(context : HTTP::Server::Context)
diff --git a/src/guff/handlers.cr b/src/guff/handlers.cr
index 4ebeae7..6eeb389 100644
--- a/src/guff/handlers.cr
+++ b/src/guff/handlers.cr
@@ -8,54 +8,54 @@ module Guff
HANDLERS = [{
# TODO: add production error handler
env: %w{development},
- init: ->(model : Model) {
+ init: ->(models : Models) {
HTTP::ErrorHandler.new as HTTP::Handler
},
}, {
- init: ->(model : Model) {
+ init: ->(models : Models) {
HTTP::LogHandler.new as HTTP::Handler
},
}, {
- init: ->(model : Model) {
+ init: ->(models : Models) {
HTTP::DeflateHandler.new as HTTP::Handler
},
}, {
env: %w{development},
- init: ->(model : Model) {
- TestHandler.new(model) as HTTP::Handler
+ init: ->(models : Models) {
+ TestHandler.new(models) as HTTP::Handler
},
}, {
- init: ->(model : Model) {
- APIHandler.new(model) as HTTP::Handler
+ init: ->(models : Models) {
+ APIHandler.new(models) as HTTP::Handler
},
}, {
- init: ->(model : Model) {
- BlogHandler.new(model) as HTTP::Handler
+ init: ->(models : Models) {
+ BlogHandler.new(models) as HTTP::Handler
},
}, {
- init: ->(model : Model) {
- SlugHandler.new(model) as HTTP::Handler
+ init: ->(models : Models) {
+ SlugHandler.new(models) as HTTP::Handler
},
}, {
- init: ->(model : Model) {
- HTTP::StaticFileHandler.new(model.config["public"]) as HTTP::Handler
+ init: ->(models : Models) {
+ HTTP::StaticFileHandler.new(models.config["public"]) as HTTP::Handler
},
}, {
- init: ->(model : Model) {
- NotFoundHandler.new(model) as HTTP::Handler
+ init: ->(models : Models) {
+ NotFoundHandler.new(models) as HTTP::Handler
},
}]
- def self.get(model : Model) : Array(HTTP::Handler)
+ def self.get(models : Models) : Array(HTTP::Handler)
HANDLERS.select { |h|
if h.has_key?(:env)
- (h[:env] as Array(String)).includes?(model.config["environment"])
+ (h[:env] as Array(String)).includes?(models.config["environment"])
else
true
end
}.map { |h|
- init = (h[:init] as Proc(Model, HTTP::Handler))
- init.call(model)
+ init = (h[:init] as Proc(Models, HTTP::Handler))
+ init.call(models)
}
end
end
diff --git a/src/guff/model.cr b/src/guff/model.cr
index 1df437b..ef1b457 100644
--- a/src/guff/model.cr
+++ b/src/guff/model.cr
@@ -3,22 +3,13 @@ require "./database-updater"
module Guff
class Model
- getter :config
- getter :db
-
- def initialize(@config : Config)
- # create site database
- db_path = "%s/site.db" % [config["data"]]
-
- # update database (if necessary)
- update_db(db_path)
-
- # open db
- @db = Database.new(db_path)
+ def initialize(@models : Models)
end
+ end
- private def update_db(path)
- DatabaseUpdater.run(path, @config)
- end
+ class PostModel < Model
+ end
+
+ class TagModel < Model
end
end
diff --git a/src/guff/models.cr b/src/guff/models.cr
new file mode 100644
index 0000000..7962c73
--- /dev/null
+++ b/src/guff/models.cr
@@ -0,0 +1,36 @@
+require "./database"
+require "./database-updater"
+
+private macro define_model_getters(hash)
+ {% for name, klass in hash %}
+ def {{ name.id }}
+ @cache[name] ||= {{ klass.id }}.new(self)
+ end
+ {% end %}
+end
+
+module Guff
+ class Models
+ getter :config
+ getter :db
+
+ def initialize(@config : Config)
+ # build model cache
+ @cache = {} of Symbol => Model
+
+ # path to db
+ path = "%s/site.db" % [config["data"]]
+
+ # update database (if necessary)
+ DatabaseUpdater.run(path, @config)
+
+ # open db
+ @db = Database.new(path)
+ end
+
+ define_model_getters({
+ post: PostModel,
+ tag: TagModel,
+ })
+ end
+end
diff --git a/src/guff/run.cr b/src/guff/run.cr
index 90afbae..37ff8b3 100644
--- a/src/guff/run.cr
+++ b/src/guff/run.cr
@@ -7,10 +7,10 @@ module Guff
# parse env and cli options
config = Config.new(app, args)
- # create model
- model = Model.new(config)
+ # create model mapper
+ models = Models.new(config)
# create and run server
- Server.run(model)
+ Server.run(models)
end
end
diff --git a/src/guff/server.cr b/src/guff/server.cr
index 1d87db7..8df68fa 100644
--- a/src/guff/server.cr
+++ b/src/guff/server.cr
@@ -1,30 +1,30 @@
require "http/server"
require "./config"
-require "./model"
+require "./models"
require "./handlers"
module Guff
class Server
- def self.run(model : Model)
- new(model).run
+ def self.run(models : Models)
+ new(models).run
end
- def initialize(@model : Model)
+ def initialize(@models : Models)
# create server
@server = HTTP::Server.new(
- @model.config["host"],
- @model.config["port"].to_i,
- get_handlers(@model)
+ @models.config["host"],
+ @models.config["port"].to_i,
+ get_handlers(@models)
)
end
def run
- puts "listening on %s:%s" % %w{host port}.map { |k| @model.config[k] }
+ puts "listening on %s:%s" % %w{host port}.map { |k| @models.config[k] }
@server.listen
end
- private def get_handlers(model : Model) : Array(HTTP::Handler)
- @handlers ||= Handlers.get(model)
+ private def get_handlers(models : Models) : Array(HTTP::Handler)
+ @handlers ||= Handlers.get(models)
end
end
end