httphandle

package module
v0.5.8 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: Apache-2.0 Imports: 19 Imported by: 0

README

httphandle

httphandle is an HTTP framework for websites that meet the following requirements:

  • Written in Golang.
  • Rendered using Go HTML templates.
  • Follows a specific API design pattern.
  • Has a PostgreSQL database.
  • Written by me.

This project is public on GitHub primarily so that open source projects that I've published will build for others. This project is likely to have many breaking changes in the future to serve my specific needs.

That being said, it is licensed under the Apache 2.0 license, so anyone is free to use it if the use case fits.

Documentation

Overview

Package httphandle contains reusable code for creating HTTP servers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Attach

func Attach[A AppSpecific](args AttachArgs[A], a A, mux *http.ServeMux) error

Attach attaches the handlers to the mux.

func ExecuteTemplate

func ExecuteTemplate(args TemplateArgs, tmplr templater.Templater) error

func Serve

func Serve(args ServeArgs, handler http.Handler)

Serve serves the http server and shuts it down gracefully.

Types

type API

type API[A AppSpecific] interface {
	ApplyMiddleware(h http.Handler) http.Handler
	Authorize(w http.ResponseWriter, r *http.Request) (authorized bool, modified *http.Request)
	ContentType() (request, response string)
	HTTPMethod() string
	Initialize(A) error
	Respond(r *http.Request) (code int, body []byte, err error)
	URLPattern() string
}

API is an interface for an API handler.

type AppSpecific

type AppSpecific interface {
	ErrorTemplate(meta TemplateRespMeta, r *http.Request, w http.ResponseWriter)
	Logger() *slog.Logger
	NotFound(w http.ResponseWriter, r *http.Request)
}

AppSpecific is an interface for the application specific implementations.

type AttachArgs

type AttachArgs[A AppSpecific] struct {
	API            []API[A]
	Files          http.FileSystem
	General        []General[A]
	MiddlewareOpts middleware.GlobalOptions
	Template       []Template[A]
	Templater      templater.Templater
}

AttachArgs are the arguments for attaching handlers to a mux.

type DevDecider

type DevDecider interface {
	DevMode() bool
}

DevDecider is a jsontype.Config that determines if the application is in development mode.

type General

type General[A AppSpecific] interface {
	ApplyMiddleware(h http.Handler) http.Handler
	Initialize(A) error
	ServeHTTP(w http.ResponseWriter, r *http.Request)
	URLPattern() string
}

General is an interface for a general handler.

type RequestData

type RequestData struct {
	Query map[string]string
}

RequestData is the data passed to the template.

type ServeArgs

type ServeArgs struct {
	Logger          *slog.Logger
	Port            uint16
	ShutdownFunc    func(ctx context.Context) error
	ShutdownTimeout time.Duration
}

ServeArgs are the arguments for the Serve function.

type SetupArgs

type SetupArgs struct {
	Static    embed.FS
	Templates embed.FS
}

SetupArgs are the arguments for setting up the application.

type SetupResults

type SetupResults[C jt.Defaulter[C]] struct {
	Conf      C
	Files     http.FileSystem
	Logger    *slog.Logger
	Templater templater.Templater
}

SetupResults are the results of setting up the application.

func Setup

func Setup[C jt.Defaulter[C]](args SetupArgs) (SetupResults[C], error)

Setup sets up the application.

type Template

type Template[A AppSpecific] interface {
	ApplyMiddleware(h http.Handler) http.Handler
	Authorize(w http.ResponseWriter, r *http.Request) (authorized bool, modified *http.Request, skipTemplate bool)
	Initialize(A) error
	Respond(r *http.Request) (meta TemplateRespMeta, templateData any, wrapperData WrapperData)
	TemplateName() string
	URLPattern() string
	WrapperTemplateName() string
}

Template is an interface for a template handler.

type TemplateArgs

type TemplateArgs struct {
	Data         any
	Name         string
	Request      *http.Request
	ResponseCode int
	WrapperData  WrapperData
	WrapperName  string
	Writer       http.ResponseWriter
}

TemplateArgs are the arguments passed to the template.

type TemplateDataResult

type TemplateDataResult struct {
	HeaderAdd    template.HTML
	InnerHTML    template.HTML
	RequestUUID  uuid.UUID
	TemplateArgs TemplateArgs
}

TemplateDataResult is the result of executing a template, used for the wrapper template.

type TemplateRespMeta

type TemplateRespMeta struct {
	Cookies      []*http.Cookie
	RedirectURL  string
	ResponseCode int
}

TemplateRespMeta is the metadata returned from the template.

type WrapperData

type WrapperData interface {
	SetResult(result TemplateDataResult)
}

Directories

Path Synopsis
Package constant contains constants for the application.
Package constant contains constants for the application.
Package middleware provides middleware used by httphandle.
Package middleware provides middleware used by httphandle.
ctxkey
Package ctxkey contains the context keys used by httphandle.
Package ctxkey contains the context keys used by httphandle.

Jump to

Keyboard shortcuts

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