-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
64 lines (53 loc) · 1.91 KB
/
main.go
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
package main
import (
"chirpy/internal/config"
"chirpy/internal/database"
"chirpy/internal/handlers"
"database/sql"
"fmt"
"log"
"net/http"
"os"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
)
var port = "8080"
func main() {
godotenv.Load()
dbURL := os.Getenv("DB_URL")
platform := os.Getenv("PLATFORM")
jwtSecret := os.Getenv("TOKEN_SECRET")
db, err := sql.Open("postgres", dbURL)
if err != nil {
log.Printf("database connection failed %s", err)
return
}
dbQueries := database.New(db)
config := &config.ApiConfig{
Db: dbQueries,
Platform: platform,
JwtSecret: jwtSecret,
}
mux := http.NewServeMux()
server := &http.Server{
Handler: mux,
Addr: ":" + port,
}
mux.Handle("/app/", http.StripPrefix("/app/", config.MiddlewareMetricsInc(http.FileServer(http.Dir("./")))))
mux.Handle("/app/assets/", http.StripPrefix("/app/assets/", config.MiddlewareMetricsInc(http.FileServer(http.Dir("./assets")))))
mux.HandleFunc("GET /api/healthz", handlers.Healthz)
mux.HandleFunc("GET /admin/metrics", config.GetMetrics)
mux.HandleFunc("POST /admin/reset", config.Reset)
mux.HandleFunc("POST /api/users", handlers.CreateUser(config))
mux.HandleFunc("PUT /api/users", config.AuthorizationMiddleware(handlers.UpdateUser(config)))
mux.HandleFunc("POST /api/chirps", config.AuthorizationMiddleware(handlers.CreateChirp(config)))
mux.HandleFunc("DELETE /api/chirps/{id}", config.AuthorizationMiddleware(handlers.DeleteChirp(config)))
mux.HandleFunc("GET /api/chirps", handlers.GetChirps(config))
mux.HandleFunc("GET /api/chirps/{id}", handlers.GetChirp(config))
mux.HandleFunc("POST /api/login", handlers.Login(config))
mux.HandleFunc("POST /api/refresh", handlers.Refresh(config))
mux.HandleFunc("POST /api/revoke", handlers.Revoke(config))
mux.HandleFunc("POST /api/polka/webhooks", handlers.UpdateUserRed(config))
fmt.Printf("Listening on port %s\n", port)
log.Fatal(server.ListenAndServe())
}