mate

package module
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2025 License: MIT Imports: 13 Imported by: 0

README ΒΆ

Mate πŸ§‰

Mate is a simple, lightweight, fast an zero-dependency web server with batteries included http server.

Getting Started πŸš€

Installation πŸ“¦

go get github.com/damianpumar/mate

Usage πŸƒβ€β™€οΈ

package main

import (
	"errors"
	"flag"
	"time"

	"github.com/damianpumar/mate"
	"github.com/damianpumar/mate/database"
)

type Example struct {
	Id   string `json:"id"`
	Name string `json:"name"`
}

var (
	port = flag.String("port", "8080", "Port to listen on")
)

func main() {
	flag.Parse()

	server := mate.New()

	db := database.Connect()

	cookie := mate.NewSecureCookie("my-secret")

	server.Get("/cookie", mate.LoggingMiddleware(func(c *mate.Context) {
		if err := cookie.SetEncryptedCookie(c.Response, "session", "user12345", 30*time.Second); err != nil {

			c.Error(500, err)

			return
		}

		c.Text(200, "Cookie set")
	}))

	server.Get("/read-cookie", mate.LoggingMiddleware(func(c *mate.Context) {
		value, err := cookie.GetEncryptedCookie(c.Request.Request, "session")

		if err != nil {
			c.Error(500, err)

			return
		}

		c.Text(200, value)
	}))

	server.Get("/delete-cookie", mate.LoggingMiddleware(func(c *mate.Context) {
		cookie.ClearCookie(c.Response, "session")
		c.Response.Text(200, "Cookie deleted")
	}))

	server.Get("/", mate.LoggingMiddleware(func(c *mate.Context) {
		data := db.Select("users")

		c.JSON(200, data)
	}))

	server.Get("/{id}", func(c *mate.Context) {
		id := c.GetPathValue("id")

		data, ok := db.SelectById("users", id)

		if !ok {
			c.Error(404, errors.New("User not found"))
		}

		c.JSON(200, data)
	})

	server.Post("/", func(c *mate.Context) {
		data := Example{}

		c.BindBody(&data)

		db.Insert("users", data)

		c.JSON(200, data)
	})

	server.Put("/{id}", func(c *mate.Context) {
		id := c.GetPathValue("id")

		data := Example{}

		c.BindBody(&data)

		if ok := db.Update("users", id, data); !ok {
			c.Status(404)

			return
		}

		c.JSON(200, data)
	})

	server.Delete("/{id}", func(c *mate.Context) {
		id := c.GetPathValue("id")

		if ok := db.Delete("users", id); !ok {
			c.Status(404)

			return
		}

		c.Text(200, "Deleted")
	})

	server.Group("/hello", func(g *mate.Group) {
		g.Get("/world", func(c *mate.Context) {
			c.Text(200, "Hello, World!")
		})
	})

	server.Get("/template", func(c *mate.Context) {
		c.Render(200, "index.html", &Example{
			Id:   "1",
			Name: "DamiΓ‘n",
		})
	})

	server.Folder("/static", "./static")
	server.File("/json", "./static/hello.json")

	server.Start(*port)
}

Running the server πŸš€

go run main.go

License πŸ“„

MIT

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

This section is empty.

Types ΒΆ

type Context ΒΆ

type Context struct {
	Response *Response
	Request  *Request
}

func NewContext ΒΆ

func NewContext(w http.ResponseWriter, r *http.Request) *Context

func (*Context) BindBody ΒΆ

func (c *Context) BindBody(data interface{}) error

func (*Context) Error ΒΆ

func (c *Context) Error(status int, err error)

func (*Context) GetPathValue ΒΆ

func (c *Context) GetPathValue(key string) string

func (*Context) GetQueryParam ΒΆ

func (c *Context) GetQueryParam(key string) string

func (*Context) JSON ΒΆ

func (c *Context) JSON(status int, data interface{})

func (*Context) Render ΒΆ added in v0.0.7

func (c *Context) Render(status int, template string, data interface{})

func (*Context) Status ΒΆ

func (c *Context) Status(status int)

func (*Context) Text ΒΆ

func (c *Context) Text(status int, text string)

type Group ΒΆ added in v0.0.6

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

func (*Group) Delete ΒΆ added in v0.0.6

func (g *Group) Delete(path string, handler HandlerFunc)

func (*Group) File ΒΆ added in v0.0.7

func (g *Group) File(path string, file string)

func (*Group) Folder ΒΆ added in v0.0.7

func (g *Group) Folder(path string, dir string)

