- Add bootstrap package to initialize application components including logger, tracer, and HTTP server. - Create config package to load runtime settings from environment variables. - Implement observability features including logging, metrics, and tracing. - Add health check and hello use cases with corresponding HTTP handlers. - Introduce middleware for request ID, access logging, metrics, and recovery. - Set up HTTP router with defined routes for health and hello endpoints. - Include tests for health and hello endpoints to ensure proper functionality. - Add OpenTelemetry collector configuration for trace exporting.
44 lines
872 B
Go
44 lines
872 B
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// Config contains all runtime settings sourced from environment variables.
|
|
type Config struct {
|
|
Port string
|
|
ServiceName string
|
|
Environment string
|
|
LogLevel string
|
|
OTLPEndpoint string
|
|
}
|
|
|
|
func Load() Config {
|
|
return Config{
|
|
Port: getEnv("PORT", "8080"),
|
|
ServiceName: getEnv("SERVICE_NAME", "switchbot-api"),
|
|
Environment: getEnv("ENV", "local"),
|
|
LogLevel: getEnv("LOG_LEVEL", "info"),
|
|
OTLPEndpoint: strings.TrimSpace(os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT")),
|
|
}
|
|
}
|
|
|
|
func (c Config) Addr() string {
|
|
if strings.HasPrefix(c.Port, ":") {
|
|
return c.Port
|
|
}
|
|
return fmt.Sprintf(":%s", c.Port)
|
|
}
|
|
|
|
func getEnv(key, fallback string) string {
|
|
if value, ok := os.LookupEnv(key); ok {
|
|
trimmed := strings.TrimSpace(value)
|
|
if trimmed != "" {
|
|
return trimmed
|
|
}
|
|
}
|
|
return fallback
|
|
}
|