introduction-to-golang-http-router-made-with-net-http

It is a repository to introduce how to implement your own HTTP router with golang. (by bmf-san)

Introduction-to-golang-http-router-made-with-net-http Alternatives

Similar projects and alternatives to introduction-to-golang-http-router-made-with-net-http based on common topics and language

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a better introduction-to-golang-http-router-made-with-net-http alternative or higher similarity.

introduction-to-golang-http-router-made-with-net-http reviews and mentions

Posts with mentions or reviews of introduction-to-golang-http-router-made-with-net-http. We have used some of these posts to build our list of alternatives and similar projects. The last one was on 2021-12-14.
  • Summary of "Introduction to Golang HTTP router made with net/http"
    4 projects | dev.to | 14 Dec 2021
    From the above code, if you read the net / http code with the following points in mind, you can see what to implement. - Where and how the multiplexer registers handlers, and what are the specifications for matching URLs and patterns? - How to create a handler (whether there is a variation of handler creation, what interface should be satisfied) Details [bmf-tech.com-Chapter 3 HTTP Server Code Reading] (https://bmf-tech.com/posts/net%EF%BC%8Fhttp%E3%81%A7%E3%81%A4 % E3% 81% 8F% E3% 82% 8BHTTP% E3% 83% AB% E3% 83% BC% E3% 82% BF% E3% 83% BC% E8% 87% AA% E4% BD% 9C% E5 % 85% A5% E9% 96% 80 #% E7% AC% AC3% E7% AB% A0HTTP% E3% 82% B5% E3% 83% BC% E3% 83% 90% E3% 83% BC% E3% 81% AE% E3% 82% B3% E3% 83% BC% E3% 83% 89% E3% 83% AA% E3% 83% BC% E3% 83% 87% E3% 82% A3% E3% 83% B3% E3% 82% B0), so please refer to it if you have time. # Implementation As I introduced at the beginning, the HTTP router implemented this time is in the following repository. [bmf-san/introduction-to-golang-http-router-made-with-net-http] (https://github.com/bmf-san/introduction-to-golang-http-router-made-with- net-http /) This time we will implement a simple HTTP router that "supports method-based routing". * Since it is a summary version, detailed explanation of the code is omitted. Method-based routing means that you can register URLs by HTTP method. In the function of Go standard package only, if you want to route by method, you need to implement conditional branching in the handler. `` `go // ex. func indexHandler (w http.ResponseWriter, r * http.Request) { switch r.Method { case http.MethodGet: // do something ... case http.MethodPost: // do something ... ... default: default: `` ``` This time, we will implement it only with the goal of eliminating this trouble. In order to achieve method-based routing, we will adopt a tri-tree-based data structure. Since it is a simple function, a simpler data structure (such as making it possible to use the standard multiplexer function for each method) is fine, but we will adopt it in anticipation of various implementations later. (To be honest, I just simplified the implementation of [bmf-san / goblin] (https://github.com/bmf-san/goblin).) The general things to do in implementing an HTTP router are as follows. - Process to add URL and handler mapping to data structure - https://github.com/bmf-san/introduction-to-golang-http-router-made-with-net-http/blob/main/trie.go#L51 - Process to search for matching data from the mapped data structure (≈ tri-tree-based data structure) - https://github.com/bmf-san/introduction-to-golang-http-router-made-with-net-http/blob/main/trie.go#L93 --Implementation of DSL for registering mapping - Around here https://github.com/bmf-san/introduction-to-golang-http-router-made-with-net-http/blob/main/router.go#L47 - Implementation of function as a multiplexer (≈ implementation of ServeHTTP) - https://github.com/bmf-san/introduction-to-golang-http-router-made-with-net-http/blob/main/router.go#L54 The first thing you want to do to implement is to implement the data structure. ! [tree_for_implementation] (https://user-images.githubusercontent.com/13291041/138551924-ca2e28f5-a0c1-4ae4-ac9f-76d6ee1c46d9.png) It is easier to work if you use the debug function of the editor (using delve in vscode) or write test code to check if it is implemented well. For implementation details, see [bmf-tech.com-Chapter 4 HTTP Router Implementation] (https://bmf-tech.com/posts/net%EF%BC%8Fhttp%E3%81%A7%E3%81] % A4% E3% 81% 8F% E3% 82% 8BHTTP% E3% 83% AB% E3% 83% BC% E3% 82% BF% E3% 83% BC% E8% 87% AA% E4% BD% 9C % E5% 85% A5% E9% 96% 80 #% E7% AC% AC4% E7% AB% A0HTTP% E3% 83% AB% E3% 83% BC% E3% 82% BF% E3% 83% BC% See E3% 81% AE% E5% AE% 9F% E8% A3% 85). # Summary HTTP routers are almost indispensable for creating web applications, and aren't they very much taken care of? My motivation was that I wanted to make something that I used as a matter of course and use it myself. There are already a lot of great HTTP routers out there, so I think one of the real thrills is that it's worth researching. This time I implemented it in Go, but if I understand the mechanism, I think that it can be implemented in languages ​​other than Go. In Go, the interface is provided in a form that makes it easy to extend the function of the standard package, so I felt that it was relatively easy to make by myself. If you are interested in this article, I would be grateful if you could take a look at [bmf-san/goblin] (https://github.com/bmf-san/goblin). Feel free to send an Issue or Pull Request. :D

Stats

Basic introduction-to-golang-http-router-made-with-net-http repo stats
1
5
0.0
10 months ago

Sponsored
SaaSHub - Software Alternatives and Reviews
SaaSHub helps you find the best software and product alternatives
www.saashub.com