gonsen

package module
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2022 License: MIT Imports: 6 Imported by: 1

README

Gonsen

Sometimes, you want to build a service with a basic web interface that deals with dynamic data, but you don't want to have to deal with building a full API and spin up a SPA with some heavy framework, etc. Instead, you'd rather just build pages using Go templates and easily feed them data from a backing repository in a type-safe manner.

Enter Gonsen!

Todo example

What is it?

Gonsen is a simple templating system that lets you write HTML pages using Go templates, and serve them with type safety using generics. It's just a simple wrapper around Go's existing templating system.

Gonsen works with either a file system on disk or using the embed package. Using embed is highly recommended with Gonsen as it will bundle the entire site into the compiled binary, making your service easily portable.

What is it not?

Gonsen is not a full routing framework. It is intended to be used with something like Gorilla Mux or other routing frameworks that already exist.

How it works

For a code example to follow, see the basic example.

Gonsen is intended to be used with the embed standard package. You supply a basic skeletal structure of what your pages should look like, and then each page can be defined as simpler components similar to something like Vue.

The skeletal structure can include any number of subcomponents. The basic example includes subcomponents for style, scripts, and the main HTML body, but you could add other subcomponents for a more dynamic menu, etc.

The given filesystem must be in the following structure:

site
 |-> pages/
 |   |-> page1.html
 |   |-> another.html
 |-> base.html

The base.html file is used as the base template, and all files found in pages will use base.html. Any other directories are ignored by Gonsen, but it can be useful to include static assets and such in other directories as the example shows.

Trying it out

make will run the basic example.

Where this could go

This is really just a simple wrapper for now, and it's almost more a proof of concept than a proper library. Is there room for it to grow into something more? Let me know in the Discussions tab if you have any thoughts!

What's with the name?

I didn't want a SPA, I wanted to build an 温泉 instead.

Documentation

Index

Constants

View Source
const (
	TemplateNameBase = "base"

	BaseFilename     = "site/base.html"
	PageSubdirectory = "site/pages"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Page

type Page[T interface{}, C interface{}] struct {
	// contains filtered or unexported fields
}

func NewPage

func NewPage[T interface{}](
	source *Source,
	pageName string,
	dataSource func(r *http.Request) (T, int),
) *Page[T, interface{}]

func NewPageWithContext added in v0.3.0

func NewPageWithContext[T interface{}, C interface{}](
	source *Source,
	pageName string,
	dataSource func(r *http.Request) (T, int),
	ctxSource func(r *http.Request) (C, int),
) *Page[T, C]

func NewStaticPageWithContext added in v0.3.0

func NewStaticPageWithContext[C interface{}](source *Source, pageName string, ctxSource func(r *http.Request) (C, int)) *Page[interface{}, C]

func (*Page[T, C]) AsHttpHandlerFunc added in v0.3.1

func (p *Page[T, C]) AsHttpHandlerFunc() http.HandlerFunc

AsHttpHandlerFunc returns a http.HandlerFunc for certain APIs that don't want a full http.Handler

func (*Page[T, C]) ServeHTTP added in v0.2.0

func (p *Page[T, C]) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Source

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

func NewSource

func NewSource(templates fs.FS) *Source

func (*Source) AssetsHandler added in v0.2.1

func (s *Source) AssetsHandler() http.Handler

func (*Source) OnRenderError added in v0.3.3

func (s *Source) OnRenderError(do func(r *http.Request, err error))

func (*Source) OnStatus added in v0.3.2

func (s *Source) OnStatus(code int, do func(w http.ResponseWriter, r *http.Request))

type StaticPage added in v0.2.0

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

func NewStaticPage added in v0.2.0

func NewStaticPage(source *Source, pageName string) *StaticPage

func (*StaticPage) AsHttpHandlerFunc added in v0.3.1

func (p *StaticPage) AsHttpHandlerFunc() http.HandlerFunc

func (*StaticPage) ServeHTTP added in v0.2.0

func (p *StaticPage) ServeHTTP(w http.ResponseWriter, r *http.Request)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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