Categorygithub.com/picatz/web
modulepackage
0.0.0-20200202152225-4512632aeaaf
Repository: https://github.com/picatz/web.git
Documentation: pkg.go.dev

# README

web

🕸 Your friendly neighborhood HTTP client and server

Download

$ go get -u -v github.com/picatz/web
...

Client Usage

package main

import (
  "fmt"

  "github.com/picatz/web"
)

func main() {
  client, _ := web.NewClient()

  resp, err := client.Get("https://www.google.com")

  if err != nil {
    panic(err)
  }

  fmt.Println(resp.StatusCode)
}

Server Usage

package main

import (
  "log"
  "net/http"
  "os"

  "github.com/picatz/web"
)

func main() {
  logger := log.New(os.Stderr, "example-web-server: ", log.LstdFlags)

  helloWorld := func(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Hello World!"))
  }

  server, _ := web.NewServer(
    web.WithRoutes(
      web.Routes{"/": helloWorld},
      web.MiddlewareLogRequest(logger),
      web.MiddlewareLimitRequestBody(web.DefaultRequestBodySize),
    ),
  )

  web.Serve(server, nil, "", "")
}

Server Authentication

A simple Google Oauth2 authenticator implementation is built in. First you'll need to generate and export the following ENV variables:

$ export GOOGLE_OAUTH_CLIENT_ID="..."
$ export GOOGLE_OAUTH_CLIENT_SECRET="..."
package main

import (
  "html/template"
  "log"
  "net/http"
  "os"

  "github.com/picatz/web"
)

func main() {
  logger := log.New(os.Stderr, "test-web-server: ", log.LstdFlags)

  authenticator, _ := web.NewOauth2GoogleAuthenticator(
    web.WithRedirectToLoginOnAuthFailure(),
  )

  tmpl, _ := template.New("homePage").Parse(`
    <!DOCTYPE html>
    <html lang="en">
    <meta charset="utf-8">

    <body>
      Hello {{.}}

      <a href="/auth/google/logout">Logout</a>
    </body>
  `)

  helloWorld := func(w http.ResponseWriter, r *http.Request) {
    v, ok := authenticator.ReadSessionValue(w, r, "name")
    if ok {
      tmpl.Execute(w, v)
    }
  }

  mainRoutes := web.Routes{
    "/": helloWorld,
  }

  server, _ := web.NewServer(
    web.WithRoutes(
      web.JoinRoutes(
        web.AuthenticatedRoutes(authenticator, mainRoutes),
        authenticator.Routes(),
      ),
      web.MiddlewareLogRequest(logger),
    ),
  )

  web.Serve(server, nil, "", "")
}

# Packages

No description provided by the author

# Functions

AuthenticatedRoutes takes multiple Routes and requires them all to be authenticated.
No description provided by the author
No description provided by the author
JoinRoutes takes multiple Routes objects and merges them into one Routes object.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
NewServer simplifies the creation of a new HTTP server using an optional list of ServerOptions for configuration.
Serve starts the listener.
WithAddr configures the IP address and port to sserve requests.
No description provided by the author
No description provided by the author
No description provided by the author
WithDefaultServerOptions provides an example method to use with the NewServer options using the default timeout values.
WithInMemoryCookieStore configures an in-memory cookie store.
No description provided by the author
No description provided by the author
No description provided by the author
WithRoutes allows for custom routes to be added to a server.
WithServerDefaultTLSConfig configures the server to use the DefaultServerTLSConfig.
WithServerIdleTimeout can change the server's idle timeout.
WithServerReadTimeout can change the server's read timeout.
WithServerTLSConfig can change the server's TLS configurations.
WithServerWriteTimeout can change the server's write timeout.

# Variables

No description provided by the author
Default server timeout values.
Default server timeout values.
DefaultServerTLSConfig defines the default TLS configuration for a server.
DefaultServerTLSConfigCipherSuites contains TLS cipher configuration.
DefaultServerTLSNextProto defines the default TLS protocol logic for a server.
Default server timeout values.
No description provided by the author

# Structs

No description provided by the author

# Interfaces

No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author
No description provided by the author
Routes contains a { key -> value } mapping of { pathString -> http.HandlerFunc }.
ServerOption is a function which always for server configurations.