ong

module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2022 License: MIT

README

ong

ci codecov

Ong is a small http toolkit.

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

Taken mainly from the talk; How I Write HTTP Web Services after Eight Years[1][2] by Mat Ryer.

You really should not be using this code/library. The Go net/http package is more than enough.
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/server"
)

func main() {
	l := log.New(context.Background(), os.Stdout, 1000)
	secretKey := "hard-password"
	mux := server.NewMux(
		l,
		middleware.WithOpts("localhost", 65081, secretKey, l),
		server.Routes{
			server.NewRoute(
				"hello/",
				server.MethodGet,
				hello("hello world"),
			),
		})

	_, _ = server.CreateDevCertKey()
	err := server.Run(mux, server.DevOpts(), 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)
	}
}

go run -race ./...

To use tls with certificates from letsencrypt:

email := "admin@example.com"
domain := "*.example.com"
err := server.Run(mux, server.LetsEncryptOpts(email, domain), l)
  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 client provides HTTP client implementation.
Package client provides HTTP client implementation.
Package cookie provides utilities for using HTTP cookies.
Package cookie provides utilities for using HTTP cookies.
Package enc provides utilities to carry out encryption and decryption.
Package enc provides utilities to carry out encryption and decryption.
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
Package log implements a simple logging package
Package log implements a simple logging package
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 server provides HTTP server implementation.
Package server provides HTTP server implementation.
automax
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 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