Use real environment variables for template rendering

This commit is contained in:
Ian Gulliver
2025-12-28 22:39:06 -08:00
parent 3422f90b52
commit c80dc1a2c5
2 changed files with 32 additions and 44 deletions

34
main.go
View File

@@ -1,7 +1,6 @@
package main package main
import ( import (
"bufio"
"context" "context"
"encoding/json" "encoding/json"
"html/template" "html/template"
@@ -14,26 +13,9 @@ import (
"google.golang.org/api/idtoken" "google.golang.org/api/idtoken"
) )
var ( var templates *template.Template
env = map[string]string{}
templates *template.Template
)
func init() { func init() {
f, err := os.Open("secrets.env")
if err != nil {
log.Fatal("[ERROR] failed to open secrets.env: ", err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
if parts := strings.SplitN(line, "=", 2); len(parts) == 2 {
env[parts[0]] = parts[1]
}
}
templates = template.Must(template.ParseGlob("static/*.html")) templates = template.Must(template.ParseGlob("static/*.html"))
} }
@@ -60,13 +42,23 @@ func handleStatic(w http.ResponseWriter, r *http.Request) {
return return
} }
w.Header().Set("Content-Type", "text/html") w.Header().Set("Content-Type", "text/html")
t.Execute(w, env) t.Execute(w, envMap())
return return
} }
http.ServeFile(w, r, filepath.Join("static", name)) http.ServeFile(w, r, filepath.Join("static", name))
} }
func envMap() map[string]string {
m := map[string]string{}
for _, e := range os.Environ() {
if parts := strings.SplitN(e, "=", 2); len(parts) == 2 {
m[parts[0]] = parts[1]
}
}
return m
}
func handleGoogleCallback(w http.ResponseWriter, r *http.Request) { func handleGoogleCallback(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost { if r.Method != http.MethodPost {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed) http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
@@ -79,7 +71,7 @@ func handleGoogleCallback(w http.ResponseWriter, r *http.Request) {
return return
} }
payload, err := idtoken.Validate(context.Background(), credential, env["GOOGLE_CLIENT_ID"]) payload, err := idtoken.Validate(context.Background(), credential, os.Getenv("GOOGLE_CLIENT_ID"))
if err != nil { if err != nil {
log.Println("[ERROR] failed to validate token:", err) log.Println("[ERROR] failed to validate token:", err)
http.Error(w, "invalid token", http.StatusUnauthorized) http.Error(w, "invalid token", http.StatusUnauthorized)

View File

@@ -4,25 +4,23 @@
<script>document.documentElement.className = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'wa-dark' : 'wa-light';</script> <script>document.documentElement.className = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'wa-dark' : 'wa-light';</script>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tickets App</title> <title>HCA Tickets</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@awesome.me/webawesome@3/dist-cdn/styles/themes/default.css"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@awesome.me/webawesome@3/dist-cdn/styles/themes/default.css">
<script type="module" src="https://cdn.jsdelivr.net/npm/@awesome.me/webawesome@3/dist-cdn/webawesome.loader.js"></script> <script type="module" src="https://cdn.jsdelivr.net/npm/@awesome.me/webawesome@3/dist-cdn/webawesome.loader.js"></script>
<script src="https://accounts.google.com/gsi/client" async></script> <script src="https://accounts.google.com/gsi/client" async></script>
<style> <style>
body { body {
font-family: var(--wa-font-sans); display: flex;
max-width: 800px; justify-content: center;
margin: 0 auto; align-items: center;
padding: 2rem; min-height: 100vh;
margin: 0;
} }
</style> </style>
</head> </head>
<body style="opacity: 0"> <body style="opacity: 0">
<wa-card>
<h1 slot="header">Hello World</h1>
<p>Welcome to the Tickets App!</p>
<div id="g_id_onload" <div id="g_id_onload"
data-client_id="{{.GOOGLE_CLIENT_ID}}" data-client_id="{{index . "GOOGLE_CLIENT_ID"}}"
data-login_uri="/auth/google/callback" data-login_uri="/auth/google/callback"
data-auto_prompt="false"> data-auto_prompt="false">
</div> </div>
@@ -34,12 +32,10 @@
data-shape="rectangular" data-shape="rectangular"
data-logo_alignment="left"> data-logo_alignment="left">
</div> </div>
</wa-card>
<script type="module"> <script type="module">
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', e => { window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', e => {
document.documentElement.className = e.matches ? 'wa-dark' : 'wa-light'; document.documentElement.className = e.matches ? 'wa-dark' : 'wa-light';
}); });
await customElements.whenDefined('wa-card');
document.body.style.opacity = 1; document.body.style.opacity = 1;
</script> </script>
</body> </body>