Gin framework-A summary of problems encountered in running the terminal and their solutions (1)

1. panic: html/template: pattern matches no files: `template/**/**/*`

Terminal full text code:

10:3:12 app | [GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env: export GIN_MODE=release
 - using code: gin.SetMode(gin.ReleaseMode)

10:3:13 app | panic: html/template: pattern matches no files: `template/**/**/*`

goroutine 1 [running]:
html/template.Must(...)
        /usr/local/go/src/html/template/template.go:368
github.com/gin-gonic/gin.(*Engine).LoadHTMLGlob(0xc0005001a0, {0x15bb7b4, 0x10})
        /Users/mac/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:255 + 0x306
main.main()

This error occurs because no file was found in the given path.

In this particular error message, the path is template/**/**/*. This path appears to be a wildcard pattern, intended to match one or more files.

However, according to the error message, this wildcard pattern did not match any files. There may be several reasons causing this error:

  1. Path error: Check that the path is correct and make sure it points to the correct directory and contains the files you want to match.

  2. Ignore files: Some files may be configured to be ignored and therefore not matched. You can check your project’s .gitignore or .hgignore file to confirm whether there is any relevant ignore configuration. If so configured, files specified in the match pattern will not be matched.

  3. File does not exist: The file specified in the path may not exist. You can verify this by checking if the given path exists on the command line or in a file browser.

Please double-check the path to make sure the file exists so this error can be resolved.

The first (1) problem above is due to carelessness in template rendering. So is the template path name consistent with what you wrote? Here are templates, and in mian.go it is like this

 //Load the template and place it in front of the configuration route
router.LoadHTMLGlob("template/**/**/*")

Correct way to write:
//Load the template and place it in front of the configuration route
router.LoadHTMLGlob("templates/**/**/*")


There is an s letter missing here. At the same time, when typing, you must confirm the name of the template folder you agreed on. 

2. [GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.

This warning message is generated by the GIN framework and reminds you not to trust all proxies when using them.

By default, the GIN framework will trust all proxies, which means it will treat the header information in the proxy request as trustworthy. However, trusting all proxies may lead to security issues because the header information of proxy requests can be forged.

To improve security, the GIN framework recommends that you set an appropriate value to limit trusted proxies. You can specify trusted proxies by setting the TrustedProxies field in the gin.Engine structure.

For specific usage methods, please refer to the instructions provided in the Gin documentation.

Overall, for security reasons, you should carefully consider which proxies you trust and only trust those that you are sure are trustworthy. Follow the GIN framework’s recommendations to set appropriate values to increase the security of your application.

3. controllers/admin/login.go:20:19: undefined: models

func makeCaptcha() (string, string, error) {
var driver base64Captcha.Driver

driverString := base64Captcha.DriverString{
Height: 40,
Width: 100,
NoiseCount: 0,
ShowLineOptions: 2 | 4,
Length: 2,
Source: "1234567890qwertyuioplkjhgfdsazxcvbnm",
BgColor: & amp;color.RGBA{
R: 3,
G: 102,
B: 214,
A: 125,
},
Fonts: []string{"wqy-microhei.ttc"},
}

driver = driverString.ConvertFonts()

c := base64Captcha.NewCaptcha(driver, store)
id, b64s, err := c.Generate()

return id, b64s, err

}

Is there any error in the above code? If you look at it alone, there is no problem. Then why is the error “models undefined” reported in login.go?

In fact, the problem is that we put the method of generating the verification code base64Captcha separately in a file named after the verification code (captch.go) under the models package. It implements the basic methods of set, get, and verfiy.

This error is caused by using undefined models in Go code.

“models undefeated” This error message is not a standard Go error message, so it may be a custom error message, or a specific third-party library or framework is used.

To solve this problem, you need to check whether the model is correctly defined in the code. Please confirm the following aspects:

  1. Whether the model is imported correctly: Check whether the statement of the imported module is correct and ensure that the relevant files containing the model definition are correctly imported.

  2. Whether the model is correctly defined: Ensure that the structure or type defined by the model exists and that there are no omissions or wrong definitions. Check that the model’s naming and fields are correct.

  3. Model accessibility: Ensure that the model definition is accessible. In Go, if the first letter of an identifier (such as a struct or type) is a lowercase letter, then it is private and can only be used within the package in which it is defined. If your model is private, you may need to define it as public so that it can be used in other packages.

Combine solutions and draw conclusions. Our defined model package type should be public, so the first letter of the defined identifier should be uppercase, complying with the Go language naming rules.

4. Failed to load resource: the server responded with a status of 404 (Not Found) In the transformation project, if the HTML page needs to introduce static resources, return to the page HTML to find the css introduction link link to view the address Is there a problem?

After comparing the code, we can see that we have changed the code, but there is still no rendering problem on the page! ! !

<link rel="stylesheet" href="/static/admin/css/login.css">
    <script type="text/javascript" src="/static/admin/bootstrap/js/jquery-1.10.1.js"></script>
    <script type="text/javascript" src="/static/admin/js/login.js"></script>

login.css:1 Failed to load resource: the server responded with a status of 404 (Not Found)

… Wait for f12, open the debugging background, check the console, you can see that it is

So, how to solve this problem?

Question 1: The static resource path is imported correctly, and there is no obvious error message on the program running terminal!

Analysis: First of all, there is no problem with the static resource introduction address path. Then the current login page is also displayed, but there is no css. The console prints out the page and reports an error: css introduction is missing.

Therefore, we check whether there is code for loading static resources in the main.go file after rendering the template. That is, in the main entry file, add the code that introduces the static resource file

    //Configure the static web directory. The first parameter represents the route, and the second parameter represents the mapped directory.

    r.Static("/static", "./static")

Code snippet, r.Static("/static", "./static") is used to set up static file service for routing.

The function of this code is to map the /static path to the ./static directory and provide static file services for files under this path. This means that when the client requests a static file under the path /static, the server will return the corresponding file.

Specifically, the first parameter of the r.Static() function is the routing path, which defines the URL path for clients to access static files. The second parameter is the file system path, which represents the actual file system path where the static files are stored.

In this example, when the client requests /static/css/style.css, the server will return the contents of the ./static/css/style.css file. Note that the actual path is determined based on your project directory structure and the actual path in the file system.

Make sure your static files directory exists and has the correct path. Also, make sure your routing is configured correctly so that the paths to static files are correctly mapped.

If the problem persists, you can provide more contextual information to analyze and resolve the issue more specifically.

Hope this helps! If you have any other questions, please feel free to ask.

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Go Skill Tree Home Page Overview 4414 people are learning the system