Initial commit

main v0.0.1
René Jochum 2 years ago
commit 5f1b334324
Signed by: jochum
GPG Key ID: F7D906F5E51E8E5E

10
.gitignore vendored

@ -0,0 +1,10 @@
.env
.DS_STORE
.task/
!.gitkeep
go.work
go.work.sum

@ -0,0 +1,13 @@
Copyright 2022 René Jochum
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

@ -0,0 +1,14 @@
# Logrus component
use with [jo-micro/components](https://jochum.dev/jo-micro/components)
## Authors
- René Jochum - rene@jochum.dev
## License
Its dual licensed:
- Apache-2.0
- GPL-2.0-or-later

156
bun.go

@ -0,0 +1,156 @@
package buncomponent
import (
"context"
"database/sql"
"fmt"
"strings"
"github.com/golang-migrate/migrate/v4"
migratePostgres "github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
pgxLogrus "github.com/jackc/pgx-logrus"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/stdlib"
"github.com/jackc/pgx/v5/tracelog"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/extra/bundebug"
"go-micro.dev/v4/errors"
"go-micro.dev/v4/server"
"github.com/urfave/cli/v2"
"jochum.dev/jo-micro/components"
"jochum.dev/jo-micro/logruscomponent"
)
const Name = "bun"
type BunComponent struct {
initialized bool
sqlDB *sql.DB
bun *bun.DB
}
func New() *BunComponent {
return &BunComponent{initialized: false}
}
func Must(ctx context.Context) *BunComponent {
return components.Must(ctx).Must(Name).(*BunComponent)
}
func MustReg(cReg *components.Registry) *BunComponent {
return cReg.Must(Name).(*BunComponent)
}
func (c *BunComponent) Name() string {
return Name
}
func (c *BunComponent) Priority() int {
return 10
}
func (c *BunComponent) Initialized() bool {
return c.initialized
}
func (c *BunComponent) Init(r *components.Registry, cli *cli.Context) error {
if c.initialized {
return nil
}
if cli.String(fmt.Sprintf("%s_database_url", strings.ToLower(r.FlagPrefix()))) == "" {
return errors.InternalServerError("DATABASE_URL_NOT_GIVEN", "%s_DATABASE_URL is required", strings.ToUpper(r.FlagPrefix()))
} else if strings.HasPrefix(cli.String("auth2_database_url"), "postgres://") {
config, err := pgx.ParseConfig(cli.String(fmt.Sprintf("%s_database_url", strings.ToLower(r.FlagPrefix()))))
if err != nil {
return err
}
if logruscomponent.MustReg(r).Initialized() {
config.Tracer = &tracelog.TraceLog{Logger: pgxLogrus.NewLogger(logruscomponent.MustReg(r).Logger()), LogLevel: tracelog.LogLevelInfo}
}
connStr := stdlib.RegisterConnConfig(config)
c.sqlDB, _ = sql.Open("pgx", connStr)
driver, err := migratePostgres.WithInstance(c.sqlDB, &migratePostgres.Config{MigrationsTable: cli.String(fmt.Sprintf("%s_migrations_table", strings.ToLower(r.FlagPrefix())))})
if err != nil {
return err
}
m, err := migrate.NewWithDatabaseInstance(
fmt.Sprintf("file://%s/postgres", cli.String(fmt.Sprintf("%s_migrations_dir", strings.ToLower(r.FlagPrefix())))), "postgres", driver)
if err != nil {
return errors.InternalServerError("internal/ibun.Start|migrate.NewWithDatabaseInstance", fmt.Sprintf("%s", err))
}
if err := m.Up(); err != migrate.ErrNoChange && err != nil {
return errors.InternalServerError("internal/ibun.Start|migrate.Up", fmt.Sprintf("%s", err))
}
c.bun = bun.NewDB(c.sqlDB, pgdialect.New())
if c.bun == nil {
return errors.InternalServerError("internal/ibun.Start|bun.NewDB", "failed to create bun")
}
if cli.Bool(fmt.Sprintf("%s_database_debug", strings.ToLower(r.FlagPrefix()))) {
// Print all queries to stdout.
c.bun.AddQueryHook(bundebug.NewQueryHook(bundebug.WithVerbose(true)))
}
} else {
return errors.InternalServerError("internal/ibun.Start|sqltype", "unknown MICRO_AUTH2_DATABASE_URL type")
}
c.initialized = true
return nil
}
func (c *BunComponent) Stop() error {
c.initialized = false
return nil
}
func (c *BunComponent) Flags(r *components.Registry) []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: fmt.Sprintf("%s_database_url", strings.ToLower(r.FlagPrefix())),
Usage: "bun Database URL",
EnvVars: []string{fmt.Sprintf("%s_DATABASE_URL", strings.ToUpper(r.FlagPrefix()))},
},
&cli.BoolFlag{
Name: fmt.Sprintf("%s_database_debug", strings.ToLower(r.FlagPrefix())),
Usage: "Set it to the debug the database queries",
EnvVars: []string{fmt.Sprintf("%s_DATABASE_DEBUG", strings.ToUpper(r.FlagPrefix()))},
DefaultText: "false",
Value: false,
},
&cli.StringFlag{
Name: fmt.Sprintf("%s_migrations_table", strings.ToLower(r.FlagPrefix())),
Value: "schema_migrations",
Usage: "Table to store migrations info",
EnvVars: []string{fmt.Sprintf("%s_MIGRATIONS_TABLE", strings.ToUpper(r.FlagPrefix()))},
},
&cli.StringFlag{
Name: fmt.Sprintf("%s_migrations_dir", strings.ToLower(r.FlagPrefix())),
Value: "/migrations",
Usage: "Folder which contains migrations",
EnvVars: []string{fmt.Sprintf("%s_MIGRATIONS_DIR", strings.ToUpper(r.FlagPrefix()))},
},
}
}
func (c *BunComponent) Health(context context.Context) error {
return nil
}
func (c *BunComponent) WrapHandlerFunc(ctx context.Context, req server.Request, rsp interface{}) error {
return nil
}
func (c *BunComponent) Bun() *bun.DB {
return c.bun
}

