wsl

package module
v0.0.0-...-c0b2e16 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2022 License: MIT Imports: 21 Imported by: 2

README

WSL - Web SQL Lite

A web interface for executing SQL scripts.

Getting started

Installation
go get -u github.com/elgs/wsl
Simple Example
package main

import (
	"log"

	"github.com/elgs/wsl"
	"github.com/elgs/wsl/interceptors"
	"github.com/elgs/wsl/scripts"
	_ "github.com/go-sql-driver/mysql"
)

func main() {
	app, err := wsl.NewWithConfigPath("/etc/wsld.json")
	if err != nil {
		log.Fatal(err)
	}

	// optionally load built in user management interceptors and scripts
	scripts.LoadBuiltInScripts(app)
	interceptors.RegisterBuiltInInterceptors(app)

	// done manully
	// wsld.RegisterGlobalInterceptors(&interceptors.AuthInterceptor{})
	// wsld.RegisterQueryInterceptors("signup", &interceptors.SignupInterceptor{})
	// ...

	// wsld.Scripts["init"] = scripts.Init
	// wsld.Scripts["signup"] = scripts.Signup
	// ...

	app.Start()
	wsl.Hook()
}
wsld.json
{
   "web": {
      "http_addr": "127.0.0.1:1103",
      "https_addr": "127.0.0.1:1443",
      "cors": true,
      "cert_file": "cert.pem",
      "key_file": "key.pem"
   },
   "databases": {
      "main": {
         "db_type": "mysql",
         "db_url": "root:password@tcp(host:3306)/db"
      },
      "audit": {
         "db_type": "mysql",
         "db_url": "root:password@tcp(host:3306)/db"
      }
   },
   "mail": {
      "mail_host": "host:587",
      "mail_username": "mail",
      "mail_password": "password",
      "mail_from": "noreply@host"
   },
   "app": {
      "foo": "bar"
   }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertArray

func ConvertArray[T any, U any](arrayOfInterfaces []T) (*[]U, error)

func ConvertMap

func ConvertMap[T any, U any](data map[string]T) (map[string]U, error)

func ExtractSQLParameter

func ExtractSQLParameter(statement string) string

func ExtractScriptParamsFromMap

func ExtractScriptParamsFromMap(m map[string]any) map[string]any

func Hook

func Hook(clean func())

func IsQuery

func IsQuery(sql string) bool

func ShouldExport

func ShouldExport(sql string) bool

true if the first character is uppercase, false otherwise

func SplitSqlLabel

func SplitSqlLabel(sqlString string) (label string, s string)

func SqlNormalize

func SqlNormalize(sql *string)

returns whether to export the result of this sql statement or not

Types

type App

type App struct {
	Config             *Config
	Databases          map[string]*sql.DB
	Cache              *gorediscache.Cache
	Scripts            map[string]*Script
	Interceptors       map[string][]Interceptor
	GlobalInterceptors []Interceptor
}

func NewApp

func NewApp(config *Config) *App

func (*App) BuildScript

func (this *App) BuildScript(scriptString string, scriptId string) (*Script, error)

func (*App) GetDB

func (this *App) GetDB(dbName string) *sql.DB

func (*App) GetScript

func (this *App) GetScript(scriptId string, forceReload bool) (*Script, error)

func (*App) RegisterGlobalInterceptors

func (this *App) RegisterGlobalInterceptors(is ...Interceptor)

func (*App) RegisterScriptInterceptors

func (this *App) RegisterScriptInterceptors(scriptID string, is ...Interceptor)

func (*App) SendMail

func (this *App) SendMail(from, subject, body string, to ...string) error

func (*App) Start

func (this *App) Start()

type AuthLevel

type AuthLevel int
const (
	AuthFailed AuthLevel = iota
	AuthNoToken
	AuthUserAuthorized
	AuthRootAuthorized
)

type Config

type Config struct {
	Web       *Web                `json:"web"`
	Databases map[string]Database `json:"databases"`
	RedisURL  string              `json:"redis_url"`
	Mail      *Mail               `json:"mail"`
	Opt       map[string]any      `json:"opt"`
}

func NewConfig

func NewConfig(confBytes []byte) (*Config, error)

type Context

type Context struct {
	App         *App
	Script      *Script
	AccessToken string
	AuthLevel   AuthLevel
	ClientIP    string
	Session     map[string]string
	Flags       map[string]string
	Params      map[string]any
	Opt         map[string]any
}

type Database

type Database struct {
	Type string `json:"type"`
	Url  string `json:"url"`
}

type DefaultInterceptor

type DefaultInterceptor struct {
}

func (*DefaultInterceptor) After

func (this *DefaultInterceptor) After(tx *sql.Tx, context *Context, exportedResults any, cumulativeResults any) error

func (*DefaultInterceptor) AfterEach

func (this *DefaultInterceptor) AfterEach(tx *sql.Tx, context *Context, statement *Statement, cumulativeResults map[string]any, result any) error

func (*DefaultInterceptor) Before

func (this *DefaultInterceptor) Before(tx *sql.Tx, context *Context) error

func (*DefaultInterceptor) BeforeEach

func (this *DefaultInterceptor) BeforeEach(tx *sql.Tx, context *Context, statement *Statement, cumulativeResults map[string]any) (bool, error)

func (*DefaultInterceptor) GetAuthLevel

func (this *DefaultInterceptor) GetAuthLevel() AuthLevel

type Interceptor

type Interceptor interface {
	Before(tx *sql.Tx, context *Context) error
	After(tx *sql.Tx, context *Context, exportedResults any, cumulativeResults any) error
	BeforeEach(tx *sql.Tx, context *Context, statement *Statement, cumulativeResults map[string]any) (bool, error)
	AfterEach(tx *sql.Tx, context *Context, statement *Statement, cumulativeResults map[string]any, result any) error
	GetAuthLevel() AuthLevel
}

type Mail

type Mail struct {
	Host     string `json:"host"`
	Username string `json:"username"`
	Password string `json:"password"`
	From     string `json:"from"`
}

type Script

type Script struct {
	ID           string
	DBKey        string
	Text         string
	Statements   []*Statement
	Interceptors []Interceptor
}

type Statement

type Statement struct {
	Index        int
	Label        string
	Text         string
	Param        string
	IsQuery      bool
	ShouldExport bool
	Script       *Script
}

type Web

type Web struct {
	HttpAddr  string `json:"http_addr"`
	HttpsAddr string `json:"https_addr"`
	Cors      bool   `json:"cors"`
	CertFile  string `json:"cert_file"`
	KeyFile   string `json:"key_file"`
}

Jump to

Keyboard shortcuts

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