Last updated
Last updated
Slide Routing examples
app := slide.InitServer(&config)
app.Get("/", func(ctx *slide.Ctx) error {
return ctx.Send(http.StatusOK, "Hello, World")
})
app.Post("/", func(ctx *slide.Ctx) error {
return ctx.Send(http.StatusOK, "Hello, World")
})
app.Put("/", func(ctx *slide.Ctx) error {
return ctx.Send(http.StatusOK, "Hello, World")
})
app.Delete("/", func(ctx *slide.Ctx) error {
return ctx.Send(http.StatusOK, "Hello, World")
})
auth := app.Group("/auth")
// this becomes /auth/login
auth.Get("/login", func(ctx *slide.Ctx) error {
return ctx.Send(http.StatusOK, "Hello, World")
})
[a-zA-Z0-9_-]*
app.Get("/:name", func(ctx *slide.Ctx) error {
name := ctx.GetParam("name")
return ctx.Send(http.StatusOK, fmt.Sprintf("hello, %s", name))
})
// returns value of a single query Param
//
// route path /hello?key=test&value=bbp
//
// keyValue = GetQueryParam(key)
//
// keyValue = test
app.Get("/hello", func(ctx *slide.Ctx) error {
params := ctx.GetQueryParams()
return ctx.JSON(http.StatusOK, params)
})
// returns map of query Params
//
// route path /hello?key=test&value=bbp
//
// returns {key : test, value : bbp}
app.Get("/hello/single", func(ctx *slide.Ctx) error {
params := ctx.GetQueryParam("key")
return ctx.Send(http.StatusOK, fmt.Sprintf("key is %s", params))
})
// redirect to new url
app.Get("/redirect", func(ctx *slide.Ctx) error {
return ctx.Redirect(http.StatusTemporaryRedirect, "http://localhost:3000/static")
})
app.HandleNotFound(func(ctx *slide.Ctx) error {
return ctx.JSON(http.StatusNotFound, "check url")
})
app.HandleErrors(func(ctx *slide.Ctx, err error) error {
return ctx.Send(http.StatusInternalServerError, err.Error())
})