-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
94 lines (76 loc) · 2.66 KB
/
Copy pathmain.go
File metadata and controls
94 lines (76 loc) · 2.66 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"database/sql"
"log"
"net/http"
"os"
"github.com/danielmiranda22/social-network-server/internal/api"
"github.com/danielmiranda22/social-network-server/internal/api/admin"
"github.com/danielmiranda22/social-network-server/internal/api/chirps"
"github.com/danielmiranda22/social-network-server/internal/api/health"
"github.com/danielmiranda22/social-network-server/internal/api/users"
"github.com/danielmiranda22/social-network-server/internal/database"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
)
func main() {
const filepathRoot = "."
const port = "8080"
const apiSecretEnvKey = "API_SECRET"
const polkaKeyEnvKey = "POLKA_KEY"
godotenv.Load()
dbURL := os.Getenv("DB_URL")
if dbURL == "" {
log.Fatal("DB_URL environment variable is required")
}
apiSecret := os.Getenv(apiSecretEnvKey)
if apiSecret == "" {
log.Fatalf("%s environment variable is required", apiSecretEnvKey)
}
polkaKey := os.Getenv(polkaKeyEnvKey)
if polkaKey == "" {
log.Fatalf("%s environment variable is required", polkaKeyEnvKey)
}
dbConn, err := sql.Open("postgres", dbURL)
if err != nil {
log.Fatalf("Failed to connect to database: %v", err)
}
defer dbConn.Close()
apiCfg := &api.ApiConfig{
DB: database.New(dbConn),
Platform: os.Getenv("PLATFORM"),
APISecret: apiSecret,
PolkaKey: polkaKey,
}
mux := http.NewServeMux()
// file server
mux.Handle("/app/",
apiCfg.MiddlewareMetricsInc(
http.StripPrefix("/app", http.FileServer(http.Dir(filepathRoot))),
),
)
// health
mux.HandleFunc("GET /api/healthz", health.IsHealthHandler)
// admin
mux.HandleFunc("GET /admin/metrics", admin.HandlerMetrics(apiCfg))
mux.HandleFunc("POST /admin/reset", admin.HandlerReset(apiCfg))
// users
mux.HandleFunc("POST /api/users", users.HandlerCreateUser(apiCfg))
mux.HandleFunc("PUT /api/users", users.HandlerUpdateUser(apiCfg))
mux.HandleFunc("POST /api/login", users.HandlerLogin(apiCfg))
mux.HandleFunc("POST /api/refresh", users.HandlerRefresh(apiCfg))
mux.HandleFunc("POST /api/revoke", users.HandlerRevoke(apiCfg))
// webhooks
mux.HandleFunc("POST /api/polka/webhooks", users.HandlerUpdateChirpyRed(apiCfg))
// chirps
mux.HandleFunc("GET /api/chirps", chirps.HandlerGetAllChirps(apiCfg))
mux.HandleFunc("GET /api/chirps/{chirpID}", chirps.HandlerGetChirpByID(apiCfg))
mux.HandleFunc("POST /api/chirps", chirps.HandlerCreateChirp(apiCfg))
mux.HandleFunc("DELETE /api/chirps/{chirpID}", chirps.HandlerDeleteChirpByID(apiCfg))
server := &http.Server{
Addr: ":" + port,
Handler: mux,
}
log.Printf("Serving files from %s on port: %s\n", filepathRoot, port)
log.Fatal(server.ListenAndServe())
}