-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
91 lines (70 loc) · 2.58 KB
/
Copy pathmain.go
File metadata and controls
91 lines (70 loc) · 2.58 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
package main
import (
"database/sql"
"github.com/e-300/http-server-go/internal/database"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
"log"
"net/http"
"os"
"sync/atomic"
)
// Stateful handler to track number of requests that have been processed since t-0
// atomic.Int32 -> safely increment and read int val across multiple goroutines or https requests
type apiConfig struct {
fileserverHits atomic.Int32
db *database.Queries
platform string
jwtSecret string
polkaSecret string
}
func main() {
godotenv.Load()
dbURL := os.Getenv("DB_URL")
platform := os.Getenv("PLATFORM")
token_string := os.Getenv("JWT_SECRET")
polka_api_key := os.Getenv("POLKA_SECRET")
db, err := sql.Open("postgres", dbURL)
if err != nil {
log.Fatal("DataBase failed to open", err)
}
dbQueries := database.New(db)
const filePathRoot = "."
const port = "8080"
// State Object created
apiCfg := apiConfig{
fileserverHits: atomic.Int32{},
db: dbQueries,
platform: platform,
jwtSecret: token_string,
polkaSecret: polka_api_key,
}
// NewServeMux -> lookup table matching incoming request -> endpoint -> Handler
mux := http.NewServeMux()
// Handle -> expects: Handler Object, http.FilerServer returns one of those handler objects
// http.FileServer(http.Dir(".") -> This gives us a fileserver that can handle requests
// Why StripPrefix -> /app/ doesnt exist in our root
fsHandler := http.StripPrefix("/app/", http.FileServer(http.Dir(filePathRoot)))
wrappedHandler := apiCfg.middlewareMetricsInc(fsHandler)
mux.Handle("/app/", wrappedHandler)
mux.HandleFunc("GET /api/healthz", handlerReadiness)
mux.HandleFunc("POST /api/polka/webhooks", apiCfg.handlerPolkaWebhook)
mux.HandleFunc("POST /api/login", apiCfg.handlerUserLogin)
mux.HandleFunc("POST /api/refresh", apiCfg.handlerRefresh)
mux.HandleFunc("POST /api/revoke", apiCfg.handlerRevoke)
mux.HandleFunc("POST /api/users", apiCfg.handlerCreateUser)
mux.HandleFunc("PUT /api/users", apiCfg.handlerUserUpdateLogin)
mux.HandleFunc("POST /api/chirps", apiCfg.createChirp)
mux.HandleFunc("GET /api/chirps", apiCfg.handlerChirpsRetrieve)
mux.HandleFunc("GET /api/chirps/{chirpID}", apiCfg.handlerChirpsGet)
mux.HandleFunc("DELETE /api/chirps/{chirpID}", apiCfg.handlerDeleteChirp)
mux.HandleFunc("GET /admin/metrics", apiCfg.numOfHits)
mux.HandleFunc("POST /admin/reset", apiCfg.resetHits)
// added pointer to server
server := &http.Server{
Addr: ":" + port,
Handler: mux,
}
log.Printf("Serving files from %s on port: %s\n", filePathRoot, port)
log.Fatal(server.ListenAndServe())
}