In the previous chapter we talked about how to start a dotweb program, and this article will show you how to register a route.
Router is the structure used by Dotweb to manage routing, and it provides some information about the routing operations function.
App: = dotweb. New () router:= app. Httpserver.router ()
In the previous article we talked about Dotweb. The use of New (), Httpserver is responsible for processing requests, management routing, session, middleware and other functions. I'll introduce the server object later. Here we get the routing object also obtained through Httpserver. In general, we declare a function that registers the route in the function, avoiding excessive code in the main function. Examples are as follows:
Func Main () { app:= dotweb. New () initrouter (app. Httpserver)
app. StartServer (8080)
*
HS. Router (). Get ("/index"
*
CTX. Witrestring ("Hello dotweb!"
}
In the example above we declare a function named Initrouter, which will be executed in the main function, we register a different route in the Initrouter function, and the Index function is our Userhandler it is used to process the request. The above code will return "Hello dotweb!" String.
Hs. Router (). GET ("/index", Index)//httpget request HS. Router (). Post ("/post", Post)//HttpPost request HS. Router (). Head ("/head", head)//httphead request HS. Router (). Options ("/pptions", Options)//httpoptions request HS. Router (). Put ("/put", put)//httpput request HS. Router (). Delete ("/delete", delete)//Httpdelete Request
There are eight types of request methods for HTTP definitions, in addition to the above six kinds of trace and connect two. Most scenarios we use the above six request methods to meet the project requirements, and the RESTful Web API defines the request method does not include trace and connect. Although Dotweb does not provide a teace and connect request method, it offers four additional options.
Hs. Router (). Any ("/any", any)
The any function means that you can request the address with any request method, and if it is a get function, the address can be accessed only by the Get method's request, but if it is an any function you can use a GET request or a POST request, a put request, and so on.
Hs. Router (). HiJack ("/hijack", HiJack)
Hijack uses a Get method request, but it is not the same as a normal GET request. It is generally used for long connections, and general requests do not need to use it. If you want to learn more about hijack use, refer to Dotweb's sister project-longweb. It provides long connection services for Web applications, GitHub address: Https://github.com/devfeel/longweb. The project is measured by millions of long connections, if you need to refer to or use the project. It's free and open-source.
Hs. Router (). Serverfile ("/img/xxx.jpg", "/webroot/img")// The first parameter represents the routing address, and the second parameter indicates the file address on the server
Serverfile is used to access static files.
Hs. Router (). Registerroute ("WebSocket", "/websocket", WebSocket)
Registerroute can register all of the above types of routes, the method type is distinguished by the first parameter, and WebSocket needs to register the route in this way.
The above is the Dotweb provides the function of registering the route, more route registration way you can read the source to know. Here I explain the two parameters of the route registration function, the first being a string-type parameter, which represents the routing address. Suppose the native address is 192.168.0.1 and the port number is 8080. If the parameter is "/" then it represents the root address, you enter 192. The 168.0.1:8080 can be accessed. If it is "/index" its address is 192.168.0.1:8080/index. Dotweb also has a routing configuration called parameter routing, "/news/:id" here ": ID" represents a parameter, if you understand the restful style you will understand the meaning of this configuration, of course, the parameters here allow a plurality of "/news/:uid/:id". The second parameter is a formal parameter of *dotweb. HttpContext function, which is responsible for processing user requests, parameters represent the context of the current request, you can access the session, cache, request and so on objects.
Note that Dotweb has a setting, if you start it then all non-head routes will be registered with a head method route, which is useful in scenarios where validation requests are available, and the following is the Enable method with the default value of FALSE.
App. Httpserver.setenabledautohead (true)
Next I will introduce Userhandler, if you have javaweb or ASP. NET MVC development experience so userhandler very good understanding, it is the controller of the method. You can do simple crud here as well as complex business processing, and even you can use the three layer design here. You can write userhandler to a go file, or to a different go file according to your business, in fact I suggest you separate. I will introduce userhandler in the following article, in fact you have mastered the dotweb. The use of HttpContext can be achieved userhandler.
Of course in Dotweb also support configuration file load routing, I will be in the next article specifically to write an article about how to use the configuration file, where I will put dotweb configuration related to the introduction. With the attention of dotweb more and more people involved in dotweb this project, we work together to improve the enhancement of dotweb, here I hope you can also participate in dotweb, because we need your strength. Project Address: Https://github.com/devfeel/dotweb, you can find our official QQ group in the GitHub project address, we welcome you to join the Dotweb family.
A miniature web framework for Dotweb--go Language (iii) route registration