This is a creation in Article, where the information may have evolved or changed.
Routing planning
Routing is the entry for all requests, where we configure all requests, go to the corresponding name for each request, and request different controllers according to the name to obtain the final data
Here, we have made a simple code example as follows:
Package Routerimport (//Introducing Controller "Github.com/gohouse/kuaixinwen/controller"//Introducing Dotweb "Github.com/devfeel/dot Web "//introduced Dotweb middleware" github.com/devfeel/middleware/cors ") func Run (Route *dotweb. Httpserver) {//sets the Cors option middleware and uses the default cross-domain configuration option: cors. Newconfig (). Usedefault ()//home Test/Route.get ("/", Func (CTX Dotweb. Context) Error {return CTX. WriteString ("Quick News homepage!") })//JSON return test/json route.get ("/json", func (CTX Dotweb. Context) Error {return CTX. Writejson ("Waves in a wave ...")}). Use (cors. Middleware (option))//Front desk display list/getnewslist route.get ("/getnewslist", controller. Getnewslist). Use (cors. Middleware (option))//Background management admin: = Route.group ("/admin"). Use (cors. Middleware (option))//News list/admin/newsdel Admin. GET ("/getnewslist", controller. getnewslist)//Get a single news message/admin/getnewsbyid Admin. GET ("/getnewsbyid", controller. Getnewsbyid)//news additions or modifications, according to whether the ID to determine, passed the ID on the modification, otherwise add/admin/newsaddoredit admin. POST ("/newsaddoredit", CONtroller. Newsaddoredit)//News Delete/admin/newsdel Admin. POST ("/newsdel", controller. Newsdel)}
Description
Here we use the relevant components of the Dotweb, with HttpServer , Context and cors middleware
They are used for routing, request parameters and other related and middleware (such as cross-domain), etc.
Route.GETRepresents the request bit GET request
Route.POSTRepresents the request bit POST request
Route.GroupThe request is categorized, in the example, all of the background management requests are categorized admin below
Route.Group.UseUsing middleware
optionMiddleware initialization
cors.Middleware(option)Load middleware option
controller.xxxxxThe corresponding method of loading controller
Here the routing section is completed, and the controller part is loaded, controller see the next section
The project Source: Https://github.com/gohouse/kuaixinwen