Skip to content

Commit 7b847e0

Browse files
Adding presentation materials
1 parent b288a13 commit 7b847e0

12 files changed

+224
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello-world*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.bak
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# 1. Build Stage
2+
#https://hub.docker.com/r/racket/racket-ci
3+
FROM racket/racket:8.0-full AS builder
4+
WORKDIR /app
5+
COPY . /app
6+
RUN raco exe server.rkt
7+
ENV "FUNCTIONS_HTTPWORKER_PORT" 7071
8+
ENTRYPOINT [ "./server" ]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#lang web-server/insta
2+
3+
; A blog is a (listof post)
4+
; and a post is a (post title body)
5+
(struct post (title body))
6+
7+
; BLOG: blog
8+
; The static blog.
9+
(define BLOG
10+
(list (post "Second Post" "This is another post")
11+
(post "First Post" "This is my first post")))
12+
13+
; start: request -> response
14+
; Consumes a request, and produces a page that displays all of the
15+
; web content.
16+
(define (start request)
17+
(render-blog-page BLOG request))
18+
19+
; render-blog-page: blog request -> response
20+
; Consumes a blog and a request, and produces an HTML page
21+
; of the content of the blog.
22+
(define (render-blog-page a-blog request)
23+
(response/xexpr
24+
`(html (head (title "My Blog"))
25+
(body (h1 "My Blog")
26+
,(render-posts a-blog)))))
27+
28+
; render-post: post -> xexpr
29+
; Consumes a post, produces an xexpr fragment of the post.
30+
(define (render-post a-post)
31+
`(div ((class "post"))
32+
,(post-title a-post)
33+
(p ,(post-body a-post))))
34+
35+
; render-posts: blog -> xexpr
36+
; Consumes a blog, produces an xexpr fragment
37+
; of all its posts.
38+
(define (render-posts a-blog)
39+
`(div ((class "posts"))
40+
,@(map render-post a-blog)))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#lang web-server/insta
2+
3+
(define (start request)
4+
(response/xexpr
5+
'(html
6+
(head (title "My Blog"))
7+
(body (h1 "Under construction")))))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#lang racket
2+
3+
;;; This is a comment
4+
5+
6+
;;; Literals
7+
(println "Literals")
8+
9+
1
10+
"Hello World"
11+
12+
;;; Assign / Store values
13+
14+
(printf "~n")
15+
(println "Assign / Store Values")
16+
(define x 3)
17+
(println x)
18+
(printf "The values of x is ~v ~n" x)
19+
20+
;;; Operations
21+
22+
(printf "~n")
23+
(println "Operations")
24+
(+ 1 1)
25+
(/ 8 2)
26+
(+ x 2)
27+
28+
(define four
29+
(+ 3 1))
30+
31+
;;; Functions
32+
(printf "~n")
33+
(println "Functions")
34+
35+
;;; Define function
36+
(define (double n)
37+
(* n 2))
38+
39+
;;; Call function
40+
(double 3)
41+
42+
;;;Define function
43+
(define (make-animal-sound animal sound)
44+
(printf "The ~a says ~a ~n" animal sound))
45+
46+
;;; Partially apply function
47+
(define dog (curry make-animal-sound "dog"))
48+
(define cat (curry make-animal-sound "cat"))
49+
50+
;;; Partially apply
51+
(dog "woof")
52+
(cat "meow")
53+
54+
;;; Collections
55+
(printf "~n")
56+
(println "Collections")
57+
58+
;;; Pairs
59+
(cons 1 2) ;;; Pairs
60+
(define myPair '(1 2))
61+
(car myPair)
62+
(cdr myPair)
63+
64+
;;; List
65+
'(1 2 3 4)
66+
(define list1 (list 1 2 3 4))
67+
68+
;;; List operations
69+
(length list1)
70+
(map double list1)
71+
(filter
72+
(lambda (n)
73+
(= 0 (modulo n 2)))
74+
list1)
75+
76+
;;; Structs
77+
(printf "~n")
78+
(println "Structs")
79+
(struct point (x y))
80+
(define myPoint (point 1 2))
81+
(printf "x: ~a, y: ~a ~n" (point-x myPoint) (point-y myPoint))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"version": "2.0",
3+
"httpWorker": {
4+
"description": {
5+
"defaultExecutablePath": "server.exe"
6+
}
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 1. Build Stage
2+
#https://hub.docker.com/r/racket/racket-ci
3+
FROM racket/racket:8.0-full AS builder
4+
WORKDIR /app
5+
COPY . /app
6+
RUN raco exe server.rkt
7+
8+
9+
# 2. Deployment
10+
FROM racket/racket:8.0
11+
WORKDIR /home
12+
COPY --from=builder /app/server .
13+
ENV "FUNCTIONS_HTTPWORKER_PORT" 7071
14+
ENTRYPOINT [ "./server" ]
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#lang racket
2+
(require json)
3+
(require web-server/servlet)
4+
(require web-server/servlet-env)
5+
6+
;;; Define utility functions
7+
8+
;;; Convert raw request body to jsexrp
9+
(define (parse-json-body req)
10+
(bytes->jsexpr (request-post-data/raw req)))
11+
12+
;;; Function to get value from hash given a key
13+
(define (get-hash-value h v)
14+
(hash-ref h v))
15+
16+
;;; Define PORT
17+
(define PORT (string->number (getenv "FUNCTIONS_HTTPWORKER_PORT")))
18+
19+
;;; Define handlers
20+
(define (get-values req)
21+
(response/jsexpr
22+
(hasheq
23+
'values '(1 2 3))))
24+
25+
(define (post-values req)
26+
(define get-property
27+
(curry get-hash-value (parse-json-body req)))
28+
29+
(define x (string->number (get-property 'x)))
30+
(define y (string->number (get-property 'y)))
31+
32+
(response/jsexpr
33+
(hasheq 'sum (+ x y))))
34+
35+
;;; Define router
36+
(define-values (dispatch req)
37+
(dispatch-rules
38+
[("values") #:method "get" get-values]
39+
[("values") #:method "post" post-values]
40+
[else (error "Route does not exist")]))
41+
42+
;; Define and start server
43+
(serve/servlet
44+
(lambda (req) (dispatch req))
45+
#:launch-browser? #f
46+
#:quit? #f
47+
#:port PORT
48+
#:servlet-path "/"
49+
#:servlet-regexp #rx"")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"bindings": [
3+
{
4+
"type": "httpTrigger",
5+
"direction": "in",
6+
"name": "req",
7+
"methods": ["get","post"]
8+
},
9+
{
10+
"type": "http",
11+
"direction": "out",
12+
"name": "res"
13+
}
14+
]
15+
}
Binary file not shown.

0 commit comments

Comments
 (0)