ong

module
v0.0.57-rc.4 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2023 License: MIT

README

ong

Go Reference
ci
codecov

Ong is a small http toolkit.

It's name is derived from Tanzanian artiste, Remmy Ongala.

Inspired by; How I Write HTTP Web Services after Eight Years[1][2] by Mat Ryer.

You really should not use this library/toolkit.
Instead, use the Go net/http package; and if you need some extra bits, may I suggest the awesome github.com/gorilla web toolkit.

This library is made just for me, it might be unsafe & it does not generally accept code contributions.

package main

import (
	"context"
	"fmt"
	"net/http"
	"os"

	"github.com/komuw/ong/log"
	"github.com/komuw/ong/middleware"
	"github.com/komuw/ong/mux"
	"github.com/komuw/ong/server"
)

func main() {
	l := log.New(os.Stdout, 1000)(context.Background())
	secretKey := "super-h@rd-pa$$word"
	mux := mux.New(
		l,
		middleware.WithOpts(
			"localhost",
			65081,
			secretKey,
			middleware.DirectIpStrategy,
			l,
		),
		nil,
		mux.NewRoute(
			"hello/",
			mux.MethodGet,
			hello("hello world"),
		),
		mux.NewRoute(
			"check/:age/",
			mux.MethodAll,
			check(),
		),
	)

	opts := server.DevOpts(l) // dev options.
	// alternatively for production:
	//   opts := server.LetsEncryptOpts("hey@example.com", "*.some-domain.com")
	err := server.Run(mux, opts, l)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}

func hello(msg string) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		cspNonce := middleware.GetCspNonce(r.Context())
		csrfToken := middleware.GetCsrfToken(r.Context())
		fmt.Printf("hello called cspNonce: %s, csrfToken: %s", cspNonce, csrfToken)

		// use msg, which is a dependency specific to this handler
		fmt.Fprint(w, msg)
	}
}

func check() http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		age := mux.Param(r.Context(), "age")
		_, _ = fmt.Fprintf(w, "Age is %s", age)
	}
}

go run -race ./...

A more complete example can be found in the example/ folder.

references:
  1. https://www.youtube.com/watch?v=rWBSMsLG8po
  2. https://pace.dev/blog/2018/05/09/how-I-write-http-services-after-eight-years.html

Directories

Path Synopsis
Package automax automatically sets GOMEMLIMIT & GOMAXPROCS to match the linux container memory & cpu quotas, if any.
Package automax automatically sets GOMEMLIMIT & GOMAXPROCS to match the linux container memory & cpu quotas, if any.
Package client provides a HTTP client implementation.
Package client provides a HTTP client implementation.
Package cookie provides utilities for using HTTP cookies.
Package cookie provides utilities for using HTTP cookies.
Package cry provides utilities for cryptography.
Package cry provides utilities for cryptography.
Package errors implements functions to manipulate errors.
Package errors implements functions to manipulate errors.
Package id generates unique random strings.
Package id generates unique random strings.
internal
clientip
Package clientip provides(in a best effort manner) a client's IP address.
Package clientip provides(in a best effort manner) a client's IP address.
dmn
Package dmn contains domain utilities used by multiple ong packages.
Package dmn contains domain utilities used by multiple ong packages.
finger
Package finger provides(in a best effort manner) a client's TLS fingerprint.
Package finger provides(in a best effort manner) a client's TLS fingerprint.
octx
Package octx houses context keys used by multiple ong packages.
Package octx houses context keys used by multiple ong packages.
Package log implements a simple logging handler.
Package log implements a simple logging handler.
Package middleware provides helpful functions that implement some common functionalities in http servers.
Package middleware provides helpful functions that implement some common functionalities in http servers.
Package mux implements a HTTP request multiplexer.
Package mux implements a HTTP request multiplexer.
Package server provides HTTP server implementation.
Package server provides HTTP server implementation.
Package sess provides an implementation of http sessions that is backed by tamper-proof & encrypted cookies.
Package sess provides an implementation of http sessions that is backed by tamper-proof & encrypted cookies.
Package xcontext is a package to offer the extra functionality we need from contexts that is not available from the standard context package.
Package xcontext is a package to offer the extra functionality we need from contexts that is not available from the standard context package.

Jump to

Keyboard shortcuts

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