kasi

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

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

Go to latest
Published: May 4, 2015 License: GPL-2.0 Imports: 11 Imported by: 0

README

kasi

kasi is the transparent gateway(or bridge, or proxy) for the privately managed or public services, which are based on HTTP(HTTPS) . It can be in the middle of between your clients and services and be possible to make a single point of service.

In Kiswahili, kasi means speedy or speed. (http://en.wiktionary.org/wiki/kasi)

Feature

  • support HTTP, HTTPS service 12 14
  • virtualhost 1 9 16
  • reshape the endpoint 7 8
  • user-defined middlewares using javascript 13
  • cache control 15 17
  • timeout
  • KeepAlive
  • in-place response in config
  • error back
  • support CORS (Cross-Origin Resource Sharing) 2
  • statistic
  • service discovery
  • configuration by YAML 3
In-Place Response

If your managed source has some trouble and you want to send redirect response to the client for safety, you can easily make custom response within configuration e.g.

response: |
  HTTP/1.1 302 Found
  Location: http://www.iana.org/domains/example/
Error Back

When failed to get the expected response from source, the custom response will be sent to the client, that response can be made in configuration easily. As you guess, this error back also can be possible using middleware.

when-error:
  status:
    - 400 - 499
    - 500
  response: |
    HTTP/1.1 302 Found
    Location: http://www.iana.org/
User-Defined Middlewares

kasi has it's own middlewares and support the user-defined middlewares. You can write your own middlewares using javascript, so you easily manipulate the request and response.

The current middleware behaviors are higly affected by the Django Web Frameworks.

/*
`process_request` will be called before sending request to the target service.
*/
var process_request = function (endpointSetting, request) {
    // remove `If-Modified-Since`
    if (request.header["If-Modified-Since"]) {
        delete request.header["If-Modified-Since"];
    }
    
      // or, just return the response.
    var response = new Response();
    response.StatusCode = 304;
    response.Body = "";
    
    return response;
}

/*
`process_response` will be called after receiving the response from target service.
*/
var process_response = function (endpointSetting, reqeust, response) {
    response.Header["Expires"] = 10000;
    response.Body += "\n;";
    
    return response;
}
CORS

If you make the web application with ajax and your target service does not provide the CORS, you can make kasi can do it.

With cors and it's children settings, you can easily support CORS by services, endpoints or globally.

Service Discovery

kasi provides it's own API, so you can add or remove the services without any break or reload of kasi.

Statistic

kasi provides the special dashboard, it has the special statistic page. It will show you the current statistics for services.

  • number of requests by services and it's endpoints
  • statistics of status code by services and it's endpoints
  • etc.

You also apply the "Measurement Protocole" of Google Analytics using the user defined middlewares by services. 6

Cache Control

Usually for performance reason, most of the web applications and servers support cache with their own ways, e.g. ETAG4, or using headers like If-Modified-Since or Expires.

kasi respects their own cache mechanisms, and further more, explicitly supports cache.

With using cache of kasi, kasi will cache the data from the services and produce the response with it.

Support Virtual Domain

Like nginx, kasi supports the virtualhost with SSL.

Reshape Endpoints

If you connect to the API of github, you can rename the API endpoint like this,

https://api.github.com/users/spikeekips to https://api.kasi.org/github/u/spikeekips/

Naturally the original endpoint will be hided. With the custom middleware, which you can write, you can manipulate the request and response.

The interesting feature in reshaping is the using the regular expression to reshape. e.g.

https://api.github.com/users/(?P<username>) to https://api.kasi/{username}

timeout

In config, the timeout will set the tomeout globally, services or endpoints.

YAML configuration Example

This is example configuration.

%YAML 1.1

- default:
    cache:
        expire: 10m
        backend: [memory, memcache://127.0.0.1:11211, redis://127.0.0.1:6379]
    timeout: 5s

- service:
    bind: :8000
    hostname:
        - my0.github.com
        - my1.github.com
    ssl:
        cert: /secret/kasi-github.cert
        key: /secret/kasi-github.key
        pem: /secret/kasi-github.pem

    source: https://github.com/api/v1
    timeout: 10s
    endpoints:
        - endpoint:
            open: No
            to: /find/{username}
            from: /users/(?P<username>.*)
            source: [https://api0.github.com/v2, https://api1.github.com/v2]
        - endpoint:
            open: Yes
            source: https://api0.github.com/v2
            to: /github/\1
            from: (.*)
            timeout: 60s
            cors:
                allow-origin: http://localhost:9090
                # or allow-origin: [http://localhost:9090, http://localhost:9091]
                allow-methods: [GET, POST, PUT, OPTIONS, HEAD]
                allow-headers: [X-My-Header]
                allow-credentials: true
                max-age: 178000
...

Todo

  • more testing code
  • refactoring the entire code
  • clean up the monkey patch

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RESPONSE_HEADER_KEY_MUST_BE_SKIPPED []string = []string{
	"Content-Length",
}
View Source
var RE_STRIP_BLANKS *regexp.Regexp = func() *regexp.Regexp { r, _ := regexp.Compile("[\\s\\t]*$"); return r }()
View Source
var RE_STRIP_PREFIXED_TABS *regexp.Regexp = func() *regexp.Regexp { r, _ := regexp.Compile("^[\\t][\\t]*"); return r }()
View Source
var RE_STRIP_TABED_LINE *regexp.Regexp = func() *regexp.Regexp { r, _ := regexp.Compile("^[\\s\\t]*$"); return r }()

Functions

func HTTPServe

func HTTPServe(setting *kasi_conf.CoreSetting)

func HTTPServiceHandler

func HTTPServiceHandler(service *kasi_conf.ServiceSetting, w http.ResponseWriter, r *http.Request)

func ParseConfig

func ParseConfig(yaml string) (*kasi_conf.CoreSetting, error)

parse functions

func Run

func Run(yml string)

func SetLogging

func SetLogging(level logging.Level) *logging.Logger

Types

type HTTPResponse

type HTTPResponse struct {
	SourceResponse   *http.Response
	StatusCode       int
	Proto            string
	Header           map[string][]string
	Body             []byte
	TransferEncoding []string
	Trailer          map[string][]string
	Error            error
}

func GetFromSource

func GetFromSource(url string, r *http.Request) *HTTPResponse

func (*HTTPResponse) HasError

func (response *HTTPResponse) HasError() bool

type MiddlewareResponse

type MiddlewareResponse struct {
	StatusCode int
	Header     map[string]string
	Body       string
	Error      error
}

func HTTPMiddlewarePostResponse

func HTTPMiddlewarePostResponse(
	endpoint *kasi_conf.EndpointSetting,
	r *http.Request,
	response *HTTPResponse,
) *MiddlewareResponse

func HTTPMiddlewarePreRequest

func HTTPMiddlewarePreRequest(endpoint *kasi_conf.EndpointSetting, r *http.Request) *MiddlewareResponse

func NewMiddlewareResponseFromHTTPResponse

func NewMiddlewareResponseFromHTTPResponse(response *HTTPResponse) MiddlewareResponse

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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