Gotron

用于平台独立桌面应用程序的Golang和Electron Boilerplate。(Golang & Electron Boilerplate For Platform Independent Desktop Applications.)

  • Owner: Equanox/gotron
  • Platform: Linux, Mac, Windows
  • License:: MIT License
  • Category::
  • Topic:
  • Like:
    0
      Compare:

Github stars Tracking Chart

Build Status

Gotron

Go Api for Electron.

Example Projects

A list of boilerplate projects using gotron.

Prerequisites

go1.11 with modules enabled, nodejs and npm must be available on your system.

Quick Start

On the first run it will download Electron and stores it in .gotron in your working directory.

package main

import (
    "github.com/Equanox/gotron"
)

func main() {
    // Create a new browser window instance
    window, err := gotron.New()
    if err != nil {
        panic(err)
    }

    // Alter default window size and window title.
    window.WindowOptions.Width = 1200
    window.WindowOptions.Height = 980
    window.WindowOptions.Title = "Gotron"

    // Start the browser window.
    // This will establish a golang <=> nodejs bridge using websockets,
    // to control ElectronBrowserWindow with our window object.
    done, err := window.Start()
    if err != nil {
        panic(err)
    }
    
    // Open dev tools must be used after window.Start 
    // window.OpenDevTools()
    
    // Wait for the application to close
    <-done
}

When everything worked you should see this

Use Your Own WebUI

gotron expects a folder containing your HTML/JS/CSS code and passes it to electronJS. make sure it contains at least a index.html as entrypoint.

Pass a path to your webUI on gotrons New(uiFolder ...string) function.

window, err := gotron.New("path/to/your/webui")
if err != nil {
    panic(err)
}

Communicate between backend and frontend

Frontend to backend communication is realized through javascript like event driven apporach.

Backend

Handle incoming events

window.On(&gotron.Event{Event: "event-name"}, func(bin []byte) {
	//Handle event here
}

Send event to frontend

// Create a custom event struct that has a pointer to gotron.Event
type CustomEvent struct {
    *gotron.Event
    CustomAttribute string 'json:"AtrNameInFrontend"'
}

window.Send(&CustomEvent{
    Event: &gotron.Event{Event: "event-name"},
    CustomAttribute: "Hello World!",
    })

Frontend

In frontend a websocket needs to be created. Adress is always localhost and port can be taken from global variable global.backendPort

let ws = new WebSocket("ws://localhost:" + global.backendPort + "/web/app/events");

Handle incoming events

// This is being called for all incoming messages
ws.onmessage = (message) => {
    let obj = JSON.parse(message.data);
    
    // event name
    console.log(obj.event);

    // event data
    console.log(obj.AtrNameInFrontend);
}

Send event to backend

ws.send(JSON.stringify({
    "event": "event-name",
    "AtrNameInFrontend": "Hello World!",
}))

Distribution/Packaging

To package a go application together with electornjs use gotron-builder.

Install gotron-builder

We provide executables for Linux, MacOS and Windows.
Download the newest release from https://github.com/Equanox/gotron/releases and add it to your $PATH.

Using gotron-builder

It expects...

  • a directory containing a golang main package
  • and a directory with a webUI containing at least a index.html

By default it will implicitly use...

  • golang main package from the current directory
  • webUI from .gotron/assets

To pack the code from Quick Start use

gotron-builder

in the root of your repo.

Pass your go code and webUI explicitly.

gotron-builder --go=your/go/dir --app=your/webapp/dir

For cross compilation you can use the same flags as electron-builder would expect them

gotron-builder --win 

Read about the requirements for cross-compilation in electron-builders documentation.

Tasks

  • Basic js + webpack example
  • React example
  • Typescript-React example
  • Vue.js example
  • Elm example
  • Flutter Hummingbird example
  • Hide nodejs/Electron behind go api
  • Create executables for Win, Linux, MacOS
  • Hide nodejs/Electron behind go api
  • Msgs between golang and Electron renderer process,
    abstracted in a javascript/typescript package
  • Implement complete BrowserWindow api see => BrowserWindow.md
  • Implement complete electron-builder api in gotron-builder

Sponsors

License

MIT

Except Roboto (ui/js/src/Roboto-Light.ttf , ui/react/src/Roboto-Light.ttf) which is licensed under Apache 2.0
https://github.com/google/roboto

Overview

Name With OwnerEquanox/gotron
Primary LanguageJavaScript
Program languageCSS (Language Count: 8)
PlatformLinux, Mac, Windows
License:MIT License
Release Count6
Last Release Namev0.2.23 (Posted on )
First Release Namev0.2.0 (Posted on )
Created At2017-03-31 12:05:44
Pushed At2021-03-08 09:42:25
Last Commit At2021-03-08 10:40:50
Stargazers Count784
Watchers Count44
Fork Count63
Commits Count174
Has Issues Enabled
Issues Count34
Issue Open Count19
Pull Requests Count18
Pull Requests Open Count4
Pull Requests Close Count1
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private
To the top