-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (69 loc) · 1.78 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
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"personalwebsite/routes"
"personalwebsite/utils"
"syscall"
"time"
"github.com/joho/godotenv"
)
var (
vsCodeUser string
vsCodePass string
)
func main() {
// Load .env file
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
// Initialize OpenAI client
err = utils.Init()
if err != nil {
log.Fatal(err)
}
// Get VS Code credentials from .env
vsCodeUser = os.Getenv("VSCODEUSER")
vsCodePass = os.Getenv("VSCODEPASS")
if vsCodeUser == "" || vsCodePass == "" {
log.Fatal("VSCODEUSER and VSCODEPASS must be set in .env file")
}
fmt.Println("OpenAI client and VS Code credentials initialized successfully")
// Get PORT from .env
port := os.Getenv("PORT")
if port == "" {
port = "8080" // default port if not specified
}
// Setup routes
handler := routes.SetupRoutes(vsCodeUser, vsCodePass)
// Create server
srv := &http.Server{
Addr: ":" + port,
Handler: handler,
}
// Start server in a goroutine
go func() {
fmt.Printf("Server starting on http://localhost:%s\n", port)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("ListenAndServe(): %v", err)
}
}()
// Wait for interrupt signal to gracefully shutdown the server with a timeout of 5 seconds
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("Shutting down server...")
// Create a deadline to wait for
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// Doesn't block if no connections, but will otherwise wait until the timeout deadline
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server forced to shutdown:", err)
}
log.Println("Server exiting")
}