[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-en-1-1-all-go-http-middleware-control-request-all--*":3,"academy-blog-translations-yz4435iy3erpoh5":139},{"data":4,"page":127,"perPage":127,"totalItems":127,"totalPages":127},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":133,"keywords":134,"locale":109,"published_at":135,"scheduled_at":13,"school_blog":131,"short_description":136,"slug":137,"status":129,"title":6,"updated":138,"updated_by":13,"views":132},"Ep.21  Go and HTTP Middleware: Mastering Request Control!","sclblg987654321","school_blog_translations","\u003Cp class=\"p1\">\u003Cstrong>Go and HTTP Middleware: Mastering Request Control!\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p2\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>What is Middleware?\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">Middleware is a component that manages requests or responses before they reach our main handler. This can include tasks such as authentication, logging, or header management.\u003Cbr>Example of Middleware Functionality :\u003C\u002Fp>\u003Cul class=\"ul1\">\u003Cli class=\"li3\">Receives a request from the user\u003C\u002Fli>\u003Cli class=\"li3\">Checks or modifies the request (e.g., verifying a token)\u003C\u002Fli>\u003Cli class=\"li3\">Forwards it to the main handler\u003C\u002Fli>\u003Cli class=\"li3\">After the handler processes the request, the middleware may modify the response before sending it back to the user.\u003C\u002Fli>\u003C\u002Ful>\u003Cp class=\"p2\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>Creating Simple Middleware in Go\u003C\u002Fstrong> \u003Cstrong>Example of Middleware Creation\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">In this example, the loggingMiddleware logs every incoming request.\u003Cbr>The middleware operates before the helloHandler.\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">package main\r\n\r\nimport (\r\n    \"fmt\"\r\n    \"net\u002Fhttp\"\r\n)\r\n\r\nfunc loggingMiddleware(next http.Handler) http.Handler {\r\n    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\r\n        fmt.Printf(\"Request: %s %s\\n\", r.Method, r.URL.Path)\r\n        next.ServeHTTP(w, r) \u002F\u002F ส่ง Request ไปยัง Handler ถัดไป\r\n    })\r\n}\r\n\r\nfunc helloHandler(w http.ResponseWriter, r *http.Request) {\r\n    fmt.Fprintln(w, \"Hello, World!\")\r\n}\r\n\r\nfunc main() {\r\n    mux := http.NewServeMux()\r\n    mux.Handle(\"\u002F\", loggingMiddleware(http.HandlerFunc(helloHandler)))\r\n\r\n    fmt.Println(\"Server started at :8080\")\r\n    http.ListenAndServe(\":8080\", mux)\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>Authentication Middleware\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">Middleware can be used to verify a token to confirm whether a user has access rights.\u003Cbr>In this example, the authMiddleware checks for the presence of an Authorization header and whether it has the value of \"valid-token.\"\u003C\u002Fp>\u003Cp class=\"p3\">If not, it will return an \"Unauthorized\" message with a 401 status.\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">func authMiddleware(next http.Handler) http.Handler {\r\n    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\r\n        token := r.Header.Get(\"Authorization\")\r\n        if token != \"valid-token\" {\r\n            http.Error(w, \"Unauthorized\", http.StatusUnauthorized)\r\n            return\r\n        }\r\n        next.ServeHTTP(w, r) \u002F\u002F ส่งต่อไปยัง Handler ถ้าสิทธิ์ถูกต้อง\r\n    })\r\n}\r\n\r\nfunc main() {\r\n    mux := http.NewServeMux()\r\n    mux.Handle(\"\u002F\", authMiddleware(http.HandlerFunc(helloHandler)))\r\n\r\n    fmt.Println(\"Server started at :8080\")\r\n    http.ListenAndServe(\":8080\", mux)\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>Chaining Multiple Middleware\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">We can chain multiple middleware together to enhance request and response control.\u003Cbr>Example of Multiple Middleware :\u003C\u002Fp>\u003Cp class=\"p3\">In this example, we use chainMiddleware to connect several middleware components, arranging their execution from top to bottom.\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">func chainMiddleware(h http.Handler, middlewares ...func(http.Handler) http.Handler) http.Handler {\r\n    for _, middleware := range middlewares {\r\n        h = middleware(h)\r\n    }\r\n    return h\r\n}\r\n\r\nfunc main() {\r\n    mux := http.NewServeMux()\r\n\r\n    mux.Handle(\"\u002F\", chainMiddleware(\r\n        http.HandlerFunc(helloHandler),\r\n        loggingMiddleware,\r\n        authMiddleware,\r\n    ))\r\n\r\n    fmt.Println(\"Server started at :8080\")\r\n    http.ListenAndServe(\":8080\", mux)\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>Common Use Cases for Middleware\u003C\u002Fstrong>\u003C\u002Fp>\u003Cul class=\"ul1\">\u003Cli class=\"li3\">Authentication\u003C\u002Fli>\u003Cli class=\"li3\">Logging\u003C\u002Fli>\u003Cli class=\"li3\">Managing CORS (Cross-Origin Resource Sharing)\u003C\u002Fli>\u003Cli class=\"li3\">Data Compression\u003C\u002Fli>\u003Cli class=\"li3\">Error Handling\u003C\u002Fli>\u003C\u002Ful>\u003Cp class=\"p2\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>In Summary\u003C\u002Fstrong>\u003C\u002Fp>\u003Cul class=\"ul1\">\u003Cli class=\"li3\">Middleware helps manage requests\u002Fresponses before and after they reach the main handler.\u003C\u002Fli>\u003Cli class=\"li3\">Multiple middleware can be used together (chaining) to handle various steps.\u003C\u002Fli>\u003Cli class=\"li3\">Middleware is ideal for repetitive tasks such as authentication and logging.\u003C\u002Fli>\u003C\u002Ful>","12_11zon_15r6fjugo7.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002F8p06pufto50mx9e\u002F12_11zon_15r6fjugo7.webp","2026-03-04 08:34:29.240Z","",{"keywords":15,"locale":103,"school_blog":113},[16,23,28,33,38,43,48,53,58,63,68,73,78,83,88,93,98],{"collectionId":17,"collectionName":18,"created":19,"created_by":13,"id":20,"name":21,"updated":22,"updated_by":13},"sclkey987654321","school_keywords","2026-03-04 08:20:11.547Z","ey3puyme01a9bsw","Go","2026-04-10 16:07:25.893Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:20:14.253Z","ah6lvy4x8qe08l5","Golang","2026-04-10 16:07:26.172Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":32,"updated_by":13},"2026-03-04 08:33:59.315Z","btmgtfwmgpke1aa","Go language","2026-04-10 16:08:04.625Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"updated_by":13},"2026-03-04 08:34:24.161Z","ru06o5mbma6nukl","HTTP","2026-04-10 16:08:13.801Z",{"collectionId":17,"collectionName":18,"created":39,"created_by":13,"id":40,"name":41,"updated":42,"updated_by":13},"2026-03-04 08:34:25.436Z","bctyeonwhcrzvq3","Middleware","2026-04-10 16:08:14.305Z",{"collectionId":17,"collectionName":18,"created":44,"created_by":13,"id":45,"name":46,"updated":47,"updated_by":13},"2026-03-04 08:34:27.953Z","rym9g5mcvdc7vpd","request management","2026-04-10 16:08:15.066Z",{"collectionId":17,"collectionName":18,"created":49,"created_by":13,"id":50,"name":51,"updated":52,"updated_by":13},"2026-03-04 08:34:18.714Z","7cb29z95923lmhe","authentication","2026-04-10 16:08:11.822Z",{"collectionId":17,"collectionName":18,"created":54,"created_by":13,"id":55,"name":56,"updated":57,"updated_by":13},"2026-03-04 08:33:55.302Z","0mue4zt83jcdtq9","Logging","2026-04-10 16:08:03.396Z",{"collectionId":17,"collectionName":18,"created":59,"created_by":13,"id":60,"name":61,"updated":62,"updated_by":13},"2026-03-04 08:19:55.412Z","hz7yzm54i2o6cl7","web development","2026-04-10 16:07:24.402Z",{"collectionId":17,"collectionName":18,"created":64,"created_by":13,"id":65,"name":66,"updated":67,"updated_by":13},"2026-03-04 08:32:51.346Z","tmzmy6jyz1n35rr","Go Programming","2026-04-10 16:08:01.434Z",{"collectionId":17,"collectionName":18,"created":69,"created_by":13,"id":70,"name":71,"updated":72,"updated_by":13},"2026-03-04 08:32:15.843Z","m0x7wo77i8iycf1","Programming Education","2026-04-10 16:07:51.675Z",{"collectionId":17,"collectionName":18,"created":74,"created_by":13,"id":75,"name":76,"updated":77,"updated_by":13},"2026-03-04 08:31:22.575Z","lfjse4xivbgg5wu","Practice programming","2026-04-10 16:07:39.541Z",{"collectionId":17,"collectionName":18,"created":79,"created_by":13,"id":80,"name":81,"updated":82,"updated_by":13},"2026-03-04 08:31:49.362Z","2m9vv13etpn6zkx","programming language","2026-04-10 16:07:45.606Z",{"collectionId":17,"collectionName":18,"created":84,"created_by":13,"id":85,"name":86,"updated":87,"updated_by":13},"2026-03-04 08:32:26.073Z","vnvj1oaxje9m1q8","programming for beginners","2026-04-10 16:07:54.133Z",{"collectionId":17,"collectionName":18,"created":89,"created_by":13,"id":90,"name":91,"updated":92,"updated_by":13},"2026-03-04 08:20:33.316Z","ln1ntwattzmxo0o","programming","2026-04-10 16:07:27.299Z",{"collectionId":17,"collectionName":18,"created":94,"created_by":13,"id":95,"name":96,"updated":97,"updated_by":13},"2026-03-04 08:31:54.955Z","264sfjffyhspetq","programmers","2026-04-10 16:07:47.221Z",{"collectionId":17,"collectionName":18,"created":99,"created_by":13,"id":100,"name":101,"updated":102,"updated_by":13},"2026-03-04 08:26:59.195Z","gab60xd583s3qaw","Superdev School","2026-04-10 16:07:37.087Z",{"code":104,"collectionId":105,"collectionName":106,"created":107,"flag":108,"id":109,"is_default":110,"label":111,"updated":112},"en","pbc_1989393366","locales","2026-01-22 11:00:02.726Z","twemoji:flag-united-states","qv9c1llfov2d88z",false,"English","2026-04-10 15:42:46.825Z",{"category":114,"collectionId":115,"collectionName":116,"expand":117,"id":131,"views":132},"wqxt7ag2gn7xcmk","pbc_2105096300","school_blogs",{"category":118},{"blogIds":119,"collectionId":120,"collectionName":121,"created":122,"created_by":13,"id":114,"image":123,"image_alt":13,"image_path":124,"label":125,"name":126,"priority":127,"publish_at":128,"scheduled_at":13,"status":129,"updated":130,"updated_by":13},[],"sclcatblg987654321","school_category_blogs","2026-03-04 08:33:53.210Z","59ty92ns80w_15oc1implw.png","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclcatblg987654321\u002Fwqxt7ag2gn7xcmk\u002F59ty92ns80w_15oc1implw.png",{"en":126,"th":126},"Golang The Series",1,"2026-03-16 04:39:38.440Z","published","2026-04-25 02:32:15.470Z","yz4435iy3erpoh5",214,"8p06pufto50mx9e",[20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100],"2025-01-27 04:38:23.170Z","Learn about HTTP Middleware in Go to efficiently manage requests and responses, including creating middleware for logging and authentication.","go-http-middleware-control-request","2026-04-25 02:47:32.566Z",{"en":137}]