Speaker 1: All right, diving into Rust for web development.
Speaker 2: That's a that's a great choice.
Speaker 1: It's known for its speed and security, right yeah. Yeah,
Speaker 1: And to help us kind of break it all down,
Speaker 1: we have excerpts from Rust Web Programming, a hands on
Speaker 1: guide Excellent. I'm excited to unpack this. It's a good one,
Speaker 1: I think so. Yeah, So Rust is gaining traction, it is.
Speaker 1: It is, especially for those high performance web apps for sure.
Speaker 2: For sure, it kind of.
Speaker 1: Bridges the gap, you know, between that low level control
Speaker 1: and high level abstraction. Absolutely, so it seems like this
Speaker 1: book is a perfect starting point.
Speaker 2: I think so too, especially for.
Speaker 1: Those who may have some background and web development already.
Speaker 2: Yeah, if you've got HTML, CSS, JavaScript out of your belt,
Speaker 2: you're you're going to be in a good spot.
Speaker 1: Like the usual suspects for sure. But it also delves
Speaker 1: into deploying on AWS, yeah, which I think is a
Speaker 1: major win absolutely for practical experience.
Speaker 2: You gotta get that real world story. Yeah.
Speaker 1: So there's a lot to unpack, there is, But we
Speaker 1: can't forget about those core Rust concepts like.
Speaker 2: Yeah, like data types, memory management, or handling, right, and
Speaker 2: these can be stumbling blocks, especially if you're coming from
Speaker 2: a language like Python or JavaScript. Oh for sure, you
Speaker 2: know where things are maybe a little bit.
Speaker 1: Looser, yeah, a little more forgiving exactly. So how Rust
Speaker 1: handles things differently is one of the first things that
Speaker 1: grab my attention. Okay, strong typing. This concept of ownership
Speaker 1: of variables and memory management without a garbage collector, right,
Speaker 1: that's a lot to process.
Speaker 2: It is, It is a lot to process, and that's
Speaker 2: that's what makes us so unique. This, this ownership system
Speaker 2: in particular, is key to Rust's memory safety. Okay, without
Speaker 2: needing that garbage collector. Interesting, Yeah, so think of it
Speaker 2: this way. Every piece of data in Rust has a
Speaker 2: designated owner, and when that owner is no longer needed,
Speaker 2: the data is automatically cleaned up. No more memory leaks
Speaker 2: or those pesky dangling pointers.
Speaker 1: It's like having a built in cleanup crew your code.
Speaker 2: That's a great way to put it.
Speaker 1: Always making sure things are tidy, yeah and efficient.
Speaker 2: Keep everything nice and clean.
Speaker 1: But how does that play out in a real world scenario?
Speaker 2: Okay, so let's take strings as an example. On page sixteen,
Speaker 2: the book shows how a string is created and assigned
Speaker 2: to a variable, making that variable the owner. Then if
Speaker 2: you pass that string to a function, ownership is transferred.
Speaker 2: You're essentially handing over responsibility, gotcha, for that piece of data.
Speaker 2: And this prevents situations where multiple parts of your code
Speaker 2: might try to modify the same data simultaneously.
Speaker 1: Which would cause chaos.
Speaker 2: Yeah, it can lead to all sorts of problems.
Speaker 1: So it's like having a clear chain of command for
Speaker 1: your data absolutely, so everyone knows who's in charge exactly.
Speaker 2: And it's visually represented in that diagram on page sixteen
Speaker 2: showing the strings relationship to strains, So it really helps
Speaker 2: to see it laid out like that.
Speaker 1: Definitely, now onto something that always makes me a bit nervous,
Speaker 1: error handling.
Speaker 2: Oh yeah.
Speaker 1: It seems that Rust takes a very unique approach, as
Speaker 1: it does with these things called options and results. Oh right,
Speaker 1: It kind of sounds a little intimidating.
Speaker 2: They do. They do sound a little intimidating at first,
Speaker 2: but options and results are Rust's way of forcing you
Speaker 2: to confront those potential errors head on, okay, like, really
Speaker 2: think about what could go wrong, gotcha. The book uses
Speaker 2: a great example on page ten with a hash mat. Okay,
Speaker 2: when you try to retrieve a value using a key
Speaker 2: that the key might not exist and So instead of
Speaker 2: just like returning the value, which could lead to unexpected crashes,
Speaker 2: Rust returns an option.
Speaker 1: So it's like Rust is saying, hey, be prepared, Yeah,
Speaker 1: this might not work out as plans, exactly.
Speaker 2: It's a bit more upfront than other.
Speaker 1: Languages that kind of let you get away with ignoring
Speaker 1: those potential errors.
Speaker 2: Right. In those other languages, you can just kind of
Speaker 2: like cross your fingers and hope for the best.
Speaker 1: Yeah, hope it works.
Speaker 2: But Rust is like, nope, let's deal with this right now.
Speaker 1: Yeah, let's be explicit.
Speaker 2: Yeah.
Speaker 1: So that option acts like a container. It either holds
Speaker 1: the value you're looking for, or it indicates that it's empty, exact,
Speaker 1: and you have to explicitly check and unwrap it to
Speaker 1: access that value.
Speaker 2: Yep, And that prevents those nasty runtime surprises.
Speaker 1: So you're not getting this, like you said.
Speaker 2: Crashes exactly, No, no crashes.
Speaker 1: Later on down the line, I'm starting to see how
Speaker 1: this emphasis on explicit error handling can lead to more
Speaker 1: robust code in the long run.
Speaker 2: Absolutely, it's like building a safety net, yeah, into your
Speaker 2: program from the ground up.
Speaker 1: Okay, great, now onto structs and treats. I have to
Speaker 1: admit those names sound a little intimidating a little bit.
Speaker 2: Yeah they sound a little scary, yeah, but they're actually
Speaker 2: pretty straightforward.
Speaker 1: Okay.
Speaker 2: So think of structs as blueprints, okay for your data.
Speaker 2: They let you define the structure of the information you're
Speaker 2: working with.
Speaker 1: Gotcha.
Speaker 2: Traits, on the other hand, are like contracts.
Speaker 1: Okay.
Speaker 2: They define be behavior that different structs can share.
Speaker 1: So if you have a struct that represents a user, okay,
Speaker 1: you can have a trait that defines how to authenticate
Speaker 1: that user right exactly, okay, and any other struct that
Speaker 1: wants to handle authentication would need to implement that trait exactly.
Speaker 2: Yeah. And that's how you ensure consistency, yeah, across your code.
Speaker 1: Gotcha.
Speaker 2: So the book illustrates this concept nicely on pages twenty eight,
Speaker 2: thirty and fifty one with the coordinates struct and traits
Speaker 2: like get to delete and edit. Cool, and it shows
Speaker 2: you how you can add specific functionality to a struck,
Speaker 2: making your code more modular and easier to maintain.
Speaker 1: So it's about those building blocks, creating those reusable components
Speaker 1: that you can mix and match to create more complex functionality.
Speaker 2: Exactly.
Speaker 1: Awesome. So, speaking of toolboxes, okay, we can't forget about Cargo.
Speaker 2: Of course not. Cargo's the best rusts.
Speaker 1: Build system and package manager.
Speaker 2: Right.
Speaker 1: It's like having a personal assistant for your rest projects. Yeah,
Speaker 1: it seems like Cargo handles everything. It pretty much does,
Speaker 1: from managing dependencies, building your code, running tests, generating documentation.
Speaker 2: It's like the Swiss army knife.
Speaker 1: It is a Swiss army knife.
Speaker 2: Of Rust develops.
Speaker 1: You don't have to manually download and install libraries, worry
Speaker 1: about compatibility issues, any of that headache inducing stuff.
Speaker 2: None of that.
Speaker 1: Cargo just handles it all for you.
Speaker 2: Yep, it just takes care of it.
Speaker 1: Now, let's move on to web frameworks. Okay, the book
Speaker 1: focuses on Actics Web, known for its speed.
Speaker 2: It is blazing fast.
Speaker 1: What makes it so fast, so.
Speaker 2: Actis Web leverages Rust's asynchronous capabilities. It uses async programming,
Speaker 2: which essentially allows your web app to juggle multiple tasks
Speaker 2: concurrently without getting bogged down by waiting. Think of it
Speaker 2: like a chef who can prep ingredients for one dish
Speaker 2: while another is simmering on the stove.
Speaker 1: So instead of waiting for one task to finish before
Speaker 1: moving on to the next, exactly, it can keep things moving.
Speaker 2: Yep, keep everything flow and smoothly.
Speaker 1: Making it much more efficient and responsive.
Speaker 2: It's all about that efficiency.
Speaker 1: So Rust has built in support for this asynchronous programming
Speaker 1: it does, which Actic's Web expertly uses right out of
Speaker 1: the box. The book breaks down the structure of an
Speaker 1: Actic's web app on page eighty six, Yeah, showing how
Speaker 1: views are modularized for different functionalities like authentication or handling
Speaker 1: specific data items.
Speaker 2: Yeah, it's all about keeping your code nice and organized.
Speaker 1: Yeah, it's all about organization and keeping your code clean exactly.
Speaker 1: And then we have routes which we see defined on
Speaker 1: pages ninety eight and one oh two. Yeah, and these
Speaker 1: are like the signposts of your web app.
Speaker 2: Yeah, think like the roadmap for your application.
Speaker 1: So they direct incoming requests to the appropriate views based.
Speaker 2: On the URL exactly. So somebody goes to you know,
Speaker 2: slash log in, yeah, it knows to send them to
Speaker 2: the log in view. Perfect, things like that.
Speaker 1: Speaking of data, I see that actics Web uses Jason
Speaker 1: extensively for communication.
Speaker 2: Jason is the language of the web. It is, it is,
Speaker 2: it really is.
Speaker 1: But luckily there's Sarday.
Speaker 2: Yes, Sauraday is a life saver.
Speaker 1: To simplify that whole process.
Speaker 2: Makes it so easy.
Speaker 1: Saraday effortlessly converts Rust data structures into JSON format for
Speaker 1: sending over the network, and seamlessly converts incoming Jason back
Speaker 1: into Rust.
Speaker 2: Structs, right back into those nice, neat data structures.
Speaker 1: The book showcases this beautifully on page one oh five,
Speaker 1: where a j request is handled and the response is
Speaker 1: sent back as Jason data, all beautifully structured using a
Speaker 1: Toto item struct defined on page one ten.
Speaker 2: It's a great example.
Speaker 1: Yeah, I think so too. So Rust combines power with elegance.
Speaker 1: You get the control and safety of RUSS with the
Speaker 1: convenience and readability of well structured code.
Speaker 2: And that's what we all want, right we do. We
Speaker 2: want our code to be readable and understandable.
Speaker 1: So far we've explored how Rust hands data, yeah, manages
Speaker 1: memory structures web apps. Right, it's a lot to take in,
Speaker 1: but it's pretty impressive.
Speaker 2: It is. It is very impressive.
Speaker 1: So what's next on a Rust adventure?
Speaker 2: Well, no web app is complete without data persistence, right,
Speaker 2: We need a way to store information beyond those temporary
Speaker 2: JSON files. So for that we have diesel therm for Rust.
Speaker 1: All right, let's dive into that in part two. That's
Speaker 1: good So, dear listener, take a moment to digest all
Speaker 1: this amazing Rust knowledge.
Speaker 2: There's a lot to unpack.
Speaker 1: There is. We'll be right back with part two.
Speaker 2: Yeah, looking forward to it.
Speaker 1: To explore diesel and other fascinating aspects of Rust web development.
Speaker 1: See you soon, Welcome back. Last time, we got a
Speaker 1: glimpse of how Rust approaches you know, memory management and
Speaker 1: error handling.
Speaker 2: It's a different way of thinking, but I'm starting to
Speaker 2: appreciate how it leads to more robust code.
Speaker 1: Oh yeah, for sure. Yeah, but let's dive back into
Speaker 1: the book and see how those concepts play out in
Speaker 1: web development.
Speaker 2: So good.
Speaker 1: I'm particularly interested in those closures mentioned in the chapter
Speaker 1: on handling HTTP requests? What exactly are they?
Speaker 2: So? Closures are essentially anonymous functions that have this needability
Speaker 2: to capture variables from their surrounding environment.
Speaker 1: Do they have a memory?
Speaker 2: Yeah, they kind of have a memory. They hold on
Speaker 2: to information from the context in which they were created.
Speaker 1: Okay, that sounds intriguing, but how does that translate to
Speaker 1: building web servers?
Speaker 2: So think about how you define routes in a web framework.
Speaker 2: You're essentially mapping URLs to specific code that handles those requests.
Speaker 2: Closures provide this really concise and flexible way to do this.
Speaker 2: Remember that httpserver dot new function in aactics web.
Speaker 1: Yeah, that's where you create the web server exactly.
Speaker 2: You pass a closure to that function, and that closure
Speaker 2: defines the applications logic.
Speaker 1: So the closure acts like the brain of the web server.
Speaker 2: Yeah, that's a great way to put.
Speaker 1: It, decide what to do with each incoming request.
Speaker 2: Yeah, it's like the control center okay. And because closures
Speaker 2: can capture variables from their surroundings, they can access things
Speaker 2: like database connections or configuration settings okay, that are defined
Speaker 2: outside the closer itself.
Speaker 1: So it's like having everything the server needs readily available.
Speaker 2: Exactly. It's all right there at its fingertips.
Speaker 1: It seems like closures bring a lot of organization and
Speaker 1: efficiency to the table.
Speaker 2: They really do, they really do.
Speaker 1: But web development also often involves a lot of asynchronous programming.
Speaker 2: Oh yeah, it's essential these days.
Speaker 1: Which I'm still trying to wrap my head around.
Speaker 2: It can be a little mind bending, it can, but
Speaker 2: once you get it, it's really powerful.
Speaker 1: Okay.
Speaker 2: So a secretist programming is all about making your applications
Speaker 2: more responsive and efficient. Okay, It allows a program to
Speaker 2: work on other things while it's waiting for something like
Speaker 2: a network request to complete.
Speaker 1: Gotcha. So instead of just sitting idle, right, instead of.
Speaker 2: Just blocking and waiting, it can go do other things.
Speaker 2: It can multitask exactly, get multitask.
Speaker 1: Making better use of resources and time.
Speaker 2: Yeah, it's all about maximizing efficiency.
Speaker 1: So instead of waiting for like a database query to
Speaker 1: finish before moving on exactly, the server can handle other requests.
Speaker 2: Yep, keep things moving.
Speaker 1: It's like a well coordinated dance.
Speaker 2: That's a great way to put it.
Speaker 1: So Rust has this built in support. It does it
Speaker 1: for asynchronous programming, which acticx web expertly uses.
Speaker 2: Yeah, it just takes full advantage of it.
Speaker 1: The book does a good job of breaking down the
Speaker 1: core concepts of asinc in Rust. Okay, like the async
Speaker 1: and awaight keywords.
Speaker 2: Right, those are the key players.
Speaker 1: And it even explores different ways to run these asynchronous
Speaker 1: tasks concurrently using the futures crate.
Speaker 2: Yeah, the Future's crate is essential for working with asink
Speaker 2: and Rust.
Speaker 1: Okay. So now let's shift gears a bit and talk
Speaker 1: about managing the different views within our web app.
Speaker 2: Right, sounds good?
Speaker 1: You know, things like a home page, a log in page,
Speaker 1: a user.
Speaker 2: Profile, all the different sections.
Speaker 1: How does rust approach that?
Speaker 2: So Actic's Web encourages modularity by organizing views into separate modules.
Speaker 1: So it's all about organization.
Speaker 2: Yeah, keeping things tidy as our application grows, right, because
Speaker 2: things can get messy quickly, they can if you're not careful.
Speaker 1: So the book takes that a step further, okay, and
Speaker 1: introduces this concept of view factories.
Speaker 2: Oh yeah. View factories are great.
Speaker 1: Functions dedicated to configuring and registering your views within the
Speaker 1: web application.
Speaker 2: Yeah. It's a nice way to centralize things.
Speaker 1: So instead of scattering route definitions throughout our code, exactly,
Speaker 1: have this centralized.
Speaker 2: Place yep, one place to rule them all.
Speaker 1: I like that.
Speaker 2: It definitely makes things easier to manage.
Speaker 1: So these view factories can be as simple or as
Speaker 1: complex as you need them to be.
Speaker 2: You can tailor them to your specific needs.
Speaker 1: The book provides a clear example on page eighty seven, okay,
Speaker 1: where a view factory is defined for the auth routs.
Speaker 2: All right, so that's handling all your authentic stuff.
Speaker 1: It's fascinating how they use the path struct to define
Speaker 1: the base URL and then register each individual view using
Speaker 1: the service config struct.
Speaker 2: Yeah, it's very elegant solution.
Speaker 1: That's very structured and elegant.
Speaker 2: Yeah. Rust is all about that elegance.
Speaker 1: Okay, So let's switch gears again and talk about data handling.
Speaker 2: All right, data data data.
Speaker 1: They've already seen how handles Jason.
Speaker 2: Right, surday is the Jason Master.
Speaker 1: But what about extracting data, okay from incoming requests and
Speaker 1: sending data back to the client.
Speaker 2: So that's that that communication back and forth between the
Speaker 2: client and server.
Speaker 1: So rust has an elegant solution for this as well.
Speaker 1: Of course it does using structs and trades.
Speaker 2: Structs and traits to the rescue.
Speaker 1: They really are the backbone of rest. We really are
Speaker 1: providing this amazing flexibility and organization.
Speaker 2: All about structure, it is.
Speaker 1: So let's imagine you have a form on your website
Speaker 1: for users to enter their name and email address Classic
Speaker 1: contact form, act contact form.
Speaker 2: Yeah, you would define as struct in rusty to represent
Speaker 2: that data with fields for name and email.
Speaker 1: Yep, make a nice little struct.
Speaker 2: And then in your Arctic's WebView you specify that you
Speaker 2: expect this struct as the request body. Gotcha, and Actics
Speaker 2: automatically takes care of de serializing the incoming Jason data
Speaker 2: into that struct.
Speaker 1: It just handles it for you, So no.
Speaker 2: More manual parsing, potential errors.
Speaker 1: It's all taken care of.
Speaker 2: Pactics, just hands you the data.
Speaker 1: Yep, nice and neat.
Speaker 2: Neatly packaged and ready to use, ready to go. And
Speaker 2: sending data back to the client follows a similar approach
Speaker 2: pretty much.
Speaker 1: Yeah, you can take a restruct, serialize it into Jason
Speaker 1: using SORDE, and send it back.
Speaker 2: As the response. So again, it's about making that data
Speaker 2: exchange smooth and effortless.
Speaker 1: Exactly, it should be a seamless process.
Speaker 2: It's amazing how Rust manages to make even complex tasks yeah,
Speaker 2: feel so streamlined and elegant.
Speaker 1: It's a beautiful language.
Speaker 2: But there's one crucial aspect we haven't touched upon yet.
Speaker 1: Security.
Speaker 2: Oh yeah, we can't forget about security.
Speaker 1: How do we ensure our web applications are protected from
Speaker 1: those malicious attacks?
Speaker 2: So security is paramount and Rust lays a really strong
Speaker 2: foundation for building secure applications. But we can't just rely
Speaker 2: on the language alone. We need to be mindful of
Speaker 2: common vulnerabilities and take steps to mitigate them.
Speaker 1: So the book covers several security considerations from guarding against
Speaker 1: cross site scripting EXSS attacks to implementing proper authentication and
Speaker 1: authorization mechanisms.
Speaker 2: Yeah, all the important stuff.
Speaker 1: So it's a combination of leveraging Rust's strengths and following
Speaker 1: those best practices for secure coding.
Speaker 2: Yeah, you gotta have both.
Speaker 1: So let's start with croft site scripting or EXSS. Right,
Speaker 1: it's a common attack where malicious code is injected into
Speaker 1: a web page that other users.
Speaker 2: Yep, so it's like a way to sneak in code.
Speaker 1: Imagine a comment section on a blog, Okay, where users
Speaker 1: can submit their HTML.
Speaker 2: Oh yeah, that's a classic example.
Speaker 1: If an attacker manages to inject a malicious script, they
Speaker 1: could potentially steal user data or hijack accounts, all sorts
Speaker 1: of bad things. So that's why sanitizing user input is
Speaker 1: so crucial.
Speaker 2: It is absolutely crucial. You can't trust anything that comes
Speaker 2: from the user.
Speaker 1: Especially when dealing with HTML.
Speaker 2: Yeah, especially EACHTML, because that's where a lot of these
Speaker 2: attacks happen.
Speaker 1: The book offers best practices for escaping special characters and
Speaker 1: ensuring that users submitted content is displayed safely.
Speaker 2: Right, So you're essentially neutralizing any potentially harmful code.
Speaker 1: It's about treating all incoming data with a healthy dose
Speaker 1: of suspicion.
Speaker 2: Exactly, trust no one.
Speaker 1: Now let's talk about authentication and authorization. Okay, we briefly
Speaker 1: mentioned JWT's and password hashing earlier.
Speaker 2: Right, those are our security heroes, but how do.
Speaker 1: We actually implement those in our rust web app.
Speaker 2: So jbt's and password hashing are key components of a
Speaker 2: secure authentication system, and the book provides a great walkthrough
Speaker 2: on how to implement them, starting with defining a user model. Okay,
Speaker 2: much like the to do item model.
Speaker 1: Right, so we're creating those rust strucks again exactly.
Speaker 2: We love our structure that mirror.
Speaker 1: The structure of our data, this time representing a user
Speaker 1: in our system.
Speaker 2: Right, so you'd have feels for things like username, email address,
Speaker 2: and of course the password.
Speaker 1: Okay, but with passwords we have to be extra careful. Right.
Speaker 2: Oh yeah, passwords are super sensitive.
Speaker 1: Storing them in plaintext is a big no no.
Speaker 2: Absolutely, you never want to store passwords in plain text.
Speaker 1: It's like leaving the keys to the kingdom out in
Speaker 1: the open.
Speaker 2: Yeah, it's just asking for trouble.
Speaker 1: So that's where password hashing comes into play.
Speaker 2: Password hashing to the rescue.
Speaker 1: Instead of storing the actual password, right, we use a
Speaker 1: special function to transform it into a scrambled, unreadable format.
Speaker 2: Exactly. It's like turning it into gibberish.
Speaker 1: The book suggests using the b crypt crate for this, okay,
Speaker 1: and provides an example on page one seventy.
Speaker 2: B crypt is a good choice. It's a very strong
Speaker 2: hashing algorithm.
Speaker 1: Okay. So even if someone gets their hands on those
Speaker 1: hashed passwords, they're essentially useless pretty much.
Speaker 2: Yeah. Yeah, it's extremely difficult to reverse that process.
Speaker 1: It's like trying to solve the complex puzzle without all.
Speaker 2: The pieces, exactly. It's a very difficult puzzle.
Speaker 1: Okay. So we've hashed and stored those passwords securely.
Speaker 2: All right, so far, so good.
Speaker 1: What happens when a user tries to log in?
Speaker 2: So, when a user tries to log in, we take
Speaker 2: the password they enter, okay, hash it using the same
Speaker 2: algorithm and compare the resulting hash to the one store
Speaker 2: in the database.
Speaker 1: So we're not comparing the actual.
Speaker 2: Passwords, No, we're comparing the hashes.
Speaker 1: Right, we're comparing the gibberius exactly, and if the hash
Speaker 1: is match, we know they entered the correct password okay,
Speaker 1: and if everything checks out all right.
Speaker 2: If they pass the test, we issue them a JA
Speaker 2: ANDBA Jason web tooken act like a digital passport containing
Speaker 2: information about the user in their session. This token is
Speaker 2: then sent back to the client, who usually stores it
Speaker 2: in their browser okay and presents it with each subsequent request.
Speaker 1: So it's like they're carrying their identification with them as
Speaker 1: they navigate the application.
Speaker 2: Is their ticket to ride it.
Speaker 1: On the server side, we have middleware checking those JWTs,
Speaker 1: acting like security guards.
Speaker 2: Making sure everything's legit, ensuring.
Speaker 1: That only authorized users can access certain areas.
Speaker 2: Exactly. It's all about control access.
Speaker 1: And if the JWT is invalid.
Speaker 2: Or missing right, then we deny access.
Speaker 1: We deny the request and send back an.
Speaker 2: Error exactly, you shall not pass.
Speaker 1: So it's a multi layered approach to security.
Speaker 2: It is.
Speaker 1: It is using different tools and techniques to protect our
Speaker 1: application and its users.
Speaker 2: You got to keep everyone safe.
Speaker 1: We've covered a lot of ground here. We have from
Speaker 1: closures and asynchronous programming to security and data handling all
Speaker 1: the important stuff. What other gems are hidden?
Speaker 2: Oh, there are plenty more gems.
Speaker 1: Within this world of rust Web development.
Speaker 2: Rust is full of surprises.
Speaker 1: So the book touches on logging, okay, a crucial aspect
Speaker 1: of monitoring and debugging web applications.
Speaker 2: Logging is essential.
Speaker 1: You gotta know what's going on, especially when building RESTful.
Speaker 2: Services, especially in production, when things go wrong.
Speaker 1: When things go wrong, you need to know why. So
Speaker 1: logging it's like having a detailed record.
Speaker 2: Yes, it's like a history book of.
Speaker 1: Everything that's happening within our application.
Speaker 2: Exactly, so you can go back and see what happened.
Speaker 1: The book introduces some popular Rust logging libraries, like what
Speaker 1: like log in on vlogger. Those are good ones, making
Speaker 1: it super easy to integrate logging into our applications.
Speaker 2: Yeah, they make it really straightforward.
Speaker 1: But there's one more crucial piece of the puzzle okay, deployment.
Speaker 2: Ah, yes, deployment the final frontier.
Speaker 1: How do we actually get our rust web application out
Speaker 1: into the world.
Speaker 2: Out into the wild for others to use. So the
Speaker 2: book uses AWS as an example. Deploying to an easy
Speaker 2: two instance using Docker.
Speaker 1: Docker, our trusty friend.
Speaker 2: Docker is a lifesaver.
Speaker 1: It's like packaging your application and all its dependencies into
Speaker 1: this neat little container that can run anywhere exactly.
Speaker 2: It's like a little self contained.
Speaker 1: World regardless of the underlying operating system. Yep, it's platform
Speaker 1: agnostic and AWSC two provides those virtual servers in the
Speaker 1: cloud ready to host our containerized applications.
Speaker 2: Yeah, it's a match made in heaven.
Speaker 1: But how do we get our Docker image from our
Speaker 1: computer to the cloud.
Speaker 2: So that's where services like Docker Hub come in, which
Speaker 2: act as a registry where you can store and share
Speaker 2: your Docker images.
Speaker 1: So you build your image locally, push it to Docker Hub,
Speaker 1: and then your server on AWS can easily pull it
Speaker 1: down and run it.
Speaker 2: Yeah, it's a very streamline process.
Speaker 1: We've covered so much ground in this deep.
Speaker 2: Dive we have. It's been a whirlwind, from.
Speaker 1: The fundamentals of RUSS to handling requests, securing our app
Speaker 1: and even deploying it to the cloud.
Speaker 2: The whole nine yards just mind boggling. It is a
Speaker 2: lot to take.
Speaker 1: In, but we've only scratched the surface.
Speaker 2: Oh yeah, just the tip of the iceberg.
Speaker 1: Of what's possible with Rust web development.
Speaker 2: Rust is an incredibly powerful language.
Speaker 1: So what other adventures await us?
Speaker 2: Oh, there's always more to explore, and.
Speaker 1: What exciting new concepts will we uncover?
Speaker 2: I guess you'll have to stay tuned to find out.
Speaker 1: So, dear listener, take a break, Yeah, grab a coffee
Speaker 1: or something. Let all this information sink in. There's a
Speaker 1: lot to process, and we'll be back with Part three.
Speaker 2: Looking forward to it to.
Speaker 1: Dive into even more fascinating aspects of Rust.
Speaker 2: I see you soon.
Speaker 1: Welcome back to our rest journey. We've covered so much already,
Speaker 1: like how Rust handles data, manages memory, and even secures
Speaker 1: our web applications.
Speaker 2: It's been quite the adventure it has.
Speaker 1: Now I'm really eager to dive into the practical side
Speaker 1: of things, like interacting with databases and building those powerful,
Speaker 1: RESTful APIs.
Speaker 2: You're in for a tree database interaction is a cornerstone
Speaker 2: it is of most web applications, and thankfully Rust has
Speaker 2: a really powerful tool for that. Diesel the orm we
Speaker 2: briefly touched on before.
Speaker 1: Yeah, Diesel lets us communicate with our Postgres school database
Speaker 1: using nice, clean, type safe rest code.
Speaker 2: It's a beautiful thing.
Speaker 1: I'm excited to see it in action.
Speaker 2: Yeah, me too.
Speaker 1: So the book provides a fantastic step by step guide
Speaker 1: on using diesels, starting with setting up our development environment using.
Speaker 2: Docker ah Docker our trusty friends.
Speaker 1: Our trusty friend for creating those isolated environments.
Speaker 2: Right. It keeps everything nice and contained.
Speaker 1: It's like having a mini virtual machine dedicated to our database,
Speaker 1: keeping things organized and preventing conflicts with other software on
Speaker 1: our system.
Speaker 2: Exactly. It's like its own little sandbox.
Speaker 1: So docer makes setting up and managing these environments a breeze.
Speaker 2: Oh yeah, it takes all the headache out of it.
Speaker 1: The book then guides you through defining our database schemakay
Speaker 1: using Diesel's migration system.
Speaker 2: Ah. Migration's very import.
Speaker 1: Like version control for our database. Right.
Speaker 2: Yeah, that's a great way to think about it.
Speaker 1: So they allow us to track changes, roll back to
Speaker 1: previous versions if made, and keep everything organized.
Speaker 2: It makes collaboration so much easier, I bet, yeah, because
Speaker 2: everybody can be working on their own migrations and then
Speaker 2: you just apply them in order and it all works out.
Speaker 1: So Diesel makes this process very smooth, it does it?
Speaker 1: Does You create these migration files that describe the changes
Speaker 1: you want to make to your database, and Diesel takes
Speaker 1: care of applying them in the correct order.
Speaker 2: Yeah. It even has a command line tool does it
Speaker 2: to simplify running those migrations.
Speaker 1: So it's like a one stop shop pretty much. Yeah,
Speaker 1: I'm looking at an example on page one fifty six.
Speaker 1: It seems pretty straightforward.
Speaker 2: Yeah, it's very intuitive.
Speaker 1: You define the migration file and inside you write SQL
Speaker 1: statements to create or modify tables.
Speaker 2: So it's still SQL under the hood. It is, but
Speaker 2: Diesel just gives you a nice way to manage it.
Speaker 1: And the beauty of it is that Diesel ensures that
Speaker 1: those migrations are applied in correct sequence, preventing any inconsistencies
Speaker 1: or conflicts that could arise from out of order changes.
Speaker 2: It's like a safety net for your database.
Speaker 1: It takes a lot of the headache out of database management,
Speaker 1: oh for sure.
Speaker 2: For sure.
Speaker 1: Okay, so we've defined our schema, we've run our migrations.
Speaker 2: Now we need to create some Rust models, okay that
Speaker 2: represent those database tables.
Speaker 1: So these models are like the bridge between our RUST
Speaker 1: code and the database, right.
Speaker 2: Yeah, they're the intermediaria.
Speaker 1: Making interacting with data types safe and easy to manage,
Speaker 1: and they.
Speaker 2: Keep our code nice and clean.
Speaker 1: So we're essentially creating Rust structs.
Speaker 2: We love our structure.
Speaker 1: That mirror the structure of our table exactly.
Speaker 2: Each field in the struct corresponds to a column in
Speaker 2: the database table.
Speaker 1: I love how Rust encourages us to think about these
Speaker 1: data structures and types.
Speaker 2: It makes the code so much more explicit and readable.
Speaker 1: It really does.
Speaker 2: Yeah, and Diesel leverages this to its advantage. It makes
Speaker 2: working with databases in Rust a really enjoyable experience.
Speaker 1: I bet so. No more wrestling with raw SQL queries.
Speaker 2: No more raw sequel unless you really want.
Speaker 1: It, unless you really want to.
Speaker 2: But Diesel can handle most things for you.
Speaker 1: I'm looking at this example on page one sixty one. Okay,
Speaker 1: where they define a model for a to do item.
Speaker 2: Right, the trustee to do list.
Speaker 1: They're using this table macro, which seems to be doing
Speaker 1: all the heavy lifting of mapping that's stretched to the
Speaker 1: database table.
Speaker 2: Yeah. That macro is a lifesaver.
Speaker 1: Taking care of all that boilerplate code for you.
Speaker 2: Exactly. It just does all the work behind the scenes.
Speaker 1: Okay, so we've got our model set up, right, how
Speaker 1: do we actually use them to interact with the database.
Speaker 2: So Diesel provides this really expressive query builder that allows
Speaker 2: you to write database queries in this really type safe way.
Speaker 1: So we're not just throwing raw SEQL queries at the database, no, no, no,
Speaker 1: and hoping for the best.
Speaker 2: Diesels are there to guide us and make sure we're
Speaker 2: doing things correctly.
Speaker 1: I see an example on page one sixty eight where
Speaker 1: they're using diesel to fetch all the to do items
Speaker 1: from the database. It's an interesting blend of that sequel
Speaker 1: like syntax with those Rust function calls.
Speaker 2: Yeah, it's a nice fusion of the two worlds. Is
Speaker 2: so you get the familiarity of sequel, but with the
Speaker 2: type safety of Rust.
Speaker 1: This is fantastic. So we can build these sophisticated database
Speaker 1: interactions all within the safety and structure of RUSS exactly.
Speaker 1: But what about user authentication?
Speaker 2: Ah? Yes, security.
Speaker 1: We talked about jbts and password hashing.
Speaker 2: Earlier, right, our security heroes.
Speaker 1: How do we put those into practice?
Speaker 2: Okay, so user authentication is crucial for any application it
Speaker 2: is that handles sensitive data. Yeah, and the book does
Speaker 2: not disappoint. I bet it provides a really detailed guide
Speaker 2: on implementing authentication using jtabts and password hashing. And it
Speaker 2: starts by defining a user model. Okay, much like we
Speaker 2: did with our to do item model.
Speaker 1: So again we're creating those Rust strucks that mirror the
Speaker 1: structure of our data. We love our structs, this time
Speaker 1: representing a user in our system.
Speaker 2: Right, so we'll have fields for username, email address, and
Speaker 2: the all important password.
Speaker 1: But here's where we have to be extra careful, right, Oh.
Speaker 2: Yeah, extra careful with passwords.
Speaker 1: Storing them in plaintext is a huge security risk.
Speaker 2: Absolutely, never store passwords in plain text.
Speaker 1: It's like leaving the keys to the Kingdom out in
Speaker 1: the open.
Speaker 2: Yeah, it's just asking for trouble.
Speaker 1: So that's where password hashing comes in to save the day.
Speaker 2: Password hashing to the rescue.
Speaker 1: Instead of storing the actual password, we use a special
Speaker 1: function to transform it into a scrambled, unreadable format.
Speaker 2: We turn it into gobbledygook.
Speaker 1: The book suggests using the b crypt create okay and
Speaker 1: provides an example on page one seventy nine.
Speaker 2: B crypt is a good choice. It's a very strong
Speaker 2: hashing algorithm.
Speaker 1: So even if someone gets their hands on those hashed passwords,
Speaker 1: they're essentially useless.
Speaker 2: Yeah, it's extremely difficult to reverse that hashing process.
Speaker 1: Okay, So when a user tries to log in, we
Speaker 1: take the password they enter, right, hash it using the
Speaker 1: same algorithm, and compare the resulting hash to the ones
Speaker 1: in the database Exactly, we're comparing the gobbledygook, and if
Speaker 1: the hashes match, we know they've entered the correct password.
Speaker 2: That's how we know they're who they say they are.
Speaker 1: And if everything checks out, what happens then?
Speaker 2: Okay? So if the credentials are valid, we generate a
Speaker 2: JWT that Jason webtok.
Speaker 1: Can we talked about before, the digital passport.
Speaker 2: Right, it's their ticket to ride.
Speaker 1: Containing information about the user and their session exactly. Okay,
Speaker 1: So we send this token back to the client, who
Speaker 1: typically stores it in their browser and includes it in
Speaker 1: each subsequent request.
Speaker 2: Right, It's like their badge of authentication.
Speaker 1: So on the server side, we need middleware right that
Speaker 1: intercepts those requests, verifies the JWT, and only allows the
Speaker 1: request to proceed if everything checks out.
Speaker 2: Yeah, the middleware is like the bouncer at the club
Speaker 2: checking IDs, making sure everyone's got the right credentials.
Speaker 1: And if the JWT is invalid or missing, boot them out.
Speaker 1: We deny the request and send back an ERA.
Speaker 2: You shall not pass.
Speaker 1: It's all about adding those layers of protection to ensure
Speaker 1: that only authorized users can access specific resources or perform
Speaker 1: certain actions within our application.
Speaker 2: Security is a never ending battle it is, but rust
Speaker 2: gives us the tools to fight it effectively.
Speaker 1: Now that we have user authentication in place, are we
Speaker 1: ready to build those APIs we've been talking about.
Speaker 2: Let's do APIs or the backbone of the modern web?
Speaker 1: They are?
Speaker 2: Yeah.
Speaker 1: So RESTful APIs are a way to structure our web
Speaker 1: services to make them easily accessible and usable by other applications, right.
Speaker 2: Exactly, think mobile apps, third party integrations, all sorts of things.
Speaker 1: The book focuses on using actis web to build RESTful APIs.
Speaker 2: Good choice.
Speaker 1: Actics is great for that it is, and it highlights
Speaker 1: these important concepts like statelessness, caching, and of course logging
Speaker 1: all the essentials. So statelessness means that each request is independent, right.
Speaker 2: Right, Each request is treated in isolation.
Speaker 1: Making it easier to handle a large number of requests concurrently.
Speaker 2: Exactly, it's all about scalability.
Speaker 1: Caching helps boost performance by storing frequently accessed.
Speaker 2: Data right, so we don't have to hit the data every.
Speaker 1: Time, reducing the load on our servers.
Speaker 2: Ye, keep those servers happy.
Speaker 1: And logging provides those valuable insights into what's happening within
Speaker 1: our API.
Speaker 2: Right, It's like having a window into your application.
Speaker 1: Helping us track down those issues and monitor performance.
Speaker 2: Gotta love logs.
Speaker 1: The book even touches on techniques like code on demand,
Speaker 1: which sounds pretty powerful.
Speaker 2: Code on demand is really interesting, it is. Yeah, it
Speaker 2: lets you send code from the server to the client okay,
Speaker 2: to be executed.
Speaker 1: Interesting.
Speaker 2: Yeah, it can be really powerful, Okay, but it also
Speaker 2: comes with some security risks.
Speaker 1: So you got to be careful.
Speaker 2: Oh yeah, definitely.
Speaker 1: Okay, So let's talk about.
Speaker 2: Testing, okay, testing one, two, three.
Speaker 1: We build all this amazing functionality, but how do we
Speaker 1: actually ensure that it works as expected?
Speaker 2: Testing is crucial. Yeah, you got to make sure your
Speaker 2: code actually does what you think it does.
Speaker 1: So Rust has excellent support for both unit testing right
Speaker 1: where you test individual components in isolation.
Speaker 2: Ye break it down into smaller pieces.
Speaker 1: And functional testing okay, where you test the application.
Speaker 2: As a whole, so testing the whole system from end
Speaker 2: to end.
Speaker 1: The book dedicates an entire chapter to testing that important.
Speaker 1: It walks through unit testing individual modules using Rust's built
Speaker 1: in testing framework, and then it dives into functional testing
Speaker 1: using tools like Postman and Newman.
Speaker 2: Ah, Postman and Newman great tools for API testing, So
Speaker 2: Postman is a popular tool for testing APIs. Yeah, it's
Speaker 2: like the industry standard.
Speaker 1: It allows you to create collections of requests, define assertions
Speaker 1: to verify the responses, and even automate the execution of
Speaker 1: those tests.
Speaker 2: Yeah. You can even run those tests for the command
Speaker 2: line using nimen oh wow, which is great for continuous
Speaker 2: integration and delivery.
Speaker 1: So you can essentially automate the testing of your entire
Speaker 1: API pretty much. Yeah, ensuring that everything keeps working as
Speaker 1: you continue to develop and add new features.
Speaker 2: It's all about automation.
Speaker 1: That's incredibly powerful. It is, so it takes a lot
Speaker 1: of that manual effort and guesswork out of testing.
Speaker 2: Yeah. It frees you up to focus on the fun stuff, right.
Speaker 1: And it allows us to catch those errors early and
Speaker 1: keep our application stable and reliable as it grows.
Speaker 2: Exactly.
Speaker 1: Wow. Rust truly seems to.
Speaker 2: Have it all. It does it does from powerful.
Speaker 1: Language features and robust libraries to comprehensive tooling and a
Speaker 1: strong emphasis on testing.
Speaker 2: It's the whole package.
Speaker 1: It feels like it was designed with the developers in mind.
Speaker 2: It was. It was definitely built by developers for developers.
Speaker 1: So to our listeners, we hope this deep dive has
Speaker 1: sparked your own curiosity about Rust.
Speaker 2: Yeah, go out there and give it a try.
Speaker 1: It's a language that's reshaping the landscape of web development.
Speaker 1: It really is, and we can't wait to see what
Speaker 1: incredible things you create with it.
Speaker 2: Yeah, the possibilities are endless.
Speaker 1: Until next time, Happy coding, Happy coding.