-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
68 lines (54 loc) · 1.64 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
package main
import (
"context"
"github.com/AvineshTripathi/orch/config"
"github.com/AvineshTripathi/orch/handlers"
"github.com/AvineshTripathi/orch/middleware"
"github.com/AvineshTripathi/orch/provisioner-client"
"github.com/AvineshTripathi/orch/provisioner/queue"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/gorilla/mux"
)
func main() {
config.Load()
queueClient := queue.NewConnection()
// Create a new router
r := mux.NewRouter()
// Apply middleware
r.Use(middleware.LoggingMiddleware)
r.Use(middleware.AuthMiddleware)
// Define routes
r.HandleFunc("/", handlers.ApiServerStatusHandler).Methods(http.MethodGet)
r.HandleFunc("/provisioner", handlers.ProvisionerStatusHandler).Methods(http.MethodGet)
r.HandleFunc("/newTask", handlers.AddTaskToQueueHandler(queueClient)).Methods(http.MethodPost)
// Set up server
srv := &http.Server{
Addr: ":" + config.Port,
Handler: r,
}
// Run server in a goroutine
go func() {
log.Printf("Server is running on port %s", config.Port)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Server failed: %v", err)
}
}()
provisioner.InitializeClient()
// Wait for interrupt signal to gracefully shut down the server
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
<-quit
log.Println("Shutting down server...")
// Create a deadline to wait for server shutdown
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 gracefully stopped")
}