-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
95 lines (78 loc) · 2 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
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
95
package main
import (
"context"
"encoding/gob"
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"github.com/artem-streltsov/url-shortener/internal/database"
"github.com/artem-streltsov/url-shortener/internal/handlers"
"github.com/artem-streltsov/url-shortener/internal/safebrowsing"
"github.com/joho/godotenv"
)
func init() {
gob.Register(&database.User{})
}
func main() {
godotenv.Load()
port := os.Getenv("PORT")
if port == "" {
log.Fatalf("PORT environment variable is not set")
}
dbPath := os.Getenv("DB_PATH")
if dbPath == "" {
log.Fatalf("DB_PATH environment variable is not set")
}
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
dir := filepath.Dir(dbPath)
if err := os.MkdirAll(dir, 0755); err != nil {
log.Fatalf("Error creating directory for database: %v", err)
}
file, err := os.Create(dbPath)
if err != nil {
log.Fatalf("Error creating database file: %v", err)
}
file.Close()
}
db, err := database.NewDB(dbPath)
if err != nil {
log.Fatalf("Error connecting to database: %v", err)
}
defer db.Close()
if err := safebrowsing.InitSafeBrowsing(); err != nil {
log.Fatalf("Error initializing Safe Browsing: %v", err)
}
defer safebrowsing.Close()
handler := handlers.NewHandler(db)
srv := &http.Server{
Addr: ":" + port,
Handler: handler.Routes(),
}
go func() {
log.Printf("Starting server at :%s", port)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Error starting server: %v", err)
}
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("Shutting down server...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatalf("Server forced to shutdown: %v", err)
}
log.Println("Server exiting")
}
func getEnvWithDefault(key, defaultValue string) string {
value := os.Getenv(key)
if value == "" {
return defaultValue
}
return value
}