httpsrv

package module
v0.11.3 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2022 License: Apache-2.0 Imports: 26 Imported by: 38

README

httpsrv

httpsrv is a Lightweight, Modular, High Performance MVC web framework for the Go language.

Documents:

Quick Start

Install httpsrv framework

go get -u github.com/hooto/httpsrv

first hello world demo

package main

import (
    "github.com/hooto/httpsrv"
)

type Index struct {
    *httpsrv.Controller
}

func (c Index) IndexAction() {
    c.RenderString("hello world")
}

// init one module
func NewModule() httpsrv.Module {

	module := httpsrv.NewModule("default")

	//register controller to module
	module.ControllerRegister(new(Index))

	return module
}


func main() {

    // register module to httpsrv
    httpsrv.GlobalService.ModuleRegister("/", NewModule())

    // listening on port 8080
    httpsrv.GlobalService.Config.HttpPort = 8080

    // start
    httpsrv.GlobalService.Start()
}

Licensing

Licensed under the Apache License, Version 2.0

Documentation

Index

Constants

View Source
const (
	RouteTypeBasic  = "basic"
	RouteTypeStatic = "static"
)
View Source
const Version = "0.11.0"

Variables

Filters is the default set of global filters. It may be set by the application on initialization.

View Source
var (
	DefaultModule = Module{
		// contains filtered or unexported fields
	}
)
View Source
var (
	GlobalService = NewService()
)
View Source
var TemplateFuncs = map[string]interface{}{
	"eq": tfEqual,

	"raw": func(text string) template.HTML {
		return template.HTML(text)
	},

	"replace": func(s, old, new string) string {
		return strings.Replace(s, old, new, -1)
	},

	"date": func(t time.Time) string {
		return t.Format("2006-01-02")
	},
	"datetime": func(t time.Time) string {

		return t.Format("2006-01-02 15:04")
	},

	"upper": func(s string) string {
		return strings.ToUpper(s)
	},

	"lower": func(s string) string {
		return strings.ToLower(s)
	},

	"T": func(lang map[string]interface{}, msg string, args ...interface{}) string {
		return i18nTranslate(lang["LANG"].(string), msg, args...)
	},
}

Functions

func ActionInvoker

func ActionInvoker(c *Controller)

func I18nFilter

func I18nFilter(c *Controller)

func ParamsFilter

func ParamsFilter(c *Controller)

func RouterFilter

func RouterFilter(c *Controller)

func SessionFilter

func SessionFilter(c *Controller)

Types

type AcceptLanguage

type AcceptLanguage struct {
	Language string
	Quality  float32
}

A single language from the Accept-Language HTTP header.

type Config

type Config struct {
	HttpAddr         string `json:"http_addr,omitempty" toml:"http_addr,omitempty"` // e.g. "127.0.0.1", "unix:/tmp/app.sock"
	HttpPort         uint16 `json:"http_port,omitempty" toml:"http_port,omitempty"` // e.g. 8080
	HttpTimeout      uint16 `json:"http_timeout,omitempty" toml:"http_timeout,omitempty"`
	UrlBasePath      string `json:"url_base_path,omitempty" toml:"url_base_path,omitempty"`
	CookieKeyLocale  string `json:"cookie_key_locale,omitempty" toml:"cookie_key_locale,omitempty"`
	CookieKeySession string `json:"cookie_key_session,omitempty" toml:"cookie_key_session,omitempty"`
	CompressResponse bool   `json:"compress_response,omitempty" toml:"compress_response,omitempty"`
}

func (*Config) I18n

func (c *Config) I18n(file string)

func (*Config) TemplateFuncRegister

func (c *Config) TemplateFuncRegister(name string, fn interface{})

type Controller

type Controller struct {
	Name       string // The controller name, e.g. "App"
	ActionName string // The action name, e.g. "Index"
	Request    *Request
	Response   *Response
	Params     *Params  // Parameters from URL and form (including multipart).
	Session    *Session // Session, stored in cookie, signed.
	AutoRender bool
	Data       map[string]interface{}
	// contains filtered or unexported fields
}

func NewController

func NewController(srv *Service, req *Request, resp *Response) *Controller

func (*Controller) Redirect

func (c *Controller) Redirect(url string)

func (*Controller) Render

func (c *Controller) Render(args ...interface{})

func (*Controller) RenderError

func (c *Controller) RenderError(status int, msg string)

func (*Controller) RenderJson

func (c *Controller) RenderJson(obj interface{})

func (*Controller) RenderJsonIndent

func (c *Controller) RenderJsonIndent(obj interface{}, indent string)

