ong

module
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 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/mux"
	"github.com/komuw/ong/server"
)

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

	opts := server.DevOpts() // dev options.
	// alternatively for production:
	//   opts := server.LetsEncryptOpts("email@email.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 ./...

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 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 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 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
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 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 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