Latest / CyberSecurity Summary / Go Web Programming
Transcript
- 0:00Speaker 1: You know, every time you pull out your phone, open
- 0:02Speaker 1: up a web app, and hit submit on a login screen,
- 0:06Speaker 1: you're basically participating in this grand illusion. Oh.
- 0:10Speaker 2: Absolutely, it completely feels like an illusion, right.
- 0:12Speaker 1: You are instantly transported into your account and to you,
- 0:16Speaker 1: the everyday user. The entire process feels completely seamless, immediate,
- 0:21Speaker 1: and I mean practically like magic.
- 0:24Speaker 2: Yeah. We have become incredibly accustomed to the polished end
- 0:28Speaker 2: product of the web. We interact with this sleek user
- 0:31Speaker 2: interface and we just completely ignore the plumbing underneath.
- 0:34Speaker 1: But if you pull back the curtain, there is absolutely
- 0:36Speaker 1: no magic involved, like none. You're looking at a highly orchestrated,
- 0:41Speaker 1: massively complex series of mechanical.
- 0:43Speaker 2: Steps, right, and they have to execute perfectly in a
- 0:45Speaker 2: fraction of a second. Yeah, when you strip away that
- 0:47Speaker 2: user interface, a web application is basically just a machine
- 0:50Speaker 2: built to catch signals from across the world, decode them,
- 0:53Speaker 2: and fire signals back.
- 0:55Speaker 1: And deconstructing that exact machine is our mission for today's
- 0:58Speaker 1: deep dive. We are taking a look at how the
- 1:00Speaker 1: Internet actually functions and how developers construct the applications that
- 1:05Speaker 1: power it. It's a fascinating topic, it really is, and
- 1:08Speaker 1: we're using the concepts from the book Go Web Programming
- 1:11Speaker 1: by Saftshang Chang to guide.
- 1:12Speaker 2: Us today, which is a great resource.
- 1:14Speaker 1: So, whether you write code every day and you want
- 1:16Speaker 1: to understand the bare metal philosophy of the Go programming language,
- 1:20Speaker 1: or you are simply curious about the raw mechanics triggered
- 1:23Speaker 1: by clicking a button on a website, this exploration will
- 1:27Speaker 1: permanently alter how you view the web.
- 1:29Speaker 2: Using Go as our lens. Here is a very deliberate
- 1:32Speaker 2: choice for this kind of exploration too. Why is that well,
- 1:36Speaker 2: Go is a programming language that inherently resists hiding the
- 1:40Speaker 2: fundamental mechanics of the web behind layers of abstraction. It
- 1:46Speaker 2: essentially forces the developer to confront the realities of how
- 1:49Speaker 2: networks actually operate.
- 1:50Speaker 1: Instead of just relying on massive, pre built frameworks that
- 1:54Speaker 1: do all the heavy lifting for you in the dark.
- 1:56Speaker 2: Exactly. But before we dissect the exact mechanics of a
- 1:59Speaker 2: web request, we really have to look at the philosophy
- 2:02Speaker 2: driving developers toward this bare metal approach in the first place.
- 2:05Speaker 1: Yeah, because there is this massive trend right now of
- 2:09Speaker 1: software engineers just leaving heavy frameworks like Rubyon rails or
- 2:14Speaker 1: django in favor of go there really is.
- 2:17Speaker 2: And to understand why, we have to look at a
- 2:19Speaker 2: rather bizarre historical anecdote from World War Two. It's known
- 2:22Speaker 2: as cargo culting.
- 2:24Speaker 1: Right, the cargo cults.
- 2:26Speaker 2: This is so interesting. There's a phenomenal example of human
- 2:28Speaker 2: psychology overlapping with engineering. So during the war and the
- 2:32Speaker 2: Pacific Allied forces established these temporary air bases on remote islands.
- 2:37Speaker 1: And they began air dropping massive amounts of supplies right
- 2:40Speaker 1: like cargo, to both the troops and the local island population.
- 2:44Speaker 2: Yeah, people who had never seen this kind of technology
- 2:46Speaker 2: before in their lives. But inevitably the war ended, the
- 2:50Speaker 2: troops packed up and the cargo planes stopped driving.
- 2:53Speaker 1: That the islanders had grown accustomed to these air drops.
- 2:55Speaker 1: They wanted the supplies to return, so they started recreating
- 2:58Speaker 1: the conditions they believed cause the planes to arrive in.
- 3:01Speaker 2: The first place, which is where it gets wild.
- 3:03Speaker 1: Yeah. They literally hacked fake air strips out of the jungle.
- 3:07Speaker 1: They carved wooden headsets out of coconuts. They stood on
- 3:10Speaker 1: these makeshift runways waving sticks.
- 3:13Speaker 2: Mimicking the exact physical movements of the air traffic controllers.
- 3:16Speaker 1: They had watched for years, completely convinced that these actions
- 3:19Speaker 1: would magically summon the cargo planes from the sky.
- 3:22Speaker 2: They replicated the outward appearance of the system without possessing
- 3:25Speaker 2: any understanding of the underlying mechanics, and this gave rise
- 3:29Speaker 2: to the phrase cargo cult programming.
- 3:32Speaker 1: Right, So, in software engineering, this happens when a developer
- 3:36Speaker 1: just copies and pastes a block of code, maybe from
- 3:39Speaker 1: a popular framework or an Internet forum, without the faintest
- 3:43Speaker 1: idea of how it actually functions.
- 3:45Speaker 2: They just wave the sticks, put on the coconut headsets,
- 3:48Speaker 2: and hope the code magically compiles and runs.
- 3:50Speaker 1: Okay, let's unpack this because it makes me think of
- 3:52Speaker 1: a microwave.
- 3:53Speaker 2: A microwave, yeah, hear me out.
- 3:55Speaker 1: Modern heavy web frameworks are basically like a modern microwave.
- 3:59Speaker 1: You know how to push the button that says popcorn,
- 4:01Speaker 1: and the machine just does the rest.
- 4:02Speaker 2: Right. It's incredibly easy.
- 4:04Speaker 1: It's fast and easy. But if that microwave suddenly stops
- 4:07Speaker 1: heating up your food and you have absolutely no idea
- 4:10Speaker 1: what a magnetron is or how it excites water molecules,
- 4:13Speaker 1: you're completely helpless.
- 4:14Speaker 2: You can't fix it, totally stuck. Yeah, so is.
- 4:16Speaker 1: The implication here that these massive frameworks are inherently bad
- 4:20Speaker 1: or just risky if you treat them like magic boxes.
- 4:23Speaker 2: Well, if we connect this to the bigger picture, frameworks
- 4:25Speaker 2: themselves are incredibly valuable tools for rapid development. They aren't
- 4:30Speaker 2: bad at all. The danger lies entirely in relying on
- 4:33Speaker 2: them as a substitute for foundational knowledge. Oh I see,
- 4:36Speaker 2: if you don't understand the underlying HDTP mechanics powering that framework,
- 4:41Speaker 2: you simply can't debug complex issues or optimize for massive scale.
- 4:46Speaker 1: Go approaches this very differently, right because of its standard
- 4:49Speaker 1: library exactly.
- 4:50Speaker 2: Specifically a package called net slash http. It is so
- 4:55Speaker 2: exceptionally robust that it essentially eliminates the need for a
- 4:59Speaker 2: massive bloe covaded framework. It hands you the raw plumbing,
- 5:03Speaker 2: but it ensures those plumbing tools are precision engineered.
- 5:06Speaker 1: And that precision engineering translates to massive performance gains, which
- 5:11Speaker 1: is well, it's exactly why Go is dominating large scale
- 5:14Speaker 1: applications right now.
- 5:15Speaker 2: Absolutely.
- 5:16Speaker 1: It tackles scalability by using something called gore routines. Right, so,
- 5:21Speaker 1: instead of relying on heavy operating system threads which consume
- 5:25Speaker 1: a ton of memory, Go uses these incredibly lightweight virtual
- 5:29Speaker 1: threads managed by its own run time.
- 5:31Speaker 2: Which is a huge deal. That means a single GO
- 5:34Speaker 2: web server can juggle literally hundreds of thousands of concurrent
- 5:38Speaker 2: connections simultaneously without crashing.
- 5:41Speaker 1: Oh wow. And it compiles down directly to native machine code,
- 5:44Speaker 1: right it does.
- 5:45Speaker 2: It puts it execution speed right up there with C. Plus,
- 5:48Speaker 2: it enforces modularity for micro services and has built in
- 5:51Speaker 2: code formatting tools to ensure maintainability across massive engineering teams.
- 5:55Speaker 1: But to harness all of that power and to guarantee
- 5:58Speaker 1: you never become a cargo cult program, you really must
- 6:01Speaker 1: fundamentally grasp the electricity running through the system.
- 6:04Speaker 2: And for the Internet, that electricity is HTTP, the Hypertext
- 6:08Speaker 2: Transfer Protocol.
- 6:09Speaker 1: The universal language of the Web, invented by Tim berners
- 6:13Speaker 1: Lee back in nineteen ninety and at its core it
- 6:15Speaker 1: is a stateless client server protocol. Yes, and for decades
- 6:20Speaker 1: it has also been entirely text based. I mean you
- 6:22Speaker 1: could literally intercept the traffic between your laptop and a
- 6:25Speaker 1: server and just read it.
- 6:27Speaker 2: Like a script. Yeah. That text based nature of HTTP
- 6:29Speaker 2: one point one made the Web highly accessible and very
- 6:32Speaker 2: easy to debug. But parsing text is actually quite slow.
- 6:35Speaker 1: For a computer because it has to read the message
- 6:37Speaker 1: character by character just to find the end of a
- 6:39Speaker 1: line right.
- 6:40Speaker 2: Exactly, which brings us to a crucial evolution that GO
- 6:43Speaker 2: supports natively. HTTP two. This newer version shifts the web
- 6:47Speaker 2: from text based conversations to a binary protocol, so.
- 6:51Speaker 1: Instead of sending easily readable words, it sends data broken
- 6:55Speaker 1: down into highly structured binary frames, just ones and zeros,
- 6:59Speaker 1: formatted and very specif chunks.
- 7:00Speaker 2: Precisely, binary is a machine's native language. A server doesn't
- 7:05Speaker 2: have to scan for the end of a sentence anymore.
- 7:07Speaker 2: The binary frame explicitly states its exact length up front.
- 7:10Speaker 1: It's like the difference between reading a dense paragraph to
- 7:12Speaker 1: find a piece of information versus just instantly scanning a barcode.
- 7:16Speaker 2: That's a great analogy. And HTTP two is also.
- 7:19Speaker 1: Multiplexed, meaning a client can send dozens of requests and
- 7:23Speaker 1: receive dozens of responses simultaneously over a single network connection,
- 7:28Speaker 1: rather than waiting in line right.
- 7:30Speaker 2: But whether the request is sent as raw text or
- 7:32Speaker 2: highly efficient binary frames, the vocabulary of the conversation remains
- 7:37Speaker 2: exactly the same. It relies on specific HTTP methods.
- 7:41Speaker 1: Let's break those down. You have safe methods like a
- 7:44Speaker 1: get request. When you type a website address into your
- 7:47Speaker 1: browser and hit enter, you are sending a get request, right,
- 7:51Speaker 1: you are merely asking the server to hand you some information.
- 7:54Speaker 1: It is considered safe because your request doesn't alter any
- 7:57Speaker 1: data on the server.
- 7:58Speaker 2: Next, you have ideentptan methods such as coot and delete. Now.
- 8:02Speaker 2: Idempotency is a critical concept in distributed systems. So Well
- 8:06Speaker 2: guarantees that if you send the exact same request once,
- 8:09Speaker 2: or if a network error causes your browser to accidentally
- 8:12Speaker 2: send it fifty times in a row, the state of
- 8:14Speaker 2: the server will only change after that very first.
- 8:17Speaker 1: Request, so the subsequent forty nine requests will have zero
- 8:20Speaker 1: additional effect. It is exactly like the call button for
- 8:23Speaker 1: an elevator. You walk up to the elevator bank and
- 8:25Speaker 1: push the button. Once the light turns on and the
- 8:27Speaker 1: car is summoned. Right, if you aggressively mash that button
- 8:30Speaker 1: twenty more times, you aren't summoning twenty elevators. The end
- 8:34Speaker 1: result remains identical to the first press.
- 8:37Speaker 2: That is a perfect way to explain it dempotency and
- 8:39Speaker 2: then standing in start contrast to that, we have the
- 8:42Speaker 2: post method. Post is neither safe nor.
- 8:45Speaker 1: Idemptant post is the workhorse of the interactive web. You
- 8:49Speaker 1: use it when you are actively submitting a form, finalizing
- 8:52Speaker 1: a checkout cart, or publishing a comment.
- 8:55Speaker 2: Right. And because it lacks idempotency, if you accidentally submit
- 8:58Speaker 2: a POC request to a shot cart ten times due
- 9:01Speaker 2: to a lagging connection, you could very well end up
- 9:04Speaker 2: purchasing ten identical pairs issues.
- 9:06Speaker 1: Yikes. So to ground these abstract concepts, let's look at
- 9:10Speaker 1: a practical example from the book, a hypothetical form application
- 9:13Speaker 1: called Chitchat.
- 9:15Speaker 2: Architecturally, an app like Chitchat splits the workload into two
- 9:18Speaker 2: distinct halves.
- 9:19Speaker 1: Okay, so you have the handler, which is essentially the brain.
- 9:22Speaker 1: It processes the incoming logic, verifies passwords, and pulls data
- 9:26Speaker 1: from the database yep.
- 9:27Speaker 2: And then you have the template engine, which is the designer.
- 9:30Speaker 1: The handler passes the raw data to the template engine,
- 9:33Speaker 1: which then injects that data into HTML to make it
- 9:36Speaker 1: visually presentable for the user's browser.
- 9:39Speaker 2: This separation of concerns ensures that the logic of the
- 9:42Speaker 2: application isn't just completely tangled up with a visual presentation.
- 9:47Speaker 2: So the browser sends the HTTP request, the server hands
- 9:51Speaker 2: it to the specific handler for that route. The handler
- 9:53Speaker 2: does the computational heavy lifting, and the template engine formats
- 9:57Speaker 2: the response.
- 9:58Speaker 1: But I need to pause here because we just glossed
- 10:00Speaker 1: over a massive contradiction.
- 10:02Speaker 2: Oh what's that?
- 10:04Speaker 1: Well? Wait, If HTTP is totally stateless and has amnesia
- 10:08Speaker 1: after every single click, how does a web app like
- 10:11Speaker 1: chitchat actually remember that I am logged in as I
- 10:15Speaker 1: navigate from the homepage to a forum thread, and then
- 10:17Speaker 1: to my profile.
- 10:19Speaker 2: Ah. Yeah, You've just hit on one of the most
- 10:20Speaker 2: fundamental engineering challenges of the modern Internet.
- 10:23Speaker 1: It seems impossible based on what we just said.
- 10:25Speaker 2: Right, because to cure the inherent amnesia of HTTP, developers
- 10:30Speaker 2: basically have to artificially manufacture a memory. In apps like Chitchat,
- 10:33Speaker 2: they do this using cookies and a robust database.
- 10:36Speaker 1: Okay, how does that work?
- 10:37Speaker 2: When you submit your username and password, the handler authenticates you.
- 10:41Speaker 2: It then generates what is known as UUID, a universally
- 10:45Speaker 2: unique identifier. This is a one hundred and twenty eight
- 10:48Speaker 2: bit label that is statistically guaranteed to be completely unique
- 10:52Speaker 2: across the entire universe.
- 10:53Speaker 1: So it generates this massive unique serial number. What does
- 10:56Speaker 1: it actually do with it?
- 10:57Speaker 2: It stores that UVIID securely on the server inside a database.
- 11:02Speaker 2: In Chitchat's case, it uses postgrescol which is a highly structured,
- 11:05Speaker 2: incredibly reliable, relational database designed to handle complex data safely. Okay,
- 11:10Speaker 2: the database essentially links that specific UID to your user account. Finally,
- 11:15Speaker 2: the server sends that UID back to your browser, wrapped
- 11:18Speaker 2: inside an HTTP cookie.
- 11:19Speaker 1: Right, so the cookie is just a tiny text file
- 11:21Speaker 1: acting as a digital passport sitting on my local machine.
- 11:24Speaker 1: And the trick here is that modern browsers are designed
- 11:26Speaker 1: to automatically attach that specific cookie to every subsequent request
- 11:29Speaker 1: I make to that domain.
- 11:31Speaker 2: Precisely, so, when you click on a new forum thread,
- 11:33Speaker 2: the stateless HTTP request travels to the server, but it
- 11:37Speaker 2: brings the cookie passport with it, and.
- 11:39Speaker 1: The server inspects a request, extracts the UID queries the
- 11:43Speaker 1: post rescool database to find out who that identifier belongs to,
- 11:47Speaker 1: and successfully bypasses the protocol's amnesia exactly.
- 11:50Speaker 2: The NDJA is cured.
- 11:51Speaker 1: Okay, so our request has traversed the Internet carrying its
- 11:54Speaker 1: cookie and it has finally arrived at the server. But
- 11:58Speaker 1: a large web application might contain hundreds of different handlers
- 12:01Speaker 1: for hundreds of different features. How does the server know
- 12:04Speaker 1: which specific handler should process this incoming request.
- 12:07Speaker 2: It requires a highly efficient traffic cop to direct the flow,
- 12:11Speaker 2: and in goes architecture. That traffic cop is known as
- 12:14Speaker 2: a multiplexer, a multiplexer YEAH. The standard library provides a
- 12:17Speaker 2: highly capable default multiplexer called serve MUCKs. Its primary job
- 12:22Speaker 2: is to inspect the URL path of the incoming request
- 12:25Speaker 2: and route it to the correct handler.
- 12:27Speaker 1: So if your browser requests the path slash log in,
- 12:30Speaker 1: servemucks catches it and routs the traffic directly to the
- 12:33Speaker 1: log in handler exactly.
- 12:35Speaker 2: And when configuring this routing, there is a design philosophy
- 12:38Speaker 2: at play called the principle of least surprise or sometimes
- 12:43Speaker 2: the principle of least astonishment.
- 12:45Speaker 1: The core idea there is that software should behave exactly
- 12:48Speaker 1: how a reasonable user expects it to behave. Like if
- 12:52Speaker 1: you walk up to a front door and press a
- 12:54Speaker 1: small button mounted on the wall next to it, you
- 12:57Speaker 1: expect a doorbell to chime.
- 12:58Speaker 2: Right, you'd be highliest if pressing that button suddenly turned
- 13:02Speaker 2: off all the street lights on the block. Rutting should
- 13:04Speaker 2: be predictably.
- 13:06Speaker 1: Logical, and servimuks adheres closely to this principle but it
- 13:09Speaker 1: implements it with a very specific literal approach to URL
- 13:13Speaker 1: matching that can really trip up developers, especially concerning trailing
- 13:17Speaker 1: slashes at the end of a URL.
- 13:19Speaker 2: Oh. Absolutely, this catches so many people off guard.
- 13:21Speaker 1: Here's where it gets really interesting, because servimux behaves less
- 13:25Speaker 1: like a modern search engine that guesses what you mean,
- 13:28Speaker 1: and well more like a brutally strict hotel concierge.
- 13:31Speaker 2: That's a great annount.
- 13:32Speaker 1: If you tell the concierge to register a handler to
- 13:35Speaker 1: the exact path slash hello, meaning there is no forward
- 13:39Speaker 1: slash at the very end of the word, that concierge
- 13:42Speaker 1: will only open the door if you ask for slash
- 13:44Speaker 1: hello exactly right. If a user tries to visit slash
- 13:48Speaker 1: hello slash there, the concierge just stares blankly, declares the
- 13:52Speaker 1: room doesn't exist, and drops the request entirely.
- 13:55Speaker 2: However, if you register the handler with a trailing slash,
- 13:59Speaker 2: writing it as slash slash, you change the rules of
- 14:02Speaker 2: the concierge. You are instructing serfmucks to treat this as
- 14:05Speaker 2: a prefix match.
- 14:07Speaker 1: Ah. So the concierge now understands that any path beginning
- 14:10Speaker 1: with slash hello slash belongs in this wing of the hotel,
- 14:14Speaker 1: so an incoming request for slash hello slash there or
- 14:17Speaker 1: slash hello slash world will successfully route to that single
- 14:20Speaker 1: hand line exactly.
- 14:21Speaker 2: It is a seemingly microscopic detail, but misunderstanding that trailing
- 14:25Speaker 2: slash is exactly how you end up debugging a broken
- 14:28Speaker 2: website for hours because you were cargo culting.
- 14:30Speaker 1: The code, just waving the sticks.
- 14:32Speaker 2: Yep. But this strict behavior reveals a limitation. What happens
- 14:37Speaker 2: when you are building a dynamic site and you want
- 14:40Speaker 2: a URL pattern like slash thread slash one two three,
- 14:44Speaker 2: where that one two three is a variable that changes
- 14:46Speaker 2: based on which post the user clicked.
- 14:48Speaker 1: That's a good point, goes default servemuks struggles with variables
- 14:51Speaker 1: in the URL path right it does.
- 14:53Speaker 2: It doesn't offer a clean, built in way to extract
- 14:55Speaker 2: that one two three dynamically. You would basically be forced
- 14:59Speaker 2: to wrote everything to a generic slash thread slash prefix
- 15:03Speaker 2: and write custom parsing logic to chop up the URL
- 15:06Speaker 2: string manually.
- 15:06Speaker 1: Which sounds tedious.
- 15:08Speaker 2: It is, and because of this, developers frequently integrate third
- 15:10Speaker 2: party multiplexers like HTT browder, ht prowder excels at the
- 15:15Speaker 2: specific task, allowing developers to define name the parameters directly
- 15:19Speaker 2: in the URL.
- 15:20Speaker 1: Structure, keeping the code elegant without sacrificing any of Go's
- 15:23Speaker 1: famous execution speed. Let's look closer at the handlers themselves,
- 15:27Speaker 1: the actual brain centers processing these requests. There's a deep
- 15:31Speaker 1: structural distinction in Go between a handler and a handler funk, Yes.
- 15:35Speaker 2: And to grasp this we have to talk about how
- 15:37Speaker 2: Go uses interfaces and structs. In Go, a struct is
- 15:41Speaker 2: a custom data type. It's essentially a collection of fields
- 15:45Speaker 2: grouped together. An interface, on the other hand, is a
- 15:48Speaker 2: strict contract.
- 15:49Speaker 1: Okay, so what does the handler interfacee contract say?
- 15:52Speaker 2: The handler interface in Go dictates a very simple rule.
- 15:56Speaker 2: Any struct, no matter how complex, is legally considered a
- 15:59Speaker 2: hand as long as it possesses a method named serve http.
- 16:04Speaker 1: And that method must accept two specific parameters, right, a
- 16:07Speaker 1: response writer to send data back and a request object
- 16:11Speaker 1: containing the incoming data.
- 16:12Speaker 2: Exactly. If your struck has that method, it is a handler.
- 16:16Speaker 2: But sometimes creating a whole new struct just to write
- 16:19Speaker 2: a three line function feels like total overkill.
- 16:21Speaker 1: Right, It's just too much boilerplate.
- 16:23Speaker 2: That is where Hammer funk comes in, it acts as
- 16:25Speaker 2: an adapter. It takes a plain normal function and automatically
- 16:29Speaker 2: wraps it, forcing it to comply with the handler interface contract.
- 16:33Speaker 2: It saves developers from writing all that repetitive code.
- 16:35Speaker 1: That's really clever.
- 16:36Speaker 2: And what's fascinating here is that when you look at
- 16:38Speaker 2: the source code for serfmucks, the traffic cop we just discussed,
- 16:41Speaker 2: you realize that servemucks is itself just a struct with
- 16:44Speaker 2: a serve HTTP method.
- 16:46Speaker 1: Wait. Really, so the multiplexer is technically just another handler
- 16:50Speaker 1: it is.
- 16:51Speaker 2: When a request comes in, serfmucks's only job inside its
- 16:54Speaker 2: serve HTTP method is to look at the URL, find
- 16:58Speaker 2: the next appropriate handler, and call that handler's serve HTTP method.
- 17:03Speaker 1: Wow. It is a profoundly elegant recursive design. Everything in
- 17:08Speaker 1: the request life cycle is simply handlers handing off responsibility
- 17:12Speaker 1: to other handlers.
- 17:13Speaker 2: Yep.
- 17:14Speaker 1: And that leads perfectly into the concept of chaining handlers
- 17:17Speaker 1: to create an assembly line. When building a commercial application,
- 17:21Speaker 1: you have cross cutting concerns. These are operations that need
- 17:25Speaker 1: to happen on nearly every single request.
- 17:28Speaker 2: Like logging the traffic for analytics or verifying a user's
- 17:31Speaker 2: security credentials.
- 17:32Speaker 1: Right and you absolutely do not want to copy and
- 17:35Speaker 1: paste your complex authentication code into fifty different handlers across
- 17:38Speaker 1: your application.
- 17:39Speaker 2: No, that violates every rule of maintainability. If you need
- 17:42Speaker 2: to pass a security vulnerability, you'd have to find and
- 17:45Speaker 2: fix it in fifty separate places.
- 17:46Speaker 1: Sounds like a nightmare. Instead, you chain the handlers together,
- 17:49Speaker 1: functioning exactly like the security checkpoints at an international airport.
- 17:53Speaker 2: Oh, I like that comparison.
- 17:55Speaker 1: So the incoming request is the passenger. First, they encounter
- 17:58Speaker 1: the ticket checker handler. Its only job is to log
- 18:01Speaker 1: that the passenger arrived, and then it passes the request
- 18:04Speaker 1: to the TSA authentication handler.
- 18:06Speaker 2: Right, and the TSA handler checks the cookie queries the
- 18:10Speaker 2: Postgres school database and verifies the UUID. If the passenger
- 18:15Speaker 2: is fraudulent, the TSA handler rejects the request, immediately returning
- 18:19Speaker 2: an error to the browser.
- 18:21Speaker 1: But if the passenger clear security, the TSA handler finally
- 18:24Speaker 1: forwards the request into the terminal, handing it off to
- 18:26Speaker 1: the main logic handler for that specific URL. You have
- 18:29Speaker 1: successfully isolated your security logic from your business logic.
- 18:33Speaker 2: The passengers made it to the right terminal. Now the
- 18:35Speaker 2: main handler has to unpack the baggage it needs to
- 18:37Speaker 2: extract the data the user actually sent.
- 18:39Speaker 1: Let's say it's a post request from our forum and
- 18:42Speaker 1: the user has submitted a new text post. How does
- 18:44Speaker 1: Go crack open that request and read the text?
- 18:47Speaker 2: It depends entirely on the formatting instructions the client's browser used.
- 18:51Speaker 2: When you submit a standard HTML text form, the browser
- 18:55Speaker 2: typically encodes it using a format called application slash xww
- 19:00Speaker 2: um Erlin coded quite a mouthful, it is. It essentially
- 19:04Speaker 2: takes all the fields and mashes them into one long,
- 19:07Speaker 2: continuous string of text.
- 19:09Speaker 1: But if you are trying to upload a high resolution
- 19:11Speaker 1: photograph or a video file, mashing that binary data into
- 19:15Speaker 1: a single text string is incredibly inefficient and frankly air prone.
- 19:19Speaker 2: Exactly so, for files browsers switch formats, they use multipart
- 19:24Speaker 2: slash form data. This slices the data into discrete chunks,
- 19:29Speaker 2: each separated by distinct boundary markers, so the server knows
- 19:32Speaker 2: where the image begins and ends.
- 19:34Speaker 1: So what does this all mean for the developer writing
- 19:36Speaker 1: the code? Because Go forces you to navigate these formats explicitly,
- 19:40Speaker 1: and it can be pretty jarring. It can be to
- 19:43Speaker 1: read what a user typed Go provides several different methods, form, postform,
- 19:48Speaker 1: and multipart form. I have to voice the frustration of
- 19:50Speaker 1: every new Go developer here. This seems wildly overcomplicated.
- 19:54Speaker 2: I get it, I really do.
- 19:55Speaker 1: Why do I need to choose between three different fields
- 19:57Speaker 1: just to extract a paragraph of texts someone.
- 19:59Speaker 2: Typed into a This raises an important question about the
- 20:02Speaker 2: fundamental trade offs between utilizing massive frameworks versus writing bare
- 20:07Speaker 2: metal code. Okay, house Well, a framework like Rubyon rails
- 20:10Speaker 2: will automatically dig through the request, find the variable, and
- 20:14Speaker 2: just hand it to you. It completely hides where it
- 20:16Speaker 2: came from, but Go prioritizes absolute control, specifically regarding memory
- 20:21Speaker 2: allocation AH memory right. The form method extracts data from
- 20:26Speaker 2: both the UR query string and the body of the request.
- 20:30Speaker 2: Postform aggressively ignores the URL and only extracts data from
- 20:33Speaker 2: the secure body.
- 20:34Speaker 1: And multipart form is explicitly reserved for slicing up those
- 20:37Speaker 1: heavy file uploads.
- 20:39Speaker 2: Exactly and understanding why Go separates these is crucial for
- 20:42Speaker 2: server performance. Imagine if a user maliciously tries to upload
- 20:46Speaker 2: a five gigabyte video file and your server automatically tries
- 20:50Speaker 2: to parse the entire mats of payload into its active RAM.
- 20:53Speaker 1: Memory, your server would immediately press right.
- 20:55Speaker 2: Goh's explicit methods like parse multipart form actually require you
- 20:59Speaker 2: to define as memory limit, say thirty two megabytes. Anything
- 21:03Speaker 2: exceeding that limit is safely spooled off into temporary files
- 21:06Speaker 2: on the hard drive, protecting your server's RAM from overflowing.
- 21:09Speaker 1: So you have absolute control over your hardware resources you do.
- 21:12Speaker 2: It's a granular level of control that prevents catastrophic failures
- 21:16Speaker 2: at scale.
- 21:17Speaker 1: But you still have to be careful because if you
- 21:19Speaker 1: are building the front end of your site using a
- 21:21Speaker 1: modern client side framework like Angular or React, those frameworks
- 21:25Speaker 1: often ignore traditional HTML form structures entirely.
- 21:29Speaker 2: Oh yeah. They prefer to bundle everything up and send
- 21:31Speaker 2: posten requests as raw JSON data.
- 21:33Speaker 1: And if a developer cargo cults their GO code and
- 21:36Speaker 1: tries to use the standard parse four method on an
- 21:39Speaker 1: incoming Angular Jason payload, GO will return absolutely nothing, complete silence.
- 21:44Speaker 2: Right. The data is there, but you use the wrong
- 21:47Speaker 2: tool to crack the crate. It demands vigilance. But once
- 21:50Speaker 2: you have successfully routed the request, change your security checkpoints
- 21:54Speaker 2: and safely extracted the data into memory, the final step
- 21:57Speaker 2: is responding.
- 21:58Speaker 1: Yes, the handler send a formatted response back to the
- 22:01Speaker 1: user's browser. It accomplishes this using the response rat interface,
- 22:05Speaker 1: which revolves around two critical methods, right header and right and.
- 22:09Speaker 2: The naming convention for right hair can be slightly deceptive.
- 22:11Speaker 2: You would assume it's used to write HTDP headers, like
- 22:15Speaker 2: defining the content type is HTML or JSON.
- 22:18Speaker 1: Right, but it actually serves a much more specific purpose.
- 22:21Speaker 1: Right header is exclusively utilized to transmit the three digit
- 22:24Speaker 1: HTTP status code.
- 22:25Speaker 2: Yes. If a user requests a page that doesn't exist,
- 22:30Speaker 2: you invoke right header to send the infamous four h
- 22:33Speaker 2: four not found code, Or.
- 22:35Speaker 1: If you are developing an apion point that isn't quite
- 22:37Speaker 1: finished yet, you might send a five oh one not
- 22:40Speaker 1: implemented code to signal the client to.
- 22:42Speaker 2: Just hold off exactly, and if everything executes flawlessly, you
- 22:46Speaker 2: want to send a two hundred ok code. Although interestingly,
- 22:49Speaker 2: if you completely forget to call right hitter in your
- 22:51Speaker 2: GO code, the nets slash HTTP package is actually smart
- 22:55Speaker 2: enough to detect that and silently slips a two hundred
- 22:58Speaker 2: okay status in for you right before we're sending the data.
- 23:00Speaker 1: Oh that's nice. It's a rare moment. Of automatic convenience
- 23:03Speaker 1: in a very manual language.
- 23:05Speaker 2: It is a helpful safeguard, but the status code only
- 23:07Speaker 2: tells the browser how the operation went To deliver the
- 23:10Speaker 2: actual content the HKML document, the Jason payload, or the
- 23:13Speaker 2: image file. You rely on the right method and.
- 23:16Speaker 1: Rate takes an array of raw binary bytes and streams
- 23:19Speaker 1: them directly over the network connection back to the user's screen, and.
- 23:22Speaker 2: The moment those bites finish streaming, the thylis amnesia kicks
- 23:25Speaker 2: back in. The server immediately forgets the interaction ever occurred,
- 23:30Speaker 2: and the entire life cycle is complete.
- 23:32Speaker 1: It is a remarkable sequence of events. We've covered an
- 23:35Speaker 1: immense amount of ground today. We started by rejecting the
- 23:38Speaker 1: temptation of cargo cult programming. We stripped away the illusion
- 23:41Speaker 1: of magic and examined the raw text based and binary
- 23:45Speaker 1: conversations happening millions of times a second via HTTP.
- 23:50Speaker 2: We really did pull back the curtain.
- 23:51Speaker 1: We watched GO act as an unforgiving but hyper efficient
- 23:55Speaker 1: traffic cop writing requests. We built an airport security line
- 23:58Speaker 1: out of chained handlers to protect our data. We navigated
- 24:01Speaker 1: the meticulously strict rules GO and forces to safely manage
- 24:05Speaker 1: memory during data extraction, and finally we streamed raw bites
- 24:09Speaker 1: back into the world.
- 24:10Speaker 2: Understanding this architecture fundamentally changes your relationship with technology. It
- 24:14Speaker 2: really does, whether you were actively compiling GO code to
- 24:17Speaker 2: handle millions of concurrent connections, or you are simply a
- 24:21Speaker 2: user casually browsing the web. Knowing how the machine operates
- 24:25Speaker 2: underneath the user interface replaces the illusion of magic with
- 24:29Speaker 2: a deep appreciation for the brutal mechanical efficiency of the Internet.
- 24:33Speaker 1: I couldn't agree more. And as we wrap up our
- 24:36Speaker 1: deep dive into these underlying mechanics today, I want to
- 24:39Speaker 1: leave you the listener with a lingering thought to explore
- 24:42Speaker 1: on your own. We spent a significant amount of time
- 24:45Speaker 1: discussing the fact that HGTP is entirely stateless. It operates
- 24:49Speaker 1: with total amnesia. We explored how engineers spend millions of
- 24:53Speaker 1: hours and billions of dollars utilizing complex cookie passports, generated UIDs,
- 24:58Speaker 1: and massive postcresstled databases just to artificially fake the concept
- 25:02Speaker 1: of state so a website can simply remember who you
- 25:05Speaker 1: are when you click to the next page.
- 25:06Speaker 2: It's a massive workaround, it really is.
- 25:09Speaker 1: So what would the digital world look like today, if
- 25:12Speaker 1: tim berners Lee had originally designed the HTTP protocol with
- 25:16Speaker 1: a built in memory, if statefulness wasn't a hack but
- 25:19Speaker 1: a foundational feature of the Web. Would forcing servers to
- 25:22Speaker 1: remember every user have completely broken the Internet's ability to
- 25:25Speaker 1: scale across the globe. Or would having a native secure
- 25:28Speaker 1: memory have revolutionized our privacy, completely eliminating the modern surveillance
- 25:33Speaker 1: industry of third party tracking cookies. It's definitely something to
- 25:36Speaker 1: mull over the next time you hit submit and watch
- 25:39Speaker 1: the magic happen.