gosvelt

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2023 License: MIT Imports: 23 Imported by: 0

README

GoSvelt

the fasthttp fullstack golang framwork using svelte (support tailwindcss). just more 10 time faster than sveltekit

why gosvelt ?

fullstack integration of svelte

yeah, gosvelt will compile, group, and serve svelte pages.
A Svelte or AdvancedSvelte handler will give you a svelte map wich contain "js" and "css" URLs and you can add to this map your own attributes that will be rendered on the html template (note: if you add for example a "test" element to the map, you have to add the &{test} element in the html template)

func main() {
	r := gosvelt.New()

	r.Svelte("/", "./static/App.svelte", func(c *gosvelt.Context, svelte gosvelt.Map) error {
		return c.Html(200, "./static/index.html", svelte) // html template
	})

	r.AdvancedSvelte("/adv", "./static/", "App.svelte", 
	func(c *gosvelt.Context, svelte gosvelt.Map) error {
		return c.Html(200, "./static/index.html", svelte) // html template
	}, 
	gs.SvelteConfig{
		Typescript:  false,
		Tailwindcss: true,
		Pnpm:        true,
	})

	r.Start(":80")
}
cool way to made sse

there are actyally to way to use sse in gosvelt: the context way wich is in a context and can use channels declared in the handler. And the handler way wich is an handler function and use channels who are declared outside the handler.

func main() {
	r := gosvelt.New()

	r.Get("/sse", func(c *gs.Context) error { // context way
		datach := make(chan interface{})
		closech := make(chan struct{})

		return c.Sse(datach, closech, func() {
			datach <- "hello"

			for i := 0; i < 10; i++ {
				time.Sleep(100 * time.Millisecond)
				datach <- fmt.Sprintf("%d -> actual time is %v", i, time.Now())
			}

			close(closech)
		})
	})

	datach := make(chan interface{})
	closech := make(chan struct{})

	r.Sse("/sse2", datach, closech, func() { // handler way
		datach <- "hello"

		for i := 0; i < 4; i++ {
			time.Sleep(time.Second)
			datach <- fmt.Sprintf("%d -> actual time is %v", i, time.Now())
		}

		close(closech)
	})

	r.Start(":80")
}
pretty simple syntax

the syntax is like popular framworks like fiber, gin, echo

func main() {
	r := gosvelt.New()

	r.Get("/gg/:name", func(c *gosvelt.Context) error { // url params
		return c.Json(200, gosvelt.Map{"gg": c.Param("name")})
	})

	r.Get("/ws", func(c *gosvelt.Context) error { // websocket handler
		return c.Ws(func(conn *websocket.Conn) {
			conn.WriteJSON(gosvelt.Map{"ez": "pz"})
		})
	})

	r.Static("/index", "./cmd/static/index.html") // static files

	r.Svelte("/", "./cmd/static/App.svelte", 
	func(c *gosvelt.Context, svelte gosvelt.Map) error { // svelte files
		return c.Html(200, "./cmd/static/index.html", svelte)
	})

	r.Start(":80")
}

todo:

  • CSR (Client Side Rendering)
  • SSR (Server Side Rendering)
  • ISR (Incremental Static Regeneration)
  • SSE (Server Sent Events)
  • WS (Web Socket)
  • CSS Engine (Tailwindcss)
  • Add layout system

Documentation

Index

Constants

View Source
const (
	CharsetUTF8 = "charset=UTF-8"

	// Methods
	MGet     = http.MethodGet     // get
	MPost    = http.MethodPost    // post
	MPut     = http.MethodPut     // put
	MDelete  = http.MethodDelete  // delete
	MConnect = http.MethodConnect // connect
	MOptions = http.MethodOptions // options

	// Mime
	MAppJSON       = "application/json"                  // json
	MAppProto      = "application/protobuf"              // protobuf
	MAppJS         = "application/javascript"            // js
	MAppXML        = "application/xml"                   // xml
	MAppForm       = "application/x-www-form-urlencoded" // form
	MOctStream     = "application/octet-stream"          // octet stream
	MTextPlain     = "text/plain"                        // text
	MTextHTML      = "text/html"                         // html
	MTextXML       = "text/xml"                          // xml text
	MAppJsonUTF8   = MAppJSON + "; " + CharsetUTF8       // json utf8
	MAppJsUTF8     = MAppJS + "; " + CharsetUTF8         // js utf8
	MAppXmlUTF8    = MAppXML + "; " + CharsetUTF8        // xml utf8
	MTextPlainUTF8 = MTextPlain + "; " + CharsetUTF8     // text utf8
	MTextHtmlUTF8  = MTextHTML + "; " + CharsetUTF8      // html utf8
	MTextXmlUTF8   = MTextXML + "; " + CharsetUTF8       // xml text utf8
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Log            bool
	Http2          bool
	TailwindcssCfg string
	PostcssCfg     string
}

type Context

type Context struct {
	Ctx context.Context
	// contains filtered or unexported fields
}

func (*Context) Args

func (c *Context) Args() *fasthttp.Args

func (*Context) Blob

func (c *Context) Blob(code int, b []byte) error

return bytes datas to client

func (*Context) CDel

func (c *Context) CDel(key string)

func (*Context) CGet

