1+ // Package {{name}} provides shared utilities and models for create-polyglot monorepo
2+ package {{name }}
3+
4+ import (
5+ "encoding/json"
6+ "fmt"
7+ "time"
8+
9+ "github.com/google/uuid"
10+ )
11+
12+ // Response represents a standardized API response
13+ type Response struct {
14+ Status string `json:"status"`
15+ Timestamp string `json:"timestamp"`
16+ Data interface {} `json:"data"`
17+ Message * string `json:"message,omitempty"`
18+ }
19+
20+ // ServiceHealth represents the health status of a service
21+ type ServiceHealth struct {
22+ ID string `json:"id"`
23+ ServiceName string `json:"service_name"`
24+ Status string `json:"status"` // "healthy", "degraded", "unhealthy"
25+ Version * string `json:"version,omitempty"`
26+ Uptime * float64 `json:"uptime,omitempty"`
27+ LastCheck * time.Time `json:"last_check,omitempty"`
28+ CreatedAt time.Time `json:"created_at"`
29+ UpdatedAt time.Time `json:"updated_at"`
30+ }
31+
32+ // ErrorResponse represents a standardized error response
33+ type ErrorResponse struct {
34+ ID string `json:"id"`
35+ ErrorCode string `json:"error_code"`
36+ ErrorMessage string `json:"error_message"`
37+ Details map [string ]interface {} `json:"details,omitempty"`
38+ CreatedAt time.Time `json:"created_at"`
39+ UpdatedAt time.Time `json:"updated_at"`
40+ }
41+
42+ // FormatResponse creates a standardized API response
43+ func FormatResponse (data interface {}, status string , message * string ) Response {
44+ return Response {
45+ Status : status ,
46+ Timestamp : time .Now ().UTC ().Format (time .RFC3339 ),
47+ Data : data ,
48+ Message : message ,
49+ }
50+ }
51+
52+ // ValidateConfig checks that all required keys exist in the configuration map
53+ func ValidateConfig (config map [string ]interface {}, requiredKeys []string ) bool {
54+ for _ , key := range requiredKeys {
55+ if _ , exists := config [key ]; ! exists {
56+ return false
57+ }
58+ }
59+ return true
60+ }
61+
62+ // SafeJSONUnmarshal safely unmarshals JSON with fallback to default value
63+ func SafeJSONUnmarshal (data []byte , v interface {}, defaultValue interface {}) interface {} {
64+ if err := json .Unmarshal (data , v ); err != nil {
65+ return defaultValue
66+ }
67+ return v
68+ }
69+
70+ // GenerateID generates a new UUID string
71+ func GenerateID () string {
72+ return uuid .New ().String ()
73+ }
74+
75+ // NewServiceHealth creates a new ServiceHealth instance
76+ func NewServiceHealth (serviceName , status string ) ServiceHealth {
77+ now := time .Now ().UTC ()
78+ return ServiceHealth {
79+ ID : GenerateID (),
80+ ServiceName : serviceName ,
81+ Status : status ,
82+ CreatedAt : now ,
83+ UpdatedAt : now ,
84+ }
85+ }
86+
87+ // NewErrorResponse creates a new ErrorResponse instance
88+ func NewErrorResponse (errorCode , errorMessage string , details map [string ]interface {}) ErrorResponse {
89+ now := time .Now ().UTC ()
90+ return ErrorResponse {
91+ ID : GenerateID (),
92+ ErrorCode : errorCode ,
93+ ErrorMessage : errorMessage ,
94+ Details : details ,
95+ CreatedAt : now ,
96+ UpdatedAt : now ,
97+ }
98+ }
99+
100+ // String returns a string representation of the ServiceHealth
101+ func (sh ServiceHealth ) String () string {
102+ return fmt .Sprintf ("ServiceHealth{ID: %s, Service: %s, Status: %s}" , sh .ID , sh .ServiceName , sh .Status )
103+ }
0 commit comments