ong

module
v0.0.88 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2024 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/config"
	"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(context.Background(), os.Stdout, 1000)
	secretKey := "super-h@rd-Pas1word"
	opts := config.WithOpts(
		"localhost",
		65081,
		secretKey,
		config.DirectIpStrategy,
		l,
	) // dev options.
	// alternatively for production:
	//   opts := config.LetsEncryptOpts("example.com", "secretKey", config.DirectIpStrategy, l, "hey@example.com", []string{"api.example.com", "example.com"})

	mux := mux.New(
		opts,
		nil,
		mux.NewRoute(
			"hello/",
			mux.MethodGet,
			hello("hello world"),
		),
		mux.NewRoute(
			"check/:age/",
			mux.MethodAll,
			check(),
		),
	)

	err := server.Run(mux, opts)
	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 config provides various parameters(configuration optionals) that can be used to configure ong.
Package config provides various parameters(configuration optionals) that can be used to configure ong.
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 identifiers.
Package id generates unique random identifiers.
internal
acme
Package acme provides automatic access to certificates from ACME-based certificate authorities(like Let's Encrypt).
Package acme provides automatic access to certificates from ACME-based certificate authorities(like Let's Encrypt).
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.
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.
key
Package key implements some common secure functionality.
Package key implements some common secure functionality.
mx
Package mx implements a HTTP request multiplexer.
Package mx implements a HTTP request multiplexer.
octx
Package octx houses context keys used by multiple ong packages.
Package octx houses context keys used by multiple ong packages.
tst
Package tst implements some common test functionality needed across ong.
Package tst implements some common test functionality needed across ong.
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 sync provides synchronization, error propagation, and Context cancelation for groups of goroutines working on subtasks of a common task.
Package sync provides synchronization, error propagation, and Context cancelation for groups of goroutines working on subtasks of a common task.

Jump to

Keyboard shortcuts

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