simgo

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

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

Go to latest
Published: Jan 20, 2017 License: Apache-2.0 Imports: 20 Imported by: 20

README

simgo

A simple framework for building a HTTP API micro service.

Usage

Get the source code

$ go get -u github.com/zieckey/simgo
$ git clone https://github.com/zieckey/simgo

Write a simplest example

$ mkdir echo
$ cd echo
$ vim echo.go
package main

import (
	"net/http"
	"github.com/zieckey/simgo"
	"io/ioutil"
)

type EchoModule struct {}

func (m *EchoModule) Initialize() error {
	println("EchoModule initializing ...")
	simgo.HandleFunc("/echo", m.Echo, m).Methods("POST")
	return nil
}

func (m *EchoModule) Uninitialize() error {
	return nil
}

func (m *EchoModule) Echo(w http.ResponseWriter, r *http.Request) {
	buf, err := ioutil.ReadAll(r.Body)
	if err == nil {
		w.Write(buf)
		return
	}

	w.WriteHeader(403)
	w.Write([]byte(err.Error()))
}


func main() {
	fw := simgo.DefaultFramework
	fw.RegisterModule("echo", new(EchoModule))
	err := fw.Initialize()
	if err != nil {
		panic(err.Error())
	}

	fw.Run()
}

Build it and run it with the default config file github.com/zieckey/simgo/conf/app.ini

$ go build
$ ls ../
simgo echo ...
$ ./echo -f ../simgo/conf/app.ini

In another console, we can use curl to test it:

$ curl http://127.0.0.1:9360/echo -d XXX
XXX

Documentation

Index

Constants

View Source
const (
	StatusOK  = "OK"
	QPollerOK = "ok"
	MAINTAIN  = "MAINTAIN"
	FAILED    = "FAILED"
)

Variables

View Source
var ConfPath = flag.String("f", "../conf/app.ini", "The config INI file path")
View Source
var DefaultFramework = &duxFramework

The default unis.instance

Functions

func HandleFunc

func HandleFunc(path string, f func(http.ResponseWriter, *http.Request), m Module) *mux.Route

HandleFunc registers a new route with a matcher for the URL path. See Route.Path() and Route.HandlerFunc().

Types

type AdminModule

type AdminModule struct {
}

func (*AdminModule) Initialize

func (m *AdminModule) Initialize() error

func (*AdminModule) Reload

func (m *AdminModule) Reload(w http.ResponseWriter, r *http.Request)

func (*AdminModule) Uninitialize

func (m *AdminModule) Uninitialize() error

type Framework

type Framework struct {
	Conf         *goini.INI
	ConfigPath   string
	DBufManager  *dbuf.Manager
	Router       *mux.Router
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
	// contains filtered or unexported fields
}

func (*Framework) Initialize

func (fw *Framework) Initialize() error

Initialize 框架初始化,在RegisterModule之后调用

func (*Framework) RegisterModule

func (fw *Framework) RegisterModule(name string, m Module) error

RegisterModule 会将应用层写的模块注册到框架中。注意必须在Run/Initialize等方法之前调用该函数

func (*Framework) Run

func (fw *Framework) Run()

Run 会启动 server 进入监听状态

type Handler

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

type HandlerFunc

type HandlerFunc func(w http.ResponseWriter, r *http.Request)

type Module

type Module interface {
	Initialize() error
	Uninitialize() error
}

type MonitorModule

type MonitorModule struct {
}

func (*MonitorModule) Initialize

func (m *MonitorModule) Initialize() error

func (*MonitorModule) Status

func (m *MonitorModule) Status(w http.ResponseWriter, req *http.Request)

func (*MonitorModule) Uninitialize

func (m *MonitorModule) Uninitialize() error

Directories

Path Synopsis
echo command

Jump to

Keyboard shortcuts

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