func (*Controller) RenderString

func (c *Controller) RenderString(body string)

func (*Controller) Translate

func (c *Controller) Translate(msg string, args ...interface{}) string

func (*Controller) UrlBase

func (c *Controller) UrlBase(path string) string

func (*Controller) UrlModuleBase

func (c *Controller) UrlModuleBase(path string) string

type Filter

type Filter func(c *Controller)

type Module

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

func NewModule

func NewModule(name string) Module

func NewStaticModule

func NewStaticModule(name, path string) Module

func (*Module) ControllerRegister

func (m *Module) ControllerRegister(c interface{})

func (*Module) RouteSet

func (m *Module) RouteSet(r Route)

func (*Module) TemplateFileSystemSet

func (m *Module) TemplateFileSystemSet(fss ...http.FileSystem)

func (*Module) TemplatePathSet

func (m *Module) TemplatePathSet(paths ...string)

type Params

type Params struct {
	url.Values // A unified view of all the individual param maps below

	// Set by the ParamsFilter
	Query url.Values // Parameters from the query string, e.g. /index?limit=10
	Form  url.Values // Parameters from the request body.
}

func (*Params) Int64

func (p *Params) Int64(key string) int64

func (*Params) String

func (p *Params) String(key string) string

func (*Params) Uint64

func (p *Params) Uint64(key string) uint64

type Request

type Request struct {
	*http.Request
	ContentType    string
	AcceptLanguage []AcceptLanguage
	Locale         string
	RequestPath    string
	UrlPathExtra   string
	RawBody        []byte
	WebSocket      *WebSocket
}

func NewRequest

func NewRequest(r *http.Request) *Request

func (*Request) JsonDecode

func (req *Request) JsonDecode(obj interface{}) error

func (*Request) RawAbsUrl

func (req *Request) RawAbsUrl() string

type Response

type Response struct {
	Status int
	Out    http.ResponseWriter
	// contains filtered or unexported fields
}

func NewResponse

func NewResponse(w http.ResponseWriter) *Response

func (*Response) Header added in v0.11.0

func (resp *Response) Header() http.Header

func (*Response) Write added in v0.11.0

func (resp *Response) Write(b []byte) (int, error)

func (*Response) WriteHeader

func (resp *Response) WriteHeader(status int)

type Route

type Route struct {
	Type       string
	Path       string
	StaticPath string
	Params     map[string]string // e.g. {id: 123}

	BinFs http.FileSystem
	// contains filtered or unexported fields
}

type Service

type Service struct {
	Config  Config
	Filters []Filter

	TemplateLoader *TemplateLoader
	// contains filtered or unexported fields
}

func NewService

func NewService() *Service

func (*Service) Error

func (s *Service) Error() error

func (*Service) HandlerFuncRegister

func (s *Service) HandlerFuncRegister(baseuri string, h http.HandlerFunc)

func (*Service) HandlerRegister

func (s *Service) HandlerRegister(baseuri string, h http.Handler)

func (*Service) ModuleRegister

func (s *Service) ModuleRegister(baseuri string, mod Module)

func (*Service) RpcRegister

func (s *Service) RpcRegister(baseuri string, rcvr interface{})

func (*Service) Start

func (s *Service) Start() error

func (*Service) Stop

func (s *Service) Stop() error

type Session

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

func (*Session) AuthToken

func (s *Session) AuthToken(key string) string

func (*Session) Get

func (s *Session) Get(key string) string

type TemplateLoader

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

This object handles loading and parsing of templates. Everything below the application's views directory is treated as a template.

func (*TemplateLoader) Clean

func (it *TemplateLoader) Clean(modName string)

func (*TemplateLoader) Render

func (it *TemplateLoader) Render(wr io.Writer, modName, tplPath string, arg interface{}) error

func (*TemplateLoader) Set

func (it *TemplateLoader) Set(modName string, viewpaths []string, viewfss []http.FileSystem)

type WebSocket

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

func (WebSocket) Close

func (ws WebSocket) Close()

func (WebSocket) Receive

func (ws WebSocket) Receive(v interface{}) error

func (WebSocket) Send

func (ws WebSocket) Send(v interface{}) error

func (WebSocket) SendJson

func (ws WebSocket) SendJson(v interface{}) error

Directories

Path Synopsis
deps
go.net/websocket
Package websocket implements a client and server for the WebSocket protocol as specified in RFC 6455.
Package websocket implements a client and server for the WebSocket protocol as specified in RFC 6455.

Jump to

Keyboard shortcuts

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