web

package module
v0.0.0-...-40ad1af Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2024 License: MIT Imports: 10 Imported by: 0

README

Warning: This package is currently (not even) in alpha, not really tested yet and should not be used yet

Todos

  • Find a good api for defining sockets -> maybe dont implement sockets
  • Content type handling in generated api

Key Features

  • Zero Dependencies: Built entirely on Go's standard library. No external packages required.
  • OpenAPI Integration: Automatically generate OpenAPI specifications for your APIs, enhancing documentation and interoperability.
  • TypeScript API generator: Automatically generate TypeScript definitions for your Go APIs, ensuring type safety across your full-stack application.

Quick Start

Install with:

go get github.com/Instantan/web

Example usage:

package main

import (
  "log"
  "net/http"
  "github.com/Instantan/web"
)

func main() {
  w := web.NewWeb()

  w.Info(web.Info{
    Title: "MyProject",
    Version: "0.0.1",
  })

  w.OpenApi(web.OpenApi{
    DocPath:   "/api/doc.json",
    UiPath:    "/api/doc",
    UiVariant: "scalar",
  })

  w.Api(web.Api{
    Method: http.MethodGet,
    Path:   "/hello/{name}",
    Parameter: web.Parameter{
      Path: web.Path{
        "name": web.PathParam{
          Description: "The name to say hello to",
          Value:       "world",
        },
      },
    },
    Responses: web.Responses{
      StatusOK: "Hello World",
    },
    Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
      w.Write([]byte("Hello " + r.PathValue("name")))
    }),
  })

  log.Println("Server listening on :8080")
  log.Println("Visit http://localhost:8080/api/doc to view the documentation")
  if err := http.ListenAndServe(":8080", w.Server()); err != nil {
    panic(err)
  }
}

Go Report Card

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Api

type Api struct {
	Method      string
	Path        string
	OperationId string
	Summary     string
	Description string
	Parameter   Parameter
	Responses   Responses
	Handler     http.Handler
}

type Body

type Body struct {
	Description string
	Optional    bool
	Value       any
}

type Contact

type Contact struct {
	Name  string
	Url   string
	Email string
}

type ContentType

type ContentType struct {
	ApplicationJson               any
	ApplicationXml                any
	ApplicationXWWWFormURLEncoded any
	ApplicationJavaScript         any
	ApplicationPdf                any
	ApplicationZip                any

	TextHtml       any
	TextPlain      any
	TextCss        any
	TextCsv        any
	TextJavaScript any

	ImageJpeg   any
	ImagePng    any
	ImageGif    any
	ImageSvgXml any
	ImageWebp   any

	AudioMpeg any
	AudioOgg  any
	AudioWav  any

	VideoMp4  any
	VideoMpeg any
	VideoOgg  any

	MultipartFormData any
}

func (*ContentType) Iterate

func (c *ContentType) Iterate() func(func(contentType string, value any) bool)
type Cookie map[string]CookieField

type CookieField

type CookieField struct {
	Optional    bool
	Description string
	Value       any
}

type ExternalDocumentation

type ExternalDocumentation struct {
	Description string
	Url         string
}

type Group

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

func (Group) Api

func (g Group) Api(api Api)

func (Group) Group

func (g Group) Group(group func(Group))

func (Group) Static

func (g Group) Static(static Static)

func (Group) Tag

func (g Group) Tag(tag Tag)

func (Group) Use

func (g Group) Use(use Use)
type Header map[string]HeaderField

type HeaderField

type HeaderField struct {
	Optional    bool
	Description string
	Value       any
}

type Info

type Info struct {
	Title          string
	Version        string
	Summary        string
	Description    string
	TermsOfService string
}

type License

type License struct {
	Name       string
	Identifier string
}

type OpenApi

type OpenApi struct {
	DocPath   string
	UiPath    string
	UiVariant string
}

type Parameter

type Parameter struct {
	Query  Query
	Path   Path
	Header Header
	Cookie Cookie
	Body   Body
}

type Path

type Path map[string]PathParam

type PathParam

type PathParam struct {
	Description string
	Value       any
}

