try ollama generated display-info handler for http request display

This commit is contained in:
2025-08-05 13:29:43 +02:00
parent 3bb67fc2c1
commit 84621ce12e
3 changed files with 86 additions and 0 deletions

View File

@@ -3,6 +3,7 @@
name: Docker Build
on:
workflow_dispatch: {}
push:
branches:
- main

View File

@@ -3,6 +3,7 @@
name: Hashicorp Vault
on: #[push,pull_request]
workflow_dispatch: {}
push: &vaultPaths
paths:
- 'iac/*.tf'

84
main.go
View File

@@ -372,6 +372,87 @@ func test_oauth2_callback(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(html))
}
// Handler pour afficher les cookies et le client
func displayInfoHandler(w http.ResponseWriter, r *http.Request) {
// Get the cookies
cookies := r.Cookies()
// Get the user IP
userIP, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
log.Printf("Failed to get user IP: %v", err)
}
// Get the headers
headers := map[string]string{}
for name, values := range r.Header {
headers[name] = strings.Join(values, ", ")
}
// Display the information in a simple HTML page
tmpl := `
<!DOCTYPE html>
<html>
<head>
<title>Info</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
padding: 20px;
}
</style>
</head>
<body>
<h1>Cookies:</h1>
<ul>
{{range .Cookies}}
<li>{{.}}</li>
{{end}}
</ul>
<h1>User IP:</h1>
<p>{{.UserIP}}</p>
<h1>Headers:</h1>
<pre>
{{range $key, $value := .Headers}}
{{$key}}: {{$value}}
{{end}}
</pre>
</body>
</html>
`
data := struct {
Cookies []string
UserIP string
Headers map[string]string
}{
Cookies: []string{},
UserIP: userIP,
Headers: headers,
}
for _, cookie := range cookies {
data.Cookies = append(data.Cookies, fmt.Sprintf("%s=%s", cookie.Name, cookie.Value))
}
w.Header().Set("Content-Type", "text/html")
tmplBytes, err := template.New("info").Parse(tmpl)
if err != nil {
http.Error(w, "Error parsing template", http.StatusInternalServerError)
fmt.Printf("%+v\n", err)
return
}
err = tmplBytes.Execute(w, data)
if err != nil {
http.Error(w, "Error rendering template", http.StatusInternalServerError)
fmt.Printf("%+v\n", err)
return
}
}
func main() {
var err error
@@ -394,6 +475,8 @@ func main() {
// Define the handler for the `/readiness` probe
http.HandleFunc("/readiness", readinessHandler)
http.HandleFunc("/display-info", displayInfoHandler)
/*
Gitea doesn't come with device flow # https://github.com/go-gitea/gitea/issues/27309
https://gitea.arcodange.duckdns.org/.well-known/openid-configuration
@@ -410,6 +493,7 @@ func main() {
http.HandleFunc("/retrieve", retrieveHandler)
http.HandleFunc("/test-oauth-callback", test_oauth2_callback)
// Start the HTTP server
port := ":8080"
log.Printf("Server starting on port %s\n", port)