Skip to content

Commit 9cfcbbd

Browse files
Add without json
1 parent 128b83d commit 9cfcbbd

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

firebaseSample/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Ignore private key json file
2+
cs3219-g24-firebase-adminsdk-9cm7h-703aac7306.json

firebaseSample/main.exe

22.1 MB
Binary file not shown.

firebaseSample/main.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"html/template"
7+
"log"
8+
"net/http"
9+
"os"
10+
11+
"cloud.google.com/go/firestore"
12+
"google.golang.org/api/option"
13+
)
14+
15+
// PageData struct for template rendering
16+
type PageData struct {
17+
Title string
18+
Description string
19+
}
20+
21+
// Initialize Firestore client
22+
func initFirestore() *firestore.Client {
23+
// Use your service account key file for authentication
24+
ctx := context.Background()
25+
sa := option.WithCredentialsFile("cs3219-g24-firebase-adminsdk-9cm7h-703aac7306.json")
26+
client, err := firestore.NewClient(ctx, "cs3219-g24", sa)
27+
if err != nil {
28+
log.Fatalf("Failed to create Firestore client: %v", err)
29+
}
30+
return client
31+
}
32+
33+
// Handler to get question data from Firestore and render the template
34+
func questionHandler(w http.ResponseWriter, r *http.Request) {
35+
// Initialize Firestore client
36+
log.Print("1")
37+
client := initFirestore()
38+
defer client.Close()
39+
40+
// Get the context and reference the "questions" collection
41+
ctx := context.Background()
42+
dsnap, err := client.Collection("questions").Doc("1").Get(ctx)
43+
44+
if err != nil {
45+
log.Print(err)
46+
return
47+
}
48+
49+
// get data from cloud firestore
50+
title := dsnap.Data()["title"].(string)
51+
description := dsnap.Data()["description"].(string)
52+
log.Printf("Document data: %#v\n", title)
53+
log.Printf("Hello World")
54+
55+
// Define the template
56+
tmpl := template.Must(template.ParseFiles("question.html"))
57+
58+
// Populate dynamic data for the template
59+
data := PageData{
60+
Title: title,
61+
Description: description,
62+
}
63+
64+
// Render the template with the dynamic data
65+
tmpl.Execute(w, data)
66+
}
67+
68+
func main() {
69+
// Handle the /question route
70+
http.HandleFunc("/question", questionHandler)
71+
72+
// Serve on port 8080
73+
port := os.Getenv("PORT")
74+
if port == "" {
75+
port = "8080"
76+
}
77+
78+
log.Printf("Server is running on http://localhost:%s", port)
79+
log.Printf("2")
80+
err := http.ListenAndServe(fmt.Sprintf(":%s", port), nil)
81+
if err != nil {
82+
log.Fatalf("Failed to start server: %v", err)
83+
}
84+
}

firebaseSample/question.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>This is {{.Title}}</title>
7+
</head>
8+
<body>
9+
<h1>{{.Title}}</h1>
10+
<p>{{.Description}}</p>
11+
</body>
12+
</html>

0 commit comments

Comments
 (0)