func (c *Context) CGet(key string) string

func (*Context) CReset

func (c *Context) CReset()

func (*Context) CSet

func (c *Context) CSet(key, value string)

func (*Context) Cookie

func (c *Context) Cookie(key string) string

get the page cookies with key

func (*Context) File

func (c *Context) File(code int, file string, compress ...bool) error

func (*Context) GetForm added in v1.3.0

func (c *Context) GetForm(key string) []byte

func (*Context) Html

func (c *Context) Html(code int, t string, args ...any) error

func (*Context) Json

func (c *Context) Json(code int, j interface{}) error

return json datas to client

func (*Context) Method

func (c *Context) Method() string

func (*Context) Param

func (c *Context) Param(key interface{}) interface{}

get the url params with key

func (*Context) Path

func (c *Context) Path() string

func (*Context) Proto

func (c *Context) Proto(code int, p proto.Message) error

return proto datas to client

func (*Context) Protocol

func (c *Context) Protocol() string

get the protocol of the request

func (*Context) Redirect

func (c *Context) Redirect(code int, url string) error

func (*Context) Req

func (c *Context) Req() *fasthttp.Request

func (*Context) Res

func (c *Context) Res() *fasthttp.Response

func (*Context) Secure

func (c *Context) Secure() bool

true if request is secure by https

func (*Context) SetCType

func (c *Context) SetCType(ctype string)

set response content type

func (*Context) SetCookie

func (c *Context) SetCookie(k, v string, expire time.Time)

add an cookie

func (*Context) SetHeader

func (c *Context) SetHeader(key, value string)

set response header

func (*Context) SetStatusCode

func (c *Context) SetStatusCode(code int)

set response status code in int

func (*Context) Sse added in v1.3.0

func (c *Context) Sse(datach chan interface{}, closech chan struct{}, fn func()) error

func (*Context) Text

func (c *Context) Text(code int, t string) error

return text datas to client

func (*Context) Write

func (c *Context) Write(body []byte)

write to client

func (*Context) Ws

func (c *Context) Ws(handler websocket.FastHTTPHandler) error

return ws connection NOTE: this need websocket.FastHTTPHandler handler and all ws code will be in the arg handler

type ErrorHandlerFunc

type ErrorHandlerFunc func(c *fasthttp.RequestCtx, err error)

type GoSvelt

type GoSvelt struct {
	Config *Config
	// contains filtered or unexported fields
}

func New

func New(cfg ...*Config) *GoSvelt

func (*GoSvelt) AdvancedSvelte

func (gs *GoSvelt) AdvancedSvelte(path, svelteRoot, svelteFile string, fn SvelteHandlerFunc, cfg ...SvelteConfig)

help to server Svelte files to client

func (*GoSvelt) Connect

func (gs *GoSvelt) Connect(path string, h HandlerFunc)

func (*GoSvelt) Delete

func (gs *GoSvelt) Delete(path string, h HandlerFunc)

func (*GoSvelt) Get

func (gs *GoSvelt) Get(path string, h HandlerFunc)

func (*GoSvelt) Middleware

func (gs *GoSvelt) Middleware(path string, fn MiddlewareFunc)

func (*GoSvelt) Options

func (gs *GoSvelt) Options(path string, h HandlerFunc)

func (*GoSvelt) Post

func (gs *GoSvelt) Post(path string, h HandlerFunc)

func (*GoSvelt) Put

func (gs *GoSvelt) Put(path string, h HandlerFunc)

func (*GoSvelt) Sse added in v1.3.0

func (gs *GoSvelt) Sse(path string, datach chan interface{}, closech chan struct{}, fn func())

func (*GoSvelt) Start

func (gs *GoSvelt) Start(addr string)

func (*GoSvelt) StartTLS

func (gs *GoSvelt) StartTLS(addr, cert, key string)

func (*GoSvelt) Static

func (gs *GoSvelt) Static(path, file string)

func (*GoSvelt) Svelte

func (gs *GoSvelt) Svelte(path, svelteFile string, fn SvelteHandlerFunc, cfg ...SvelteConfig)

help to server Svelte files to client

func (*GoSvelt) SvelteMiddleware

func (gs *GoSvelt) SvelteMiddleware(path string, fn SvelteMiddlewareFunc)

type HandlerFunc

type HandlerFunc func(c *Context) error

type Map

type Map map[string]interface{}

func (Map) Add

func (m Map) Add(key string, value interface{})

func (Map) Del

func (m Map) Del(key string)

func (Map) Get

func (m Map) Get(key string) interface{}

type MiddlewareFunc

type MiddlewareFunc func(next HandlerFunc) HandlerFunc

type SseEvent added in v1.3.0

type SseEvent struct {
	Name string // event name
	Data string // event datas
}

sse event

type SvelteConfig added in v1.3.0

type SvelteConfig struct {
	Typescript  bool // default: false
	Tailwindcss bool // default: false
	Pnpm        bool // default: false
}

svelte compilator config

type SvelteHandlerFunc

type SvelteHandlerFunc func(c *Context, svelte Map) error

type SvelteMiddlewareFunc

type SvelteMiddlewareFunc func(next SvelteHandlerFunc) SvelteHandlerFunc

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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