LogoWCF

janka102's Solution

Open in the sandbox

2026

package main

import (
	"bufio"
	"fmt"
	"log"
	"net/http"
	"unicode/utf8"
)

const santaLetterPage = `<!doctype html>
<html>
<head>
<script>
function onSubmit(e) {
	e.preventDefault();
	const form = e.target;

	const statusEl = document.querySelector("#status");
	statusEl.textContent = "Sending...";

	fetch(form.action, {
		method: form.method,
		body: [
			"Dear Santa,",
			form.body.value.trim(),
			"",
			form.presents.value.trim(),
			"",
			"Thank you!",
			form.name.value.trim(),
		].join("\n"),
	})
		.then((res) => res.text())
		.then((body) => (statusEl.textContent = body))
		.catch(() => (statusEl.textContent = "Error sending, try again."));
}
</script>
</head>
<body>
<form method="post" action="/santaletter" onsubmit="onSubmit(event)">
	<p>Dear Santa,</p>
	<input name="body" minlength="1" placeholder="Message" />
	<br/>
	<textarea name="presents" placeholder="Presents"></textarea>
	<p>Thank you!</p>
	<input name="name" minlength="1" placeholder="Name" />
	<br/>
	<button>Send</button>
</form>
<p id="status"></p>
</body>
</html>`

func logReq(r *http.Request) {
	fmt.Printf("%s %s\n", r.Method, r.URL)
}

func main() {
	http.HandleFunc("GET /santaletter", func(w http.ResponseWriter, r *http.Request) {
		logReq(r)
		fmt.Fprintln(w, santaLetterPage)
	})

	http.HandleFunc("POST /santaletter", func(w http.ResponseWriter, r *http.Request) {
		logReq(r)
		if r.Body == nil {
			w.WriteHeader(http.StatusBadRequest)
			fmt.Println("no body")
			return
		}

		scanner := bufio.NewScanner(r.Body)
		state := "start"

		var name string
		var presents []rune

		for scanner.Scan() {
			line := scanner.Text()
			switch state {
			case "start":
				if line == "Dear Santa," {
					state = "body"
				} else {
					break
				}
			case "body":
				state = "body-end"
			case "body-end":
				if line == "" {
					state = "presents"
				} else {
					break
				}
			case "presents":
				if line != "" {
					first, _ := utf8.DecodeRuneInString(line)
					presents = append(presents, first)
				} else {
					state = "thanks"
				}
			case "thanks":
				if line == "Thank you!" {
					state = "name"
				} else {
					break
				}

			case "name":
				name = line
				state = "end"
			case "end":
				if line != "" {
					state = "err"
					break
				}
			}
		}

		if state != "end" {
			fmt.Printf("not at 'end': '%s'\n", state)
			w.WriteHeader(http.StatusBadRequest)
			return
		}

		if err := scanner.Err(); err != nil {
			fmt.Println("bad scanner")
			w.WriteHeader(http.StatusBadRequest)
			return
		}

		fmt.Printf("%s: %s\n", name, string(presents))

		fmt.Fprintln(w, `Received. Please allow up to 366 days to process.`)
	})

	fmt.Println("Listening on 8080")
	log.Fatal(http.ListenAndServe(":8080", nil))
}

Chillenge 9: The North Pole Update

Well, the future is here, and kids learn how to send letters less and less, let alone know how to write them! Santa recognizes that giving kids the opportunity to write to him is an important experience for them, and he doesn’t want to let his lack of technology get in the way.

Problem: In order to allow Santa to receive letters from the newer generation, create a webserver that can receive requests from kids and respond.

We plan to open up an api to all major service providers that has just two endpoints GET /santaletter and POST /santaletter.

GET /santaletter: This endpoint should return html explaining how the children should format their letters. The expected format for a letter is:

Dear Santa,
[Body of Letter]

[List of Desired Presents]

Thank you!
[Child Name]

POST /santaletter: This endpoint should expect a body that contains children’s letters. Parse out the child’s name and what items they want for Christmas. Log out to the console the child’s name followed by the first letter of each item they want for Christmas before returning with a successful response.

Three files are included with post-request bodies to test against your server. Give the output from those requests.