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/webserver.scm | |
parent | 0baf76460ce9e15dceecafa5c82bad7e14314b20 (diff) | |
download | gn-guile-3f23cf320239cab558594b98ce75177e6fe3dac0.tar.gz |
refactoring: code cleanup
Diffstat (limited to 'web/webserver.scm')
-rwxr-xr-x | web/webserver.scm | 52 |
1 files changed, 22 insertions, 30 deletions
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))) |