func (*Group) Get ΒΆ added in v0.0.6

func (g *Group) Get(path string, handler HandlerFunc)

func (*Group) Patch ΒΆ added in v0.0.6

func (g *Group) Patch(path string, handler HandlerFunc)

func (*Group) Post ΒΆ added in v0.0.6

func (g *Group) Post(path string, handler HandlerFunc)

func (*Group) Put ΒΆ added in v0.0.6

func (g *Group) Put(path string, handler HandlerFunc)

type HandlerFunc ΒΆ

type HandlerFunc func(c *Context)

func LoggingMiddleware ΒΆ

func LoggingMiddleware(next HandlerFunc) HandlerFunc

type Middleware ΒΆ

type Middleware func(HandlerFunc) HandlerFunc

type Request ΒΆ

type Request struct {
	*http.Request
}

func (*Request) BindBody ΒΆ

func (r *Request) BindBody(data interface{}) error

func (*Request) GetPathValue ΒΆ

func (r *Request) GetPathValue(key string) string

func (*Request) GetQueryParam ΒΆ

func (r *Request) GetQueryParam(key string) string

type Response ΒΆ

type Response struct {
	http.ResponseWriter
}

func (*Response) Error ΒΆ

func (r *Response) Error(status int, err error)

func (*Response) JSON ΒΆ

func (r *Response) JSON(status int, data interface{})

func (*Response) Render ΒΆ added in v0.0.7

func (r *Response) Render(status int, file string, data interface{})

func (*Response) Status ΒΆ

func (r *Response) Status(status int)

func (*Response) Text ΒΆ

func (r *Response) Text(status int, message string)

type Router ΒΆ

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

func NewRouter ΒΆ

func NewRouter() *Router

func (*Router) Delete ΒΆ

func (r *Router) Delete(path string, handler func(c *Context))

func (*Router) File ΒΆ added in v0.0.7

func (r *Router) File(path string, file string)

func (*Router) Folder ΒΆ added in v0.0.7

func (r *Router) Folder(path string, dir string)

func (*Router) Get ΒΆ

func (r *Router) Get(path string, handler func(c *Context))

func (*Router) Patch ΒΆ

func (r *Router) Patch(path string, handler func(c *Context))

func (*Router) Post ΒΆ

func (r *Router) Post(path string, handler func(c *Context))

func (*Router) Put ΒΆ

func (r *Router) Put(path string, handler func(c *Context))

func (*Router) Routes ΒΆ

func (s *Router) Routes() *http.ServeMux

func (*Router) ServeHTTP ΒΆ

func (r *Router) ServeHTTP(res http.ResponseWriter, req *http.Request)

type SecureCookie ΒΆ

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

func NewSecureCookie ΒΆ

func NewSecureCookie(key string) *SecureCookie

func (*SecureCookie) ClearCookie ΒΆ

func (sc *SecureCookie) ClearCookie(w http.ResponseWriter, name string)

func (*SecureCookie) GetEncryptedCookie ΒΆ

func (sc *SecureCookie) GetEncryptedCookie(req *http.Request, name string) (string, error)

func (*SecureCookie) HasCookie ΒΆ

func (sc *SecureCookie) HasCookie(req *http.Request, name string) bool

func (*SecureCookie) SetEncryptedCookie ΒΆ

func (sc *SecureCookie) SetEncryptedCookie(w http.ResponseWriter, name, value string, expiry time.Duration) error

type Server ΒΆ

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

func New ΒΆ

func New() *Server

func (*Server) Delete ΒΆ

func (s *Server) Delete(path string, handler HandlerFunc)

func (*Server) File ΒΆ added in v0.0.7

func (s *Server) File(path string, file string)

func (*Server) Folder ΒΆ added in v0.0.7

func (s *Server) Folder(path string, dir string)

func (*Server) Get ΒΆ

func (s *Server) Get(path string, handler HandlerFunc)

func (*Server) Group ΒΆ added in v0.0.6

func (s *Server) Group(path string, group func(r *Group))

func (*Server) Patch ΒΆ added in v0.0.6

func (s *Server) Patch(path string, handler HandlerFunc)

func (*Server) Post ΒΆ

func (s *Server) Post(path string, handler HandlerFunc)

func (*Server) Put ΒΆ

func (s *Server) Put(path string, handler HandlerFunc)

func (*Server) ServeHTTP ΒΆ

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Server) Start ΒΆ

func (s *Server) Start(port string)

Directories ΒΆ

Path Synopsis

Jump to

Keyboard shortcuts

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