<commit_before>;; Main web-server file
;; (declaim (optimize (debug 3)))

(defpackage :piserv.main
  (:use :cl :hunchentoot :cl-who :ht-simple-ajax
	:asdf :piserv.static)
  (:export :start-server :stop-server :refresh))

(in-package :piserv.main)

(defvar *hunchentoot-server* nil
  "Hunchentoot server instance")

(defvar *admin-login* "admin"
  "Admin page password")

(defvar *admin-password* "<PASSWORD>"
  "Admin page password")

;;;;; First we create an ajax processor that will handle our function calls
(defvar *ajax-processor* 
  (make-instance 'ajax-processor :server-uri "/ajax"))

;;;;; Now we can define a function that we want to call from a web
;;;;; page. This function will take 'name' as an argument and return a
;;;;; string with a greeting.
(defun-ajax say-hi (name) (*ajax-processor*)
  (concatenate 'string "After server processing string is still " name))

;; Handler functions either return generated Web pages as strings,
;; or write to the output stream returned by write-headers

(defun update-sources-from-git ()
  (asdf:run-shell-command "git pull"))

(defun setup-dispatch-table ()
  "Set up dispatch table with file handlers for hunchentoot"
  (setq *dispatch-table*        
	(concatenate 'list
		     (piserv.static:generate-static-table)
		     (list
		      'dispatch-easy-handlers
		      (create-ajax-dispatcher *ajax-processor*)
		      ;; catch all
		      (lambda (request)
			(declare (ignore request))
			(redirect "/main"))))))

(defun stop-server ()
  "Stops the server"
  (when *hunchentoot-server*
    (stop *hunchentoot-server*)
    (setq *hunchentoot-server* nil)))

(defun start-server (&optional (port 8080) (adminpass "<PASSWORD>"))
  "Starts the server"
  (when *hunchentoot-server*
    (stop-server))
  (setup-dispatch-table)
  (setf *admin-password* <PASSWORD>)
  (setq *hunchentoot-server*
	(start (make-instance 'easy-acceptor :port port))))

(defmacro with-http-authentication (&rest body)
  `(multiple-value-bind (username password) (hunchentoot:authorization)
     (cond ((and (string= username *admin-login*) (string= password *admin-password*))
            ,@body)
           (t (hunchentoot:require-authorization "admin-login")))))

(define-easy-handler (admin :uri "/admin"
			    :default-request-type :get)
    ((action))
  (with-http-authentication
      (with-html-output-to-string (*standard-output* nil :prologue nil)
	(:html
	 (:head (:title "Admin page"))
	 (:body (:h3 "It's only for administration")
		(:p (cond ((equalp action "refresh")
			   (refresh)
			   (str "Handlers refreshed"))
			  ((equalp action "pull")
			   (with-html-output (*standard-output* nil)
			     (:p "Pull result: "
				 (fmt "~d" (update-sources-from-git))))))))))))


  
(define-easy-handler (easy-demo :uri "/main"
                                :default-request-type :get)
    ((state-variable :parameter-type 'string))
  (with-html-output-to-string (*standard-output* nil :prologue nil)
    (:html
     (:head (:title "Hello, world!")
	    (:link :rel "stylesheet" :type "text/css" :href "/main.css")
	    (:script :type "text/javascript" :src "/ecmalisp.js")
	    (princ (generate-prologue *ajax-processor*))
	    (:script :type "text/javascript" "
function pv(x) { return x==undefined? nil: x; }

lisp.write = function(str){
           document.getElementById('eval').innerHTML = str;
           return str;
        }
lisp.evalString(pv, '(CL:PACKAGE-NAME CL:*PACKAGE*)');

// will show the greeting in a message box<commit_msg>Added output line to shell git run<commit_after>;; Main web-server file
;; (declaim (optimize (debug 3)))

(defpackage :piserv.main
  (:use :cl :hunchentoot :cl-who :ht-simple-ajax
	:asdf :piserv.static)
  (:export :start-server :stop-server :refresh))

(in-package :piserv.main)

(defvar *hunchentoot-server* nil
  "Hunchentoot server instance")

(defvar *admin-login* "admin"
  "Admin page password")

(defvar *admin-password* "<PASSWORD>"
  "Admin page password")

;;;;; First we create an ajax processor that will handle our function calls
(defvar *ajax-processor* 
  (make-instance 'ajax-processor :server-uri "/ajax"))

;;;;; Now we can define a function that we want to call from a web
;;;;; page. This function will take 'name' as an argument and return a
;;;;; string with a greeting.
(defun-ajax say-hi (name) (*ajax-processor*)
  (concatenate 'string "After server processing string is still " name))

;; Handler functions either return generated Web pages as strings,
;; or write to the output stream returned by write-headers

(defun update-sources-from-git ()
  (with-output-to-string (asdf::*verbose-out*)
    (asdf:run-shell-command "git pull"))
  asdf:*verbose-out*)

(defun setup-dispatch-table ()
  "Set up dispatch table with file handlers for hunchentoot"
  (setq *dispatch-table*        
	(concatenate 'list
		     (piserv.static:generate-static-table)
		     (list
		      'dispatch-easy-handlers
		      (create-ajax-dispatcher *ajax-processor*)
		      ;; catch all
		      (lambda (request)
			(declare (ignore request))
			(redirect "/main"))))))

(defun stop-server ()
  "Stops the server"
  (when *hunchentoot-server*
    (stop *hunchentoot-server*)
    (setq *hunchentoot-server* nil)))

(defun start-server (&optional (port 8080) (adminpass "adminpassword"))
  "Starts the server"
  (when *hunchentoot-server*
    (stop-server))
  (setup-dispatch-table)
  (setf *admin-password* <PASSWORD>)
  (setq *hunchentoot-server*
	(start (make-instance 'easy-acceptor :port port))))

(defmacro with-http-authentication (&rest body)
  `(multiple-value-bind (username password) (hunchentoot:authorization)
     (cond ((and (string= username *admin-login*) (string= password *admin-password*))
            ,@body)
           (t (hunchentoot:require-authorization "admin-login")))))

(define-easy-handler (admin :uri "/admin"
			    :default-request-type :get)
    ((action))
  (with-http-authentication
      (with-html-output-to-string (*standard-output* nil :prologue nil)
	(:html
	 (:head (:title "Admin page"))
	 (:body (:h3 "It's only for administration")
		(:p (cond ((equalp action "refresh")
			   (refresh)
			   (str "Handlers refreshed"))
			  ((equalp action "pull")
			   (with-html-output (*standard-output* nil)
			     (:p "Pull result: "
				 (fmt "~a" (update-sources-from-git))))))))))))


  
(define-easy-handler (easy-demo :uri "/main"
                                :default-request-type :get)
    ((state-variable :parameter-type 'string))
  (with-html-output-to-string (*standard-output* nil :prologue nil)
    (:html
     (:head (:title "Hello, world!")
	    (:link :rel "stylesheet" :type "text/css" :href "/main.css")
	    (:script :type "text/javascript" :src "/ecmalisp.js")
	    (princ (generate-prologue *ajax-processor*))
	    (:script :type "text/javascript" "
function pv(x) { return x==undefined? nil: x; }

lisp.write = function(str){
           document.getElementById('eval').innerHTML = str;
           return str;
        }
lisp.evalString(pv, '(CL:PACKAGE-NAME CL:*PACKAGE*)');

// will show the greeting in a message box
{'fraction_non_alphanumeric': 0.1545210131597566, 'fraction_numerical': 0.0016980331116456771, 'mean_word_length': 3.5688429217840985, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '849323', 'n_tokens_mistral': 2432, 'n_tokens_neox': 2267, 'n_words': 638}