Skip to content

Commit

Permalink
Added remote messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
asticode committed Apr 23, 2017
1 parent 8cd0061 commit 8f5a4d2
Show file tree
Hide file tree
Showing 8 changed files with 439 additions and 56 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Quentin RENARD

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
104 changes: 71 additions & 33 deletions event.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,48 @@
package astilectron

import (
"encoding/json"
"errors"
)

// Event names
const (
EventNameAppEventReady = "app.event.ready"
EventNameAppClose = "app.close"
EventNameProvisionStart = "provision.start"
EventNameProvisionDone = "provision.done"
EventNameWindowCmdBlur = "window.cmd.blur"
EventNameWindowCmdCenter = "window.cmd.center"
EventNameWindowCmdClose = "window.cmd.close"
EventNameWindowCmdCreate = "window.cmd.create"
EventNameWindowCmdDestroy = "window.cmd.destroy"
EventNameWindowCmdFocus = "window.cmd.focus"
EventNameWindowCmdHide = "window.cmd.hide"
EventNameWindowCmdMaximize = "window.cmd.maximize"
EventNameWindowCmdMinimize = "window.cmd.minimize"
EventNameWindowCmdMove = "window.cmd.move"
EventNameWindowCmdResize = "window.cmd.resize"
EventNameWindowCmdRestore = "window.cmd.restore"
EventNameWindowCmdShow = "window.cmd.show"
EventNameWindowCmdUnmaximize = "window.cmd.unmaximize"
EventNameWindowDoneCreate = "window.done.create"
EventNameWindowEventBlur = "window.event.blur"
EventNameWindowEventClosed = "window.event.closed"
EventNameWindowEventFocus = "window.event.focus"
EventNameWindowEventHide = "window.event.hide"
EventNameWindowEventMaximize = "window.event.maximize"
EventNameWindowEventMinimize = "window.event.minimize"
EventNameWindowEventMove = "window.event.move"
EventNameWindowEventReadyToShow = "window.event.ready.to.show"
EventNameWindowEventResize = "window.event.resize"
EventNameWindowEventRestore = "window.event.restore"
EventNameWindowEventShow = "window.event.show"
EventNameWindowEventUnmaximize = "window.event.unmaximize"
EventNameWindowEventUnresponsive = "window.event.unresponsive"
EventNameAppEventReady = "app.event.ready"
EventNameAppClose = "app.close"
EventNameProvisionStart = "provision.start"
EventNameProvisionDone = "provision.done"
EventNameWindowCmdBlur = "window.cmd.blur"
EventNameWindowCmdCenter = "window.cmd.center"
EventNameWindowCmdClose = "window.cmd.close"
EventNameWindowCmdCreate = "window.cmd.create"
EventNameWindowCmdDestroy = "window.cmd.destroy"
EventNameWindowCmdFocus = "window.cmd.focus"
EventNameWindowCmdHide = "window.cmd.hide"
EventNameWindowCmdMaximize = "window.cmd.maximize"
EventNameWindowCmdMessage = "window.cmd.message"
EventNameWindowCmdMinimize = "window.cmd.minimize"
EventNameWindowCmdMove = "window.cmd.move"
EventNameWindowCmdResize = "window.cmd.resize"
EventNameWindowCmdRestore = "window.cmd.restore"
EventNameWindowCmdShow = "window.cmd.show"
EventNameWindowCmdUnmaximize = "window.cmd.unmaximize"
EventNameWindowCmdWebContentsCloseDevTools = "window.cmd.web.contents.close.dev.tools"
EventNameWindowCmdWebContentsOpenDevTools = "window.cmd.web.contents.open.dev.tools"
EventNameWindowEventBlur = "window.event.blur"
EventNameWindowEventClosed = "window.event.closed"
EventNameWindowEventDidFinishLoad = "window.event.did.finish.load"
EventNameWindowEventFocus = "window.event.focus"
EventNameWindowEventHide = "window.event.hide"
EventNameWindowEventMaximize = "window.event.maximize"
EventNameWindowEventMessage = "window.event.message"
EventNameWindowEventMinimize = "window.event.minimize"
EventNameWindowEventMove = "window.event.move"
EventNameWindowEventReadyToShow = "window.event.ready.to.show"
EventNameWindowEventResize = "window.event.resize"
EventNameWindowEventRestore = "window.event.restore"
EventNameWindowEventShow = "window.event.show"
EventNameWindowEventUnmaximize = "window.event.unmaximize"
EventNameWindowEventUnresponsive = "window.event.unresponsive"
)

