-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
57 lines (45 loc) · 1.34 KB
/
main.go
File metadata and controls
57 lines (45 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"database/sql"
"fmt"
"os"
"log"
"net/http"
"webapp/app"
"webapp/app/authservice"
"webapp/app/handlers"
"webapp/app/middleware"
"firebase.google.com/go/auth"
"github.com/gorilla/mux"
_ "github.com/lib/pq"
)
var client *auth.Client
var db *sql.DB
func main() {
os.Setenv("FIREBASE_CREDENTIALS", "C:/3 Year/Golang 2025/webapp/blogapi-eae9d-firebase-adminsdk-fbsvc-57f2fcc9fd.json")
//Initilizing the database here
app.InitDB()
// Initialize Firebase
var err error
client, err = authservice.InitFB()
if err != nil {
log.Fatal("Firebase initialization error:", err)
}
// PASS THE INITIALIZED DB AND CLIENT TO THE HANDLERS
handlers.Initialize(client, db)
// Create a new router
r := mux.NewRouter()
// Define routes and apply middleware
r.HandleFunc("/dash", handlers.DashboardHandler).Methods("GET")
r.HandleFunc("/home", handlers.HomeHandler).Methods("GET")
r.HandleFunc("/hello", handlers.LayoutHandler).Methods("GET")
r.HandleFunc("/register", handlers.RegisterHandler).Methods("POST")
r.HandleFunc("/protected", middleware.AuthMiddleware(client, db, handlers.ProtectedHandler)).Methods("GET")
// Start the server
port := ":8080"
fmt.Println("Server started on port", port, "http://localhost" + port)
err = http.ListenAndServe(port, r)
if err != nil {
log.Fatal("Failed to start server:", err)
}
}