mirror of
https://github.com/lorsanstand/HomeOps-Hub.git
synced 2026-06-19 17:45:17 +03:00
refactor: change structure project
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/ilyakaznacheev/cleanenv"
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
DBHost string `env:"DB_HOST"`
|
||||
DBPort int `env:"DB_PORT"`
|
||||
DBPassword string `env:"DB_PASS"`
|
||||
DBUser string `env:"DB_USER"`
|
||||
DBName string `env:"DB_NAME"`
|
||||
LogLevel string `env:"LOG_LEVEL" env-default:"INFO"`
|
||||
Mode string `env:"MODE" env-default:"DEV"`
|
||||
Port int `env:"PORT" env-default:"9000"`
|
||||
}
|
||||
|
||||
func NewConfig() (*Config, error) {
|
||||
var cfg Config
|
||||
|
||||
if err := cleanenv.ReadConfig(".env", &cfg); err != nil {
|
||||
log.Printf("failed read config: %v", err)
|
||||
|
||||
if err = cleanenv.ReadEnv(&cfg); err != nil {
|
||||
return nil, fmt.Errorf("env read failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
func (c *Config) GetURLPostgres() string {
|
||||
return fmt.Sprintf(
|
||||
"postgres://%v:%v@%v:%v/%v?sslmode=disable",
|
||||
c.DBUser,
|
||||
c.DBPassword,
|
||||
c.DBHost,
|
||||
c.DBPort,
|
||||
c.DBName)
|
||||
}
|
||||
|
||||
func (c *Config) GetLogLevel() zerolog.Level {
|
||||
level, err := zerolog.ParseLevel(c.LogLevel)
|
||||
if err != nil {
|
||||
return zerolog.InfoLevel
|
||||
}
|
||||
return level
|
||||
}
|
||||
|
||||
func (c *Config) GetMode() string {
|
||||
return c.Mode
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
func TestConfig_GetLogLevel(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
cfg Config
|
||||
wantLogLevel zerolog.Level
|
||||
}{
|
||||
{
|
||||
name: "success",
|
||||
cfg: Config{LogLevel: "DEBUG"},
|
||||
wantLogLevel: zerolog.DebugLevel,
|
||||
},
|
||||
{
|
||||
name: "failed parse",
|
||||
cfg: Config{LogLevel: "TEST"},
|
||||
wantLogLevel: zerolog.InfoLevel,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
logLevel := tt.cfg.GetLogLevel()
|
||||
|
||||
if logLevel != tt.wantLogLevel {
|
||||
t.Fatalf("expected %v, got: %v", tt.wantLogLevel, logLevel)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfig_GetURLPostgres(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
cfg Config
|
||||
wantURLPostgres string
|
||||
}{
|
||||
{
|
||||
name: "success",
|
||||
cfg: Config{
|
||||
DBHost: "TestHost",
|
||||
DBName: "TestName",
|
||||
DBPassword: "TestPassword",
|
||||
DBPort: 1234,
|
||||
DBUser: "TestUser",
|
||||
},
|
||||
wantURLPostgres: "postgres://TestUser:TestPassword@TestHost:1234/TestName?sslmode=disable",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
url := tt.cfg.GetURLPostgres()
|
||||
|
||||
if url != tt.wantURLPostgres {
|
||||
t.Fatalf("expected %v, got: %v", tt.wantURLPostgres, url)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package domain
|
||||
|
||||
type RegisterAgentRequest struct {
|
||||
AgentId string
|
||||
AgentName string
|
||||
AgentVersion string
|
||||
Host HostInfo
|
||||
Capabilities []Capability
|
||||
}
|
||||
|
||||
type HostInfo struct {
|
||||
System string
|
||||
Hostname string
|
||||
Arch string
|
||||
}
|
||||
|
||||
type Capability struct {
|
||||
Available bool
|
||||
Version string
|
||||
Name string
|
||||
Reason string
|
||||
}
|
||||
|
||||
type RegisterAgentResponse struct {
|
||||
Heartbeat int
|
||||
AgentID string
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
pb "github.com/lorsanstand/HomeOps-Hub/api/gen/homeops"
|
||||
)
|
||||
|
||||
func ToDomainAgentRequest(request *pb.RegisterAgentRequest) RegisterAgentRequest {
|
||||
if request == nil {
|
||||
return RegisterAgentRequest{}
|
||||
}
|
||||
|
||||
return RegisterAgentRequest{
|
||||
AgentId: request.AgentId,
|
||||
AgentName: request.AgentName,
|
||||
Host: HostInfo{
|
||||
System: request.Host.System,
|
||||
Hostname: request.Host.Hostname,
|
||||
Arch: request.Host.Arch,
|
||||
},
|
||||
Capabilities: ToDomainCapabilities(request.Capability),
|
||||
}
|
||||
}
|
||||
|
||||
func ToDomainAgentResponse(response *pb.RegisterAgentResponse) RegisterAgentResponse {
|
||||
if response == nil {
|
||||
return RegisterAgentResponse{}
|
||||
}
|
||||
|
||||
return RegisterAgentResponse{
|
||||
AgentID: response.AgentId,
|
||||
Heartbeat: int(response.HeartbeatIntervalSecond),
|
||||
}
|
||||
}
|
||||
|
||||
func ToDomainCapabilities(capability []*pb.Capability) []Capability {
|
||||
var caps []Capability
|
||||
|
||||
for _, capa := range capability {
|
||||
if capa == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
caps = append(caps, Capability{
|
||||
Name: capa.Name,
|
||||
Version: capa.Version,
|
||||
Reason: capa.Reason,
|
||||
Available: capa.Available,
|
||||
})
|
||||
}
|
||||
|
||||
return caps
|
||||
}
|
||||
|
||||
func ToGRPCAgentRequest(request RegisterAgentRequest) pb.RegisterAgentRequest {
|
||||
return pb.RegisterAgentRequest{
|
||||
AgentId: request.AgentId,
|
||||
AgentName: request.AgentName,
|
||||
Host: &pb.HostInfo{
|
||||
Hostname: request.Host.Hostname,
|
||||
Arch: request.Host.Arch,
|
||||
System: request.Host.System,
|
||||
},
|
||||
Version: request.AgentVersion,
|
||||
Capability: ToGRPCCapability(request.Capabilities),
|
||||
}
|
||||
}
|
||||
|
||||
func ToGRPCAgentResponse(response RegisterAgentResponse) *pb.RegisterAgentResponse {
|
||||
return &pb.RegisterAgentResponse{AgentId: response.AgentID, HeartbeatIntervalSecond: int64(response.Heartbeat)}
|
||||
}
|
||||
|
||||
func ToGRPCCapability(caps []Capability) []*pb.Capability {
|
||||
var capability []*pb.Capability
|
||||
for _, capi := range caps {
|
||||
capability = append(capability, &pb.Capability{
|
||||
Name: capi.Name,
|
||||
Available: capi.Available,
|
||||
Version: capi.Version,
|
||||
Reason: capi.Reason,
|
||||
})
|
||||
}
|
||||
return capability
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
type cfgLogStore interface {
|
||||
GetLogLevel() zerolog.Level
|
||||
GetMode() string
|
||||
}
|
||||
|
||||
func NewLogger(cfg cfgLogStore) zerolog.Logger {
|
||||
var output io.Writer = os.Stdout
|
||||
|
||||
if cfg.GetMode() != "PROD" {
|
||||
output = zerolog.ConsoleWriter{
|
||||
Out: os.Stdout,
|
||||
TimeFormat: time.Kitchen,
|
||||
}
|
||||
}
|
||||
|
||||
level := cfg.GetLogLevel()
|
||||
|
||||
return zerolog.New(output).Level(level).With().Timestamp().Logger()
|
||||
}
|
||||
Reference in New Issue
Block a user