// Other constants
Expand All @@ -50,7 +59,36 @@ type Event struct {
// This is a list of all possible payloads.
// A choice was made not to use interfaces since it's a pain in the ass asserting each an every payload afterwards
// We use pointers so that omitempty works
Message string `json:"message,omitempty"`
Message *EventMessage `json:"message,omitempty"`
URL string `json:"url,omitempty"`
WindowOptions *WindowOptions `json:"windowOptions,omitempty"`
}

// EventMessage represents an event message
type EventMessage struct {
i interface{}
}

// newEventMessage creates a new event message
func newEventMessage(i interface{}) *EventMessage {
return &EventMessage{i: i}
}

// MarshalJSON implements the JSONMarshaler interface
func (p *EventMessage) MarshalJSON() ([]byte, error) {
return json.Marshal(p.i)
}

// Unmarshal unmarshals the payload into the given interface
func (p *EventMessage) Unmarshal(i interface{}) error {
if b, ok := p.i.([]byte); ok {
return json.Unmarshal(b, i)
}
return errors.New("event message should []byte")
}

// UnmarshalJSON implements the JSONUnmarshaler interface
func (p *EventMessage) UnmarshalJSON(i []byte) error {
p.i = i
return nil
}
86 changes: 86 additions & 0 deletions examples/4.remote_messaging/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package main

import (
"flag"
"net/http"
"os"

"time"

"github.com/asticode/go-astilectron"
"github.com/asticode/go-astilog"
"github.com/pkg/errors"
)

