-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
113 lines (97 loc) · 2.65 KB
/
Copy pathapp.go
File metadata and controls
113 lines (97 loc) · 2.65 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"HRPMLW/internal/config"
"HRPMLW/internal/database"
"HRPMLW/internal/models"
"HRPMLW/internal/routes"
"HRPMLW/internal/services"
"context"
"fmt"
"io"
"net/http"
"time"
)
// App struct
type App struct {
ctx context.Context
httpClient *services.HTTPService
router *routes.Router
config *config.Config
}
// NewApp creates a new App application struct
func NewApp() *App {
cfg := config.Load()
return &App{
config: cfg,
httpClient: services.NewHTTPService("http://localhost:" + cfg.ServerPort),
}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
// Initialize database
if err := database.Initialize(database.DefaultConfig()); err != nil {
fmt.Printf("Failed to initialize database: %v\n", err)
}
// Start the Gin server in a goroutine
go a.startGinServer()
}
// shutdown is called when the app is closing
func (a *App) shutdown(ctx context.Context) {
// Close database connection
if err := database.Close(); err != nil {
fmt.Printf("Failed to close database: %v\n", err)
}
}
func (a *App) startGinServer() {
a.router = routes.NewRouter()
a.router.Setup()
a.router.GetEngine().Run(":" + a.config.ServerPort)
}
// Greet returns a greeting for the given name
func (a *App) Greet(name string) string {
return fmt.Sprintf("Hello %s, It's show time!", name)
}
// TestHealthCheck calls the backend health check API
func (a *App) TestHealthCheck() *models.ApiResponse {
return a.httpClient.Get("/")
}
// TestPing calls the backend ping API
func (a *App) TestPing() *models.ApiResponse {
return a.httpClient.Get("/ping")
}
// TestAPICall makes a generic API call
func (a *App) TestAPICall(endpoint string) *models.ApiResponse {
start := time.Now()
resp, err := http.Get("http://localhost:" + a.config.ServerPort + endpoint)
if err != nil {
return &models.ApiResponse{
Success: false,
Error: err.Error(),
Time: time.Since(start).Milliseconds(),
}
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return &models.ApiResponse{
Success: false,
Error: err.Error(),
Time: time.Since(start).Milliseconds(),
}
}
return &models.ApiResponse{
Success: resp.StatusCode >= 200 && resp.StatusCode < 300,
Data: string(body),
Time: time.Since(start).Milliseconds(),
}
}
// GetDBPath returns the database path
func (a *App) GetDBPath() string {
return database.DefaultConfig().Path
}
// GetDBSchema calls the backend API to get the database schema
func (a *App) GetDBSchema() *models.ApiResponse {
return a.httpClient.Get("/api/v1/db/schema")
}