Skip to content

Instantly share code, notes, and snippets.

@RyanKung
Last active January 19, 2024 15:20
Show Gist options
  • Save RyanKung/5058726 to your computer and use it in GitHub Desktop.
Save RyanKung/5058726 to your computer and use it in GitHub Desktop.
Demo: A simple MVC-web-framework implementation with PLT Scheme (Racket)
;;By Jhen Kung, [email protected]
;;app.rkt
#lang racket(require web-server/servlet
web-server/servlet-env
"router.rkt")
(serve/servlet mordor
#:port 8080
#:servlet-path "/"
#:servlet-regexp #rx"")
;;By Jhen Kung, [email protected]
;;model.rkt
(require db)
(define connect
(mysql-connect #:user user
#:database db
#:password password))
(query-exec connect
"create temporary table the_numbers (n integer, d varchar(20))")
;;By Jhen Kung, [email protected]
;;router.rkt
(module router racket
(require web-server/servlet
web-server/servlet-env
"view.rkt")
(define-values (dispatch req-url)
;;router
(dispatch-rules
[("") test]
[("test") index]
[else index]))
(define (mordor req)
(dispatch req))
(provide mordor))
;;By Jhen Kung, [email protected]
;;view.rkt
(module view racket
(require web-server/servlet
web-server/servlet-env
web-server/templates)
(define (index req)
(response/xexpr
`(html (head (title ,(url->string (request-uri req))))
(body (h1 "PINTOV!")
(span ,(url->string (request-uri req)))
))))
(define (test req)
(response/full
200 #"Okay"
(current-seconds) TEXT/HTML-MIME-TYPE
empty
(list (string->bytes/utf-8 (include-template "index.html")))))
(provide index test))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment