Group handler

Report a typo

Create web server that supports the /user group route with two child routes (/about and /update). Each request to the /user group must be authorized using the route parameter :auth, defined at the third level of the route (e.g., /user/about/secretToken, where secretToken is an :auth route parameter).

If the parameter is equal to the string "secret_token", the user is authorized. Upon successful authorization, the route name should be returned in the response (e.g., if the request is made to http://127.0.0.1:8082/user/about/secret_token, the server should respond with the string "about"). If the authorization fails, respond with the string "not found".

Once the routes and endpoints are defined, start a web server that listens on the address 127.0.0.1:8082.

Write a program in Go
package main

import (
"fmt"
"io"
"net/http"
"time"

"github.com/gin-gonic/gin"
)

type User struct {
Token string `uri:"token"`
IsAuth bool
}

func main() {
// DO NOT delete the two lines below! They disable Gin's debug mode to check your solution
gin.SetMode(gin.ReleaseMode)
gin.DefaultWriter = io.Discard

router := gin.Default()

var user User

userGroup := router.?("/user", func(context *gin.Context) {
err := context.ShouldBindUri(&user)
if err != nil {
context.String(http.StatusBadRequest, "error")
return
}

user.IsAuth = ? == "secret_token"

if !user.IsAuth {
context.String(http.StatusNotFound, "not found")
___

Create a free account to access the full topic