Socket
Socket
Sign inDemoInstall

gopkg.in/kwiscale/framework.v1

Package Overview
Dependencies
0
Alerts
File Explorer

Install Socket

Detect and block malicious and high-risk dependencies

Install

    gopkg.in/kwiscale/framework.v1

Kwiscale is a framework that provides Handling System. That means that you will be able to create Handlers that handles HTTP Verbs methods. Kwiscale can handle basic HTTP Verbs as Get, Post, Delete, Patch, Head and Option. Kwiscale can also serve Websockets. Kwiscale provides a basic template system based on http/template from Go SDK and has got a plugin system to provides other template engines. The built-in template allows to override templates. Example: Also, built-in template provides two functions: Example: For "url" function, the first argument can be: Others arguments are the pair "key value". See http://gopkg.in/kwiscale/template-pongo2.v1 to use Pongo2. To handle HTTP Verbs: To be able to use configuration file (yaml), you MUST register handlers. The common way to do is to use "init()" function in you handlers package: Note: if you're using kwiscale CLI, `kwiscale new handler` command create the register call for you. Kwiscale provide a way to have method with parameter that are mapped from the given route. Example: Note that parameters names for Get() method are not used to map url values. Kwiscale maps values repecting the order found in the route. So you may declare Get() method like this: Anyway, you always may use "UserHandler.Vars": UserHandler.Vars["id"] and UserHandler.Vars["name"] that are `string` typed. You may use Init() and Destroy() method that are called before and after HTTP verb invocation. You may, for example, open database connection in "Init" and close the connection in "Destroy". Kwiscale provides a CLI: See http://readthedocs.org/projects/kwiscale/


Version published

Readme

Source

kwiscale

Join the chat at https://gitter.im/kwiscale/framework Build Status Coverage Status Documentation Status GoDoc

Web Middleware for Golang

At this time, Kwiscale is at the very begining of developpement. But you can test and give'em some pull-request to improve it.

Features

  • Implement your handlers as structs with HTTP Verbs as method
  • Plugin system for template engines, session engines and ORM
  • Use gorilla to manipulate routes and mux
  • Handler spawned with concurrency

How to use

Install with "go get" command:

go get gopkg.in/kwiscale/framework.v1

Create a project:

mkdir ~/myproject && cd ~/myproject

The common way to create handlers is to append a package::

mkdir handlers
vim handlers/index.go

Let's try an example:

package handlers

import "gopkg.in/kwiscale/framework.v1"

// this is the Index Handler that
// is composed by a RequestHandler
type IndexHandler struct {
    // compose your handler with kwiscale.Handler
    kwiscale.RequestHandler
}

// Will respond to GET request. "params" are url params (not GET and POST data)
func (i *IndexHandler) Get () {
    i.WriteString("Hello !" + i.Vars["userid"])
}

Then in you main.go::


package main

import (
    "gopkg.in/kwiscale/framework.v1"
    "./handlers"
)

// HomeHandler
type HomeHandler struct {
	kwiscale.RequestHandler
}

// Get respond to GET request
func (h *HomeHandler) Get (){
	h.WriteString("reponse to GET home")
}

// Another handler
type OtherHandler struct {
	kwiscale.RequestHandler
}

func (o *OtherHandler) Get (){
	// read url params
	// it always returns a string !
	userid := o.Vars["userid"]
	o.WriteString(fmt.Printf("Hello user %s", userid))
}


func main() {
	kwiscale.DEBUG = true
	app := kwiscale.NewApp(&kswicale.Config{
        Port: ":8000",
    })
	app.AddRoute("/", HomeHandler{})
	app.AddRoute("/user/{userid:[0-9]+}", OtherHandler{})
    app.ListenAndServe()

    // note: App respects http.Mux so you can use:
    // http.ListenAndServe(":9999", app)
    // to override behaviors, testing, or if your infrastructure
    // restricts this usage
}

Then run:

go run main.go

Or build your project:

go build main.go
./main

The Kwiscale way ?

Kwiscale let you declare Handler methods with the HTTP method. This allows you to declare:

  • Get()
  • Post()
  • Head()
  • Delete()
  • Put()
  • Patch()

Basic Templates

Kwiscale provides a "basic" template engine that use http/template. Kwiscale only add a "very basic template override system".

If you plan to have a complete override system, please use http://gopkg.in/kwiscale/template-pongo2.v1 that implements pango2 template.

See the following example.

Append templates directory:

mkdir templates

Then create templates/main.html:

<!DOCTYPE html>
<html>
    <head>
        <title>{{ if .title }}{{.title}}{{ else }} Default title {{ end }}</title>
    </head>
    <body>
        {{/* Remember to use "." as context */}}
        {{ template "CONTENT" . }}
    </body>
</html>

Now create templates/home directory:

mkdir templates/home

Create templates/home/welcome.html:

{{/* override "main.html" */}}

{{ define "CONTENT" }}
    This the welcome message {{ .msg }}
{{ end }}

This template overrides "main.html" (in ./templates/ directory) and append "CONTENT" template definition. So, the "CONTENT" block will appear at template "CONTENT" in "main.html". That's all.

In handlers/index.go you may now ask for template rendering:

func (h *IndexHandler) Get() {
    h.Render("home/welcome.html", map[string]string{
        "title" : "Welcome !!!",
        "msg"   : "Hello you",
    })
}

You can override template directory using App configuration passed to the constructor:

app := kwiscale.NewApp(&kswiscale.Config{
    TemplateDir: "./my-template-dir",
})

TODO

Features in progress:

  • Database ORM interface
  • Custom Error handler

FAQs

Last updated on 18 Feb 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc