gohttp

package module
v1.0.11 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2025 License: MIT Imports: 15 Imported by: 0

README

gohttp

A simple HTTP server framework for golang.

Getting start

See samples/helloworld

package main

import (
  "github.com/Streamlet/gohttp"
)

func HelloWorld(c gohttp.HttpContext) {
  c.String("Hello, World!")
}

func main() {
  application := gohttp.NewApplication[gohttp.HttpContext](gohttp.NewContextFactory(nil))
  application.Handle("/", HelloWorld)
  application.ServePort(80)
}

HttpContext

In gohttp, HTTP handler signature is func (c gohttp.HttpContext).

HttpContext provides:

  • Raw HTTP request and response
    • HttpRequest() *http.Request
    • HttpResponseWriter() http.ResponseWriter
  • Session
    • Default to a memory cached session
    • Can be replaced to redis based session simply:
      • Implement a CacheProvider
      • Pass CacheProvider to NewContextFactory
  • Input
    • GetQueryStrings() map[string][]string
    • GetQueryStringValues(key string) []string
    • GetQueryStringValue(key string) string
    • GetRequestBodyAsBytes() ([]byte, error)
    • GetRequestBodyAsStrings() (string, error)
    • GetRequestBodyAsXml(v interface{}) error
    • GetRequestBodyAsJson(v interface{}) error
  • Output
    • HttpError(statusCode int)
    • Redirect(url string)
    • String(response string)
    • Xml(r interface{})
    • Json(r interface{})

Extend HttpContext

See samples/custom_context, samples/std_json_context and samples/midware_context.

General steps:

  1. Define a new HttpContext, with gohttp.HttpContext embedded, and other functions appended.
  2. Implement self-defined HttpContext
  3. Define a new ContextFactory to create self-defined HttpContext
  4. Pass the new ContextFactory to gohttp.NewApplication
  5. Use self-defined HttpContext for all handlers.

Typically, more helper functions for input and output can be append to HttpContext. Middleware instances, e.g. redis, mysql, are expected to be added to HttpContext, too.

Documentation

Index

Constants

View Source
const CookieSession = "SESSION"

Variables

This section is empty.

Functions

This section is empty.

Types

type Application

type Application[T HttpContext] interface {
	ServeUnix(socketFile string)
	ServeTcp(address string)
	RawHandle(pattern string, handler http.Handler)
	Handle(pattern string, handler func(T))
}

func NewApplication

func NewApplication[T HttpContext](contextFactory ContextFactory[T]) Application[T]

type BasicContext

type BasicContext interface {
	HttpRequest() *http.Request
	HttpResponseWriter() http.ResponseWriter
	Session() Session
}

type CacheProvider

type CacheProvider interface {
	Exists(key string) bool
	HExists(key, field string) bool
	HGet(key, field string) interface{}
	HSet(key, field string, value interface{}, expiration time.Duration)
	HDelete(key, field string) bool
}

type ContextFactory

type ContextFactory[T HttpContext] interface {
	NewContext(w http.ResponseWriter, r *http.Request) T
}

func NewContextFactory

func NewContextFactory(cp CacheProvider) ContextFactory[HttpContext]

type HttpContext

type HttpContext interface {
	io.Closer
	BasicContext
	RequestReader
	ResponseWriter
}

type HttpServer

type HttpServer interface {
	Serve() error
	Shutdown() error
}

func NewTcpServer added in v1.0.9

func NewTcpServer(address string, handler http.Handler, errorChan chan error) HttpServer

func NewUnixServer added in v1.0.9

func NewUnixServer(socketFile string, handler http.Handler, errorChan chan error) HttpServer

type RequestReader

type RequestReader interface {
	GetQueryStrings() map[string][]string
	GetQueryStringValues(key string) []string
	GetQueryStringValue(key string) string
	GetRequestBodyAsBytes() ([]byte, error)
	GetRequestBodyAsString() (string, error)
	GetRequestBodyAsXml(v interface{}) error
	GetRequestBodyAsJson(v interface{}) error
}

type ResponseWriter

type ResponseWriter interface {
	HttpError(statusCode int)
	Redirect(url string)
	String(response string)
	Xml(r interface{})
	Json(r interface{})
}

type RouterInterface

type RouterInterface[T HttpContext] interface {
	http.Handler
	RawHandle(pattern string, handler http.Handler)
	Handle(pattern string, handler func(T))
}

func NewRouter

func NewRouter[T HttpContext](cf ContextFactory[T]) RouterInterface[T]

type Session

type Session interface {
	Exists(key string) bool
	Get(key string) interface{}
	Set(key string, value interface{}, expiration time.Duration)
	Delete(key string) bool
}

type SessionManager

type SessionManager interface {
	GetSession(sessionId string) Session
	CreateSession() (string, Session)
}

func NewSessionManager

func NewSessionManager(cacheProvider CacheProvider) SessionManager

Jump to

Keyboard shortcuts

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