@ -0,0 +1,71 @@
module jochum.dev/jo-micro/buncomponent
go 1.19
require (
github.com/golang-migrate/migrate/v4 v4.15.2
github.com/jackc/pgx-logrus v0.0.0-20220919124836-b099d8ce75da
github.com/jackc/pgx/v5 v5.0.1
github.com/uptrace/bun v1.1.8
github.com/uptrace/bun/dialect/pgdialect v1.1.8
github.com/uptrace/bun/extra/bundebug v1.1.8
github.com/urfave/cli/v2 v2.16.3
go-micro.dev/v4 v4.8.1
jochum.dev/jo-micro/components v0.1.0
)
require (
github.com/Microsoft/go-winio v0.6.0 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20220824120805-4b6e5c587895 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/bitly/go-simplejson v0.5.0 // indirect
github.com/cloudflare/circl v1.2.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.3.1 // indirect
github.com/go-git/go-git/v5 v5.4.2 // indirect
github.com/go-micro/plugins/v4/logger/logrus v1.1.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/lib/pq v1.10.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/miekg/dns v1.1.50 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sergi/go-diff v1.2.0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/xanzy/ssh-agent v0.3.2 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
go.uber.org/atomic v1.7.0 // indirect
golang.org/x/crypto v0.0.0-20220924013350-4ba4fb4dd9e7 // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/net v0.0.0-20220923203811-8be639271d50 // indirect
golang.org/x/sync v0.0.0-20220923202941-7f9b1623fab7 // indirect
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.12 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
jochum.dev/jo-micro/logruscomponent v0.0.2 // indirect
)

1966
go.sum

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save