gohf

package module
v4.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2025 License: BSD-3-Clause Imports: 9 Imported by: 0

README

GoHF ✨

Test Go Reference Release Documentation Maintenance License

GO Http Framework (Golang)

❓ WHY GoHF

📍 Getting started

go get github.com/gohf-http/gohf/v4
import (
  "github.com/gohf-http/gohf/v4"
  "github.com/gohf-http/gohf/v4/gohf_responses"
)

Hello GoHF Example

🪄 Features

Feature: Easier Error Handing
router.Handle("GET /greeting", func(c *gohf.Context) gohf.Response {
  name := c.Req.GetQuery("name")
  if name == "" {
    return gohf_responses.NewErrorResponse(
      http.StatusBadRequest,
      errors.New("Name is required"),
    )
  }

  greeting := fmt.Sprintf("Hello, %s!", name)
  return gohf_responses.NewTextResponse(http.StatusOK, greeting)
})
Feature: Middleware
router.Use(func(c *gohf.Context) gohf.Response {
  token := c.Req.GetHeader("Authorization")
  if !isValidToken(token) {
    return gohf_responses.NewErrorResponse(
      http.StatusUnauthorized,
      errors.New("Invalid token"),
    )
  }

  return c.Next()
})

This is how middleware works in GoHF.

middleware

Feature: Sub-Router
authRouter := router.SubRouter("/auth")
authRouter.Use(AuthMiddleware)
authRouter.Handle("GET /users", func(c *gohf.Context) gohf.Response {
  // ...
})
Feature: Customizable Response

You can define a customizable response by implementing gohf.Response interface.

type Response interface {
	Send(http.ResponseWriter, *gohf.Request)
}

Refer to gohf_responses for examples.

This is one of my favorite features, as it promotes a centralized response handler and simplifies adding additional functionality, such as logging.

Hello GoHF Example

package main

import (
  "errors"
  "fmt"
  "log"
  "net/http"

  "github.com/gohf-http/gohf/v4"
  "github.com/gohf-http/gohf/v4/gohf_responses"
)

func main() {
  router := gohf.New()

  router.Handle("GET /greeting", func(c *gohf.Context) gohf.Response {
    name := c.Req.GetQuery("name")
    if name == "" {
      return gohf_responses.NewErrorResponse(
        http.StatusBadRequest,
        errors.New("Name is required"),
      )
    }

    greeting := fmt.Sprintf("Hello, %s!", name)
    return gohf_responses.NewTextResponse(http.StatusOK, greeting)
  })

  router.Use(func(c *gohf.Context) gohf.Response {
    return gohf_responses.NewErrorResponse(
      http.StatusNotFound,
      errors.New("Page not found"),
    )
  })

  mux := router.CreateServeMux()
  log.Fatal(http.ListenAndServe(":8080", mux))
}

🌟 Show your support

Give a ⭐️ if this project helped you!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetResponseWriter added in v4.1.0

func GetResponseWriter(c *Context) http.ResponseWriter

Types

type Context

type Context struct {
	Req  *Request
	Next NextFunc
	// contains filtered or unexported fields
}

func (*Context) ResHeader

func (c *Context) ResHeader() http.Header

func (*Context) SetCookie

func (c *Context) SetCookie(cookie *http.Cookie)

type HandlerFunc

type HandlerFunc func(c *Context) Response

func FromHttpHandleFunc

func FromHttpHandleFunc(httpHandleFunc func(http.ResponseWriter, *http.Request)) HandlerFunc

func FromHttpHandler

func FromHttpHandler(httpHandler http.Handler) HandlerFunc

func MaxBytesMiddleware

func MaxBytesMiddleware(n int64) HandlerFunc

type NextFunc

type NextFunc func() Response

type Request

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

func (*Request) AddCookie

func (req *Request) AddCookie(c *http.Cookie)

func (*Request) Context

func (req *Request) Context() context.Context

func (*Request) Cookie

func (req *Request) Cookie(name string) (*http.Cookie, error)

func (*Request) Cookies

func (req *Request) Cookies() []*http.Cookie

func (*Request) FormFile

func (req *Request) FormFile(key string) (multipart.File, *multipart.FileHeader, error)

func (*Request) FormValue

func (req *Request) FormValue(key string) string

func (*Request) GetBody

func (req *Request) GetBody() RequestBody

func (*Request) GetHeader

func (req *Request) GetHeader(key string) string

func (*Request) GetHttpRequest

func (req *Request) GetHttpRequest() *http.Request

func (*Request) GetQuery

func (req *Request) GetQuery(key string) string

func (*Request) GetTimestamp

func (req *Request) GetTimestamp() time.Time

func (*Request) Host

func (req *Request) Host() string

func (*Request) Method

func (req *Request) Method() string

func (*Request) PathValue

func (req *Request) PathValue(name string) string

func (*Request) RemoteAddr

func (req *Request) RemoteAddr() string

func (*Request) RequestURI

func (req *Request) RequestURI() string

func (*Request) RootContext

func (req *Request) RootContext() context.Context

func (*Request) SetContext

func (req *Request) SetContext(ctx context.Context)

func (*Request) SetHeader

func (req *Request) SetHeader(key string, value string)

type RequestBody

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

func (RequestBody) Close

func (body RequestBody) Close() error

func (RequestBody) JsonDecode

func (body RequestBody) JsonDecode(v any) error

func (RequestBody) Read

func (body RequestBody) Read(p []byte) (n int, err error)

type Response

type Response interface {
	Send(http.ResponseWriter, *Request)
}

type Router

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

func New

func New() *Router

func (*Router) CreateServeMux

func (r *Router) CreateServeMux() *http.ServeMux

func (*Router) Handle

func (r *Router) Handle(pattern string, handlerFuncs ...HandlerFunc)

func (*Router) SubRouter

func (r *Router) SubRouter(pattern string) *Router

func (*Router) Use

func (r *Router) Use(handlerFuncs ...HandlerFunc)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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