httx

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

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

Go to latest
Published: Jan 6, 2024 License: BSD-2-Clause Imports: 13 Imported by: 0

README

HTTX

small, sharp tools for HTTP.

Introduction

HTTX is an attempt to make a super simple, meta framework -- similar to NextJS, SvelteKit, SolidStart or Remix -- that is built on Go and Bash. HTTX is a library of Tools for both serving web content as websites and testing your websites. HTTX is built on top of normal file servers like npx serve and uses file base routing.

Installation

go install github.com/fly-high-bird/httx/cmd/...

Quick Start

First consider the following:

<!-- ./index.html -->
<html>
  <head>...</head>
  <body>
    Hello Luke Skywalker!
  </body>
</html>

This will be a great new homepage, but one problem not everyone's name is Luke Skywalker. To fix this lets move the file to ./_templates/homepage.html and make a new file:

# ./index.sh

# Set the name property to the value of the query param with the key name
with-prop name "$QUERY_NAME" |

# render the homepage template we created
render "homepage"

Next change the template to use Go's HTML Templating with a map of props passed in as the global object.

<!-- ./_templates/homepage.html -->
<html>
  <head>...</head>
  <body>
    Hello {{.name}}!
  </body>
</html>

Now we can run our server and navigat to http://localhost:8080?name=Mario%20Mario and see our new message.

# run the httx command to run a dev server at the given path
$ httx --path . --http :8080

A more complex vesion of this example can be found here: Readme Example.

Motivation

HTTX is built on the philosophy that moving all of our problems into more higher level abstracts does not always help us most effectively deliver content to our users. This is a philosophy heavily inspired by HTMX -- even down to the name ;)

Today's frameworks work more and more on moving logic into interpreted languages -- usually transpiled, interpreted languages like Typescript. While this is a fun effort and moves all components of a system into one runtime with one lingua franca, we often ignore the reality that we are making more complex problems for ourselves.

Some projects like Bun are focusing on taking the parts of the app that we rely on, like http servers, file io, and memory management to more modern languages -- namely Rust. We appreciate this effort but we want to remove one more contraint, we don't want to build a toolkit for Javascript.

How It Works

Javascript, and more specifically NodeJs, is a powerful runtime that support much of the web today -- both frontend and backend -- and you should not be rewriting your business logic to Bash. HTTX offers a solutution for taking the insights from your services -- NodeJs or otherwise -- and embedding that data into html templates.

The processing of a request revolves around the Context. This is a datastructure used as a state machine to hold information for the html template that should be rendered.

Once, the render -- or redir and to-json -- command is called the Context will be consumed into a Response object. This Response object will then be interpreted and formatted into an http response using Go's net/http package.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	Headers map[string]string
	Props   map[string]any
	Cookies []*http.Cookie
	Body    []byte
}

func LoadContext

func LoadContext() Context

func NewContext

func NewContext() (ctx Context)

func (*Context) FormValue

func (ctx *Context) FormValue(k string) string

func (*Context) Json

func (ctx *Context) Json() (data any, err error)

func (*Context) ParseStdin

func (ctx *Context) ParseStdin()

func (*Context) SetBody

func (ctx *Context) SetBody(b string)

type Directory

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

Directory is a pointer to the file system where a website lives

func Mount

func Mount(root string, secrets map[string]string) *Directory

Mount httx to a directory in the local file system

func (*Directory) Open

func (d *Directory) Open(file string) *Handler

Open a file inside direcory if it exists or nil

func (*Directory) ServeHTTP

func (d *Directory) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP fulfilling http.Handler interface from net/http

func (*Directory) Start

func (d *Directory) Start() error

type Handler

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

Handler handles an individual file

func Open

func Open(root, file string, secrets map[string]string) *Handler

Open file handler for serving file

func (*Handler) Exec

func (h *Handler) Exec(env []string, stdin io.Reader) (*Response, error)

Execute file underlying handler

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP fulfilling http.Handler interface from net/http

type Response

type Response struct {
	Headers  map[string]string
	Cookies  []*http.Cookie
	Redirect string
	File     string
	Body     string
}

Response is a prepared response from httx to serve

func (*Response) ServeHTTP

func (res *Response) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP fulfilling the http.Handler interface in net/http

Jump to

Keyboard shortcuts

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