diff options
author | Alexander_Kabui | 2024-08-02 18:51:37 +0300 |
---|---|---|
committer | Alexander_Kabui | 2024-08-02 19:50:41 +0300 |
commit | 3f23cf320239cab558594b98ce75177e6fe3dac0 (patch) | |
tree | ec040bccce8e650096fb7d0c2210ce4073870efa /web | |
parent | 0baf76460ce9e15dceecafa5c82bad7e14314b20 (diff) | |
download | gn-guile-3f23cf320239cab558594b98ce75177e6fe3dac0.tar.gz |
refactoring: code cleanup
Diffstat (limited to 'web')
-rw-r--r-- | web/view/markdown.scm | 33 | ||||
-rwxr-xr-x | web/webserver.scm | 52 |
2 files changed, 31 insertions, 54 deletions
diff --git a/web/view/markdown.scm b/web/view/markdown.scm index 30c9f33..900378e 100644 --- a/web/view/markdown.scm +++ b/web/view/markdown.scm @@ -67,13 +67,12 @@ (let* ( (abs_path (string-append repo "/" query_path))) (if (file-exists? abs_path) (let* ((full_path (canonicalize-path abs_path)) (content (call-with-input-file full_path get-string-all)) + (commit-sha (get-latest-commit-sha1 repo)) ) `(("path" . ,query_path) ("content" . ,content) - ("hash" . "commit hash here")) - ) (throw 'file-error (string-append "the file path " abs_path " does not exists"))) - )) - + ("hash" . ,commit-sha)) + ) (throw 'file-error (string-append "the file path " abs_path " does not exists"))))) (define (git-invoke repo-path . args) @@ -81,9 +80,8 @@ (define (is-repo? repo-path) (let ((data (git-invoke repo-path "rev-parse")) ) - (zero? data) - ) - ) + (zero? data)) +) (define (get-latest-commit-sha1 repo-path) @@ -93,18 +91,13 @@ commit-sha)) - - (define (commit-file repo file-path content commit-message username email) (if (is-repo? repo) (match (file-exists? (string-append repo "/" file-path)) (#t (with-output-to-file (string-append repo "/" file-path) (lambda () - (display content) - ) - ) - ;;prevent users from commit other people changes check for git add status + (display content))) (let* ((git-add-file (git-invoke repo "add" file-path)) (git-commit-file (git-invoke repo "commit" @@ -114,18 +107,10 @@ ) (git-commit-sha (get-latest-commit-sha1 repo)) ) - ;; check if git add had an error ;; check git commit if had an error - ;;runs the risk of commit a different user changes check this ;; potential for collisions (if (zero? git-commit-file) `(("status" . "201") ("message" . "committed file successfully") ("content" . ,content) ("commit-sha" . ,git-commit-sha) ("commit-message" . ,commit-message) ) `(("status" . "200") ("message" . "Nothing to commit, working tree clean") ("commit-sha" . ,git-commit-sha)) - ) - ) - ) + ))) (#f - (throw 'system-error (string-append file-path " File does not exist Error")) - ) - ) - (throw 'system-error (string-append repo " Is not a git repo.")) - ) - ) + (throw 'system-error (string-append file-path " File does not exist Error")))) + (throw 'system-error (string-append repo " Is not a git repo.")))) diff --git a/web/webserver.scm b/web/webserver.scm index 1e48285..d40621f 100755 --- a/web/webserver.scm +++ b/web/webserver.scm @@ -167,7 +167,7 @@ otherwise search for set/group data" (lambda () (let* ((params (decode-query (uri-query (request-uri request)))) (query_path (assoc-ref params 'file_path))) (if query_path - ;;add check for is repo + ;;TODO add check for is repo (build-json-response 400 (fetch-file repo query_path)) (throw 'file-error (string-append "Please provide a valid file path in the query")) ) @@ -176,43 +176,37 @@ otherwise search for set/group data" (lambda (key . args) (let ((msg (car args))) (build-json-response 400 `(("error" . ,key) ("msg" . ,msg)))) ))) - -(define global-repo "/home/kabui/test_repo") ;; todo ;; fix this ;;check on how to define global variables - - +(define global-repo "/home/kabui/test_repo") ;; todo ;; fix this (define (is-invalid-data? data target) - ;;maybe apply items here - (let ((target target) (b 2)) - (display target) - ) (if (string? (assoc-ref data target)) (if (string-null? (assoc-ref data target)) (throw 'system-error (format #f "Value for Key *** ~a *** Cannot be Empty" target)) (assoc-ref data target) ) - (throw 'system-error (format #f "The Key *** ~a *** is missing in your Data" target)) - ) - ) + (throw 'system-error (format #f "The Key *** ~a *** is missing in your Json Data" target)) + )) (define (commit-file-handler repo request body) - ;; add validations for email and fetch content - ;;write a macro for this too repetettive ;; may extracting keys and data + ;; TODO add validations for is email ;; maybe send a request to gn-auth + ;;call gn-auth to validate token + ;; TODO add validation for valid markdown (catch 'system-error (lambda () - ;; to much repetition - ;; (lamba (data , values (for-each lambda (value) check here thow error) - ;; maybe change data you pass - (let* ((post-data (decode-request-json body)) - (file-name (is-invalid-data? post-data "filename")) - (content (is-invalid-data? post-data "content")) - (username (is-invalid-data? post-data "username")) - (email (is-invalid-data? post-data "email")) - (commit-message (is-invalid-data? post-data "commit")) - ) - - (build-json-response 200 (commit-file global-repo file-name content commit-message username email)) - ) - ) + (let* ((post-data + (decode-request-json body) + ) + (_ (for-each (lambda (target) + (is-invalid-data? post-data target) + ) '("filename" "content" "username" "email" "commit")) + ) + (file-name (assoc-ref post-data "filename")) + (content (assoc-ref post-data "content")) + (username (assoc-ref post-data "username")) + (email (assoc-ref post-data "email")) + (commit-message (assoc-ref post-data "commit")) + ) + (build-json-response 200 (commit-file repo file-name content commit-message username email)) + )) (lambda (key . args) (let ((msg (car args))) (build-json-response 400 `(("error" . ,key) ("msg" . ,msg)))) )) ) @@ -289,6 +283,4 @@ otherwise search for set/group data" (newline) (let ((listen (inexact->exact (string->number (car (cdr args)))))) (display `("listening on" ,listen)) - ;; (write listen) - ;; (run-server hello-world-handler 'http `(#:port ,listen)))) (start-web-server "127.0.0.1" listen))) |