// This package implements three routes for a Go web application using Gin. // The Index route provides a simple greeting, the Admin route requires user authentication and renders a template with a user-provided name, and the Flask route makes a request to a Flask API and displays the response.

package route

import ( 'github.com/flosch/pongo2/v6' // Package for using pongo2 templates 'github.com/gin-gonic/gin' // Web framework 'github.com/gorilla/sessions' // Package for using session cookies 'html' // Package for HTML encoding 'io' // Package for IO utilities 'net/http' // Package for HTTP utilities 'os' // Package for OS utilities )

// Create a new cookie store with a session key var store = sessions.NewCookieStore([]byte(os.Getenv('SESSION_KEY')))

// Index route handler func Index(c *gin.Context) { // Get the session cookie session, err := store.Get(c.Request, 'session-name') if err != nil { http.Error(c.Writer, err.Error(), http.StatusInternalServerError) return } // If the name is not set in the session, set it to 'guest' and save the session cookie if session.Values['name'] == nil { session.Values['name'] = 'guest' err = session.Save(c.Request, c.Writer) if err != nil { http.Error(c.Writer, err.Error(), http.StatusInternalServerError) return } } // Return a simple greeting c.String(200, 'Hello, guest') }

// Admin route handler func Admin(c *gin.Context) { // Get the session cookie session, err := store.Get(c.Request, 'session-name') if err != nil { http.Error(c.Writer, err.Error(), http.StatusInternalServerError) return } // If the user is not logged in as an admin, return an error message if session.Values['name'] != 'admin' { http.Error(c.Writer, 'N0', http.StatusInternalServerError) return } // Get the user-supplied name from the query string and encode it for HTML name := c.DefaultQuery('name', 'ssti') xssWaf := html.EscapeString(name) // Create a pongo2 template with the user's name tpl, err := pongo2.FromString('Hello ' + xssWaf + '!') if err != nil { panic(err) } // Render the template with the context out, err := tpl.Execute(pongo2.Context{'c': c}) if err != nil { http.Error(c.Writer, err.Error(), http.StatusInternalServerError) return } // Return the rendered template c.String(200, out) }

// Flask route handler func Flask(c *gin.Context) { // Get the session cookie session, err := store.Get(c.Request, 'session-name') if err != nil { http.Error(c.Writer, err.Error(), http.StatusInternalServerError) return } // If the name is not set in the session, return an error message if session.Values['name'] == nil { if err != nil { http.Error(c.Writer, 'N0', http.StatusInternalServerError) return } } // Get the user-supplied name from the query string name := c.DefaultQuery('name', 'guest') // Make a request to a Flask API with the user's name resp, err := http.Get('http://127.0.0.1:5000/' + name) if err != nil { return } defer resp.Body.Close() // Read the response body body, _ := io.ReadAll(resp.Body) // Return the response body as a string c.String(200, string(body)) }

Gin Go Web Routes: Index, Admin, and Flask Integration

原文地址: https://www.cveoy.top/t/topic/oiQt 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录