expresso

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2022 License: MIT Imports: 11 Imported by: 0

README

expresso

Fast, unopinionated, minimalist web framework for Go

Expresso is a web framework written in Go (GoLang).

Examples
Hello World
package main

import "github.com/pr47h4m/expresso"

func main() {
	app := expresso.DefaultApp()

	app.GET("/", func(ctx *expresso.Context) {
		ctx.Send(expresso.Text{
			Text: "Hello World",
		})
	})

	app.ListenAndServe(":80")
}
Basic Routing
package main

import "github.com/pr47h4m/expresso"

func main() {
	app := expresso.DefaultApp()

	app.GET("/", func(ctx *expresso.Context) {
		ctx.Send(expresso.Text{
			Text: "Hello World",
		})
	})

	app.POST("/", func(ctx *expresso.Context) {
		ctx.Send(expresso.Text{
			Text: "Got a POST request",
		})
	})

	app.PUT("/user", func(ctx *expresso.Context) {
		ctx.Send(expresso.Text{
			Text: "Got a PUT request as /user",
		})
	})

	app.DELETE("/user", func(ctx *expresso.Context) {
		ctx.Send(expresso.Text{
			Text: "Got a DELETE request at /user",
		})
	})

	app.ListenAndServe(":80")
}
Serve Static
package main

import (
	"net/http"

	"github.com/pr47h4m/expresso"
)

func main() {
	app := expresso.DefaultApp()

	app.ServeStatic("/public/*filepath", http.Dir("public"))
	app.ServeStatic("/private/*filepath", http.Dir("private"))

	app.ListenAndServe(":80")
}
Web Service
package main

import (
	"encoding/xml"
	"net/http"

	"github.com/pr47h4m/expresso"
)

type Repo struct {
	Name string `json:"name"`
	URL  string `json:"url"`
}

type User struct {
	Name string `json:"name"`
}

var (
	apiKeys = []string{"foo", "bar", "baz"}
	repos   = []Repo{
		{
			Name: "validation_chain",
			URL:  "https://github.com/pr47h4m/validation_chain",
		},
		{
			Name: "expresso",
			URL:  "https://github.com/pr47h4m/expresso",
		},
	}
	users = []User{
		{
			Name: "pr47h4m",
		},
		{
			Name: "jhanvi1061",
		},
	}
	userRepos = map[User][]Repo{
		users[0]: repos,
		users[1]: {},
	}
)

func main() {
	app := expresso.DefaultApp()

	app.GET("/api", ValidateAPIKey, func(ctx *expresso.Context) {
		ctx.Send(expresso.HTML{
			HTML: "<html><head><title>Web Service in Go</title></head><body><h1>Web Service in Go</h1><h3>\npowered by github.com/pr47h4m/expresso</h3></body></html>",
		})
	})

	app.GET("/api/users", ValidateAPIKey, func(ctx *expresso.Context) {
		ctx.Send(expresso.JSON{
			"status": "200",
			"users":  users,
		})
	})

	app.GET("/api/repos", ValidateAPIKey, func(ctx *expresso.Context) {
		ctx.Send(expresso.JSON{
			"status": "200",
			"repos":  repos,
		})
	})

	app.GET("/api/users/:name/repos", ValidateAPIKey, func(ctx *expresso.Context) {
		username := ctx.Params.ByName("name")

		var idx int = -1

		for i := 0; i < len(users); i++ {
			if users[i].Name == username {
				idx = i
				break
			}
		}

		if idx != -1 {
			ctx.Send(expresso.JSON{
				"status": "200",
				"repos":  userRepos[users[idx]],
			})
		} else {
			ctx.Status(http.StatusNotFound).Send(expresso.JSON{
				"status": "404",
				"error":  "username not found",
			})
		}
	})

	app.ListenAndServe(":80")
}

func ValidateAPIKey(ctx *expresso.Context) {
	key := ctx.QueryParams.Get("api-key")

	keyMatched := false

	for i := 0; i < len(apiKeys); i++ {
		if apiKeys[i] == key {
			keyMatched = true
			break
		}
	}

	if keyMatched {
		ctx.Next()
	} else {
		ctx.Status(http.StatusUnauthorized).Formatted(ctx.RawRequest, expresso.Formatted{
			Text: expresso.Text{
				Text: "401 - invalid api key",
			},
			HTML: expresso.HTML{
				HTML: "<html><head><title>401 - invalid api key</title></head><body>401 - invalid api key</body></html>",
			},
			JSON: expresso.JSON{
				"status": 401,
				"error":  "invalid api key",
			},
			XML: expresso.XML{
				XML: struct {
					XMLName xml.Name `xml:"error"`
					Status  int      `xml:"status"`
					Error   string   `xml:"error"`
				}{
					Status: 401,
					Error:  "invalid api key",
				},
			},
			Default: expresso.JSON{
				"status": 401,
				"error":  "invalid api key",
			},
		})
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type App

type App struct {
	Config
	// contains filtered or unexported fields
}

func DefaultApp

func DefaultApp() App

func NewApp

func NewApp(c Config) App

func (App) DELETE

func (a App) DELETE(path string, middlewares ...Middleware)

func (App) GET

func (a App) GET(path string, middlewares ...Middleware)

func (App) HEAD

func (a App) HEAD(path string, middlewares ...Middleware)

func (App) HandleError

func (a App) HandleError(middlewares ...Middleware)

func (App) HandleNotFound

func (a App) HandleNotFound(middlewares ...Middleware)

func (App) ListenAndServe

func (a App) ListenAndServe(addr string) error

func (App) ListenAndServeTLS

func (a App) ListenAndServeTLS(addr, certFile, keyFile string) error

func (App) OPTIONS

func (a App) OPTIONS(path string, middlewares ...Middleware)

func (App) PATCH

func (a App) PATCH(path string, middlewares ...Middleware)

func (App) POST

func (a App) POST(path string, middlewares ...Middleware)

func (App) PUT

func (a App) PUT(path string, middlewares ...Middleware)

func (App) ServeStatic

func (a App) ServeStatic(path string, root http.Dir)

type Config

type Config struct {
	ReadTimeout    time.Duration
	WriteTimeout   time.Duration
	MaxHeaderBytes int
}

type Context

type Context struct {
	*Request
	Response
	Extras map[interface{}]interface{}
	// contains filtered or unexported fields
}

func (*Context) Next

func (c *Context) Next()

type File

type File struct {
	Path string
}

type Formatted

type Formatted struct {
	Text
	HTML
	JSON
	XML
	Default interface{}
}

type HTML

type HTML struct {
	HTML string
}

type JSON

type JSON map[string]interface{}

type Middleware

type Middleware func(*Context)

type Request

type Request struct {
	RawRequest  *http.Request
	Path        *url.URL
	Method      string
	Headers     http.Header
	Body        []byte
	Params      httprouter.Params
	QueryParams url.Values
}

type Response

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

func (Response) Formatted

func (r Response) Formatted(req *http.Request, data Formatted)

func (Response) Redirect

func (r Response) Redirect(url string, status int)

func (Response) Send

func (r Response) Send(data interface{})

func (Response) SendStatus

func (r Response) SendStatus(code int)

func (Response) Status

func (r Response) Status(code int) Response

type Template

type Template struct {
	Template   *template.Template
	Properties interface{}
}

type Text

type Text struct {
	Text string
}

type XML

type XML struct {
	XML interface{}
}

Jump to

Keyboard shortcuts

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