Skip to content

Commit

Permalink
Separated agent and server config packages
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkmcc committed Nov 16, 2023
1 parent efee572 commit c6d28f2
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func init() {
viper.MustBindEnv("database.connectionString", "DATABASE_CONNECTION_STRING")
}

type ServerConfig struct {
type Config struct {
Port int
Logging Logging `json:"logging"`
Auth serverAuth `json:"auth"`
Expand All @@ -37,12 +37,16 @@ type serverDatabase struct {
ConnectionString string `json:"connectionString"`
}

func NewServerConfig() (*ServerConfig, error) {
var cfg ServerConfig
type Logging struct {
Level string `json:"level"`
Debug bool `json:"debug"`
}

func New() (*Config, error) {
var cfg Config
err := viper.Unmarshal(&cfg)
if err != nil {
return nil, err
}
printStructure(cfg)
return &cfg, nil
}
4 changes: 2 additions & 2 deletions cmd/cloudcore-server/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package database
import (
"context"
"fmt"
"github.com/clarkmcc/cloudcore/cmd/cloudcore-server/config"
"github.com/clarkmcc/cloudcore/cmd/cloudcore-server/database/cockroachdb"
"github.com/clarkmcc/cloudcore/internal/config"
"github.com/clarkmcc/cloudcore/internal/rpc"
)

Expand All @@ -20,7 +20,7 @@ type Database interface {
AuthenticateAgent(ctx context.Context, psk string, md *rpc.SystemMetadata) (string, error)
}

func New(cfg *config.ServerConfig) (Database, error) {
func New(cfg *config.Config) (Database, error) {
switch cfg.Database.Type {
case config.DatabaseTypeCockroachDB:
return cockroachdb.New(cfg)
Expand Down
13 changes: 10 additions & 3 deletions internal/config/agent.go → cmd/cloudcored/config/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@ import (
"os"
)

func init() {
viper.SetDefault("server.endpoint", "127.0.0.1:10000")
viper.SetDefault("logging.level", "info")
viper.SetDefault("logging.debug", true)
viper.SetDefault("database.flavor", AgentDatabaseFlavorMemory)
}

type AgentDatabaseFlavor string

const (
AgentDatabaseFlavorMemory AgentDatabaseFlavor = "memory"
)

type AgentConfig struct {
type Config struct {
Server server
Logging Logging
Database database
Expand All @@ -33,7 +40,7 @@ type Logging struct {
Debug bool `json:"debug"`
}

func NewAgentConfig() (*AgentConfig, error) {
func New() (*Config, error) {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
if cwd, err := os.Getwd(); err == nil {
Expand All @@ -44,7 +51,7 @@ func NewAgentConfig() (*AgentConfig, error) {
return nil, err
}

var cfg AgentConfig
var cfg Config
err = viper.Unmarshal(&cfg)
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions cmd/cloudcored/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

import (
"context"
"github.com/clarkmcc/cloudcore/cmd/cloudcored/config"
"github.com/clarkmcc/cloudcore/internal/agentdb"
"github.com/clarkmcc/cloudcore/internal/client"
"github.com/clarkmcc/cloudcore/internal/config"
"github.com/clarkmcc/cloudcore/internal/logger"
"github.com/clarkmcc/cloudcore/internal/tasks"
_ "github.com/clarkmcc/cloudcore/internal/tasks/registered"
Expand All @@ -23,9 +23,9 @@ var cmd = &cobra.Command{
ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
return tomb.WithContext(ctx)
}),
fx.Provide(config.NewAgentConfig),
fx.Provide(config.New),
// Extra the logging config from the Agent-specific config
fx.Provide(func(config *config.AgentConfig) *config.Logging {
fx.Provide(func(config *config.Config) *config.Logging {
return &config.Logging
}),
fx.Provide(logger.New),
Expand Down
4 changes: 2 additions & 2 deletions internal/agentdb/agentdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"errors"
"fmt"
"github.com/clarkmcc/cloudcore/internal/config"
"github.com/clarkmcc/cloudcore/cmd/cloudcored/config"
)

var (
Expand All @@ -22,7 +22,7 @@ type AgentDB interface {
SaveAgentID(ctx context.Context, agentID string) error
}

func New(cfg *config.AgentConfig) (AgentDB, error) {
func New(cfg *config.Config) (AgentDB, error) {
switch cfg.Database.Flavor {
case config.AgentDatabaseFlavorMemory:
return newMemoryDB()
Expand Down
4 changes: 2 additions & 2 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ package client

import (
"context"
"github.com/clarkmcc/cloudcore/cmd/cloudcored/config"
"github.com/clarkmcc/cloudcore/internal/agentdb"
"github.com/clarkmcc/cloudcore/internal/config"
"github.com/clarkmcc/cloudcore/internal/rpc"
"go.uber.org/zap"
"google.golang.org/grpc"
Expand Down Expand Up @@ -119,7 +119,7 @@ func (c *Client) setupClientsLocked(ctx context.Context) (err error) {
return nil
}

func New(config *config.AgentConfig, db agentdb.AgentDB, logger *zap.Logger) *Client {
func New(config *config.Config, db agentdb.AgentDB, logger *zap.Logger) *Client {
c := &Client{
dialer: func(ctx context.Context) (*grpc.ClientConn, error) {
return grpc.Dial(config.Server.Endpoint, grpc.WithTransportCredentials(insecure.NewCredentials()))
Expand Down
6 changes: 3 additions & 3 deletions internal/client/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (
"context"
"errors"
"fmt"
"github.com/clarkmcc/cloudcore/cmd/cloudcored/config"
"github.com/clarkmcc/cloudcore/internal/agentdb"
"github.com/clarkmcc/cloudcore/internal/config"
"github.com/clarkmcc/cloudcore/internal/rpc"
"github.com/clarkmcc/cloudcore/internal/sysinfo"
"github.com/clarkmcc/cloudcore/internal/token"
Expand All @@ -41,7 +41,7 @@ import (
// that are used to authenticate the agent with the server.
type tokenManager struct {
logger *zap.Logger
config *config.AgentConfig
config *config.Config
db agentdb.AgentDB

client *Client
Expand Down Expand Up @@ -126,7 +126,7 @@ func (m *tokenManager) newToken(ctx context.Context, maybeAuthToken *agentdb.Aut
}, nil
}

func newTokenManager(config *config.AgentConfig, db agentdb.AgentDB, logger *zap.Logger, client *Client) *tokenManager {
func newTokenManager(config *config.Config, db agentdb.AgentDB, logger *zap.Logger, client *Client) *tokenManager {
return &tokenManager{
logger: logger.Named("token-manager"),
config: config,
Expand Down
10 changes: 0 additions & 10 deletions internal/config/defaults.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package logger

import (
"github.com/clarkmcc/cloudcore/internal/config"
"github.com/clarkmcc/cloudcore/cmd/cloudcored/config"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
Expand Down
4 changes: 2 additions & 2 deletions internal/token/signer.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package token

import (
"github.com/clarkmcc/cloudcore/internal/config"
"github.com/clarkmcc/cloudcore/cmd/cloudcore-server/config"
"github.com/golang-jwt/jwt/v5"
"time"
)
Expand All @@ -25,7 +25,7 @@ func (s *Signer) ValidateToken(token string) error {
return err
}

func NewSigner(config *config.ServerConfig) *Signer {
func NewSigner(config *config.Config) *Signer {
return &Signer{
secret: []byte(config.Auth.TokenSigningSecret),
}
Expand Down

0 comments on commit c6d28f2

Please sign in to comment.