GithubHelp home page GithubHelp logo

hansfordj / go-app Goto Github PK

View Code? Open in Web Editor NEW

This project forked from maxence-charriere/go-app

0.0 1.0 0.0 29.98 MB

A package to build progressive web apps with Go programming language and WebAssembly.

Home Page: https://luck.murlok.io

License: MIT License

Go 92.50% JavaScript 2.14% CSS 4.87% Makefile 0.49%

go-app's Introduction

Circle CI Go build Go Report Card GitHub release pkg.go.dev docs pkg.go.dev docs Twitter URL

go-app is a package to build progressive web apps (PWA) with Go programming language and WebAssembly.

It uses a declarative syntax that allows creating and dealing with HTML elements only by using Go, and without writing any HTML markup.

The package also provides an http.handler ready to serve all the required resources to run Go-based progressive web apps.

Install

go-app requirements:

# Init go module (if not initialized):
go mod init

# Get package:
go get -u -v github.com/maxence-charriere/go-app/v6

How it works

app diagram

  • Users: The users of your app. They request pages and resources from their web browser.
  • app.Handler: An http.Handler used by your server or cloud function. It serves your app, its static resources, and all the required files to make it work on user browsers.
  • Application: Your app built with this package. It is built as a WebAssembly (.wasm) binary and is served by the app.Handler.
  • Other static resources: Styles, images, and scripts used by your app. They are also served by the app.Handler.

Declarative syntax

go-app uses a declarative syntax so you can write component-based UI elements just by using the Go programming language.

package main

import "github.com/maxence-charriere/go-app/v6/pkg/app"

type hello struct {
    app.Compo
    name string
}

func (h *hello) Render() app.UI {
    return app.Div().Body(
        app.Main().Body(
            app.H1().Body(
                app.Text("Hello, "),
                app.If(h.name != "",
                    app.Text(h.name),
                ).Else(
                    app.Text("World"),
                ),
            ),
            app.Input().
                Value(h.name).
                Placeholder("What is your name?").
                AutoFocus(true).
                OnChange(h.OnInputChange),
        ),
    )
}

func (h *hello) OnInputChange(src app.Value, e app.Event) {
    h.name = src.Get("value").String()
    h.Update()
}

func main() {
    app.Route("/", &hello{})
    app.Route("/hello", &hello{})
    app.Run()
}

The app is built with the Go build tool by specifying WebAssembly as architecture and javascript as operating system:

GOARCH=wasm GOOS=js go build -o app.wasm

Note that we named the build output app.wasm. The reason is that the HTTP handler requires the web assembly app to be named this way in order to be served.

HTTP handler

Once your app is built, the next step is to serve it.

This package provides an http.Handler implementation ready to serve your PWA and all the required resources to make it work in a web browser.

The handler can be used to create either a web server or a cloud function (AWS Lambda, GCloud function or Azure function).

package main

import (
    "net/http"

    "github.com/maxence-charriere/go-app/v6/pkg/app"
)

func main() {
    h := &app.Handler{
        Title:  "Hello Demo",
        Author: "Maxence Charriere",
    }

    if err := http.ListenAndServe(":7777", h); err != nil {
        panic(err)
    }
}

The server is built as a standard Go program:

go build

Note that you need to add app.wasm to the server location. The reason is that app.Handler is looking for a file named app.wasm in the server directory in order to serve the web assembly binary.

hello-local        # Server directory
├── app.wasm       # Wasm binary
├── hello-local    # Server binary
└── main.go        # Server code

Works on mainstream browsers

Chrome Edge Firefox Opera Safari
Desktop ✔*
Mobile

*only Chromium based Edge

Demo

Hello app

The hello example introduced above:

Sources Description
hello Hello app.
hello-local Hello app that runs on a local server.
hello-local-external-root Hello app that runs on a local server but with a custom root directory.
hello-docker Hello app that run in a Docker container.
hello-gcloud-appengine Hello app that run on Google Cloud App Engine.
See live
hello-gcloud-func Hello app that run on Google a Cloud Function.
See live

Live apps

luck app hello app city app

Contributors

Code Contributors

This project exists thanks to all the people who contribute. [Contribute].

Financial Contributors

Become a financial contributor and help us sustain go-app development. [Contribute]

Individuals

Organizations

Support this project with your organization. Your logo will show up here with a link to your website. [Contribute]

go-app's People

Contributors

maxence-charriere avatar haraldnordgren avatar jacktuck avatar shanamaid avatar sg0hsmt avatar koddr avatar a4913994 avatar ruanwenfeng avatar stevelr avatar tectiv3 avatar talentlessguy avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.