simple

package module
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 31, 2025 License: MIT Imports: 25 Imported by: 0

README

Simple package

Simple is a Go package designed to avoid repetitive operations such as opening a database connection or making web requests. For now, it provides utility functions that help with the development of small scripts.

Features

  • Database wrappers :

    • sqlite, postgresql, clickhouse, gaussDB and mysql are supported
    • easily open, close, and modify the DSN of your database
    • insert data and run migrations
  • Web client management: :

    • pick your http client or tor socks proxy easily
    • fetch web content and parse HTML documents
    • download documents with hash computation
  • Hash functions :

    • functions supported : sha224, sha256, sha384, sha512, sha3 family and md5
    • buffer feature that allow hashing a file, block by block for better efficiency
    • constant variable for easy buffer size picking

Development

The package is currently at version v0.1.0 as I continue to write tests and improve the codebase. Once the tests are stable, I'll release the v1 of the package.

  • 🚩 v0.1.0 : first commit
  • 🪜 v0.2.0 : more feature for database and major rework of requests with a clean client management
  • 🪜 v0.2.1 : add Hash feature
  • ➡️ v0.2.2 : new feature OpenEnv() + improve registry for Hash featuure + add Test for hash and env + add Mock for requests
  • 👷‍♂️ v0.2.3 : add optional parameters for more flexible DSN, add support for advanced driver configuration and existing database connection.
  • 🪜 v0.2.4 : add Context for database and requests
  • 🏁 v1.0.0 : first release, full revision of the code and comments above the functions

Dependencies

  • Database features are based on the GORM library
  • Requests features are based on Go’s standard net/http package
  • Hash features are based on Go’s standard crypto package

Documentation

Index

Constants

View Source
const (
	KB = 1024
	MB = KB * 1024
)

Variables

View Source
var Registry = map[string]func() hash.Hash{
	"md5":      md5.New,
	"sha1":     sha1.New,
	"sha256":   sha256.New,
	"sha384":   sha512.New384,
	"sha512":   sha512.New,
	"sha3-224": sha3.New224,
	"sha3-256": sha3.New256,
	"sha3-384": sha3.New384,
	"sha3-512": sha3.New512,
	"shake-128": func() hash.Hash {
		return &shakeAdapter{shake: sha3.NewShake128(), length: 32}
	},
	"shake-256": func() hash.Hash {
		return &shakeAdapter{shake: sha3.NewShake256(), length: 64}
	},
}
View Source
var RegistryKey = map[string]HashKeyFactory{
	"blake2b-256": func(key []byte) (hash.Hash, error) {
		return blake2b.New256(key)
	},
	"blake2b-384": func(key []byte) (hash.Hash, error) {
		return blake2b.New384(key)
	},
	"blake2b-512": func(key []byte) (hash.Hash, error) {
		return blake2b.New512(key)
	},
	"blake2s-128": func(key []byte) (hash.Hash, error) {
		return blake2s.New128(key)
	},
	"blake2s-256": func(key []byte) (hash.Hash, error) {
		return blake2s.New256(key)
	},
}

Functions

func CloseDatabase

func CloseDatabase(db *gorm.DB) error

func CompareFiles

func CompareFiles(hash string, fileA string, fileB string) (bool, error)

func CompareFilesKey

func CompareFilesKey(hash string, keyA []byte, fileA string, keyB []byte, fileB string) (bool, error)

func DownloadDocumentReturnHash

func DownloadDocumentReturnHash(url string, filePath string, client *http.Client) (string, error)

func FetchBody

func FetchBody(url string, client *http.Client) (io.ReadCloser, error)

func GetContent

func GetContent(url string, client *http.Client) ([]byte, error)

func GetParsedContent

func GetParsedContent(url string, client *http.Client) (*html.Node, error)

func HashData

func HashData(hash string, data []byte) (string, error)

func HashFile

func HashFile(hash string, filePath string) (string, error)

func HashFileBuffer

func HashFileBuffer(hash string, filePath string, bufSize int) (string, error)

func HashFileKey

func HashFileKey(hash string, key []byte, filePath string) (string, error)

func HttpClient

func HttpClient() *http.Client

func InsertRows

func InsertRows(db *gorm.DB, entries ...any) error

func MigrateTables

func MigrateTables(db *gorm.DB, tables ...any) error

func OnionClient

func OnionClient(socksProxy *string) (*http.Client, error)

func OpenClickhouse

func OpenClickhouse(user string, password string, host string, port int, dbName string) (*gorm.DB, error)

func OpenDatabase

func OpenDatabase(driver string, dsn string) (*gorm.DB, error)

func OpenEnv

func OpenEnv() *gorm.DB

TODO

func OpenEnvFilenames

func OpenEnvFilenames(filenames []string, tags ...string) []string

Open a .env file with one or more filename Return results in a slice of string

func OpenGaussdb

func OpenGaussdb(host string, user string, password string, dbName string, port int, sslMode string) (*gorm.DB, error)

func OpenMysql

func OpenMysql(user string, password string, host string, port int, dbName string) (*gorm.DB, error)

func OpenMysqlUnixSocket

func OpenMysqlUnixSocket(user, password, socket, dbName string) (*gorm.DB, error)

func OpenPostgres

func OpenPostgres(host string, user string, password string, dbName string, port int, sslMode string) (*gorm.DB, error)

func OpenSqlite

func OpenSqlite(name string) (*gorm.DB, error)

func OpenSqliteMemory

func OpenSqliteMemory() (*gorm.DB, error)

Types

type DialectorFactory

type DialectorFactory func(dsn string) gorm.Dialector

type HashKeyFactory

type HashKeyFactory func(key []byte) (hash.Hash, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL