-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.go
More file actions
500 lines (412 loc) · 13.7 KB
/
example.go
File metadata and controls
500 lines (412 loc) · 13.7 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
//go:build example
package main
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
"github.com/jasoet/pkg/v2/otel"
"github.com/jasoet/pkg/v2/server"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
// Example data structures
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
}
type HealthStatus struct {
Status string `json:"status"`
Timestamp time.Time `json:"timestamp"`
Services map[string]string `json:"services"`
}
type CustomHealthChecker struct {
services map[string]func() error
}
func (c *CustomHealthChecker) CheckHealth() map[string]string {
results := make(map[string]string)
for name, check := range c.services {
if err := check(); err != nil {
results[name] = fmt.Sprintf("unhealthy: %v", err)
} else {
results[name] = "healthy"
}
}
return results
}
func main() {
fmt.Println("Server Package Examples (v2 with OpenTelemetry)")
fmt.Println("===============================================")
// Run different server examples in sequence
examples := []struct {
name string
fn func()
}{
{"Basic Server Setup", basicServerExample},
{"Server with OpenTelemetry Configuration", otelConfigExample},
{"Server with Custom Routes and Middleware", customRoutesExample},
{"Server with Health Checks", healthChecksExample},
{"Server with Graceful Shutdown", gracefulShutdownExample},
}
for i, example := range examples {
fmt.Printf("\n%d. %s\n", i+1, example.name)
fmt.Println(strings.Repeat("-", len(example.name)+4))
example.fn()
if i < len(examples)-1 {
fmt.Println("\nPress Enter to continue to next example...")
fmt.Scanln()
}
}
}
func basicServerExample() {
fmt.Println("Creating a basic HTTP server with default configuration...")
operation := func(e *echo.Echo) {
// Register custom routes
e.GET("/hello", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
}
shutdown := func(e *echo.Echo) {
// Cleanup resources
fmt.Println("Cleaning up resources...")
}
// Create server with minimal configuration
config := server.DefaultConfig(8080, operation, shutdown)
fmt.Printf("Server configuration:\n")
fmt.Printf("- Port: %d\n", config.Port)
fmt.Printf("- Health endpoints: /health, /health/ready, /health/live\n")
fmt.Printf("- OpenTelemetry: disabled (nil)\n")
fmt.Println("\nTo start this server, you would call:")
fmt.Println("if err := server.StartWithConfig(config); err != nil { log.Fatal(err) }")
fmt.Println("\nNote: Without OTelConfig, no request logging or telemetry is enabled")
fmt.Println("Basic server example completed")
}
func otelConfigExample() {
fmt.Println("Creating server with OpenTelemetry configuration...")
operation := func(e *echo.Echo) {
e.GET("/api/hello", func(c echo.Context) error {
return c.JSON(http.StatusOK, map[string]string{
"message": "Hello with telemetry!",
})
})
}
shutdown := func(e *echo.Echo) {
fmt.Println("Cleaning up resources...")
}
// OTel is configured at the middleware level, not on server.Config.
// Create an OTel config and use it in Echo middleware:
otelCfg := otel.NewConfig("server-example").
WithServiceVersion("1.0.0")
config := server.DefaultConfig(8081, operation, shutdown)
config.ShutdownTimeout = 15 * time.Second
// OTel middleware can be added via config.Middleware or EchoConfigurer
// Example: e.Use(otelecho.Middleware(otelCfg.ServiceName))
fmt.Printf("Server configuration:\n")
fmt.Printf("- Port: %d\n", config.Port)
fmt.Printf("- Shutdown Timeout: %v\n", config.ShutdownTimeout)
fmt.Printf("- OTel Config Service: %s\n", otelCfg.ServiceName)
fmt.Println("\nTo start this server, you would call:")
fmt.Println("if err := server.StartWithConfig(config); err != nil { log.Fatal(err) }")
fmt.Println("\nNote: OTel is configured via Echo middleware, not server.Config")
fmt.Println("OpenTelemetry configuration example completed")
}
func customRoutesExample() {
fmt.Println("Creating server with custom routes and middleware...")
operation := func(e *echo.Echo) {
// Add custom middleware
e.Use(middleware.RequestID())
// Add custom routes
api := e.Group("/api/v1")
api.Use(authMiddleware())
// User endpoints
api.GET("/users", getUsersHandler)
api.GET("/users/:id", getUserHandler)
api.POST("/users", createUserHandler)
api.PUT("/users/:id", updateUserHandler)
api.DELETE("/users/:id", deleteUserHandler)
// Admin endpoints
admin := api.Group("/admin")
admin.Use(adminMiddleware())
admin.GET("/stats", getStatsHandler)
// Public endpoints (no auth required)
e.GET("/public/info", getInfoHandler)
}
shutdown := func(e *echo.Echo) {
fmt.Println("Shutting down API server...")
}
config := server.DefaultConfig(8082, operation, shutdown)
fmt.Printf("Server with custom routes:\n")
fmt.Printf("- Port: %d\n", config.Port)
fmt.Printf("- API Routes: /api/v1/users/*\n")
fmt.Printf("- Admin Routes: /api/v1/admin/*\n")
fmt.Printf("- Public Routes: /public/*\n")
fmt.Printf("- Custom Middleware: RequestID, Auth, Logging\n")
fmt.Println("\nTo start this server, you would call:")
fmt.Println("if err := server.StartWithConfig(config); err != nil { log.Fatal(err) }")
fmt.Println("Custom routes example completed")
}
func healthChecksExample() {
fmt.Println("Creating server with custom health checks...")
// Create custom health checker
healthChecker := &CustomHealthChecker{
services: map[string]func() error{
"database": func() error {
// Simulate database health check
return nil // Healthy
},
"redis": func() error {
// Simulate Redis health check
return fmt.Errorf("connection failed") // Unhealthy
},
"external_api": func() error {
// Simulate external API health check
return nil // Healthy
},
},
}
operation := func(e *echo.Echo) {
// Add custom health endpoint with the health checker
e.GET("/custom-health", func(c echo.Context) error {
results := healthChecker.CheckHealth()
status := HealthStatus{
Status: "ok",
Timestamp: time.Now(),
Services: results,
}
return c.JSON(http.StatusOK, status)
})
}
shutdown := func(e *echo.Echo) {
fmt.Println("Closing health check connections...")
}
config := server.DefaultConfig(8083, operation, shutdown)
fmt.Printf("Server with custom health checks:\n")
fmt.Printf("- Port: %d\n", config.Port)
fmt.Printf("- Health services: database, redis, external_api\n")
fmt.Printf("- Custom health endpoint: /custom-health\n")
fmt.Println("\nTo start this server, you would call:")
fmt.Println("if err := server.StartWithConfig(config); err != nil { log.Fatal(err) }")
fmt.Println("Health checks example completed")
}
func gracefulShutdownExample() {
fmt.Println("Demonstrating graceful shutdown with signal handling...")
operation := func(e *echo.Echo) {
// Add a long-running endpoint for testing graceful shutdown
e.GET("/slow", func(c echo.Context) error {
fmt.Println(" Processing slow request...")
time.Sleep(5 * time.Second)
return c.JSON(200, map[string]string{"status": "completed"})
})
}
shutdown := func(e *echo.Echo) {
fmt.Println("Waiting for in-flight requests to complete...")
time.Sleep(1 * time.Second)
fmt.Println("All requests completed, shutting down gracefully")
}
config := server.DefaultConfig(8084, operation, shutdown)
config.ShutdownTimeout = 10 * time.Second
fmt.Printf("Server with graceful shutdown:\n")
fmt.Printf("- Port: %d\n", config.Port)
fmt.Printf("- Shutdown Timeout: %v\n", config.ShutdownTimeout)
fmt.Printf("- Automatic signal handling (SIGINT, SIGTERM)\n")
fmt.Println("\nTo start this server, you would call:")
fmt.Println("if err := server.StartWithConfig(config); err != nil { log.Fatal(err) }")
fmt.Println("\nNote: StartWithConfig() automatically handles graceful shutdown")
fmt.Println("Graceful shutdown example completed")
}
// Helper functions for testing endpoints
func testEndpoints(port int) {
baseURL := fmt.Sprintf("http://localhost:%d", port)
endpoints := []string{"/health", "/health/ready", "/health/live", "/metrics"}
for _, endpoint := range endpoints {
url := baseURL + endpoint
resp, err := http.Get(url)
if err != nil {
fmt.Printf("✗ %s: %v\n", endpoint, err)
continue
}
resp.Body.Close()
fmt.Printf("✓ %s: %d\n", endpoint, resp.StatusCode)
}
}
func testCustomEndpoints(port int) {
baseURL := fmt.Sprintf("http://localhost:%d", port)
endpoints := []string{
"/health",
"/health/ready",
"/health/live",
}
for _, endpoint := range endpoints {
url := baseURL + endpoint
resp, err := http.Get(url)
if err != nil {
fmt.Printf("✗ %s: %v\n", endpoint, err)
continue
}
resp.Body.Close()
fmt.Printf("✓ %s: %d\n", endpoint, resp.StatusCode)
}
}
func testAPIEndpoints(port int) {
baseURL := fmt.Sprintf("http://localhost:%d", port)
tests := []struct {
method string
endpoint string
expected int
}{
{"GET", "/public/info", 200},
{"GET", "/api/v1/users", 200},
{"GET", "/api/v1/users/1", 200},
{"GET", "/api/v1/admin/stats", 403}, // Should fail without admin token
{"POST", "/api/v1/users", 201},
}
for _, test := range tests {
url := baseURL + test.endpoint
var resp *http.Response
var err error
switch test.method {
case "GET":
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("Authorization", "Bearer user-token")
resp, err = http.DefaultClient.Do(req)
case "POST":
req, _ := http.NewRequest("POST", url, strings.NewReader(`{"name":"Test User","email":"test@example.com"}`))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer user-token")
resp, err = http.DefaultClient.Do(req)
}
if err != nil {
fmt.Printf("✗ %s %s: %v\n", test.method, test.endpoint, err)
continue
}
resp.Body.Close()
if resp.StatusCode == test.expected {
fmt.Printf("✓ %s %s: %d\n", test.method, test.endpoint, resp.StatusCode)
} else {
fmt.Printf("? %s %s: %d (expected %d)\n", test.method, test.endpoint, resp.StatusCode, test.expected)
}
}
}
func testHealthEndpoints(port int) {
baseURL := fmt.Sprintf("http://localhost:%d", port)
endpoints := []string{"/health", "/health/ready", "/health/live"}
for _, endpoint := range endpoints {
url := baseURL + endpoint
resp, err := http.Get(url)
if err != nil {
fmt.Printf("✗ %s: %v\n", endpoint, err)
continue
}
if endpoint == "/health" {
// Parse and display health details
var healthStatus map[string]interface{}
json.NewDecoder(resp.Body).Decode(&healthStatus)
fmt.Printf("✓ %s: %d\n", endpoint, resp.StatusCode)
if services, ok := healthStatus["services"].(map[string]interface{}); ok {
for service, status := range services {
fmt.Printf(" - %s: %v\n", service, status)
}
}
} else {
fmt.Printf("✓ %s: %d\n", endpoint, resp.StatusCode)
}
resp.Body.Close()
}
}
// Custom middleware and handlers
func authMiddleware() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
authHeader := c.Request().Header.Get("Authorization")
if authHeader == "" {
return echo.NewHTTPError(http.StatusUnauthorized, "Missing authorization header")
}
// Simple token validation (in real apps, use proper JWT validation)
if !strings.HasPrefix(authHeader, "Bearer ") {
return echo.NewHTTPError(http.StatusUnauthorized, "Invalid authorization format")
}
token := strings.TrimPrefix(authHeader, "Bearer ")
if token != "user-token" && token != "admin-token" {
return echo.NewHTTPError(http.StatusUnauthorized, "Invalid token")
}
// Store user info in context
c.Set("token", token)
c.Set("is_admin", token == "admin-token")
return next(c)
}
}
}
func adminMiddleware() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
isAdmin := c.Get("is_admin")
if isAdmin != true {
return echo.NewHTTPError(http.StatusForbidden, "Admin access required")
}
return next(c)
}
}
}
// API Handlers
func getUsersHandler(c echo.Context) error {
users := []User{
{ID: 1, Name: "Alice", Email: "alice@example.com"},
{ID: 2, Name: "Bob", Email: "bob@example.com"},
}
return c.JSON(http.StatusOK, users)
}
func getUserHandler(c echo.Context) error {
id := c.Param("id")
user := User{ID: 1, Name: "Alice", Email: "alice@example.com"}
// Simulate user lookup by ID
if id == "1" {
return c.JSON(http.StatusOK, user)
}
return echo.NewHTTPError(http.StatusNotFound, "User not found")
}
func createUserHandler(c echo.Context) error {
var user User
if err := c.Bind(&user); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid request body")
}
// Simulate user creation
user.ID = 3
fmt.Printf("User created: ID=%d, Name=%s\n", user.ID, user.Name)
return c.JSON(http.StatusCreated, user)
}
func updateUserHandler(c echo.Context) error {
id := c.Param("id")
var user User
if err := c.Bind(&user); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid request body")
}
// Simulate user update
user.ID = 1 // Use ID from path
fmt.Printf("User updated: ID=%s, Name=%s\n", id, user.Name)
return c.JSON(http.StatusOK, user)
}
func deleteUserHandler(c echo.Context) error {
id := c.Param("id")
fmt.Printf("User deleted: ID=%s\n", id)
return c.NoContent(http.StatusNoContent)
}
func getStatsHandler(c echo.Context) error {
stats := map[string]interface{}{
"total_users": 150,
"active_users": 120,
"total_requests": 1000,
"uptime": "2h 30m",
}
return c.JSON(http.StatusOK, stats)
}
func getInfoHandler(c echo.Context) error {
info := map[string]interface{}{
"service": "server-examples",
"version": "1.0.0",
"environment": "development",
"timestamp": time.Now(),
}
return c.JSON(http.StatusOK, info)
}