Giter Site home page Giter Site logo

glean's Introduction

Glean

A go plugin framework that can reload variables and functions from plugins automatically.

License GoDoc travis Go Report Card

Installation

go get -u github.com/smallnest/glean

Feature

  • load symbol and you don't worry about errors
  • load/reload exported variables and funtions from plugins
  • watch plugins' changes and reload pointer of variables and function in applications

Notice glean only can reload functions or variables that can be addresses.

Examples

see Examples

Let's look the httpserver example to learn how to use glean.

httpserver

httpserver is a very very simple http server.

A simple http server is just like this:

var FooHandler = func(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "Hello, world")
}

http.Handle("/foo", fooHandler)

log.Fatal(http.ListenAndServe(":9988", nil))

Our goal is to replace fooHandler with latest code dynamically (hot fix).

var FooHandler = func(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "Hello, gp")
}

No need to restart this server.

step1: build the two plugin

enter _example/httpserver/plugins/plugin1 and _example/httpserver/plugins/plugin2, and run the build.sh to generate the so file.

Currently plugin supports linux and MacOS.

step2: modify the server implementation

package main

import (
	"log"
	"net/http"

	"github.com/smallnest/glean"
)

func main() {
	g := glean.New("plugin.json")
	err := g.LoadConfig()
	if err != nil {
		panic(err)
	}

	var fooHandler func(w http.ResponseWriter, r *http.Request)

	err = g.ReloadAndWatch("FooHandlerID", &fooHandler)

	if err != nil {
		panic(err)
	}

	http.HandleFunc("/foo", WarpFuncPtr(&fooHandler))

	log.Fatal(http.ListenAndServe(":9988", nil))
}

func WarpFuncPtr(fn *func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		(*fn)(w, r)
	}
}

Firstly create the Glean instance and load config from given file. And then use ReloadAndWatch to load fooHandler and begin to watch its changes. At last use WarpFuncPtr to wrap fooHandler as a HandleFunc.

Run go run main.go to start this server, use a browser to visit "http://locakhost:9988/foo" and you will see hello world

Change the config file plugin.json and replace "file": "plugins/plugin1/plugin1.so" with :

"file": "plugins/plugin2/plugin2.so",

Browser the prior location and you will see hello gp.

glean's People

Contributors

smallnest avatar

Watchers

kevin chen avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.