aboutsummaryrefslogtreecommitdiffstats
path: root/webstats
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2025-02-09 21:28:34 +0100
committerOscar Najera <hi@oscarnajera.com>2025-02-09 21:28:34 +0100
commit5b0e2eb19bb432e4e170d9bdb1cb8169d526801f (patch)
treecd73b826fb8efcfc0ce08aa56937dddf83b63766 /webstats
parent756fc597d9e4a77588341ce20cb9f28c8e9422af (diff)
downloadscratch-5b0e2eb19bb432e4e170d9bdb1cb8169d526801f.tar.gz
scratch-5b0e2eb19bb432e4e170d9bdb1cb8169d526801f.tar.bz2
scratch-5b0e2eb19bb432e4e170d9bdb1cb8169d526801f.zip
[webstats] define system and package image
Diffstat (limited to 'webstats')
-rw-r--r--webstats/makefile16
-rw-r--r--webstats/scratch.lisp32
-rw-r--r--webstats/server.lisp81
-rw-r--r--webstats/webstats.asd17
4 files changed, 102 insertions, 44 deletions
diff --git a/webstats/makefile b/webstats/makefile
new file mode 100644
index 0000000..1b3b830
--- /dev/null
+++ b/webstats/makefile
@@ -0,0 +1,16 @@
+##
+# Web stats
+#
+# @file
+# @version 0.1
+
+build:
+ sbcl \
+ --eval '(pushnew *default-pathname-defaults* asdf:*central-registry*)' \
+ --eval '(asdf:load-asd "webstats.asd")' \
+ --eval '(asdf:load-system :webstats)' \
+ --eval '(webstats::create-static-assets)' \
+ --eval '(asdf:make :webstats)' \
+ --eval '(quit)'
+
+# end
diff --git a/webstats/scratch.lisp b/webstats/scratch.lisp
new file mode 100644
index 0000000..b66310c
--- /dev/null
+++ b/webstats/scratch.lisp
@@ -0,0 +1,32 @@
+
+(hunchentoot:define-easy-handler (vars-disp :uri "/vars") ()
+ (setf (hunchentoot:content-type*) "text/plain")
+ (format nil "Hey ~{~A ~% ~}~%"
+ (list
+ (remote-addr*)
+ (header-in* :x-forwarded-for)
+ (authorization)
+ (hunchentoot::iso-time)
+ (request-method*)
+ (script-name*)
+ (query-string*)
+ (server-protocol*)
+ (return-code*)
+ (content-length*)
+ (referer)
+ (user-agent))))
+
+(hunchentoot:define-easy-handler (link :uri "/link") ()
+ (with-html-string
+ (:doctype)
+ (:html
+ (:header (:title "hu yu ipi")
+ (:script :src "/stats/stats.js" :type "text/javascript"))
+ (:body
+ (:p "learning "
+ (:a :href "vars" "some explodiert")
+ (:a :href "visit" "count visit"))
+ (:form :action "/stats/visit"
+ :method "post"
+ (:input :type "text" :name "title")
+ (:input :type "submit" :value "submit" :name "submit"))))))
diff --git a/webstats/server.lisp b/webstats/server.lisp
index 6c74b3e..b0b18d0 100644
--- a/webstats/server.lisp
+++ b/webstats/server.lisp
@@ -1,5 +1,3 @@
-(ql:quickload '(hunchentoot spinneret cl-yesql cl-yesql/sqlite parenscript cl-json))
-
(defpackage :webstats
(:use :common-lisp :hunchentoot :spinneret))
@@ -17,12 +15,12 @@
(ps::defprinter ps-js::for-of (var object body-block)
"for (const "(ps::ps-print var)" of "(ps::ps-print object)") "
(ps::ps-print body-block))
+
(in-package :webstats)
-(overlord:set-package-base "/home/titan/dev/scratch/webstats/")
(yesql:import log-queries
- :from "/home/titan/dev/scratch/webstats/queries.sql"
+ :from "queries.sql"
:as :cl-yesql/sqlite
:binding :all-functions)
@@ -39,29 +37,6 @@
;; :user-agent "sly"
;; :title "try"))
-
-(defvar *acceptor* (make-instance 'hunchentoot:easy-acceptor
- :port 4252))
-
-(hunchentoot:start *acceptor*)
-
-(hunchentoot:define-easy-handler (vars-disp :uri "/vars") ()
- (setf (hunchentoot:content-type*) "text/plain")
- (format nil "Hey ~{~A ~% ~}~%"
- (list
- (remote-addr*)
- (header-in* :x-forwarded-for)
- (authorization)
- (hunchentoot::iso-time)
- (request-method*)
- (script-name*)
- (query-string*)
- (server-protocol*)
- (return-code*)
- (content-length*)
- (referer)
- (user-agent))))
-
(hunchentoot:define-easy-handler
(visit :uri "/visit" :default-request-type :both)
(title page referer click)
@@ -80,23 +55,6 @@
(setf (hunchentoot:content-type*) "text/javascript")
(ps:ps-compile-file "stats.paren"))
-(hunchentoot:define-easy-handler (link :uri "/link") ()
- (with-html-string
- (:doctype)
- (:html
- (:header (:title "hu yu ipi")
- (:script :src "/stats/stats.js" :type "text/javascript"))
- (:body
- (:p "learning "
- (:a :href "vars" "some explodiert")
- (:a :href "visit" "count visit"))
-
- (:form :action "/stats/visit"
- :method "post"
- (:input :type "text" :name "title")
- (:input :type "submit" :value "submit" :name "submit"))))
- ))
-
(hunchentoot:define-easy-handler (metric :uri "/metric.json") (q)
(setf (hunchentoot:content-type*) "application/json")
(sqlite:with-open-database (db "test.db")
@@ -126,3 +84,38 @@
(ps:chain (fetch "/stats/metric.json")
(then #'response-to-json)
(then #'plot)))))))))))
+
+(defvar *acceptor*)
+
+(defun start-server (port)
+ (setf *acceptor*
+ (make-instance 'hunchentoot:easy-acceptor :port port))
+ (hunchentoot:start *acceptor*))
+
+(defun main ()
+ (let ((port (parse-integer (or (uiop:getenv "PORT")
+ "4252"))))
+ (start-server port)
+ (format *standard-output* "Hunchentoot server started on port ~d.~&" port)
+ (handler-case (bt:join-thread
+ (find-if (lambda (th)
+ (search "hunchentoot" (bt:thread-name th)))
+ (bt:all-threads)))
+ ;; Catch a user's C-c
+ (#+sbcl sb-sys:interactive-interrupt
+ #+ccl ccl:interrupt-signal-condition
+ #+clisp system::simple-interrupt-condition
+ #+ecl ext:interactive-interrupt
+ #+allegro excl:interrupt-signal
+ () (progn
+ (format *error-output* "Aborting.~&")
+ (hunchentoot:stop *acceptor*)
+ (uiop:quit)))
+ (error (c) (format t "Woops, an unknown error occured:~&~a~&" c)))))
+
+(defun create-static-assets ()
+ (let ((ps:*ps-print-pretty* nil))
+ (with-open-file (ps:*parenscript-stream*
+ "stats.js" :direction :output
+ :if-exists :supersede)
+ (ps:ps-compile-file "stats.paren"))))
diff --git a/webstats/webstats.asd b/webstats/webstats.asd
new file mode 100644
index 0000000..688c4f5
--- /dev/null
+++ b/webstats/webstats.asd
@@ -0,0 +1,17 @@
+(defsystem "webstats"
+ :version "0.1.0"
+ :author "Oscar"
+ :license "AGPL-3"
+ :depends-on ("hunchentoot"
+ "cl-json"
+ "spinneret"
+ "cl-yesql"
+ "cl-yesql/sqlite"
+ "parenscript")
+ :components ((:file "server")
+ (:static-file "queries.sql")
+ (:static-file "stats.paren"))
+ :description ""
+ :build-operation "program-op" ;; leave as is
+ :build-pathname "webstats.bin"
+ :entry-point "webstats::main")