go-util

module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2019 License: MIT

README

Build Status GoDoc

go-util

Collection of utils for logging, configuration, http basic auth etc.

HTTP Basic authentication

Example:

package main

import (
	"fmt"
	"net/http"
	
	httpUtil "github.com/ncraft/go-util/pkg/http"
)

type colors []string

func main() {
	colors := colors{"red", "blue", "green"}

	authHandler := httpUtil.BasicAuthHandler{
		Username:        "user123",
		Password:        "secret",
		Realm:           "Please provide username and password",
		OriginalHandler: http.HandlerFunc(colors.list),
	}

	http.Handle("/colors", authHandler)

	http.ListenAndServe(":8080", nil)
}

func (colors colors) list(w http.ResponseWriter, req *http.Request) {
	for _, c := range colors {
		fmt.Fprintf(w, "%s\n", c)
	}
}

Logging

Example:

package main

import "github.com/ncraft/go-util/pkg/log"

func main() {
	log.SetDebug(true)

	log.Info("running logging example")

	colors := []string{"red", "blue", "green"}

	for _, color := range colors {
		log.Debug("added color %s", color)
	}
}

Prints:

INFO: 2018/12/07 11:28:23 main.go:8: this is a log example
DEBUG: 2018/12/07 11:28:23 main.go:13: added color one
DEBUG: 2018/12/07 11:28:23 main.go:13: added color two
DEBUG: 2018/12/07 11:28:23 main.go:13: added color three

Directories

Path Synopsis
examples
log
pkg
http
HTTP utils for basic authentication etc.
HTTP utils for basic authentication etc.
log
Very simple logging implementation with log levels DEBUG, INFO and ERROR.
Very simple logging implementation with log levels DEBUG, INFO and ERROR.

Jump to

Keyboard shortcuts

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