Initial commit.

This commit is contained in:
2018-04-30 18:42:17 +05:00
commit 3456ecd312
511 changed files with 199388 additions and 0 deletions

43
database/database.go Normal file
View File

@@ -0,0 +1,43 @@
package database
import (
// stdlib
"fmt"
// other
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
type Database struct {
db *sqlx.DB
}
func (db *Database) GetDatabaseConnection() *sqlx.DB {
return db.db
}
func (db *Database) Initialize() {
c.Logger.Info().Msg("Initializing database connection...")
var userpass = ""
if c.Config.Database.Password == "" {
userpass = c.Config.Database.Username
} else {
userpass = c.Config.Database.Username + ":" + c.Config.Database.Password
}
dbConnString := fmt.Sprintf("%s@tcp(%s:%s)/%s?parseTime=true&collation=utf8mb4_unicode_ci&charset=utf8mb4", userpass, c.Config.Database.Address, c.Config.Database.Port, c.Config.Database.Database)
c.Logger.Debug().Msgf("Database connection string: %s", dbConnString)
dbConn, err := sqlx.Connect("mysql", dbConnString)
if err != nil {
c.Logger.Panic().Msgf("Failed to connect to database: %s", err.Error())
}
// Force UTC for current connection.
_ = dbConn.MustExec("SET @@session.time_zone='+00:00';")
c.Logger.Info().Msg("Database connection established")
db.db = dbConn
}

18
database/exported.go Normal file
View File

@@ -0,0 +1,18 @@
package database
import (
// local
"github.com/pztrn/fastpastebin/context"
"github.com/pztrn/fastpastebin/database/interface"
)
var (
c *context.Context
d *Database
)
func New(cc *context.Context) {
c = cc
d = &Database{}
c.RegisterDatabaseInterface(databaseinterface.Interface(Handler{}))
}

16
database/handler.go Normal file
View File

@@ -0,0 +1,16 @@
package database
import (
// other
"github.com/jmoiron/sqlx"
)
type Handler struct{}
func (dbh Handler) GetDatabaseConnection() *sqlx.DB {
return d.GetDatabaseConnection()
}
func (dbh Handler) Initialize() {
d.Initialize()
}

View File

@@ -0,0 +1,11 @@
package databaseinterface
import (
// other
"github.com/jmoiron/sqlx"
)
type Interface interface {
GetDatabaseConnection() *sqlx.DB
Initialize()
}

View File

@@ -0,0 +1,15 @@
package migrations
import (
// stdlib
"database/sql"
)
func InitialUp(tx *sql.Tx) error {
_, err := tx.Exec("CREATE TABLE `pastes` (`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Paste ID', `title` text NOT NULL COMMENT 'Paste title', `data` longtext NOT NULL COMMENT 'Paste data', `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Paste creation timestamp', `keep_for` int(4) NOT NULL DEFAULT 1 COMMENT 'Keep for integer. 0 - forever.', `keep_for_unit_type` int(1) NOT NULL DEFAULT 1 COMMENT 'Keep for unit type. 1 - minutes, 2 - hours, 3 - days, 4 - months.', PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='Pastes';")
if err != nil {
return err
}
return nil
}

View File

@@ -0,0 +1,31 @@
package migrations
import (
// local
"github.com/pztrn/fastpastebin/context"
// other
//"github.com/jmoiron/sqlx"
"github.com/pressly/goose"
)
var (
c *context.Context
)
func New(cc *context.Context) {
c = cc
}
func Migrate() {
c.Logger.Info().Msg("Migrating database...")
goose.SetDialect("mysql")
goose.AddNamedMigration("1_initial.go", InitialUp, nil)
dbConn := c.Database.GetDatabaseConnection()
err := goose.Up(dbConn.DB, ".")
if err != nil {
c.Logger.Panic().Msgf("Failed to migrate database to latest version: %s", err.Error())
}
}