Use real environment variables for template rendering
This commit is contained in:
34
main.go
34
main.go
@@ -1,7 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"html/template"
|
||||
@@ -14,26 +13,9 @@ import (
|
||||
"google.golang.org/api/idtoken"
|
||||
)
|
||||
|
||||
var (
|
||||
env = map[string]string{}
|
||||
templates *template.Template
|
||||
)
|
||||
var templates *template.Template
|
||||
|
||||
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"))
|
||||
}
|
||||
|
||||
@@ -60,13 +42,23 @@ func handleStatic(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
t.Execute(w, env)
|
||||
t.Execute(w, envMap())
|
||||
return
|
||||
}
|
||||
|
||||
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) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
@@ -79,7 +71,7 @@ func handleGoogleCallback(w http.ResponseWriter, r *http.Request) {
|
||||
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 {
|
||||
log.Println("[ERROR] failed to validate token:", err)
|
||||
http.Error(w, "invalid token", http.StatusUnauthorized)
|
||||
|
||||
@@ -4,42 +4,38 @@
|
||||
<script>document.documentElement.className = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'wa-dark' : 'wa-light';</script>
|
||||
<meta charset="UTF-8">
|
||||
<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">
|
||||
<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>
|
||||
<style>
|
||||
body {
|
||||
font-family: var(--wa-font-sans);
|
||||
max-width: 800px;
|
||||
margin: 0 auto;
|
||||
padding: 2rem;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
min-height: 100vh;
|
||||
margin: 0;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body style="opacity: 0">
|
||||
<wa-card>
|
||||
<h1 slot="header">Hello World</h1>
|
||||
<p>Welcome to the Tickets App!</p>
|
||||
<div id="g_id_onload"
|
||||
data-client_id="{{.GOOGLE_CLIENT_ID}}"
|
||||
data-login_uri="/auth/google/callback"
|
||||
data-auto_prompt="false">
|
||||
</div>
|
||||
<div class="g_id_signin"
|
||||
data-type="standard"
|
||||
data-size="large"
|
||||
data-theme="outline"
|
||||
data-text="sign_in_with"
|
||||
data-shape="rectangular"
|
||||
data-logo_alignment="left">
|
||||
</div>
|
||||
</wa-card>
|
||||
<div id="g_id_onload"
|
||||
data-client_id="{{index . "GOOGLE_CLIENT_ID"}}"
|
||||
data-login_uri="/auth/google/callback"
|
||||
data-auto_prompt="false">
|
||||
</div>
|
||||
<div class="g_id_signin"
|
||||
data-type="standard"
|
||||
data-size="large"
|
||||
data-theme="outline"
|
||||
data-text="sign_in_with"
|
||||
data-shape="rectangular"
|
||||
data-logo_alignment="left">
|
||||
</div>
|
||||
<script type="module">
|
||||
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', e => {
|
||||
document.documentElement.className = e.matches ? 'wa-dark' : 'wa-light';
|
||||
});
|
||||
await customElements.whenDefined('wa-card');
|
||||
document.body.style.opacity = 1;
|
||||
</script>
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user