opal

package module
v0.0.0-...-fe14df0 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2019 License: MIT Imports: 16 Imported by: 0

README

Opal Maskot

Opal - A HTTP2 Webframework in Go

Go Report Card Build Status

Opal is a simple HTTP2 web-framework implemented in Go (Golang), made for fast and reliable development. Opal is a powerful package for quickly writing modular web applications/services in Go.

Content

  1. Installation
  2. Examples
  3. Implementations
  4. Todo
  5. Dependencies
  6. Tests
  7. Documentation
  8. Authors

Installation

To use Opal, you need to install Go and set your Go workspace first, see Go installation docs.

  1. Download and install it:
$ go get -u github.com/SveinungOverland/opal
  1. Import it in your code:
import (
    "github.com/SveinungOverland/opal"
    "github.com/SveinungOverland/opal/http"
    "github.com/SveinungOverland/opal/router"
)

Examples

Basic Usage
srv, err := opal.NewTLSServer("./server.crt", "./server.key")
r := router.NewRouter("/")

// A simple GET-endpoint
r.Get("/", func(req *http.Request, res *http.Response) {
  res.String(200, "Hello World! :D")
})

// A simple PUT-endpoint
r.Put("/:id", func(req *http.Request, res *http.Response) {
  id := req.Param("id") // Read path parameter
  res.String(200, id)
})

srv.Register(r) // Register router
srv.Listen(443)
Server Push
srv, err := opal.NewTLSServer("./server.crt", "./server.key")
r := router.NewRouter("/")

// A simple endpoint that returns a html document
r.Get("/", func(req *http.Request, res *http.Response) {
    res.HTML("./index.html", nil)
    res.Push("/static/app.js") // Push app.js
    res.Push("/static/index.css") // Push index.css
})

// Register all files in given path as accessible files, 
// without the need to register every single file 
r.Static("/static", "./MY_STATIC_PATH")

srv.Register(r)
srv.Listen(443)
Middlewares
// Authorization middleware
auth := func(req *http.Request, res *http.Response) {
  token := req.Query("token")
  
  if token != "MY_SECRET_PASSWORD" {
    res.Unauthorized()
    req.Finish() 
    // Finish tells Opal that the request ends here and doesn't 
    // get passed along to other endpoint handlers or middleware
  }
}


srv, err := opal.NewTLSServer("./server.crt", "./server.key")
srv.Use(corsHandler) // Adding an optional cors middleware

r := router.NewRouter("/")

//               \/ Auth middleware function that is defined above gets added here
r.Post("/todo", auth, func(req *http.Request, res *http.Response) {
  task := string(req.Body)
  
  // Send new todo item
  res.JSON(201, http.JSON {
   "todo": task,
   "done": false,
  })
})

srv.Register(r)
srv.Listen(443)
Static Content

Opal provides an easy way to serve static content, like html, css and js files generated by for example a React or Vue build and is easily achieved by providing a path to the files to router.Static()

srv, err := opal.NewTLSServer("./server.crt", "./server.key")
r := router.NewRouter("/")

r.Static("/", "./build") // Serves the entire build folder on root path

srv.Register(r)
srv.Listen(443)

Implementations

Opal implements a robust HTTP2-library managing multiple clients with REST-support, Server-Push, and support for serving static files.

Core of the HTTP/2 Protocol

Implemented most of the HTTP2-protocol, specified by RFC7540

HPACK - Header compression

Created a robust and solid HPACK library, RFC7541

HTTP Router library

A high preformance HTTP-Router with parameter- and filehandling-functionality.

Todo

  • Add support for HTTP/1.1
  • Implement Stream Priority, RFC7540 Section 5.3
  • Implement better support for middlewares
  • Implement server push for static routes

Dependencies

  • crypto/tls - A TLS-library from the standard-library. Docs
  • github.com/go-test/deep - A library for test-support. Github Repo
  • github.com/fatih/color - A color-library for changing colors in the console. Github Repo

Tests

For running the test the following command can be executed at the root directory.

go test -v ./...

For seeing test-coverage the following commands can be exectuted:

go test -v ./... -coverageprofile=coverage.out
go tool cover -html=coverage.out

Documentation

GoDoc is generated and hosted at godoc.org/opal. At the bottom of the page there is also docs for the subpackages (like http and hpack)

HTTP Docs (Request and Response)

GoDoc: https://godoc.org/github.com/SveinungOverland/opal/http

Router Docs (Get, Post, Put...)

GoDoc: https://godoc.org/github.com/SveinungOverland/opal/router

HPACK Docs

GoDoc: https://godoc.org/github.com/SveinungOverland/opal/hpack

Authors

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WriteStream

func WriteStream(c *Conn)

Types

type Conn

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

Conn represents a HTTP-connection

func (*Conn) GetStream

func (c *Conn) GetStream(id uint32) (*Stream, bool)

GetStream gets a stream

func (*Conn) SetStream

func (c *Conn) SetStream(s *Stream)

SetStream sets the stream

type Server

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

Server represents a HTTP-server

func NewTLSServer

func NewTLSServer(certPath, privateKeyPath string) (*Server, error)

NewTLSServer creates a new http2-server with a TLS configuration

func (*Server) Listen

func (s *Server) Listen(port int16) error

Listen establishes a TCP-listener on a given port

func (*Server) Register

func (s *Server) Register(r *router.Router)

Register registers a router to the server

func (*Server) SetErrorChan

func (s *Server) SetErrorChan(errorChannel *chan error)

SetErrorChan sets a errorChannel for retrieving internal errors from the server

func (*Server) Use

func (s *Server) Use(handler router.HandleFunc)

Use registers a handler as a middleware

type Stream

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

type StreamState

type StreamState uint8
const (
	Idle StreamState = iota + 1
	ReservedLocal
	ReservedRemote
	Open
	HalfClosedLocal
	HalfClosedRemote
	Closed
)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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