From 99e507e3138dbb695af85f61ecb955b7ed5d2e47 Mon Sep 17 00:00:00 2001 From: Craig Oates Date: Tue, 25 Oct 2022 02:25:44 +0100 Subject: [PATCH] start implementing /danger/restore-snapshot defroute (web.lisp). The server needs to be restarted after restoring the website from a Snapshot. This commit has code which establishes if the website is running on localhost and informs the user to restart the server (most likely in SLIME) manaully. If the website is running in prod. and using Systemd, the service will need to be restarted that way (user doesn't have access to SBCL or SLIME in that context). So, a Bash script will need to be written and that script will need to be called using (most likely) utils:run-bash-command. At the moment, I haven't got far enough into developing this website to have established a Systemd service or running outside my local dev. machine. So, I have left a TODO comment here stating the prod. side of the defroute is not implemented yet. --- src/web.lisp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/web.lisp b/src/web.lisp index 7eddef0..bdba758 100644 --- a/src/web.lisp +++ b/src/web.lisp @@ -1758,6 +1758,44 @@ "You are not authorised to delete page." "error") (redirect "/login"))))))) +(defroute ("/danger/restore-snapshot" :method :POST) () + (destructuring-bind + (&key snapshot-name authenticity-token &allow-other-keys) + (utils:request-params (lack.request:request-body-parameters ningle:*request*)) + (cond ((not (string= authenticity-token (auth:csrf-token))) + `(,+forbidden+ (:content-type "text/plain") ("Denied"))) + (t (hermetic:auth + (:administrator) + ;; Authorised + (cond ((validation::string-is-nil-or-empty? snapshot-name) + (utils:set-alert "No Snapshot Name provided." + "missing-data") + (redirect "/danger/manage-snapshots")) + ((not (storage:raw-directory-exists? + (format nil "snapshots/~a/" snapshot-name))) + (utils:set-alert "Cannot find selected Snapshot." + "invalid-data") + (redirect "/danger/manage-snapshots")) + (t (snapshot:restore-from-snapshot snapshot-name) + (if (str:contains? + "://localhost:" (utils:build-url-root ningle:*request*)) + (utils:set-alert + "Snapshot restored. Restart local server for changes to take effect." + "success") + (progn + (utils:set-alert + "Snapshot restored restarting server. Please wait..." + "success") + ;; TODO: WRITE SCRIPT TO RESTART SYSTEMD SERVICE. + ;; utils:run-bash-command "Restart Systemd Service" + (format + t "[INFO] Restarting Systemd service is not yet implemented."))) + (redirect "/danger/manage-snapshots"))) + ;; Not Authorised + (progn (utils:set-alert + "You are not authorised to delete page." "error") + (redirect "/login"))))))) + ;; ;; Error pages