core: provide context / services setup machinery
This commit is contained in:
parent
cb9b9204b4
commit
e19b60bf9f
5 changed files with 45 additions and 21 deletions
|
@ -4,8 +4,10 @@
|
||||||
(:use :common-lisp)
|
(:use :common-lisp)
|
||||||
(:local-nicknames (:config :scopes/config)
|
(:local-nicknames (:config :scopes/config)
|
||||||
(:message :scopes/core/message))
|
(:message :scopes/core/message))
|
||||||
(:export #:config #:service-config
|
(:export #:*root* #:setup #:find-service
|
||||||
#:context #:name #:actions #:add-action #:send
|
#:config #:service-config
|
||||||
|
#:name #:start #:actions
|
||||||
|
#:context #:add-action #:add-service #:send
|
||||||
#:action-spec
|
#:action-spec
|
||||||
#:printer))
|
#:printer))
|
||||||
|
|
||||||
|
@ -13,11 +15,12 @@
|
||||||
|
|
||||||
;;;; config
|
;;;; config
|
||||||
|
|
||||||
(defclass config (config:root)
|
(defclass config (config:root) ())
|
||||||
(services))
|
|
||||||
|
|
||||||
(defclass service-config (config:base)
|
(defclass service-config (config:base)
|
||||||
(start))
|
((name :reader name :initarg :name)
|
||||||
|
(start :reader start :initarg :start :initform nil)
|
||||||
|
(actions :reader actions :initarg :actions :initform nil)))
|
||||||
|
|
||||||
;;;; actions
|
;;;; actions
|
||||||
|
|
||||||
|
@ -48,11 +51,22 @@
|
||||||
|
|
||||||
;;;; context
|
;;;; context
|
||||||
|
|
||||||
(defvar *context* nil)
|
(defvar *root* nil)
|
||||||
|
|
||||||
(defclass context ()
|
(defclass context ()
|
||||||
((name :reader name :initarg :name)
|
((config :reader config :initarg :config)
|
||||||
(actions :accessor actions :initform nil)))
|
(actions :accessor actions :initform nil)
|
||||||
|
(services :accessor services :initform nil)))
|
||||||
|
|
||||||
|
(defun find-service (name)
|
||||||
|
(with-slots (services) *root*
|
||||||
|
(when services
|
||||||
|
(gethash name services))))
|
||||||
|
|
||||||
|
(defun setup (cfg)
|
||||||
|
(setf *root* (make-instance 'context :config cfg))
|
||||||
|
(dolist (c (config:children cfg))
|
||||||
|
(add-service *root* c)))
|
||||||
|
|
||||||
(defun add-action (ctx pat hdlr)
|
(defun add-action (ctx pat hdlr)
|
||||||
(let* ((acts (actions ctx))
|
(let* ((acts (actions ctx))
|
||||||
|
@ -62,6 +76,16 @@
|
||||||
(push (make-instance 'action-spec :pattern pat :handlers (list hdlr))
|
(push (make-instance 'action-spec :pattern pat :handlers (list hdlr))
|
||||||
(actions ctx)))))
|
(actions ctx)))))
|
||||||
|
|
||||||
|
(defun add-service (ctx cfg)
|
||||||
|
(with-slots (services) ctx
|
||||||
|
(unless services
|
||||||
|
(setf services (make-hash-table)))
|
||||||
|
;(let ((child (make-instance 'context :config cfg)))
|
||||||
|
(let ((child (funcall (start cfg) cfg)))
|
||||||
|
(dolist (a (actions cfg))
|
||||||
|
(add-action child (car a) (cadr a)))
|
||||||
|
(setf (gethash (name cfg) services) child))))
|
||||||
|
|
||||||
(defgeneric send (rcvr msg)
|
(defgeneric send (rcvr msg)
|
||||||
(:method ((rcvr context) msg)
|
(:method ((rcvr context) msg)
|
||||||
(let* ((acts (actions rcvr))
|
(let* ((acts (actions rcvr))
|
||||||
|
|
|
@ -4,3 +4,7 @@
|
||||||
|
|
||||||
(setf *config* (make-instance 'core:config))
|
(setf *config* (make-instance 'core:config))
|
||||||
|
|
||||||
|
(make-instance 'core:service-config :parent *config*
|
||||||
|
:name :test-receiver
|
||||||
|
:start #'(lambda (cfg) (make-instance 'test-receiver :config cfg))
|
||||||
|
:actions '(((:test) check-message)))
|
||||||
|
|
|
@ -18,8 +18,7 @@
|
||||||
;;;; core/testing: test-receiver
|
;;;; core/testing: test-receiver
|
||||||
|
|
||||||
(defclass test-receiver (core:context)
|
(defclass test-receiver (core:context)
|
||||||
((core:name :initform :test-rcvr)
|
((expected :accessor expected
|
||||||
(expected :accessor expected
|
|
||||||
:initform (make-hash-table :test #'equalp))))
|
:initform (make-hash-table :test #'equalp))))
|
||||||
|
|
||||||
(defun check-message (ctx msg)
|
(defun check-message (ctx msg)
|
||||||
|
@ -44,16 +43,16 @@
|
||||||
;;;; test runner
|
;;;; test runner
|
||||||
|
|
||||||
(defclass test-suite (t:test-suite)
|
(defclass test-suite (t:test-suite)
|
||||||
((receiver :reader receiver
|
((receiver :accessor receiver :initarg :receiver)))
|
||||||
:initform (make-instance 'test-receiver))))
|
|
||||||
|
|
||||||
(defun run ()
|
(defun run ()
|
||||||
(let* ((*config* nil)
|
(let* ((*config* nil)
|
||||||
(t:*test-suite* (make-instance 'test-suite :name "core"))
|
(t:*test-suite* (make-instance 'test-suite :name "core")))
|
||||||
(rcvr (receiver t:*test-suite*)))
|
|
||||||
(log4cl:log-config :sane :daily (t:test-path "scopes-test.log" "log"))
|
(log4cl:log-config :sane :daily (t:test-path "scopes-test.log" "log"))
|
||||||
(load (t:test-path "config-core" "etc"))
|
(load (t:test-path "config-core" "etc"))
|
||||||
(core:add-action rcvr '(:test) #'check-message)
|
;(core:add-action rcvr '(:test) #'check-message)
|
||||||
|
(core:setup *config*)
|
||||||
|
(setf (receiver t:*test-suite*) (core:find-service :test-receiver))
|
||||||
(test-send)
|
(test-send)
|
||||||
(check-expected)
|
(check-expected)
|
||||||
(t:show-result)))
|
(t:show-result)))
|
||||||
|
@ -63,6 +62,6 @@
|
||||||
(msg (message:simple-message '(:test :dummy) "dummy payload"))
|
(msg (message:simple-message '(:test :dummy) "dummy payload"))
|
||||||
(msg-exp (message:simple-message '(:test :dummy) "dummy payload")))
|
(msg-exp (message:simple-message '(:test :dummy) "dummy payload")))
|
||||||
(expect rcvr msg-exp)
|
(expect rcvr msg-exp)
|
||||||
(== (core:name rcvr) :test-rcvr)
|
(== (core:name (core:config rcvr)) :test-receiver)
|
||||||
(core:send rcvr msg)
|
(core:send rcvr msg)
|
||||||
))
|
))
|
||||||
|
|
|
@ -33,7 +33,4 @@
|
||||||
|
|
||||||
(t:deftest test-client (cfg)
|
(t:deftest test-client (cfg)
|
||||||
(== (client:url cfg) "http://localhost:8899")
|
(== (client:url cfg) "http://localhost:8899")
|
||||||
;(print (dexador:get "https://github.com")))
|
(== (dexador:get (client:url cfg)) "Hello World!"))
|
||||||
;(print (dexador:get "http://127.0.0.1:8899")))
|
|
||||||
(print (client:url cfg))
|
|
||||||
(print (dexador:get (client:url cfg))))
|
|
||||||
|
|
|
@ -24,7 +24,7 @@
|
||||||
|
|
||||||
(defun start (cfg)
|
(defun start (cfg)
|
||||||
(setf *listener*
|
(setf *listener*
|
||||||
(clack:clackup #'(lambda (env) (app env))
|
(clack:clackup #'app
|
||||||
:port (parse-integer (port cfg))
|
:port (parse-integer (port cfg))
|
||||||
:address (address cfg))))
|
:address (address cfg))))
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue