fibre

package module
v0.0.0-...-7c4d347 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2019 License: MIT Imports: 13 Imported by: 3

README

ls-fibre

🌐 generic default web service with basic handlers 🌐

usage

fibre provides a generic set of handlers for favicon, pages, not found, health checks, etc as a wrapper around gorilla/mux:

  r.HandleFunc("/favicon.ico", ws.FavicoHandler)
  r.HandleFunc("/", ws.HomeHandler)
  r.HandleFunc("/healthcheck", ws.HealthCheckHandler)
  r.HandleFunc("/page/{page}.html", ws.PageHandler)

fibre also provides generic api key middleware and logging middleware (for debugging):

  ...
	address := config.Getenv("MAIN_HOST", "127.0.0.1") + ":" + config.Getenv("MAIN_PORT", "8080")
	ws := fibre.NewWebService("main", address)

  ws.Apikey = config.Getenv("MAIN_API_KEY", "default_key")

  ws.Router.Use(ws.LogMiddleware)
  ws.Router.Use(ws.APIKeyMiddleware)

fibre also provides a simple method for proxying requests:

  cfg := []service.ProxyConfig{
    service.ProxyConfig{
      Path: "/",
      Host: "redirecthost.co"
    },
    service.ProxyConfig{
      Path: "/whatever/path",
      Host: "google.com",
      Override: service.ProxyOverride{
        Match: "/api/v2",
        Path: "/api/v3",
      },
    },
  }

  ws.Proxy(cfg)

To use pages with templates, make sure your app has a bin folder layout matching the service name (in this case, main) such as:

/
  main.go
  /bin
    main
    /web
      /main
        /page
          index.html
        /static
        /templates
          base.html
    $ go build -o bin/main main.go
    $ cd bin
    $ ./main
index.html
{{define "content"}}
<p>main service.</p>
{{end}}

base.html
{{define "base"}}
<!DOCTYPE html>
<html lang="en">
<head>
  <link href="data:image/x-icon;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQEAYAAABPYyMiAAAABmJLR0T///////8JWPfcAAAACXBIWXMAAABIAAAASABGyWs+AAAAF0lEQVRIx2NgGAWjYBSMglEwCkbBSAcACBAAAeaR9cIAAAAASUVORK5CYII=" rel="icon" type="image/x-icon">
</head>
<body>
  {{template "content" .}}
</body>
</html>
{{end}}
main.go
package main

import (
	"github.com/lakesite/ls-config"
	"github.com/lakesite/ls-fibre"
)

func main() {
	address := config.Getenv("MAIN_HOST", "127.0.0.1") + ":" + config.Getenv("MAIN_PORT", "8080")
	ws := fibre.NewWebService("main", address)
	ws.RunWebServer()
}

testing

$ go test

running

$ cd examples $ go run main.go

Visit http://localhost:8080/

license

MIT

Documentation

Overview

Package ls-fibre includes generic handlers, net/http and mux code for instances of servers with API endpoints further defined within their respective packages.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ProxyConfig

type ProxyConfig struct {
	Path     string
	Host     string
	Override ProxyOverride
}

type ProxyOverride

type ProxyOverride struct {
	Match string
	Host  string
	Path  string
}

type WebService

type WebService struct {
	Router *mux.Router

	Instance string
	Address  string
	Apikey   string
}

struct WebService holds a Router (*mux.Router), Instance name (string), Address (string), and optional Apikey (string).

func NewWebService

func NewWebService(instance string, address string) *WebService

Create a web service with appropriate handlers. instance is a key that will be used in loading templates, static files, etc. address is the host and port to listen on

func (*WebService) APIKeyMiddleware

func (ws *WebService) APIKeyMiddleware(next http.Handler) http.Handler

APIKeyMiddleware provides a built in check for api key, for json api services

func (*WebService) FavicoHandler

func (ws *WebService) FavicoHandler(w http.ResponseWriter, r *http.Request)

func (*WebService) HealthCheckHandler

func (ws *WebService) HealthCheckHandler(w http.ResponseWriter, r *http.Request)

HealthCheckHandler provides a default health check response (in JSON) for the instance.

func (*WebService) HomeHandler

func (ws *WebService) HomeHandler(w http.ResponseWriter, r *http.Request)

Home handler provides a default index handler for the instance.

func (*WebService) JsonStatusResponse

func (ws *WebService) JsonStatusResponse(w http.ResponseWriter, response string, status int)

JsonStatusResponse takees a response writer, response string and status, and writes the status and encodes the response string.

func (*WebService) LogMiddleware

func (ws *WebService) LogMiddleware(next http.Handler) http.Handler

LogMiddleware simply prints request URIs.

func (*WebService) NotFoundHandler

func (ws *WebService) NotFoundHandler(w http.ResponseWriter, r *http.Request)

NotFoundHandler provides a default not found handler for the instance.

func (*WebService) PageHandler

func (ws *WebService) PageHandler(w http.ResponseWriter, r *http.Request)

Generic handler for /page/<page>.html requests, which reads from the root/web/<instance>/templates/<page>.html template.

func (*WebService) Proxy

func (ws *WebService) Proxy(config []ProxyConfig)

func (*WebService) RunWebServer

func (ws *WebService) RunWebServer()

Creates a new net/http service with a WebService configuration, then run the http.Server

func (*WebService) SetupProxy

func (ws *WebService) SetupProxy(config ProxyConfig) http.Handler

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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