flash

package module
v0.0.0-...-89db033 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2022 License: GPL-2.0 Imports: 11 Imported by: 0

README

flash

A golang frontend web framework

forthebadge

Features:

  • Fast ⚡
  • Easy To Use 🙂
  • Cross Platform 💻

Installation:

$ go get -u https://github.com/krishpranav/flash

Usage:

  • import flash into your project:
import "github.com/krishpranav/flash"
  • form view:
package main

import (
	"strconv"

	"github.com/krishpranav/flash"
)

type People struct {
	Names []string
	Name  string
}

func (c *People) Render() (string, error) {
	return `
		<table>
			{{ range $i, $name := .Names }}
				<tr><td>
					{{ $name }}
					<button key="{{ $i }}" @click="Delete">Delete</button>
				</td></tr>
			{{ end }}
		</table>
		Add name: <input type="text" @value="Name">
		<button @click="Add">Add</button>
	`, nil
}

func (c *People) Delete(key string) {
	index, _ := strconv.Atoi(key)
	c.Names = append(c.Names[:index], c.Names[index+1:]...)
}

func (c *People) Add() {
	c.Names = append(c.Names, c.Name)
	c.Name = ""
}

func main() {
	panic(flash.NewServer().Start(func(_ *flash.Connection) flash.Component {
		return &People{
			Names: []string{"NameOne", "NameTwo"},
		}
	}))
}
  • clock view:
package main

import (
	"time"

	"github.com/krishpranav/flash"
)

type Clock struct{}

func (c *Clock) Render() (string, error) {
	return `
		The time now is {{ call.Now }}.
	`, nil
}

func (c *Clock) Now() string {
	return time.Now().Format(time.RFC1123)
}

func main() {
	panic(flash.NewServer().Start(func(conn *flash.Connection) flash.Component {
		go func() {
			for range time.NewTicker(time.Second).C {
				conn.Update()
			}
		}()

		return &Clock{}
	}))
}

  • counter view:
package main

import "github.com/krishpranav/flash"

type Counter struct {
	Number int
}

func (c *Counter) Render() (string, error) {
	return `
		Counter: {{ .Number }}
		<button @click="AddOne">+</button>
	`, nil
}

func (c *Counter) AddOne() {
	c.Number++
}

func main() {
	panic(flash.NewServer().Start(func(_ *flash.Connection) flash.Component {
		return &Counter{}
	}))
}

License:

Documentation

Index

Constants

View Source
const (
	OfflineActionDisablePage = iota
	OfflineActionDisableForms
	OfflineActionDoNothing
)

Variables

This section is empty.

Functions

func Render

func Render(c Component) (string, error)

Types

type Component

type Component interface {
	Render() (string, error)
}

type Connection

type Connection struct {
	RootComponent Component
	Connection    *websocket.Conn
}

func (*Connection) Update

func (conn *Connection) Update()

type NewConnectionFunc

type NewConnectionFunc func(*Connection) Component

type OfflineAction

type OfflineAction int

type Server

type Server struct {
	OfflineAction     OfflineAction
	ReconnectInterval time.Duration
}

func NewServer

func NewServer() *Server

func (*Server) Start

func (server *Server) Start(newConnectionFn NewConnectionFunc) error

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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