Intro: Rediscovering WeaveBooks and the Challenge
What's up boys and girls. Seven years ago I wrote this beautiful package which is called WeaveBooks. Right, you can see it's in the BubblePost repo. But I wrote this code, it's backtrackable. It's when I worked at the startup, eight years ago, the BubblePost startup in Belgium, a company I worked for, and I wrote this minimalistic, super fast, easy-to-use web framework for Go nine years ago. Seven years ago, and it's exactly the same as we can see these frameworks today. GoFiber, for example, as I think it's the most popular one, it works exactly the same. But I wrote it seven years ago and GoFiber is basically, I think it's only two or three years old, right? So did they copy me? Did GoFiber copy the undisputed champion? Well, let's figure this out. Let's bring WeaveBooks back to life, check what it can, and at the end of the video we are going to benchmark WeaveBooks versus GoFiber to see who is the undisputed champion. But before we continue, if you're not subscribed to the channel consider subscribing to the channel, give me a thumbs up, leave some questions in the comments, ring the notifications bell so you will always be up to date when I go live or when I make a new video, and very important jump into my Discord community so I can turn you into a professional. I also have an announcement to make. I created the FullTimeGoDev program which will be live around January or mid-January and I'm doing a pre-sale right now which will be 30% off. If you pre-sale the full program right now it serves both sides of the skill spectrum for people that have no Go experience at all but also for people that have already some experience in Go. I will cover the whole shebang from a complete Go introduction to mastering concurrency, building JSON APIs, the complete microservice stack, gRPC and circuit breaking, Apache Kafka, all that shenanigans, and I'm even gonna cover how to deal with Go interview jobs, how to build confidence, how to make a killer resume, and I'm gonna give you a complete free new blockchain from scratch in Go language, gRPC on top of that. So 30% off fulltimegodev.com. I'm looking forward to seeing you as a student in my new program.
Cloning the Repo and Opening WeaveBooks
So I'm going to clone this thing real quick, git clone, paste that in. Ah, what's going on? Git clone. By the way, still early in the morning. All right, let's go to WeaveBooks. WeaveBooks. Let's open up GoTee real quick. New version, what's going on? All right, cool. Let me quickly boost up the font size. Let's do two scaling two and then we're going to make this 18. Boom. Let's open up WeaveBooks itself. Right, you see this is WeaveBooks. It's using HTTP router under the hood like I said it's from eight years ago. Not quite sure if these things are going to work. Let's see. What I'm going to do basically is I'm going to make it an example. I'm going to make a new folder and we're going to call this 2023 which is basically what the year we are in two weeks. Let's make a new main file in this thing. package main so we can test this package outright. package main, func main just like that. And now we're going to see in WeaveBooks itself we're gonna let's do a go mod init actually go mod init github.com I think I can do weavebox 2023 which was my nickname back in the day with boxes just like that. That's good. Then we're going to install this Julian Schmidt HTTP router. God damn it it's so annoying that I cannot go get github.com. It's going to be Julius Schmidt HTTP router. Let's go. That's fine. The context was back in the day, guys, context was not in the standard library so we can delete that, it's gonna be fine. So I think this is good, WeaveBooks test. There's going to be some issues I guess. WeaveBooks test, let's do it like this. What's going on? WeaveBox test. The font is so big it's annoying for me but it is what it is. I think that's fine. Yeah I think it's good. Let's open up the folder again. What do we need to fix? servers. What's going on man. It's annoying here. server, Brad Fitz we need to install that. It's going to be GitHub. go get github.com. It's gonna be Brad Fitz HTTP 2. Boom, taken care of. That's fine. All right so we can I think we can go to our examples 23. So basically WeaveBooks is like I said, it's a very simple minimalistic web framework and let's do some stuff. So I'm going to say app is going to be WeaveBooks new just like that. It's not actually importing it for some reason that's annoying.
Building Routes and Parameters with WeaveBooks
How are we gonna do this? Let's import it manually then. Man this font is crazy. HTTP no it's going to be github.com tvanis weavebooks. Yes. Fine. We have this app and then we're gonna say for example app.Get we're going to say users slash users, we're going to say handle GetUsers and I'm going to say app.Serve we need to give a port which is an int 3000 and we're going to say func handleGetUsers is going to be a weavebooks context and then an error just like it is the perfect automatic stuff and I'm going to say return, for example ctx.Text, no it's going to be ctx.Text. I'm going to say http.Status OK and we're gonna say "hello from 2015" because this is written in 2015 can you imagine. Yes that's fine. So then we can actually say let's cd into examples 2023 here and then say go run main.go boom and let me open up what is my GitHub thingy here. Let's go to localhost:3000 and this is going to be users. Boom. Hello from 2015. Working perfectly fine. What we also could do is basically do a slash get users with an ID for example and it's going to be handleGetUsersByID. Right let's copy this whole function, paste it in handleGetUsersByID and then we can say that the ID is going to be ctx.Param, is it Param? Param is going to be ID, right. And then we're going to say let's just dump the ID in here, right. I'm going to say the ID and let's see if it's all going to work out fine. Let's kill this, go run main.go. Boom users slash 4 and you can see the ID here it's 4. Perfectly fine. And if you do this ID will be this. Hey, working like intended right. So yes.
Boxes, Grouping Routes and Middleware Scopes
What we also could do is boxing. That's why basically I call this package WeaveBooks is because we can make boxes, we can weave all these boxes together and that's basically super outs, right. So we could do something like this. Let's say users is going to be app.Box slash users right and I'm going to say app.Get instead of saying users we can do just slash and instead of doing slash users we can do just slash ID but we need to change this to users right so we have a separate route of users. Right. And then PS Code is doing so annoying today it's insane. We're going to say go run main.go boom, open up this tender request teach and you can see it works perfectly fine right. We can do users with ID and just users. Boom it works perfectly fine right. Boxing. So you can see it's coming right. We could do something like books is going to be app.Box slash books right and then we could do something like grab this you're gonna say books slash handleGetBooks like that and then we can copy this function paste it in and call this handleGetBooks and now we're gonna say hello books from 2015. Kill this application, go run main.go here boom. New request, actually boom and I'm gonna say books, /books. Hello books from 2015. Hey amazing isn't it. What we also could do is very simple if you want to have some logging we're going to say app.EnableAccessLog = true, very simple. And if you go then to request actually I need to kill the server real quick. go run main.go boom. Go to new request each time we do this you can see this beautiful access log for us. Isn't that amazing. There is also support for static file rendering, it is a template engine. Oh middleware, yes. Yes there is something very simple for middleware for examples. Let's make some not quite sure how I need to do this middleware by the way so we have this users, can I do something like users.Use? I don't know who I'm not quite sure what the middleware function is going to be. A simple handler actually it's just a context, a func context error. It's the same thing it's fine. So we're gonna make a middleware function actually, true middleware, let's make its full metal wedge could be some authentication or something. Be creative. We're going to say FullMetalWedge C is going to be a weavebooks context and an error I guess and we're going to say here return nil and we're going to do fmt.Println doing some stuff in the middle wedge function like this. Boom. You see so right now you can see that we and that's the beauty of this boxing of this weave boxing that we have users.Use so this Full middleware will only be available for the users group, for the users box right, not for the books. So we're going to test this real quick go run main.go. That's perfectly fine. Let's do this so let's do books and you can see nothing happens right only the access log is doing but it is printing out. If we do users you can see that the middleware for the users is working right and that's the beauty so you can have a different middleware attached to different groups of boxes of handlers. Right. You could do like this we're gonna say books.Use the book middleware for example. Let's make a book middleware real quick and then we're gonna do some stuff in the book actually reading books just like that. Scale it again, go request so you can see doing some stuff in the middleware function if we go to /books you can see reading books right. Hey the beauty of WeaveBooks.
Preparing and Running Benchmarks Against GoFiber
So the next thing we're gonna do is we're gonna benchmark this. Very important. And I already have a bench and we're going to compare this with GoFiber and see who is the fastest, right. So let me quickly see if I can benchmark. It's a bench test, BenchmarkWithValues, it's this simple thingy that's fine. The question is can I run this? We can't because we have this error seat and weavebooks_test that's nasty. What is this needs to be adult half Edward F. Let's do all of them real quick, while refactoring it and that is basically fixing stuff right. Boom. This guy here I wrote F I don't know I'm not sure why I did didn't do this back in the day it's crazy. This is a problem because code is an integer. That's fine now this guy hit. Boom you can see there are a lot a lot of tests in this package, a lot of tests, look at this insanely. All right let's open up back this bench test. Can we do this right now it's busy, it's bension. All right so we have five Alex which is almost zero allocations, uh for 482 ns per operation so that's perfectly fine. So what we're gonna do is we're going to open up actually we don't, we're gonna make another bench test we're going to say func BenchmarkGetWithValuesFiber. I'm gonna say b it's going to be testing.B. Okay let's install Fiber real quick. Never use this thing by the way. Never use this thing because you can see I'm writing my own stuff right seven years ago. These guys from GoFiber were still and they're diapers they were still themselves in their bed in their pajamas you know what I mean. Go get this thing Fiber. Look at this all this that needs to be installed but hey it is. So we're gonna it's gonna be something like app, you see it's the same thing right, it's gonna be appFiberS. I'm 100% sure it's gonna be this fiber.New is that a thing probably need to install this let me paste copy this again paste it in here delete this go get do this and that boom fiber.New. Um it's going to be app.Get you see it's the same thing they just copy me man they copied me. Actually I can copy this thing paste it in here hello name is going to be a func city because it's probably going to be a fiber.Context is that a thing it's not it's going to be a fiber.Ctx then. So predictable. And then we're gonna copy the whole shebang hitch hop hop and it's gonna be app.Serve. The problem is they don't have, they did something not good which basically this is not a Handler actually it cannot do it doesn't have the ServeHTTP on it so it cannot do that. So we're gonna probably do something like how can we fix this? How do they do that? How do they test in this thing? App maybe root handlers and the lesson let's handle it. Test. Yeah this this is what we need this is what we need because with TestServer we can just pass in a request which is exactly the same what we did with our thingy so it's going to be the r.h. right it's the same thing right, we do app.Serve with the request and here we're gonna add test with the request. Right so let's run this benchmark real quick.
Benchmark Results, Conclusions and Signoff
26 allocations, 31 nanoseconds per operation, 31,000 nanoseconds per operation and WeaveBooks my solution 507 nanoseconds per operation, five looks. Hey think about this what you want from it but you can see the clear results right. So that's basically it. If you like this kind of videos, if you feel amazed that I actually wrote this stuff seven years ago and that they basically copied me and copied me in a bad way because they're still slower although they claim to be the fastest one, but hey the underdog is still the undisputed champion. If you like the videos I'm providing to you guys please consider subscribing to my channel, give me a thumbs up. Very important jump into the Discord community for free education and for the people that are willing to become a full-time Go dev go check out fulltimegodev.com and us happy to see you and my program. Thanks for watching and I'm looking forward to seeing you in my live streams or future videos or in the FullTimeGoDev program. Cheers.