task/1.9
Raw Download raw file
 1package main
 2
 3import (
 4	"log"
 5	"net/http"
 6)
 7
 8func main() {
 9	// Initialize a new servemux (router) - this stores the mapping between
10	// URL patterns and their corresponding handlers.
11	mux := http.NewServeMux()
12
13	// Create a file server which serves files out of the "./ui/static" directory.
14	// Note that the path given to the http.Dir function is relative to the project
15	// directory root.
16	fileServer := http.FileServer(http.Dir("./ui/static/"))
17
18	// Use the mux.Handle() function to register the file server as the handler for
19	// all URL paths that start with "/static/". For matching paths, we strip the
20	// "/static" prefix before the request reaches the file server.
21	mux.Handle("GET /static/", http.StripPrefix("/static", fileServer))
22
23	// Register handlers for buylater.email routes
24	mux.HandleFunc("GET /{$}", home)                         // Exact match for home page
25	mux.HandleFunc("GET /submit", submitForm)                // Display email submission form
26	mux.HandleFunc("POST /submit", processSubmit)            // Process form submission
27	mux.HandleFunc("GET /confirm/{token}", confirmWithToken) // Magic link confirmation with token
28	mux.HandleFunc("GET /about", about)                      // About page
29
30	// Print a log message to indicate the server is starting.
31	log.Printf("Starting server on http://localhost:4000")
32
33	// Start the web server on port 4000. If ListenAndServe returns an error
34	// we use log.Fatal() to log the error and terminate the program.
35	err := http.ListenAndServe(":4000", mux)
36	log.Fatal(err)
37}