Skip to content

Commit

Permalink
refactor env
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobnguyenn committed Sep 10, 2023
1 parent 677a6d6 commit e8c2bb3
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 35 deletions.
14 changes: 8 additions & 6 deletions cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/nduyphuong/gorya/internal/api"
"github.com/nduyphuong/gorya/internal/api/config"
"github.com/nduyphuong/gorya/internal/constants"
"github.com/nduyphuong/gorya/internal/os"
queueOptions "github.com/nduyphuong/gorya/internal/queue/options"
"github.com/nduyphuong/gorya/internal/types"
Expand Down Expand Up @@ -38,14 +39,15 @@ func newServerCommand() *cobra.Command {
errCh := make(chan error, 2)
taskProcessor := worker.NewClient(worker.Options{
QueueOpts: queueOptions.Options{
Name: os.GetEnv("GORYA_QUEUE_NAME", "gorya"),
Addr: os.GetEnv("GORYA_REDIS_ADDR", "localhost:6379"),
Name: os.GetEnv(constants.ENV_GORYA_QUEUE_NAME, "gorya"),
Addr: os.GetEnv(constants.ENV_GORYA_REDIS_ADDR, "localhost:6379"),
//check in queue every 5 seconds
PopInterval: 5 * time.Second,
},
})
ticker := time.NewTicker(2 * time.Second)
numWorkers := types.MustParseInt(os.GetEnv("GORYA_NUM_WORKER", "2"))
numWorkers := types.MustParseInt(os.GetEnv(
constants.ENV_GORYA_NUM_WORKER, "2"))
for i := 0; i <= numWorkers; i++ {
// dispatch item to the queue
go func(stop <-chan struct{}) {
Expand All @@ -54,7 +56,7 @@ func newServerCommand() *cobra.Command {
case <-stop:
return
case <-ticker.C:
requestURL := fmt.Sprintf("http://localhost:%d%s", types.MustParseInt(os.GetEnv("PORT",
requestURL := fmt.Sprintf("http://localhost:%d%s", types.MustParseInt(os.GetEnv(constants.ENV_GORYA_API_PORT,
"8080")), v1alpha1.GoryaTaskScheduleProcedure)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestURL, nil)
if err != nil {
Expand Down Expand Up @@ -90,8 +92,8 @@ func newServerCommand() *cobra.Command {
"tcp",
fmt.Sprintf(
"%s:%s",
os.GetEnv("HOST", "0.0.0.0"),
os.GetEnv("PORT", "8080"),
os.GetEnv(constants.ENV_GORYA_API_HOST, "0.0.0.0"),
os.GetEnv(constants.ENV_GORYA_API_PORT, "8080"),
),
)
if err != nil {
Expand Down
9 changes: 5 additions & 4 deletions cmd/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"time"

"github.com/nduyphuong/gorya/internal/constants"
"github.com/nduyphuong/gorya/internal/os"
queueOptions "github.com/nduyphuong/gorya/internal/queue/options"
"github.com/nduyphuong/gorya/internal/types"
Expand All @@ -30,13 +31,13 @@ func newWorkCommand() *cobra.Command {
taskProcessResultChan := make(chan string)
taskProcessor := worker.NewClient(worker.Options{
QueueOpts: queueOptions.Options{
Addr: os.GetEnv("GORYA_REDIS_ADDR", "localhost:6379"),
Name: os.GetEnv("GORYA_QUEUE_NAME", "gorya"),
Addr: os.GetEnv(constants.ENV_GORYA_REDIS_ADDR, "localhost:6379"),
Name: os.GetEnv(constants.ENV_GORYA_QUEUE_NAME, "gorya"),
PopInterval: 2 * time.Second,
},
})

numWorkers := types.MustParseInt(os.GetEnv("GORYA_NUM_WORKER", "1"))
numWorkers := types.MustParseInt(os.GetEnv(constants.ENV_GORYA_NUM_WORKER, "1"))
for i := 0; i < numWorkers; i++ {
go taskProcessor.Process(ctx, ctx.Done(), errCh)
}
Expand All @@ -54,7 +55,7 @@ func newWorkCommand() *cobra.Command {
CredentialRef: elem.CredentialRef,
Provider: elem.Project,
}
requestURL := fmt.Sprintf("http://localhost:%d%s", types.MustParseInt(os.GetEnv("PORT",
requestURL := fmt.Sprintf("http://localhost:%d%s", types.MustParseInt(os.GetEnv(constants.ENV_GORYA_API_PORT,
"8080")), v1alpha1.GoryaTaskChangeStageProcedure)
b, err := json.Marshal(changeStateRequest)
if err != nil {
Expand Down
17 changes: 9 additions & 8 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package api

import (
"context"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armsubscriptions"
"github.com/nduyphuong/gorya/pkg/azure"
"net"
"net/http"
"strings"
"sync"
"time"

"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armsubscriptions"
"github.com/nduyphuong/gorya/pkg/azure"

"github.com/nduyphuong/gorya/internal/api/config"
"github.com/nduyphuong/gorya/internal/api/handler"
"github.com/nduyphuong/gorya/internal/constants"
Expand Down Expand Up @@ -73,7 +74,7 @@ func (s *server) Serve(ctx context.Context, l net.Listener) error {
constants.Default: true,
},
}
providers := strings.Split(os.GetEnv("GORYA_ENABLED_PROVIDERS", ""), ",")
providers := strings.Split(os.GetEnv(constants.ENV_GORYA_ENABLED_PROVIDERS, ""), ",")
ticker := time.NewTicker(30 * time.Second)
for _, provider := range providers {
provider = strings.ToLower(provider)
Expand All @@ -99,8 +100,8 @@ func (s *server) Serve(ctx context.Context, l net.Listener) error {
ctx,
c.credentialRef,
//currently we only support multi account in 1 region
awsOptions.WithRegion(os.GetEnv("AWS_REGION", "ap-southeast-1")),
awsOptions.WithEndpoint(os.GetEnv("AWS_ENDPOINT", "")),
awsOptions.WithRegion(os.GetEnv(constants.ENV_AWS_REGION, "ap-southeast-1")),
awsOptions.WithEndpoint(os.GetEnv(constants.ENV_AWS_ENDPOINT, "")),
)
if err != nil {
log.Errorf("update aws client pool %v", err)
Expand Down Expand Up @@ -210,8 +211,8 @@ func (s *server) Serve(ctx context.Context, l net.Listener) error {

s.taskProcessor = worker.NewClient(worker.Options{
QueueOpts: queueOptions.Options{
Addr: os.GetEnv("GORYA_REDIS_ADDR", "localhost:6379"),
Name: os.GetEnv("GORYA_QUEUE_NAME", "gorya"),
Addr: os.GetEnv(constants.ENV_GORYA_REDIS_ADDR, "localhost:6379"),
Name: os.GetEnv(constants.ENV_GORYA_QUEUE_NAME, "gorya"),
PopInterval: 2 * time.Second,
},
})
Expand Down
10 changes: 6 additions & 4 deletions internal/api/middleware/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package middleware
import (
"context"
"crypto/tls"
"github.com/coreos/go-oidc"
"github.com/nduyphuong/gorya/internal/os"
"net/http"
"time"

"github.com/coreos/go-oidc"
"github.com/nduyphuong/gorya/internal/constants"
"github.com/nduyphuong/gorya/internal/os"
)

// Claims claims component of jwt contains many fields , we need only roles of GoryaServiceClient
Expand All @@ -25,8 +27,8 @@ type clientRoles struct {
}

var (
issuerUrl = os.GetEnv("GORYA_OIDC_ISSUER_URL", "http://localhost:8080/auth/realms/demorealm")
clientID string = os.GetEnv("GORYA_OIDC_CLIENT_ID",
issuerUrl = os.GetEnv(constants.ENV_GORYA_OIDC_ISSUER_URL, "http://localhost:8080/auth/realms/demorealm")
clientID string = os.GetEnv(constants.ENV_GORYA_OIDC_CLIENT_ID,
"GoryaServiceClient")
)

Expand Down
24 changes: 20 additions & 4 deletions internal/constants/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,24 @@ package constants
const (
OffStatus int = iota
OnStatus
PROVIDER_AWS = "aws"
PROVIDER_GCP = "gcp"
PROVIDER_AZURE = "azure"
Default = ""
PROVIDER_AWS = "aws"
PROVIDER_GCP = "gcp"
PROVIDER_AZURE = "azure"
Default = ""
ENV_GORYA_QUEUE_NAME = "GORYA_QUEUE_NAME"
ENV_GORYA_REDIS_ADDR = "GORYA_REDIS_ADDR"
ENV_GORYA_NUM_WORKER = "GORYA_NUM_WORKER"
ENV_GORYA_API_PORT = "PORT"
ENV_GORYA_API_HOST = "HOST"
ENV_GORYA_ENABLED_PROVIDERS = "GORYA_ENABLED_PROVIDERS"
ENV_AWS_REGION = "AWS_REGION"
ENV_AWS_ENDPOINT = "AWS_ENDPOINT"
ENV_GORYA_OIDC_ISSUER_URL = "GORYA_OIDC_ISSUER_URL"
ENV_GORYA_OIDC_CLIENT_ID = "GORYA_OIDC_CLIENT_ID"
ENV_LOG_LEVEL = "LOG_LEVEL"
ENV_GORYA_DB_TYPE = "GORYA_DB_TYPE"
ENV_GORYA_DB_HOST = "GORYA_DB_HOST"
ENV_GORYA_DB_USER = "GORYA_DB_USER"
ENV_GORYA_DB_PASSWORD = "GORYA_DB_PASSWORD"
ENV_GORYA_DB_NAME = "GORYA_DB_NAME"
)
3 changes: 2 additions & 1 deletion internal/logging/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

log "github.com/sirupsen/logrus"

"github.com/nduyphuong/gorya/internal/constants"
"github.com/nduyphuong/gorya/internal/os"
)

Expand All @@ -17,7 +18,7 @@ var globalLogger *log.Entry

func init() {
globalLogger = log.New().WithFields(nil)
level, err := log.ParseLevel(os.GetEnv("LOG_LEVEL", "INFO"))
level, err := log.ParseLevel(os.GetEnv(constants.ENV_LOG_LEVEL, "INFO"))
if err != nil {
panic(err)
}
Expand Down
11 changes: 6 additions & 5 deletions internal/store/store_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"gorm.io/gorm/clause"

"github.com/nduyphuong/gorya/internal/constants"
"github.com/nduyphuong/gorya/internal/models"
"github.com/nduyphuong/gorya/internal/os"
"gorm.io/gorm"
Expand All @@ -25,11 +26,11 @@ func GetOnce() (Interface, error) {
if modelStorage != nil {
return modelStorage, nil
}
dbType := os.GetEnv("GORYA_DB_TYPE", "sqlite")
host := os.GetEnv("GORYA_DB_HOST", "localhost:3306")
user := os.GetEnv("GORYA_DB_USER", "root")
password := os.GetEnv("GORYA_DB_PASSWORD", "root")
dbName := os.GetEnv("GORYA_DB_NAME", "gorya")
dbType := os.GetEnv(constants.ENV_GORYA_DB_TYPE, "sqlite")
host := os.GetEnv(constants.ENV_GORYA_DB_HOST, "localhost:3306")
user := os.GetEnv(constants.ENV_GORYA_DB_USER, "root")
password := os.GetEnv(constants.ENV_GORYA_DB_PASSWORD, "root")
dbName := os.GetEnv(constants.ENV_GORYA_DB_NAME, "gorya")
switch dbType {
case "mysql":
db, err := NewMySQLDB(host, user, password, dbName)
Expand Down
3 changes: 2 additions & 1 deletion internal/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"net/http"

"github.com/nduyphuong/gorya/internal/constants"
"github.com/nduyphuong/gorya/internal/logging"
"github.com/nduyphuong/gorya/internal/os"
"github.com/nduyphuong/gorya/internal/queue"
Expand Down Expand Up @@ -89,7 +90,7 @@ func (c *client) Process(ctx context.Context, stop <-chan struct{}, errChan chan
CredentialRef: elem.CredentialRef,
Provider: elem.Provider,
}
requestURL := fmt.Sprintf("http://localhost:%d%s", types.MustParseInt(os.GetEnv("PORT",
requestURL := fmt.Sprintf("http://localhost:%d%s", types.MustParseInt(os.GetEnv(constants.ENV_GORYA_API_PORT,
"8080")), v1alpha1.GoryaTaskChangeStageProcedure)
b, err := json.Marshal(changeStateRequest)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions pkg/aws/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import (
"context"
"testing"

"github.com/nduyphuong/gorya/internal/constants"
"github.com/nduyphuong/gorya/internal/os"
awsOptions "github.com/nduyphuong/gorya/pkg/aws/options"
"github.com/stretchr/testify/assert"
)

func TestSmoke(t *testing.T) {
ctx := context.TODO()
awsRegion := os.GetEnv("AWS_REGION", "ap-southeast-1")
awsEndpoint := os.GetEnv("AWS_ENDPOINT", "")
awsRegion := os.GetEnv(constants.ENV_AWS_REGION, "ap-southeast-1")
awsEndpoint := os.GetEnv(constants.ENV_AWS_ENDPOINT, "")
c, err := new(ctx,
awsOptions.WithRegion(awsRegion),
awsOptions.WithEndpoint(awsEndpoint),
Expand Down

0 comments on commit e8c2bb3

Please sign in to comment.