-
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.
refactor: use kong for parsing CLI flags and arguments (#13)
- Loading branch information
Showing
17 changed files
with
680 additions
and
304 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
LISTEN=":4040" | ||
DATABASE_DSN=postgresql://postgres:docker@localhost:5432/snippets?sslmode=disable | ||
HTTP_LISTEN=":4040" | ||
POSTGRES_DSN=postgresql://postgres:docker@localhost:5432/snippets?sslmode=disable | ||
API_TOKEN= |
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,28 @@ | ||
package flags | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
|
||
_ "github.com/lib/pq" // import pg driver | ||
) | ||
|
||
// PostgreSQL represents PostgreSQL connection flags | ||
// and provides methods to open a connection to the database. | ||
type PostgreSQL struct { | ||
DSN string `kong:"required,group='Postgres',name=postgres-dsn,default=localhost,env='POSTGRES_DSN,DATABASE_DSN',help='Data Source Name for PostgreSQL database server.'"` | ||
} | ||
|
||
// OpenStdSQLDB opens a new connection to the PostgreSQL database | ||
// using the standard library's sql package. | ||
func (p PostgreSQL) OpenStdSQLDB() (*sql.DB, error) { | ||
db, err := sql.Open("postgres", p.DSN) | ||
if err != nil { | ||
return nil, fmt.Errorf("open PostgreSQL connection: %w", err) | ||
} | ||
|
||
if err = db.Ping(); err != nil { | ||
return nil, fmt.Errorf("ping PostgreSQL: %w", err) | ||
} | ||
return db, 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
Oops, something went wrong.