gwv

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2018 License: MIT Imports: 23 Imported by: 0

README

GWV β

Golang Web Valve - to be connected to your series of tubes

Build Status Build status Coverage Status codecov.io Coverage Status GoDoc

install

go get -u -t simonwaldherr.de/go/gwv

features

  • HTTP Server
  • HTTPS Server
  • SPDY/HTTP2 Server
  • Static File Server
  • Automatic SSL cert generator
  • Realtime Webserver (SSE)
  • gracefully stoppable
  • channelised log
  • session and cookie handling
  • SNI for multiple Domains

license

MIT (see LICENSE file)

it depends on:

Documentation

Overview

Example
package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"path/filepath"
	"simonwaldherr.de/go/golibs/as"
	"simonwaldherr.de/go/golibs/cachedfile"
	"simonwaldherr.de/go/golibs/gopath"
	"simonwaldherr.de/go/gwv"
	"time"
)

func HTTPRequest(url string) string {
	timeout := time.Duration(2 * time.Second)
	client := &http.Client{Timeout: timeout}
	req, _ := http.NewRequest("GET", url, nil)
	rsp, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
	} else {
		if rsp.StatusCode == 200 {
			bodyBytes, _ := ioutil.ReadAll(rsp.Body)
			return string(bodyBytes)
		} else if err != nil {
			fmt.Println(err)
		} else {
			return as.String(rsp.StatusCode)
		}
		rsp.Body.Close()
	}
	return ""
}

func main() {
	dir := gopath.Dir()
	HTTPD := gwv.NewWebServer(8095, 60)

	HTTPD.URLhandler(
		gwv.Robots(as.String(cachedfile.Read(filepath.Join(dir, "static", "robots.txt")))),
	)

	HTTPD.Start()

	time.Sleep(50 * time.Millisecond)

	str := HTTPRequest("http://127.0.0.1:8095/robots.txt")
	fmt.Println(str)

	HTTPD.Stop()
	HTTPD.WG.Wait()

}
Output:

User-agent: *
Disallow: /
Allow: /humans.txt

Index

Examples

Constants

View Source
const (
	AUTO mimeCtrl = iota
	HTML
	JSON
	ICON
	PLAIN
	REDIRECT
	PROXY
	DOWNLOAD
	MANUAL
)

Variables

This section is empty.

Functions

func CheckSSL

func CheckSSL(certPath string, keyPath string) error

func GenerateSSL

func GenerateSSL(options map[string]string) error

Types

type Connections

type Connections struct {
	Messages chan string
	// contains filtered or unexported fields
}

func (*Connections) ClientDetails added in v0.4.0

func (hub *Connections) ClientDetails() (int, []string)

type Cryptor added in v0.6.0

type Cryptor struct {
	SecretKey      []byte
	CookieName     string
	MakeCookieFunc MakeCookieFunc
}

func NewSimpleCryptor added in v0.6.0

func NewSimpleCryptor(secretKey []byte, cookieName string) *Cryptor

NewSimpleCryptor returns a pointer to crypto cookie object

func (*Cryptor) Clear added in v0.6.0

func (sc *Cryptor) Clear(w http.ResponseWriter, r *http.Request)

Clear removes the cookie (effectively destroying the session)

func (*Cryptor) Read added in v0.6.0

func (sc *Cryptor) Read(v interface{}, r *http.Request) error

Read returns the decrypted value of the cookie

func (*Cryptor) Write added in v0.6.0

func (sc *Cryptor) Write(v interface{}, w http.ResponseWriter, r *http.Request) error

Write seralize and encrypts values and write them to a cookie

type HandlerWrapper added in v0.2.0

type HandlerWrapper struct {
	// contains filtered or unexported fields
}

func Download

func Download(re string, view handler) *HandlerWrapper

Download creates a handler for a given URL and sends the attachment header

func Favicon

func Favicon(path string) *HandlerWrapper

Favicon creates a handler for a favicon, its only argument is the path to the favicon file

func Humans

func Humans(data string) *HandlerWrapper

Humans creates a handler for the humans.txt file

func Proxy added in v0.6.0

func Proxy(path, destination string) *HandlerWrapper

Proxy creates proxy handler

func Redirect

func Redirect(path, destination string, code int) *HandlerWrapper

Redirect creates a handler for HTTP redirects

func Robots

func Robots(data string) *HandlerWrapper

Robots creates a handler for the robots.txt file

func SSE

func SSE(re string, hub *Connections) *HandlerWrapper

func SSEA added in v0.4.0

func SSEA(re string) *HandlerWrapper

func StaticFiles

func StaticFiles(reqpath string, paths ...string) *HandlerWrapper

StaticFiles creates a handler for a given request path and a folder

func URL

func URL(re string, view handler, handler mimeCtrl) *HandlerWrapper

URL creates a handler for a given URL, the URL can contain a regular expression

func (*HandlerWrapper) String added in v0.2.0

func (u *HandlerWrapper) String() string

type MakeCookieFunc added in v0.6.0

type MakeCookieFunc func(w http.ResponseWriter, r *http.Request) *http.Cookie

makes an empty cookie, no value

type WebServer

type WebServer struct {
	WG sync.WaitGroup

	LogChan chan string
	// contains filtered or unexported fields
}

func NewWebServer

func NewWebServer(port int, timeout time.Duration) *WebServer

NewWebServer returns a pointer to the webserver object

func (*WebServer) ConfigSSL

func (GWV *WebServer) ConfigSSL(port int, sslkey string, sslcert string, spdy bool)

ConfigSSL sets parameter for the HTTPS configuration

func (*WebServer) ConfigSSLAddCert added in v0.7.0

func (GWV *WebServer) ConfigSSLAddCert(sslkey, sslcert string)

ConfigSSLAddCert adds additional SSL Certs (select Cert by Server Name Indication (SNI))

func (*WebServer) Handler404

func (GWV *WebServer) Handler404(fn handler)

func (*WebServer) Handler500

func (GWV *WebServer) Handler500(fn handler)

func (*WebServer) InitLogChan added in v0.2.0

func (GWV *WebServer) InitLogChan()

func (*WebServer) InitRealtimeHub

func (GWV *WebServer) InitRealtimeHub() *Connections

func (*WebServer) ServeHTTP

func (GWV *WebServer) ServeHTTP(rw http.ResponseWriter, req *http.Request)

func (*WebServer) Start

func (GWV *WebServer) Start()

Start starts the web server

func (*WebServer) Stop

func (GWV *WebServer) Stop()

Stop stops all listeners and wait until all connections are closed

func (*WebServer) URLhandler

func (GWV *WebServer) URLhandler(patterns ...*HandlerWrapper)

Jump to

Keyboard shortcuts

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