Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Create database connection and scaffold server command #15

Merged
merged 2 commits into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"log/slog"
"os"
"path"
"time"

"github.com/glass-cms/glasscms/item"
"github.com/glass-cms/glasscms/parser"
Expand Down Expand Up @@ -60,9 +59,10 @@ type ConvertCommandOptions struct {
func NewConvertCommand() *ConvertCommand {
c := &ConvertCommand{
logger: slog.New(
// TODO: Make handler type configurable.
tint.NewHandler(os.Stdout, &tint.Options{
Level: slog.LevelDebug,
TimeFormat: time.TimeOnly,
// TODO: Make configurable.
Level: slog.LevelDebug,
}),
),
opts: ConvertCommandOptions{},
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func init() {
//
rootCmd.AddCommand(NewConvertCommand().Command)
rootCmd.AddCommand(NewDocsCommand().Command)
rootCmd.AddCommand(NewServerCommand().Command)

//
// Register flags.
Expand Down
42 changes: 42 additions & 0 deletions cmd/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cmd

import (
"log/slog"
"os"

"github.com/lmittmann/tint"
"github.com/spf13/cobra"
)

type ServerCommand struct {
Command *cobra.Command

logger *slog.Logger
}

// NewServerCommand creates a new cobra.Command for
// starting the CMS server.
func NewServerCommand() *ServerCommand {
sc := &ServerCommand{
logger: slog.New(
// TODO: Make handler type configurable.
tint.NewHandler(os.Stdout, &tint.Options{
// TODO: Make configurable.
Level: slog.LevelDebug,
}),
),
}

sc.Command = &cobra.Command{
Use: "server",
Short: "Start the cms server",
RunE: sc.Execute,
}

return sc
}

func (c *ServerCommand) Execute(_ *cobra.Command, _ []string) error {
c.logger.Info("Starting server")
return nil
}
73 changes: 73 additions & 0 deletions database/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package database

import (
"database/sql"
"fmt"
)

type Driver int32

const (
DriverUnrecognized Driver = -1
DriverUnspecified Driver = iota
DriverPostgres
DriverSqlite

MaxConnectionsDefault = 5
MaxIdleConnectionsDefault = 1
)

var (
DriverName = map[int32]string{
int32(DriverUnspecified): "unspecified",
int32(DriverPostgres): "postgres",
int32(DriverSqlite): "sqlite3",
}
DriverValue = map[string]int32{
"unspecified": int32(DriverUnspecified),
"postgres": int32(DriverPostgres),
"sqlite3": int32(DriverSqlite),
}
)

// Config represents the configuration for a database connection.
type Config struct {
// Driver is the name of the database driver.
Driver string

// DSN is the Data Source Name. It specifies the username, password, and database name
// that are used to connect to the database.
DSN string

// MaxConnection is the maximum number of connections that can be opened to the database.
MaxConnections int

// MaxIdleConnections is the maximum number of idle connections that can be maintained.
// Idle connections are connections that are open but not in use.
MaxIdleConnections int
}

// NewConnection creates a new database connection using the provided configuration.
// It returns a pointer to the sql.DB object and an error, if any occurred during the connection process.
// The sql.DB object represents a pool of zero or more underlying connections.
// It's safe for concurrent use by multiple goroutines.
func NewConnection(cfg Config) (*sql.DB, error) {
db, err := sql.Open(cfg.Driver, cfg.DSN)
if err != nil {
return nil, fmt.Errorf("failed to open database connection: %w", err)
}

if cfg.MaxConnections <= 0 {
db.SetMaxOpenConns(MaxConnectionsDefault)
} else {
db.SetMaxOpenConns(cfg.MaxConnections)
}

if cfg.MaxIdleConnections <= 0 {
db.SetMaxIdleConns(MaxIdleConnectionsDefault)
} else {
db.SetMaxIdleConns(cfg.MaxIdleConnections)
}

return db, err
}
10 changes: 10 additions & 0 deletions database/migrations/1_create_items.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE items (
id INTEGER PRIMARY KEY AUTOINCREMENT,
create_time TIMESTAMP NOT NULL,
update_time TIMESTAMP NOT NULL,
hash TEXT,
name TEXT NOT NULL,
path TEXT NOT NULL,
content TEXT,
properties JSON
);
Loading