-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
67 lines (50 loc) · 1.55 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
65
66
67
package main
import (
"database/sql"
"log"
"net/http"
"os"
"url-short/internal/database"
_ "github.com/lib/pq"
"github.com/redis/go-redis/v9"
)
func main() {
serverPort := os.Getenv("SERVER_PORT")
dbURL := os.Getenv("PG_CONN")
rdbURL := os.Getenv("RDB_CONN")
jwtSecret := os.Getenv("JWT_SECRET")
db, err := sql.Open("postgres", dbURL)
if err != nil {
log.Fatal(err)
}
dbQueries := database.New(db)
mux := http.NewServeMux()
server := &http.Server{
Addr: ":" + serverPort,
Handler: mux,
}
opt, err := redis.ParseURL(rdbURL)
if err != nil {
log.Fatal(err)
}
redisClient := redis.NewClient(opt)
apiCfg := apiConfig{
DB: dbQueries,
RDB: redisClient,
JWTSecret: jwtSecret,
}
// utility endpoints
mux.HandleFunc("GET /api/v1/healthz", apiCfg.healthz)
// url management endpoints
mux.HandleFunc("POST /api/v1/data/shorten", apiCfg.authenticationMiddleware(apiCfg.postLongURL))
mux.HandleFunc("GET /api/v1/{shortUrl}", apiCfg.getShortURL)
mux.HandleFunc("DELETE /api/v1/{shortUrl}", apiCfg.authenticationMiddleware(apiCfg.deleteShortURL))
mux.HandleFunc("PUT /api/v1/{shortUrl}", apiCfg.authenticationMiddleware(apiCfg.putShortURL))
// user management endpoints
mux.HandleFunc("POST /api/v1/users", apiCfg.postAPIUsers)
mux.HandleFunc("PUT /api/v1/users", apiCfg.authenticationMiddleware(apiCfg.putAPIUsers))
mux.HandleFunc("POST /api/v1/login", apiCfg.postAPILogin)
mux.HandleFunc("POST /api/v1/refresh", apiCfg.postAPIRefresh)
log.Printf("Serving port : %v \n", serverPort)
log.Fatal(server.ListenAndServe())
}