server

package
v0.0.0-...-e26be59 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const ROWS_PER_PAGE = 50

ROWS_PER_PAGE is the number of rows returned per page of table data. Mirrors the Rust constant ROWS_PER_PAGE.

Variables

This section is empty.

Functions

This section is empty.

Types

type Count

type Count struct {
	Name  string `json:"name"`
	Count int    `json:"count"`
}

type Database

type Database interface {
	Overview(ctx context.Context) (*Overview, error)
	Tables(ctx context.Context) (*Tables, error)
	Table(ctx context.Context, name string) (*Table, error)
	TableData(ctx context.Context, name string, page int) (*TableData, error)
	TablesWithColumns(ctx context.Context) (*TablesWithColumns, error)
	Query(ctx context.Context, query string) (*Query, error)
	Erd(ctx context.Context) (*Erd, error)
}

Database is the interface every backend implements to serve the UI's API. It mirrors the Rust `Database` trait. Additional backends (Postgres, MySQL, etc.) can be added later by implementing this interface.

type Erd

type Erd struct {
	Tables        []ErdTable        `json:"tables"`
	Relationships []ErdRelationship `json:"relationships"`
}

type ErdColumn

type ErdColumn struct {
	Name         string `json:"name"`
	DataType     string `json:"data_type"`
	Nullable     bool   `json:"nullable"`
	IsPrimaryKey bool   `json:"is_primary_key"`
}

type ErdRelationship

type ErdRelationship struct {
	FromTable  string `json:"from_table"`
	FromColumn string `json:"from_column"`
	ToTable    string `json:"to_table"`
	ToColumn   string `json:"to_column"`
}

type ErdTable

type ErdTable struct {
	Name    string      `json:"name"`
	Columns []ErdColumn `json:"columns"`
}

type Metadata

type Metadata struct {
	Version     string `json:"version"`
	CanShutdown bool   `json:"can_shutdown"`
}

type Options

type Options struct {
	// Address to bind to, e.g. "127.0.0.1:3030".
	Address string
	// BasePath to serve the UI under, e.g. "/sql-studio". Empty for root.
	// Must start with "/" when set.
	BasePath string
	// NoShutdown disables the /api/shutdown endpoint and the UI shutdown button.
	NoShutdown bool
	// Version reported by /api/metadata.
	Version string
}

Options configures the HTTP server. Mirrors the relevant Rust CLI args.

type Overview

type Overview struct {
	FileName      string     `json:"file_name"`
	DBSize        string     `json:"db_size"`
	SQLiteVersion *string    `json:"sqlite_version"`
	Created       *time.Time `json:"created"`
	Modified      *time.Time `json:"modified"`
	Tables        int        `json:"tables"`
	Indexes       int        `json:"indexes"`
	Triggers      int        `json:"triggers"`
	Views         int        `json:"views"`
	RowCounts     []Count    `json:"row_counts"`
	ColumnCounts  []Count    `json:"column_counts"`
	IndexCounts   []Count    `json:"index_counts"`
}

type Query

type Query struct {
	Columns []string `json:"columns"`
	Rows    [][]any  `json:"rows"`
}

type SQLiteDB

type SQLiteDB struct {
	// contains filtered or unexported fields
}

SQLiteDB is the SQLite implementation of Database. It ports the Rust `sqlite` module, reusing the same SQL queries and result ordering.

func NewSQLite

func NewSQLite(db *sql.DB, path string, queryTimeout time.Duration) *SQLiteDB

NewSQLite wraps an already-open *sql.DB in a *SQLiteDB without opening a new connection or mutating the pool. The caller owns the handle's lifecycle and tuning (e.g. SetMaxOpenConns); SQL Studio never closes the injected handle, so db must outlive the server. path is retained only for os.Stat-based file metadata in Overview and Tables and is not used to open db.

func OpenSQLite

func OpenSQLite(path string, queryTimeout time.Duration, sample []byte) (*SQLiteDB, error)

OpenSQLite opens a SQLite database. If path == "preview", the bundled sample database is written to "sample.db" and opened read-only; otherwise the file at path is opened read-write. sample is the embedded sample DB bytes (see server.SampleDB()).

func (*SQLiteDB) Erd

func (d *SQLiteDB) Erd(ctx context.Context) (*Erd, error)

func (*SQLiteDB) Overview

func (d *SQLiteDB) Overview(ctx context.Context) (*Overview, error)

func (*SQLiteDB) Query

func (d *SQLiteDB) Query(ctx context.Context, query string) (*Query, error)

func (*SQLiteDB) Table

func (d *SQLiteDB) Table(ctx context.Context, name string) (*Table, error)

func (*SQLiteDB) TableData

func (d *SQLiteDB) TableData(ctx context.Context, name string, page int) (*TableData, error)

func (*SQLiteDB) Tables

func (d *SQLiteDB) Tables(ctx context.Context) (*Tables, error)

func (*SQLiteDB) TablesWithColumns

func (d *SQLiteDB) TablesWithColumns(ctx context.Context) (*TablesWithColumns, error)

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server serves the embedded UI and the JSON API backed by a Database.

func New

func New(db Database, ui fs.FS, opts Options) (*Server, error)

New builds a Server. The ui filesystem holds the built UI assets (e.g. an embed.FS rooted at the dist directory); the caller owns embedding so this package stays free of bundled binaries. New reads and rewrites index.html according to opts.BasePath, mirroring the Rust startup logic.

func (*Server) Handler

func (s *Server) Handler() http.Handler

Handler returns the fully-wired HTTP handler: the JSON API and the embedded UI, all served under the server's prefix (see Prefix). A host application can reach into this package as a library and mount the returned handler, e.g.:

mux.Handle(srv.Prefix()+"/", srv.Handler())

http.ServeMux does not strip the matched pattern before dispatching, so the inner handler still sees the full Prefix()+"/..." path and routes it correctly. CORS is applied to the app routes; the prefix redirects are not wrapped. Under such a subtree mount the host owns "/", so the inner standalone-root redirect below is unreachable (and harmless) — it exists only for running this handler directly as a standalone server.

func (*Server) Prefix

func (s *Server) Prefix() string

Prefix is the path subtree the app is served under: the configured base path followed by "/db". A host server mounts Handler() at Prefix()+"/".

func (*Server) Run

func (s *Server) Run(ctx context.Context) error

Run starts the HTTP server and blocks until the context is cancelled (e.g. SIGINT) or a shutdown request arrives on /api/shutdown.

type Table

type Table struct {
	Name        string  `json:"name"`
	SQL         *string `json:"sql"`
	RowCount    int     `json:"row_count"`
	IndexCount  int     `json:"index_count"`
	ColumnCount int     `json:"column_count"`
	TableSize   string  `json:"table_size"`
}

type TableData

type TableData struct {
	Columns []string `json:"columns"`
	Rows    [][]any  `json:"rows"`
}

type TableWithColumns

type TableWithColumns struct {
	TableName string   `json:"table_name"`
	Columns   []string `json:"columns"`
}

type Tables

type Tables struct {
	Tables []Count `json:"tables"`
}

type TablesWithColumns

type TablesWithColumns struct {
	Tables []TableWithColumns `json:"tables"`
}

Jump to

Keyboard shortcuts

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