Programming
99 Artikel tersedia
06 Handle Not Found Page
Understanding Not Found Handler
Apart from the Router being able to control panic
, it can also have a handler for pages not found or what we often call pages that cannot be accessed. The Not Found handler is a handler that is executed when a client tries to make a request for a page or URL of our website that is not in our Router service. By default, if there is no route it will not be found, but the Router will continue the request to http.NotFound
, but we can also change it to a specific router page by changing
05 Learn About Panic Handler
Understanding Panic Handlers
When we create logic in the handler, we must think of a case where panic occurs, then what should we do? In this Panic handler an error will automatically occur and the website will stop returning responses. In the previous material, we discussed how the Web handles panic by creating special Middleware manually. However, the Router has been provided to handle panic
by using the PanicHandler
attribute.
04 Learn About Serve File
Understanding File Server
In the material
Creating Golang Web. So the Router also supports serving static files using the ServeFiles(Path, FileSystem)
function where in Path
we have to use Catch All Parameters. Meanwhile, in FileSystem
you can manually load it from a folder or use Golang Embed as we discussed in the previous material.
03 Learn About Route Pattern
Use of Named Parameter
The Pattern Router have a have a Pattern to handle http or web application for every endpoint has a parameter pattern in a URL and is often called a Named Parameter. Named Parameter is a pattern for creating parameters using names. Each parameter name begins with a colon :
followed by the parameter name. For example, an example like the one below.
02 Learning About HTTP Router Params
Use of HTTP Router Params
The httprouter.Handle
has an additional parameter, namely Params
, which is used to store parameters sent from the client, but this Params
is not a query for parameters
but is a parameter from the URL. Sometimes we need to create URLs that are not fixed or can change, for example /product/1
, /product/2
and so on.
01 Introduction HTTP Router
Introduction
HttpRouter
is a popular open source library for HTTP Handler in Golang. This HttpRouter
is famous for its speed and is very minimalist or simple because it only has features for routing, it doesn’t have any features other than that. If you want to see more details, you can visit the github here https://github.com/julienschmidt/httprouter
20 How to Understanding Routing Library in Golang
Routing Library
Golang actually provides ServeMux
as a handler that can handle several endpoints or the term is routing
. But most Golang programmers will usually use libraries to do this routing because ServeMux
does not have advanced features such as path variables, auto binding parameters and middleware. So there are many other alternatives that we can use for routing libraries besides ServeMux
.
19 How to Understanding HTTP Middleware in Golang
Middleware
In web creation, we often hear the concept of middleware or filter or interceptor which is a feature that we can add code to before and after a handler is executed.
18 How to Download File in Golang
Download Files
Apart from uploading files, we also need or need a page that can download files or something on our website. The Golang library provides FileServer
and ServeFile
. If we want to force the file to be downloaded without having to be rendered by the browser then we can use the Content-Disposition
header. More details can be seen on this page https://developer.mozilla.org/en-US/docs/Web/Headers/Content-Disposition.
17 How to Upload File in Golang
Upload Files
Apart from receiving data input in the form of forms and query params, we usually also need data input in the form of files from our users. Golang actually already has this feature to handle file upload management. This makes it easier for us if we create a website that can accept file input.
16 How to Understanding HTML Web Redirect in Golang
Redirects
When we create a website, if the user accesses various pages randomly, the page will not be found. So we need to ‘redirect’ a page if the user performs an action on our website page. For example, after logging in, we will redirect to the dashboard page. Redirect
itself is actually standard in HTTP and can be seen on the website page https://developer.mozilla.org/en-US/docs/Web/HTTP/Redirections. We only need to create a 3xx response code and add the destination location header. Luckily in Golang there is a function we can use to make this easier.
15 How to Understanding Web XSS (Cross Site Scripting) in Golang
Introduction to XSS (Cross Site Scripting)
XSS is one of the security issues that usually occurs when creating a website. XSS usually has security holes where people can deliberately enter parameters containing script or Javascript so that the website page can be rendered. The purpose of this XSS is to steal the browser cookies of users who are accessing the website and can cause user accounts to be taken over if we as website owners don’t handle it properly.