Hi Stephen,

On 3 Jan 2019, at 1:07, Stephen De Gabrielle wrote:

I'm fooling around with #lang web-server/insta, and I can serve .html files
by setting (static-files-path page-root), but I'd like to trigger my
servlet if there is a '?action=edit' parameter set.

e.g
https://localhost/start.html just serves the file (I can do this!)
https://localhost/start.html?action=edit calls my servlet (I can't work
out how to do this :( )

(There is a lot of documentation but I'm a beginner with webdev so don't
know where to look)

This doesn't work (in the sense that it is deliberately incomplete or broken), but just to elaborate on what Jay is suggesting, here's how I might get started on this kind of project:

#lang racket

(require net/url
         web-server/http
         web-server/servlet-env)

(define/contract (url->action url)
  (url? . -> . (or/c false/c string?))
  (define (action? x)
    (eq? 'action (car x)))
  (define result (assoc 'action (url-query url)))
  (cond [(pair? result)
         (bytes->string/utf-8 (cdr result))]
        [else
         #f]))

; build a path for interpreting the URL
; as pointing to something on the disk
(define/contract (url->path url)
  (url? . -> . path?)
  (apply build-path
         (map path/param-path (url-path url))))

(define/contract (edit-file p)
  (path? . -> . response?)
  (define ip (open-input-file p))
  (define response-body
    `(html
      (head
       (title "Edit"))
      (body
       (form
        ((action "/save-file")
         (method "post"))
        (textarea ,(path->string ip))))))
  (response/xexpr response-body))

(define/contract (save-file req)
  (request? . -> . response?)
  ;; process the changed file and generate a response
  1
  )

(define (start req)
  (define url (request-uri req))
  (define path (url->path url))
  (define action (url->action url))
  (cond [(string? action)
         (edit-file path)]
        [else
         ((url->path url))
         ]))

(serve/servlet
 start
 #:servlet-path ""
 #:servlet-regexp #rx"")

--
You received this message because you are subscribed to the Google Groups "Racket 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to