gopress

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2019 License: MIT Imports: 15 Imported by: 0

README

gopress

Build Status Go Report Card codecov

基于 Echo 的应用级Golang Web框架。

特性

  1. 基于Echo
  2. View层使用 Handlebars (raymond) 做为模板引擎,支持全局 Partials
  3. Controller 层抽象
  4. Service 层抽象
  5. Service 注册中心
  6. Logging 使用logrus,输出格式为JSON
  7. 脚手架代码生成:service, controller, middleware, model, view, main

安装

使用 go get 命令安装最新版本。下面的代码同时安装 gopress 和它的命令行工具。

go get -u github.com/fpay/gopress/gopress

然后在项目中引入 gopress

import "github.com/fpay/gopress"

脚手架

按照上面的步骤安装了命令行工具后,就可以使用项目脚手架了。

# 生成 main.go
gopress make entry

# 生成 users 和 posts 控制器
gopress make controller users posts

# 生成 login 页面模板
gopress make view login

# 生成 cache 服务
gopress make service cache

详情可执行 gopress make --help 查看具体使用方法。

Sample Project

gopress-kick-starter

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// RequestHeaderProtocol header for identifying the protocol (HTTP or HTTPS) that a client used to connect to your proxy or load balancer.
	RequestHeaderProtocol = "X-Forwarded-Proto"

	// RequestHeaderForwarded header for identifying the originating IP address of a client connecting to a web server through an HTTP proxy or a load balancer.
	RequestHeaderForwarded = "X-Forwarded-For"

	// RequestHeaderRequestedWith header for JavaScript libraries sending requests from browser.
	RequestHeaderRequestedWith = "X-Requested-With"
)
View Source
var ContextLogger = log.Extract

ContextLogger returns logger entry for current request context

View Source
var ContextRequestID = ExtractRequestID

Functions

func ExtractRequestID

func ExtractRequestID(ctx Context) string

ExtractRequestID returns ID for current request

func NewTemplateRenderer

func NewTemplateRenderer(root string) echo.Renderer

NewTemplateRenderer 创建新的模板渲染器 root是模板文件的根路径

func RequestIsAJAX added in v1.0.1

func RequestIsAJAX(req *http.Request) bool

RequestIsAJAX check request header to see if the request is a XMLHttpRequest.

func RequestRawURL

func RequestRawURL(r *http.Request) string

RequestRawURL returns request original URL.

func RequestRemoteAddr

func RequestRemoteAddr(req *http.Request) string

RequestRemoteAddr finds the real remote address from request.

If the web server is behind an HTTP proxy or a load balancer, http.Request.RemoteAddr is IP of the proxy or load balancer. But most HTTP proxies and load balancers will attach a header to tell the web server request's real IP.

func RequestScheme

func RequestScheme(r *http.Request) string

RequestScheme try to parses request scheme.

If the web server is behind an HTTP proxy or a load balancer, it's hard to known if original request's scheme by http.Request.TLS property. Most HTTP proxies and load balancers will attach a header to tell upstream server the request's real scheme. So check if the header is set first.

Types

type App

type App struct {
	*echo.Echo

	Logger *Logger
}

App wrapper of echo.Echo and Container

func ContextApp

func ContextApp(ctx Context) *App

ContextApp try to get App instance from Context

type AppContext

type AppContext struct {
	echo.Context
	// contains filtered or unexported fields
}

AppContext is wrapper of echo.Context. It holds App instance of server.

func NewAppContext

func NewAppContext(c echo.Context, app *App, logger *logrus.Entry) *AppContext

func (*AppContext) App

func (c *AppContext) App() *App

App returns the App instance

type Context

type Context = echo.Context

Context is alias of echo.Context

type Controller

type Controller interface {

	// RegisterRoutes 注册控制器路由
	RegisterRoutes(app *App)
}

Controller 控制器接口

type HandlerFunc

type HandlerFunc = echo.HandlerFunc

HandlerFunc is alias of echo.HandlerFunc

type Logger

type Logger = log.Logger

Logger wraps logrus.Logger

type MiddlewareFunc

type MiddlewareFunc = echo.MiddlewareFunc

MiddlewareFunc is alias of echo.MiddlewareFunc

func NewAppContextMiddleware

func NewAppContextMiddleware(app *App) MiddlewareFunc

NewAppContextMiddleware returns a middleware which extends echo.Context

type Server

type Server struct {
	Logger *Logger
	// contains filtered or unexported fields
}

Server HTTP服务器

func NewServer

func NewServer(options ServerOptions) *Server

NewServer 创建HTTP服务器

func (*Server) App

func (s *Server) App() *App

App returns App instance of server

func (*Server) RegisterControllers

func (s *Server) RegisterControllers(cs ...Controller)

RegisterControllers 注册控制器列表

func (*Server) RegisterGlobalMiddlewares

func (s *Server) RegisterGlobalMiddlewares(middlewares ...MiddlewareFunc)

RegisterGlobalMiddlewares 注册全局中间件

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown 关闭服务器

func (*Server) Start

func (s *Server) Start() error

Start 启动HTTP服务器

func (*Server) StartTLS

func (s *Server) StartTLS(cert, key string) error

StartTLS 启动HTTPS服务器

type ServerOptions

type ServerOptions struct {
	Host   string        `yaml:"host" mapstructure:"path"`
	Port   int           `yaml:"port" mapstructure:"port"`
	Views  string        `yaml:"views" mapstructure:"views"`
	Static StaticOptions `yaml:"static" mapstructure:"static"`
}

ServerOptions 服务器配置

type StaticOptions

type StaticOptions struct {
	Path string `yaml:"path" mapstructure:"path"`
	Root string `yaml:"root" mapstructure:"root"`
}

StaticOptions 静态文件配置

type TemplateRenderer

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

TemplateRenderer 自定义模板渲染器

func (*TemplateRenderer) Parse

func (t *TemplateRenderer) Parse(name string) (*raymond.Template, error)

Parse 根据name获取解析后的模板 模板解析成功后缓存在Map中,下次使用时直接从Map读取

func (*TemplateRenderer) Render

func (t *TemplateRenderer) Render(w io.Writer, name string, data interface{}, c echo.Context) error

Render 渲染模板 实现 echo.Renderer 接口

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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