sittella

package module
v0.0.0-...-76727dd Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2025 License: MIT Imports: 10 Imported by: 0

README

sittella logo

Sittella

A simple, slightly opinionated SSR framework for Go


Built predominantly with interfaces so you can implement your own if you hate mine :')

Features

  • HTMX aware redirect and requests
  • Templ rendering
  • Built in sessions and auth for simplicity
  • Central request context

Planned

  • CSRF (token generation for templ, automatic csrf checking for requests)
  • Backend form parsing and validation
  • Better documentation
  • ????
  • Profit

Quick Start

package main

import (
	"net/http"

	"github.com/dimmerz92/sittella"
	"github.com/dimmerz92/sittella/auth/sqliteauth"
	"github.com/dimmerz92/sittella/codec/gob"
	"github.com/dimmerz92/sittella/database/sqlitedb"
	"github.com/dimmerz92/sittella/mailer"
	"github.com/dimmerz92/sittella/sessions"
	"github.com/dimmerz92/sittella/sessions/memstore"
)

func main() {
	db := sqlitedb.New(sqlitedb.MEMORY_DSN)

	app := sittella.New(sittella.Config{
		DB:           db,
		AuthManager:  sqliteauth.New(sqliteauth.Config{DB: db.Unsafe().DB}),
		SessionStore: memstore.New(memstore.Config{Codec: gob.Codec{}, Options: sessions.DefaultOptions()}),
		Mailer:       &mailer.DefaultMailer{Host: "localhost", From: "test@email.com"},
	})

	app.GET("/", func(c sittella.Context) error { return c.String(200, "hello, world") })

	err := app.Start(8000)
	if err != http.ErrServerClosed {
		println(err.Error())
	}
}

Installation

go get github.com/dimmerz92/sittella@latest

License

MIT - LICENSE

Contributing

Issues and PRs are welcome!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type App

type App interface {
	// OnStart runs the given callback immediately before the sever starts.
	OnStart(callback func())

	// On stop runs the given callback immediately before the server stops.
	OnStop(callback func())

	// OnHandlerError runs the given callback as a central error handler.
	OnHandlerError(callback func(c Context, err error))

	// Start runs the http server on the given port number.
	Start(port int) error

	// Stop sends a stop signal to the http server.
	Stop() error

	// Use applies the given middleware to all registered handlers.
	Use(middleware ...MiddlewareFunc)

	// Any registers a handler for any HTTP request method.
	Any(path string, handler HandlerFunc, middleware ...MiddlewareFunc)

	// GET registers a handler for HTTP GET requests.
	GET(path string, handler HandlerFunc, middleware ...MiddlewareFunc)

	// POST registers a handler for HTTP POST requests.
	POST(path string, handler HandlerFunc, middleware ...MiddlewareFunc)

	// PUT registers a handler for HTTP PUT requests.
	PUT(path string, handler HandlerFunc, middleware ...MiddlewareFunc)

	// DELETE registers a handler for HTTP DELETE requests.
	DELETE(path string, handler HandlerFunc, middleware ...MiddlewareFunc)

	// HEAD registers a handler for HTTP HEAD requests.
	HEAD(path string, handler HandlerFunc, middleware ...MiddlewareFunc)

	// OPTIONS registers a handler for HTTP OPTIONS requests.
	OPTIONS(path string, handler HandlerFunc, middleware ...MiddlewareFunc)

	// CONNECT registers a handler for HTTP CONNECT requests.
	CONNECT(path string, handler HandlerFunc, middleware ...MiddlewareFunc)

	// TRACE registers a handler for HTTP TRACE requests.
	TRACE(path string, handler HandlerFunc, middleware ...MiddlewareFunc)

	// PATCH registers a handler for HTTP PATCH requests.
	PATCH(path string, handler HandlerFunc, middleware ...MiddlewareFunc)
}

App defines the interface for the central application.

func New

func New(config Config) App

type Config

type Config struct {
	DB           *database.Database
	AuthManager  auth.AuthManager
	SessionStore sessions.Store
	Mailer       mailer.Mailer
}

type Context

type Context interface {
	// Request returns the underlying request.
	Request() *http.Request

	// Response returns the underlying response writer.
	Response() http.ResponseWriter

	// Set adds the key value pair to the context store.
	Set(key string, value any)

	// Get returns the value mapped to by the given key from the context store if
	// it exists.
	Get(key string) (any, bool)

	// DB returns the underlying database.
	DB() *database.Database

	// Mailer returns the underlying mailer.
	Mailer() mailer.Mailer

	// Auth returns the current auth session.
	Auth() auth.Auth

	// Session returns the unique user session.
	Session() sessions.Session

	// HTML writes the given status and html string to the response.
	HTML(status int, html string) error

	// JSON writes the given status and json data to the response.
	JSON(status int, data any) error

	// String writes the given status and string to the response.
	String(status int, text string) error

	// NoContent writes the given status to the response without a body.
	NoContent(status int) error

	// NotFound writes a status 404 to the response without a body.
	NotFound() error

	// Render writes the given status and templates to the response.
	Render(status int, tpls ...templ.Component) error

	// Redirect is a HTMX aware redirect method.
	// Non-HTMX requests result in a redirect to the given path with status.
	// HTMX requests return a 200 - OK status with HTMX redirect headers.
	// https://github.com/bigskysoftware/htmx/issues/2052#issuecomment-1979805051
	Redirect(status int, path string) error

	// IsHTMX returns true if the current request is HTMX, otherwise false.
	IsHTMX() bool
}

Context defines the request scoped context.

type HandlerFunc

type HandlerFunc func(c Context) error

type MiddlewareFunc

type MiddlewareFunc func(next HandlerFunc) HandlerFunc

Directories

Path Synopsis
gob

Jump to

Keyboard shortcuts

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