mwebserv

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2021 License: MIT Imports: 12 Imported by: 0

README

mwebserv (the tiny web framework)

This framework is made for personal used, you may take it by your own risk

Example

package main

import (
	m "github.com/authapon/mwebserv"
)

func main() {
	web := m.New()
	web.Get("/", wserve)
	web.Post("/", pserve)
	web.Serve(":9900")
}

func wserve(c *m.MContext) {
	c.WriteString("Hello, world! in GET Method")
}

func pserve(c *m.MContext) {
	c.WriteString("Hello, world! in POST Method")
}

Static file

func main() {
	web := m.New()
	web.Static("static")
	...
}

Embeded Static file

func main() {
	web := m.New()
	web.SetAsset(Asset, AssetNames) // This method must call before "StaticBindata"
	web.StaticBindata("static")	
	...
}

Route Variable

func main() {
	web := m.New()
	web.Get("/hello/:name", hello)
	...
}

func hello(c *m.MContext) {
	c.WriteString("Hello, " + c.V["name"])
}

Query Variable

example query http://example.com/send?name=jonny&food=apple

func hello(c *m.MContext) {
	c.WriteString("Hello, " + c.Q.Get("name") + " and you eat " + c.Q.Get("food"))
}

Middleware

func main() {
	web := m.New()
	web.Use(log)
	...
}

func log(c *m.MContext) {
	fmt.Printf("URL : %s\n", c.R.URL.Path)
	c.Next()
}

JSON read and write

type DataStruct struct {
	Name string  `json:"name"`
	Food string  `json:"food"`
}

func hello(c *m.MContext) {
	var data DataStruct
	c.ReadJSON(&data)
	...
	...
	...
	c.WriteJSON(&DataStruct{Name: "jonny", Food: "apple"})
}

Render from template

func main() {
	web := m.New()
	web.View("template") // Add template folder, allow only .html file template with html/template
	web.Get("/", startPage)
	...
}

func startPage(c *m.MContext) {
	data := make(map[string]string)
	data["name"] = "jonny"
	data["food"] = "apple"
	c.Render("index.html", data)
}

File: template/index.html

{{ define "index.html" }}
<html>
<body>
Hello, {{ .name }} and you eat {{ .food }}
</body>
</html>
{{ end }}

Render from embeded template

func main() {
	web := m.New()
	web.SetAsset(Asset, AssetNames) // This method must call before "ViewBindata"
	web.ViewBindata("template")	
	...	
}

Redirect

func page1(c *m.MContext) {
	c.Redirect("/hello")
}

Get Remote Address IP

func page(c *m.MContext) {
	ip := c.RemoteAddr()
	...
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MContext

type MContext struct {
	W    http.ResponseWriter
	R    *http.Request
	Data map[string]interface{}
	V    map[string]string
	Q    url.Values
	// contains filtered or unexported fields
}

func (*MContext) Next

func (c *MContext) Next()

func (*MContext) ReadJSON

func (c *MContext) ReadJSON(v interface{}) error

func (*MContext) Redirect

func (c *MContext) Redirect(route string)

func (*MContext) RemoteAddr

func (c *MContext) RemoteAddr() string

func (*MContext) Render

func (c *MContext) Render(tname string, data interface{})

func (*MContext) RenderStatus

func (c *MContext) RenderStatus(status int, tname string, data interface{})

func (*MContext) ServeFileStatic

func (c *MContext) ServeFileStatic(fname string)

func (*MContext) ServeFileStaticBindata

func (c *MContext) ServeFileStaticBindata(fname string)

func (*MContext) WriteHTML

func (c *MContext) WriteHTML(data string)

func (*MContext) WriteHTMLStatus

func (c *MContext) WriteHTMLStatus(status int, data string)

func (*MContext) WriteJSON

func (c *MContext) WriteJSON(v interface{})

func (*MContext) WriteJSONStatus

func (c *MContext) WriteJSONStatus(status int, v interface{})

func (*MContext) WriteString

func (c *MContext) WriteString(data string)

func (*MContext) WriteStringStatus

func (c *MContext) WriteStringStatus(status int, data string)

type MHandler

type MHandler func(*MContext)

type MWeb

type MWeb struct {
	NotFound MHandler
	// contains filtered or unexported fields
}

func New

func New() *MWeb

func (*MWeb) Get

func (m *MWeb) Get(r string, f MHandler)

func (*MWeb) Post

func (m *MWeb) Post(r string, f MHandler)

func (*MWeb) ReadTimeout

func (m *MWeb) ReadTimeout(t time.Duration)

func (*MWeb) Serve

func (m *MWeb) Serve(host string)

func (*MWeb) ServeHTTP

func (m *MWeb) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*MWeb) SetAsset

func (m *MWeb) SetAsset(asset func(string) ([]byte, error), assetnames func() []string)

func (*MWeb) Static

func (m *MWeb) Static(dir string)

func (*MWeb) StaticBindata

func (m *MWeb) StaticBindata(dir string)

func (*MWeb) Use

func (m *MWeb) Use(f MHandler)

func (*MWeb) View

func (m *MWeb) View(dir string)

func (*MWeb) ViewBindata

func (m *MWeb) ViewBindata(dir string)

func (*MWeb) WriteTimeout

func (m *MWeb) WriteTimeout(t time.Duration)

Jump to

Keyboard shortcuts

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