Routing

Introduction

Goravel routing module can operated by facades.Route.

Default Routing File

All routing files are defined in the /routes directory. The framework defaults to a sample route /routes/web.go, in which the func Web() method is registered in the app/providers/route_service_provider.go file to achieve routing binding.

You can add routing files under the routes directory to perform more fine-grained management, then register them in the app/providers/route_service_provider.go file.

Start HTTP Server

Start the HTTP server in main.go in the root directory.

package main

import (
  "github.com/goravel/framework/facades"

  "goravel/bootstrap"
)

func main() {
  //This bootstraps the framework and gets it ready for use.
  bootstrap.Boot()

  //Start http server by facades.Route.
  go func() {
    if err := facades.Route.Run(facades.Config.GetString("app.host")); err != nil {
      facades.Log.Errorf("Route run error: %v", err)
    }
  }()

  select {}
}

Routing Methods

MethodsAction
RunStart HTTP Server
GroupGroup Routing
PrefixRouting Prefix
ServeHTTPTesting Routing
GetBasic Routing
PostBasic Routing
PutBasic Routing
DeleteBasic Routing
PatchBasic Routing
OptionsBasic Routing
AnyBasic Routing
StaticFile Routing
StaticFileFile Routing
StaticFSFile Routing
MiddlewareMiddleware

Basic Routing

facades.Route.Get("/", userController.Show)
facades.Route.Post("/", userController.Show)
facades.Route.Put("/", userController.Show)
facades.Route.Delete("/", userController.Show)
facades.Route.Patch("/", userController.Show)
facades.Route.Options("/", userController.Show)
facades.Route.Any("/", userController.Show)

Group Routing

facades.Route.Group(func(route route.Route) {
  route.Get("group/{id}", func(ctx http.Context) {
    ctx.Response().Success().String(ctx.Request().Query("id", "1"))
  })
})

Routing Prefix

facades.Route.Prefix("users").Get("/", userController.Show)

File Routing

facades.Route.Static("static", "./public")
facades.Route.StaticFile("static-file", "./public/logo.png")
facades.Route.StaticFS("static-fs", nethttp.Dir("./public"))

Routing Parameters

facades.Route.Get("/input/{id}", func(ctx http.Context) {
  ctx.Response().Success().Json(http.Json{
    "id": ctx.Request().Input("id"),
  })
})

Detail Request

Middleware

import "github.com/goravel/framework/http/middleware"

facades.Route.Middleware(middleware.Cors()).Get("users", userController.Show)

Detail Middleware

Cross-Origin Resource Sharing (CORS)

Goravel has CORS enabled by default, the configuration can be modified in config/cors.go, the funciton is registered in app/http/kernel.go as global middleware.

For more information on CORS and CORS headers, please consult the MDN web documentation on CORSopen in new window.

Testing Routing

func TestHttp(t *testing.T) {
  w := httptest.NewRecorder()
  req, err := http.NewRequest("GET", "/users", nil)
  assert.Nil(t, err)
  facades.Route.ServeHTTP(w, req)
  assert.Equal(t, 200, w.Code)
  assert.Equal(t, "1", w.Body.String())
}
Last Updated:
Contributors: Bowens, Bowens