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 ¶
- Constants
- func CheckSSL(certPath string, keyPath string) error
- func GenerateSSL(options map[string]string) error
- type Connections
- type Cryptor
- type HandlerWrapper
- func Download(re string, view handler) *HandlerWrapper
- func Favicon(path string) *HandlerWrapper
- func Humans(data string) *HandlerWrapper
- func Proxy(path, destination string) *HandlerWrapper
- func Redirect(path, destination string, code int) *HandlerWrapper
- func Robots(data string) *HandlerWrapper
- func SSE(re string, hub *Connections) *HandlerWrapper
- func SSEA(re string) *HandlerWrapper
- func StaticFiles(reqpath string, paths ...string) *HandlerWrapper
- func URL(re string, view handler, handler mimeCtrl) *HandlerWrapper
- type MakeCookieFunc
- type WebServer
- func (GWV *WebServer) ConfigSSL(port int, sslkey string, sslcert string, spdy bool)
- func (GWV *WebServer) ConfigSSLAddCert(sslkey, sslcert string)
- func (GWV *WebServer) Handler404(fn handler)
- func (GWV *WebServer) Handler500(fn handler)
- func (GWV *WebServer) InitLogChan()
- func (GWV *WebServer) InitRealtimeHub() *Connections
- func (GWV *WebServer) ServeHTTP(rw http.ResponseWriter, req *http.Request)
- func (GWV *WebServer) Start()
- func (GWV *WebServer) Stop()
- func (GWV *WebServer) URLhandler(patterns ...*HandlerWrapper)
Examples ¶
Constants ¶
const ( AUTO mimeCtrl = iota HTML JSON ICON PLAIN REDIRECT PROXY DOWNLOAD MANUAL )
Variables ¶
This section is empty.
Functions ¶
func GenerateSSL ¶
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
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)
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
makes an empty cookie, no value
type WebServer ¶
type WebServer struct { WG sync.WaitGroup LogChan chan string // contains filtered or unexported fields }
func NewWebServer ¶
NewWebServer returns a pointer to the webserver object
func (*WebServer) ConfigSSLAddCert ¶ added in v0.7.0
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) 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)