This repository has been archived by the owner on Apr 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use uber fx and central logger instance
Clean up prototype code. Signed-off-by: Alexander Trost <[email protected]>
- Loading branch information
Showing
53 changed files
with
1,195 additions
and
529 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,9 @@ | |
*.iml | ||
*.idea | ||
|
||
.env | ||
config.yaml | ||
|
||
# JetBrains IDEs | ||
.idea/ | ||
postgres-data/ | ||
postgres-data/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,4 @@ test: | |
go test ./... | ||
|
||
push: | ||
docker push koorinc/genesis:$(TAG) | ||
docker push koorinc/genesis:$(TAG) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,19 @@ | ||
 | ||
|
||
# Genesis | ||
|
||
# Koor-Tech Genesis | ||
Welcome to Koor Technologies, Inc. Genesis project! | ||
|
||
Our stack: | ||
|
||
Welcome to koor Genesis | ||
|
||
Our stack | ||
- Go | ||
- RabbitMQ | ||
- Postgres | ||
- PostgreSQL | ||
|
||
In order to run local please install direnv | ||
- https://direnv.net/ | ||
and run this command in order to create your file `.envrc` from `env.template` | ||
```bash | ||
chmod +x setup.sh | ||
./setup.sh | ||
``` | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package cmd | ||
|
||
import ( | ||
"log/slog" | ||
"os" | ||
|
||
"github.com/koor-tech/genesis/cmd/migrations" | ||
"github.com/koor-tech/genesis/pkg/config" | ||
"github.com/koor-tech/genesis/pkg/database" | ||
"github.com/spf13/cobra" | ||
"go.uber.org/fx/fxtest" | ||
) | ||
|
||
var migrateCmd = &cobra.Command{ | ||
Use: "migrate", | ||
Short: "run migration commands using goose", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
var arguments []string | ||
if len(args) > 1 { | ||
arguments = append(arguments, args[1:]...) | ||
} | ||
var command string | ||
if len(args) == 0 { | ||
command = "status" | ||
|
||
} else { | ||
command = args[0] | ||
} | ||
|
||
cfg, err := config.Load() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Setup database connection for goose | ||
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{})) | ||
d, err := database.NewDB(database.Params{ | ||
LC: fxtest.NewLifecycle(nil), | ||
Logger: logger, | ||
Config: cfg.Config, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := d.Connect(database.BuilDSNUri(cfg.Config.Database)); err != nil { | ||
return err | ||
} | ||
defer d.Conn.Close() | ||
|
||
return migrations.RunMigrationCommand(command, d.Conn.DB, arguments...) | ||
}, | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(migrateCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package migrations | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"embed" | ||
"fmt" | ||
|
||
_ "github.com/lib/pq" | ||
"github.com/pressly/goose/v3" | ||
) | ||
|
||
//go:embed migrations/*.sql | ||
var embedMigrations embed.FS | ||
|
||
func RunMigrationCommand(command string, db *sql.DB, args ...string) error { | ||
goose.SetBaseFS(embedMigrations) | ||
|
||
if err := goose.RunContext(context.Background(), command, db, ".", args...); err != nil { | ||
return fmt.Errorf("goose %v: dir %s. %w", command, ".", err) | ||
} | ||
|
||
return nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package cmd | ||
|
||
import ( | ||
"log/slog" | ||
"os" | ||
"time" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/koor-tech/genesis/pkg/fxslog" | ||
"github.com/spf13/cobra" | ||
"go.uber.org/fx" | ||
"go.uber.org/fx/fxevent" | ||
|
||
"github.com/koor-tech/genesis/gateway" | ||
"github.com/koor-tech/genesis/gateway/handler" | ||
"github.com/koor-tech/genesis/internal/cluster" | ||
sshSvc "github.com/koor-tech/genesis/internal/ssh" | ||
"github.com/koor-tech/genesis/internal/worker" | ||
"github.com/koor-tech/genesis/pkg/config" | ||
"github.com/koor-tech/genesis/pkg/database" | ||
"github.com/koor-tech/genesis/pkg/notification" | ||
"github.com/koor-tech/genesis/pkg/providers/hetzner" | ||
"github.com/koor-tech/genesis/pkg/rabbitmq" | ||
clusters "github.com/koor-tech/genesis/pkg/repositories/postgres/cluster" | ||
"github.com/koor-tech/genesis/pkg/repositories/postgres/customers" | ||
"github.com/koor-tech/genesis/pkg/repositories/postgres/providers" | ||
"github.com/koor-tech/genesis/pkg/repositories/postgres/ssh" | ||
"github.com/koor-tech/genesis/pkg/repositories/postgres/state" | ||
) | ||
|
||
func getFxBaseOpts() []fx.Option { | ||
return []fx.Option{ | ||
fx.StartTimeout(90 * time.Second), | ||
fx.WithLogger(func(logger *slog.Logger) fxevent.Logger { | ||
return fxslog.New(logger) | ||
}), | ||
|
||
config.Module, | ||
LoggerModule, | ||
notification.Module, | ||
gateway.HTTPServerModule, | ||
|
||
fx.Provide( | ||
state.NewClusterStateRepository, | ||
customers.NewCustomersRepository, | ||
providers.NewProviderRepository, | ||
clusters.NewClusterRepository, | ||
ssh.NewSshRepository, | ||
|
||
sshSvc.NewService, | ||
|
||
hetzner.New, | ||
worker.NewWorker, | ||
), | ||
|
||
fx.Provide( | ||
database.NewDB, | ||
rabbitmq.NewClient, | ||
handler.NewCluster, | ||
cluster.NewService, | ||
|
||
// Add any dependencies here so they can just be injected | ||
), | ||
} | ||
} | ||
|
||
var RootCmd = &cobra.Command{ | ||
Use: "gensis", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
fxOpts := getFxBaseOpts() | ||
|
||
fxOpts = append(fxOpts, fx.Invoke(func(*gin.Engine) {})) | ||
|
||
app := fx.New(fxOpts...) | ||
app.Run() | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
var LoggerModule = fx.Module("logger", | ||
fx.Provide( | ||
NewLogger, | ||
), | ||
) | ||
|
||
func NewLogger(cfg *config.Config) (*slog.Logger, error) { | ||
return slog.New(slog.NewJSONHandler(os.Stdout, nil)), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/koor-tech/genesis/internal/worker" | ||
"github.com/spf13/cobra" | ||
"go.uber.org/fx" | ||
) | ||
|
||
var workerCmd = &cobra.Command{ | ||
Use: "monitor", | ||
Short: "Monitors the state of the each cluster", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
fxOpts := getFxBaseOpts() | ||
|
||
fxOpts = append(fxOpts, fx.Invoke(func(*worker.Worker) {})) | ||
|
||
app := fx.New(fxOpts...) | ||
app.Run() | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(workerCmd) | ||
} |
Oops, something went wrong.