ris

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2023 License: MIT Imports: 12 Imported by: 0

README

ris - 基于iris的插件式框架

ris是对Iris框架进一步插件式封装,定义和组合了组件、插件、中间件、日志、测试、全局实例等,方便开发者快速开发和调试,期待您的星星🌟🌟🌟🌟🌟🌟。

快速开始

package main

import (
	"github.com/vnaki/ris"
	"github.com/vnaki/ris/examples/routes"
	"github.com/vnaki/ris/middlewares"
	"github.com/vnaki/ris/plugins"
)

func main()  {
	e := ris.New()

	// post max memory
	e.SetPostMemory(20 << 20)

	e.RouteMiddleware(middlewares.Cors)

	e.Plugin("logger", plugins.LoggerPlugin)
	//e.Plugin("data", plugins.MysqlPlugin)
	e.Plugin("data", plugins.SqlitePlugin)

	// default module
	e.Module("/", routes.ApiRoute)

	if err := e.Run("./config/app.yaml"); err != nil {
		panic(err)
	}
}

详细用例

see examples

框架定义
package types

import (
	"github.com/vnaki/ris/config"

	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
)

// Database 数据库接口
type Database interface {
	Connect() (db.Session, error)
}

// PluginHandler 插件函数
type PluginHandler func(string, Engine) error

// Plugin 插件结构
type Plugin struct {
	// 插件名称
	Name string
	// 插件函数
	Handler PluginHandler
}

// Module 模块函数
type Module func(*mvc.Application)

// MiddlewareHandler 中间件处理
type MiddlewareHandler func(Engine) iris.Handler

// Middleware 中间件
type Middleware struct {
	// 是否路由中间件
	Route bool
	// 中间件处理
	Handler MiddlewareHandler
}

// Component 组件类型
type Component interface{}

// Worker 工作协程
type Worker func(string, Engine)

// Engine 应用引擎
type Engine interface {
	// App iris应用
	App() *iris.Application
	// Set 注册实例
	Set(name string, component Component)
    // SetPostMemory 设置POST最大内存
	SetPostMemory(memory int64)
	// Get 返回实例
	Get(name string) Component
	// Reset 重置配置
	Reset(func(c *config.Config))
	// Config 配置信息
	Config() *config.Config
	// Plugin 注册插件
	Plugin(name string, plugin PluginHandler)
	// Worker 注册工作协程
	Worker(name string, plugin Worker)
	// Module 注册模块
	Module(party string, module Module)
	// Middleware 注册通用中间件
	Middleware(handler MiddlewareHandler)
	// RouteMiddleware 路由中间件
	RouteMiddleware(middleware MiddlewareHandler)
	// IfMiddleware 注册条件中间件
	IfMiddleware(mode string, middleware MiddlewareHandler)
	// IfRouteMiddleware 注册路由条件中间件
	IfRouteMiddleware(mode string, middleware MiddlewareHandler)
	// Stop 停止服务
	Stop() error
	// Implement 自定义业务
	Implement(func() error)
	// Defer 延迟函数
	Defer(f func())
	// IsDev 是否开发环境
	IsDev() bool
	// Run 运行程序
	Run(file string) error
	// Test 执行测试
	Test(file string) error
	// Parse 解析配置
	Parse(file string, out interface{}) error
}

插件定义
package plugins

import (
	"fmt"
	"github.com/vnaki/ris/components/database"
	"github.com/vnaki/ris/types"
)

func MysqlPlugin(name string, e types.Engine) error {
	n := database.New()

	if err := e.Parse(e.Config().Mysql, n); err != nil {
		return err
	}

	sess, err := n.Connect()
	if err != nil {
		return err
	}

	e.Set(name, sess)

	e.Defer(func() {
		_ = sess.Close()

		// verbose
		fmt.Println("defer: mysql closed")
	})

	return nil
}

框架依赖
期待赞助

有了您的赞助👑,我们可以加快ris的设计和开发进度,为用户提供更高质量的软件,期待合作~

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Engine

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

func New

func New() *Engine

func (*Engine) App

func (e *Engine) App() *iris.Application

func (*Engine) Config

func (e *Engine) Config() *config.Config

func (*Engine) Defer

func (e *Engine) Defer(f func())

func (*Engine) Get

func (e *Engine) Get(name string) types.Component

func (*Engine) IfMiddleware

func (e *Engine) IfMiddleware(env string, middleware types.MiddlewareHandler)

func (*Engine) IfRouteMiddleware

func (e *Engine) IfRouteMiddleware(env string, middleware types.MiddlewareHandler)

func (*Engine) Implement

func (e *Engine) Implement(fn func() error)

func (*Engine) IsDev

func (e *Engine) IsDev() bool

func (*Engine) Middleware

func (e *Engine) Middleware(middleware types.MiddlewareHandler)

func (*Engine) Module

func (e *Engine) Module(name string, module types.Module)

func (*Engine) Parse

func (e *Engine) Parse(file string, out interface{}) error

func (*Engine) Plugin

func (e *Engine) Plugin(name string, plugin types.PluginHandler)

func (*Engine) Reset

func (e *Engine) Reset(f func(c *config.Config))

func (*Engine) RouteMiddleware

func (e *Engine) RouteMiddleware(middleware types.MiddlewareHandler)

func (*Engine) Run

func (e *Engine) Run(file string) error

func (*Engine) Set

func (e *Engine) Set(name string, component types.Component)

func (*Engine) SetPostMemory

func (e *Engine) SetPostMemory(memory int64)

func (*Engine) Stop

func (e *Engine) Stop() error

func (*Engine) Test

func (e *Engine) Test(file string) error

func (*Engine) Worker

func (e *Engine) Worker(name string, worker types.Worker)

Directories

Path Synopsis
components

Jump to

Keyboard shortcuts

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