func main() {
// Parse flags
flag.Parse()

// Set up logger
astilog.SetLogger(astilog.New(astilog.FlagConfig()))

// Start server
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello world</title>
</head>
<body>
<span id="message">Hello world</span>
<script>
document.addEventListener('astilectron-ready', function() {
astilectron.listen(function(message) {
document.getElementById('message').innerHTML = message
astilectron.send("I'm good bro")
});
})
</script>
</body>
</html>`))
})
go http.ListenAndServe("127.0.0.1:4000", nil)

// Create astilectron
var a *astilectron.Astilectron
var err error
if a, err = astilectron.New(astilectron.Options{BaseDirectoryPath: os.Getenv("GOPATH") + "/src/github.com/asticode/go-astilectron/examples"}); err != nil {
astilog.Fatal(errors.Wrap(err, "creating new astilectron failed"))
}
defer a.Close()
a.HandleSignals()
a.On(astilectron.EventNameAppClose, func(e astilectron.Event) (deleteListener bool) {
a.Stop()
return
})

// Start
if err = a.Start(); err != nil {
astilog.Fatal(errors.Wrap(err, "starting failed"))
}

// Create window
var w *astilectron.Window
if w, err = a.NewWindow("http://127.0.0.1:4000", &astilectron.WindowOptions{
Center: astilectron.PtrBool(true),
Height: astilectron.PtrInt(600),
Width: astilectron.PtrInt(600),
}); err != nil {
astilog.Fatal(errors.Wrap(err, "new window failed"))
}
w.On(astilectron.EventNameWindowEventMessage, func(e astilectron.Event) (deleteListener bool) {
var m string
e.Message.Unmarshal(&m)
astilog.Infof("Received message %s", m)
return
})
w.Create()

// Send message
time.Sleep(time.Second)
w.Send("What's up?")

// Blocking pattern
a.Wait()
}
26 changes: 23 additions & 3 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

"net/url"

"strings"

"github.com/asticode/go-astilog"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -82,6 +84,9 @@ func Download(c *http.Client, dst, src string) (err error) {
return errors.Wrapf(err, "copying content from %s to %s failed", src, dst)
}

// We need to close the file manually before removing it
fp.Close()

// Remove dst.processing file
if err = os.Remove(dstProcessing); err != nil {
return errors.Wrapf(err, "removing %s failed", dstProcessing)
Expand All @@ -90,10 +95,19 @@ func Download(c *http.Client, dst, src string) (err error) {
}

// Unzip unzips a src into a dst
// Possible src formats are /path/to/zip.zip or /path/to/zip.zip/internal/path
func Unzip(dst, src string) (err error) {
// Log
astilog.Debugf("Unzipping %s into %s", src, dst)

// Parse src path
var split = strings.Split(src, ".zip")
src = split[0] + ".zip"
var internalPath string
if len(split) >= 2 {
internalPath = split[1]
}

// Open overall reader
var r *zip.ReadCloser
if r, err = zip.OpenReader(src); err != nil {
Expand All @@ -103,15 +117,21 @@ func Unzip(dst, src string) (err error) {

// Loop through files
for _, f := range r.File {
// Validate internal path
var n = string(os.PathSeparator) + f.Name
if internalPath != "" && !strings.HasPrefix(n, internalPath) {
continue
}

// Open file reader
var fr io.ReadCloser
if fr, err = f.Open(); err != nil {
return errors.Wrapf(err, "opening zip reader on file %s failed", f.Name)
return errors.Wrapf(err, "opening zip reader on file %s failed", n)
}
defer fr.Close()

// Only unzip files
var p = filepath.Join(dst, f.Name)
var p = filepath.Join(dst, strings.TrimPrefix(n, internalPath))
if !f.FileInfo().IsDir() {
// Make sure the directory of the file exists
if err = os.MkdirAll(filepath.Dir(p), 0775); err != nil {
Expand All @@ -127,7 +147,7 @@ func Unzip(dst, src string) (err error) {

// Copy
if _, err = io.Copy(fl, fr); err != nil {
return errors.Wrapf(err, "copying %s into %s failed", f.Name, p)
return errors.Wrapf(err, "copying %s into %s failed", n, p)
}
}
}
Expand Down
20 changes: 17 additions & 3 deletions paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ type Paths struct {
astilectronDirectory string
astilectronDownloadSrc string
astilectronDownloadDst string
astilectronUnzipSrc string
baseDirectory string
electronDirectory string
electronDownloadSrc string
electronDownloadDst string
electronExecutable string
electronUnzipSrc string
vendorDirectory string
}

Expand All @@ -37,11 +39,13 @@ func newPaths(baseDirectoryPath string) (p *Paths, err error) {
p.vendorDirectory = filepath.Join(p.baseDirectory, "vendor")
p.initAstilectronDirectory()
p.astilectronApplication = filepath.Join(p.astilectronDirectory, "main.js")
p.astilectronDownloadSrc = fmt.Sprintf("https://github.com/asticode/astilectron/releases/download/v%s/astilectron-v%s.zip", versionAstilectron, versionAstilectron)
p.astilectronDownloadDst = filepath.Join(p.vendorDirectory, filepath.Base(p.astilectronDownloadSrc))
p.astilectronDownloadSrc = fmt.Sprintf("https://github.com/asticode/astilectron/archive/v%s.zip", versionAstilectron)
p.astilectronDownloadDst = filepath.Join(p.vendorDirectory, fmt.Sprintf("astilectron-v%s.zip", versionAstilectron))
p.astilectronUnzipSrc = filepath.Join(p.astilectronDownloadDst, fmt.Sprintf("astilectron-%s", versionAstilectron))
p.electronDirectory = filepath.Join(p.vendorDirectory, "electron")
p.initElectronDownloadSrc()
p.electronDownloadDst = filepath.Join(p.vendorDirectory, filepath.Base(p.electronDownloadSrc))
p.electronDownloadDst = filepath.Join(p.vendorDirectory, fmt.Sprintf("electron-v%s.zip", versionElectron))
p.electronUnzipSrc = p.electronDownloadDst
p.initElectronExecutable()
return
}
Expand Down Expand Up @@ -132,6 +136,11 @@ func (p *Paths) AstilectronDownloadSrc() string {
return p.astilectronDownloadSrc
}

// AstilectronUnzipSrc returns the astilectron unzip source path
func (p *Paths) AstilectronUnzipSrc() string {
return p.astilectronUnzipSrc
}

// ElectronDirectory returns the electron directory path
func (p *Paths) ElectronDirectory() string {
return p.electronDirectory
Expand All @@ -152,6 +161,11 @@ func (p *Paths) ElectronExecutable() string {
return p.electronExecutable
}

// ElectronUnzipSrc returns the electron unzip source path
func (p *Paths) ElectronUnzipSrc() string {
return p.electronUnzipSrc
}

// BaseDirectory returns the vendor directory path
func (p *Paths) VendorDirectory() string {
return p.vendorDirectory
Expand Down
10 changes: 5 additions & 5 deletions provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ func (p *defaultProvisioner) Provision(paths *Paths) (err error) {

// provisionAstilectron provisions astilectron
func (p *defaultProvisioner) provisionAstilectron(paths *Paths) error {
return p.provisionDownloadableZipFile("Astilectron", paths.AstilectronApplication(), paths.AstilectronDownloadSrc(), paths.AstilectronDownloadDst(), paths.AstilectronDirectory())
return p.provisionDownloadableZipFile("Astilectron", paths.AstilectronApplication(), paths.AstilectronDownloadSrc(), paths.AstilectronDownloadDst(), paths.AstilectronUnzipSrc(), paths.AstilectronDirectory())
}

// provisionElectron provisions electron
func (p *defaultProvisioner) provisionElectron(paths *Paths) error {
return p.provisionDownloadableZipFile("Electron", paths.ElectronExecutable(), paths.ElectronDownloadSrc(), paths.ElectronDownloadDst(), paths.ElectronDirectory())
return p.provisionDownloadableZipFile("Electron", paths.ElectronExecutable(), paths.ElectronDownloadSrc(), paths.ElectronDownloadDst(), paths.ElectronUnzipSrc(), paths.ElectronDirectory())
}

// provisionDownloadableZipFile provisions a downloadable .zip file
func (p *defaultProvisioner) provisionDownloadableZipFile(name, pathExists, pathDownloadSrc, pathDownloadDst, pathDirectory string) (err error) {
func (p *defaultProvisioner) provisionDownloadableZipFile(name, pathExists, pathDownloadSrc, pathDownloadDst, pathUnzipSrc, pathDirectory string) (err error) {
// Log
astilog.Debugf("Provisioning %s...", name)

Expand All @@ -68,8 +68,8 @@ func (p *defaultProvisioner) provisionDownloadableZipFile(name, pathExists, path
}

// Unzip
if err = Unzip(pathDirectory, pathDownloadDst); err != nil {
return errors.Wrapf(err, "unzipping %s into %s failed", pathDownloadDst, pathDirectory)
if err = Unzip(pathDirectory, pathUnzipSrc); err != nil {
return errors.Wrapf(err, "unzipping %s into %s failed", pathUnzipSrc, pathDirectory)
}
} else if err != nil {
return errors.Wrapf(err, "stating %s failed", pathExists)
Expand Down
Loading

0 comments on commit 8f5a4d2

Please sign in to comment.