Advertisement
Guest User

Simple Go-postgresql page

a guest
May 14th, 2025
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.91 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "database/sql"
  5.     "fmt"
  6.     "net/http"
  7.  
  8.     "github.com/labstack/echo/v4"
  9.     _ "github.com/lib/pq"
  10. )
  11.  
  12. // User represents the user data structure
  13. type User struct {
  14.     ID     int
  15.     Name   string
  16.     City   string
  17.     Salary float64
  18. }
  19.  
  20. // getUserHandler handles GET requests to fetch user data by ID
  21. func getUserHandler(db *sql.DB) echo.HandlerFunc {
  22.     return func(c echo.Context) error {
  23.         // Get user ID from URL parameter
  24.         id := c.Param("id")
  25.  
  26.         // Query the database
  27.         var user User
  28.         err := db.QueryRow("SELECT id, name, city, salary FROM users WHERE id = $1", id).
  29.             Scan(&user.ID, &user.Name, &user.City, &user.Salary)
  30.         if err == sql.ErrNoRows {
  31.             return c.HTML(http.StatusNotFound, "<h1>User not found</h1>")
  32.         }
  33.         if err != nil {
  34.             return c.HTML(http.StatusInternalServerError, "<h1>Error fetching user</h1>")
  35.         }
  36.  
  37.         // Render basic HTML response
  38.         html := fmt.Sprintf(`
  39.             <!DOCTYPE html>
  40.             <html>
  41.             <head>
  42.                 <title>User Details</title>
  43.                 <style>
  44.                     body { font-family: Arial, sans-serif; margin: 40px; }
  45.                     .container { max-width: 600px; margin: 0 auto; }
  46.                     h1 { color: #333; }
  47.                     ul { list-style: none; padding: 0; }
  48.                     li { margin: 10px 0; }
  49.                 </style>
  50.             </head>
  51.             <body>
  52.                 <div class="container">
  53.                     <h1>User Details</h1>
  54.                     <ul>
  55.                         <li><strong>ID:</strong> %d</li>
  56.                         <li><strong>Name:</strong> %s</li>
  57.                         <li><strong>City:</strong> %s</li>
  58.                         <li><strong>Salary:</strong> $%.2f</li>
  59.                     </ul>
  60.                 </div>
  61.             </body>
  62.             </html>
  63.         `, user.ID, user.Name, user.City, user.Salary)
  64.  
  65.         return c.HTML(http.StatusOK, html)
  66.     }
  67. }
  68.  
  69. // Example main function (for context, not required for the handler)
  70. func main() {
  71.     // Assume db is an initialized *sql.DB connection to PostgreSQL
  72.     var db *sql.DB // Replace with actual database connection
  73.  
  74.     e := echo.New()
  75.     e.GET("/users/:id", getUserHandler(db))
  76.     e.Logger.Fatal(e.Start(":8080"))
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement