httputils

package module
v0.0.0-...-2c0076f Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2019 License: GPL-3.0 Imports: 12 Imported by: 0

README

This library includes functions of various usefulness that I use in my Golang web applications.

Included amongst them are:
- A single function to setup handlers for /assets/, /favicon.{ico,png}, and /robots.txt
    - All served from a relative ./assets/ directory
    - /assets/ allows for easy serving of CSS and such
- Generate random bytes and strings
- HTTP logger middleware
- Some html/template helpers

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Debug bool

	Logfile string
)

Functions

func Debugln

func Debugln(v ...interface{})

func FaviconICO

func FaviconICO(w http.ResponseWriter, r *http.Request)

func FaviconPNG

func FaviconPNG(w http.ResponseWriter, r *http.Request)

func GetRenderTime

func GetRenderTime(c context.Context) string

GetRenderTime calculates the time an HTTP request took, if the Logger middleware was used

func GetScheme

func GetScheme(r *http.Request) (scheme string)

GetScheme is a hack to allow me to make full URLs due to absence of http:// from URL.Scheme in dev situations When behind Nginx, use X-Forwarded-Proto header to retrieve this, then just tack on "://" getScheme(r) should return http:// or https://

func ImgClass

func ImgClass(s string) string

func ImgExt

func ImgExt(s string) string

func Logger

func Logger(next http.Handler) http.Handler

Logger is my custom logging middleware It prints all HTTP requests to a file called http.log, as well as helps the expvarHandler log the status codes

func PrettyDate

func PrettyDate(date int64) string

func RandBytes

func RandBytes(n int) ([]byte, error)

func RandKey

func RandKey(n int) (string, error)

RandKey generates a random key of specific length

func Robots

func Robots(w http.ResponseWriter, r *http.Request)

func SafeHTML

func SafeHTML(s string) template.HTML

SafeHTML is a template function to ensure HTML isn't escaped

func ServeContent

func ServeContent(w http.ResponseWriter, r *http.Request, dir, file string)

ServeContent checks for file existence, and if there, serves it so it can be cached

func StaticInit

func StaticInit()

StaticInit initializes handlers for /assets/, /robots.txt, /favicon.ico, and /favicon.png

func TimeTrack

func TimeTrack(start time.Time, name string)

TimeTrack is a simple function to time the duration of any function you wish Example (at the beginning of a function you wish to time): defer utils.TimeTrack(time.Now(), "[func name]")

func Timer

func Timer(next http.Handler) http.Handler

Types

This section is empty.

Jump to

Keyboard shortcuts

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