type Query

type Query map[string]QueryParam

type QueryParam

type QueryParam struct {
	Optional    bool
	Description string
	Value       any
}

type Responses

type Responses struct {
	Default any

	// All status codes can be found at http/status.go
	StatusContinue           any
	StatusSwitchingProtocols any
	StatusProcessing         any
	StatusEarlyHints         any

	StatusOK                   any
	StatusCreated              any
	StatusAccepted             any
	StatusNonAuthoritativeInfo any
	StatusNoContent            any
	StatusResetContent         any
	StatusPartialContent       any
	StatusMultiStatus          any
	StatusAlreadyReported      any
	StatusIMUsed               any

	StatusMultipleChoices   any
	StatusMovedPermanently  any
	StatusFound             any
	StatusSeeOther          any
	StatusNotModified       any
	StatusUseProxy          any
	StatusTemporaryRedirect any
	StatusPermanentRedirect any

	StatusBadRequest                   any
	StatusUnauthorized                 any
	StatusPaymentRequired              any
	StatusForbidden                    any
	StatusNotFound                     any
	StatusMethodNotAllowed             any
	StatusNotAcceptable                any
	StatusProxyAuthRequired            any
	StatusRequestTimeout               any
	StatusConflict                     any
	StatusGone                         any
	StatusLengthRequired               any
	StatusPreconditionFailed           any
	StatusRequestEntityTooLarge        any
	StatusRequestURITooLong            any
	StatusUnsupportedMediaType         any
	StatusRequestedRangeNotSatisfiable any
	StatusExpectationFailed            any
	StatusTeapot                       any
	StatusMisdirectedRequest           any
	StatusUnprocessableEntity          any
	StatusLocked                       any
	StatusFailedDependency             any
	StatusTooEarly                     any
	StatusUpgradeRequired              any
	StatusPreconditionRequired         any
	StatusTooManyRequests              any
	StatusRequestHeaderFieldsTooLarge  any
	StatusUnavailableForLegalReasons   any

	StatusInternalServerError           any
	StatusNotImplemented                any
	StatusBadGateway                    any
	StatusServiceUnavailable            any
	StatusGatewayTimeout                any
	StatusHTTPVersionNotSupported       any
	StatusVariantAlsoNegotiates         any
	StatusInsufficientStorage           any
	StatusLoopDetected                  any
	StatusNotExtended                   any
	StatusNetworkAuthenticationRequired any
}

func (*Responses) Iterate

func (r *Responses) Iterate() func(func(status int, value any) bool)

func (*Responses) Set

func (r *Responses) Set(status int, value any)

type Static

type Static struct {
	PathPrefix  string
	Tags        []string
	Summary     string
	Description string
	FS          http.FileSystem
	SpaMode     bool
}

type Tag

type Tag struct {
	Name        string
	Description string
}

type TypescriptApi

type TypescriptApi struct {
	Path   string
	Writer io.Writer
}

type Use

type Use func(next http.Handler) http.Handler

func Chain

func Chain(middlewares ...Use) Use

type Web

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

func NewWeb

func NewWeb() *Web

func (*Web) Api

func (web *Web) Api(route Api)

func (*Web) Contact

func (web *Web) Contact(contact Contact)

func (*Web) ExternalDocumentation

func (web *Web) ExternalDocumentation(externalDocumentation ExternalDocumentation)

func (*Web) Group

func (web *Web) Group(group func(Group))

func (*Web) Info

func (web *Web) Info(info Info)

func (*Web) License

func (web *Web) License(license License)

func (*Web) OpenApi

func (web *Web) OpenApi(openapi OpenApi)

func (*Web) Server

func (web *Web) Server() http.Handler

func (*Web) Static

func (web *Web) Static(static Static)

func (*Web) Tag

func (web *Web) Tag(tag Tag)

func (*Web) TypescriptApi

func (web *Web) TypescriptApi(typescriptApi TypescriptApi)

func (*Web) Use

func (web *Web) Use(use Use)

Directories

Path Synopsis
examples
web command
internal

Jump to

Keyboard shortcuts

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