diff options
author | Alexander_Kabui | 2024-08-02 15:31:43 +0300 |
---|---|---|
committer | Alexander_Kabui | 2024-08-02 19:50:31 +0300 |
commit | 0baf76460ce9e15dceecafa5c82bad7e14314b20 (patch) | |
tree | 6c20c5741d58bf45fab3ce4c206db493798f839e /web/webserver.scm | |
parent | 35b092cf400a5f3fda788f5d74e9b030d81368dd (diff) | |
download | gn-guile-0baf76460ce9e15dceecafa5c82bad7e14314b20.tar.gz |
Add main endpoints for editing and committing file
Diffstat (limited to 'web/webserver.scm')
-rwxr-xr-x | web/webserver.scm | 65 |
1 files changed, 56 insertions, 9 deletions
diff --git a/web/webserver.scm b/web/webserver.scm index f9fc8a7..1e48285 100755 --- a/web/webserver.scm +++ b/web/webserver.scm @@ -22,6 +22,7 @@ (srfi srfi-19) ; time (srfi srfi-26) (rnrs io ports) ; bytevector-all + (rnrs bytevectors) (web http) (web client) (web request) @@ -148,6 +149,12 @@ otherwise search for set/group data" '() (map decode-query-component (string-split query #\&)))) +(define (decode-request-json body) + (if (not body) + '() + (json-string->scm (utf8->string body)))) + + (define (decode-query-component component) (let* ([index (string-index component #\=)] [key (if index (substring component 0 index) component)] @@ -155,22 +162,60 @@ otherwise search for set/group data" (cons (string->symbol (uri-decode key)) (uri-decode value)))) - - (define (edit-file-handler repo request) (catch 'file-error (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 - (build-json-response 400 (fetch-file repo query_path)) - (throw 'file-error (string-append "Please provide a valid file path in the query")) - ) - ) + (if query_path + ;;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")) + ) + ) ) (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 (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)) + ) + ) + +(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 + (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)) + ) + ) + (lambda (key . args) + (let ((msg (car args))) (build-json-response 400 `(("error" . ,key) ("msg" . ,msg)))) + )) ) (define (controller request body) (match-lambda (('GET) @@ -200,7 +245,9 @@ otherwise search for set/group data" (('GET "species") (render-json (get-species-meta))) (('GET "edit") - (edit-file-handler "/home/kabui/test_repo" request)) + (edit-file-handler global-repo request)) + (('POST "commit") + (commit-file-handler global-repo request body)) (('GET id) (let ([names (get-species-shortnames (get-expanded-species))]) (match (string->list id) |