Skip to content

Commit

Permalink
Functionnal electron version
Browse files Browse the repository at this point in the history
  • Loading branch information
ScullWM committed May 13, 2018
1 parent 112cf70 commit 1caecf7
Show file tree
Hide file tree
Showing 32 changed files with 3,211 additions and 232 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
client-log.log
.DS_STORE
*.zip
*.zip
bind_darwin_amd64.go
output/*
vendor/*
9 changes: 4 additions & 5 deletions bundler.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"app_name": "Updemia Client",
"icon_path_darwin": "resources/logo.icns",
"icon_path_linux": "resources/logo.png",
"icon_path_windows": "resources/logo.ico",
"output_path": "output"
"app_name": "Updemia",
"icon_path_darwin": "resources/icon.icns",
"icon_path_linux": "resources/icon.png",
"icon_path_windows": "resources/icon.ico"
}
98 changes: 22 additions & 76 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,107 +9,53 @@ import (
"github.com/pkg/errors"
)

// Constants
const htmlAbout = `Welcome on <b>Astilectron</b> demo!<br>
This is using the bootstrap and the bundler.`

// Vars
var (
AppName string
BuiltAt string
debug = flag.Bool("d", false, "if yes, the app is in debug mode")
window *astilectron.Window
debug = flag.Bool("d", true, "enables the debug mode")
w *astilectron.Window
updemiaUser Auth
)

var updemiaUser *Updemia

type Updemia struct {
Email string
Passphrase string
Hash string
Imgs []UpdemiaImg
}

type UpdemiaImg struct {
Key string
Url string
Img string
}

type ImgsResponse struct {
Collection []UpdemiaImg
}

func main() {
// Init
flag.Parse()
astilog.FlagInit()

// Run bootstrap
astilog.Debugf("Running app built at %s", BuiltAt)
if err := bootstrap.Run(bootstrap.Options{
Asset: Asset,
Asset: Asset,
AssetDir: AssetDir,
AstilectronOptions: astilectron.Options{
AppName: AppName,
AppIconDarwinPath: "resources/logo.icns",
AppIconDefaultPath: "resources/logo.png",
},
Debug: *debug,
Homepage: "index.html",
MenuOptions: []*astilectron.MenuItemOptions{
{
Label: astilectron.PtrStr(AppName),
SubMenu: []*astilectron.MenuItemOptions{
{
Role: astilectron.MenuItemRoleClose,
},
},
},
{
Label: astilectron.PtrStr("Style"),
SubMenu: []*astilectron.MenuItemOptions{
{
Checked: astilectron.PtrBool(true),
Label: astilectron.PtrStr("Dark"),
OnClick: func(e astilectron.Event) (deleteListener bool) {
// Send
if err := window.Send(bootstrap.MessageOut{Name: "set.style", Payload: "dark"}); err != nil {
astilog.Error(errors.Wrap(err, "setting dark style failed"))
return
}
return
},
Type: astilectron.MenuItemTypeRadio,
},
{
Label: astilectron.PtrStr("Light"),
OnClick: func(e astilectron.Event) (deleteListener bool) {
// Send
if err := window.Send(bootstrap.MessageOut{Name: "set.style", Payload: "light"}); err != nil {
astilog.Error(errors.Wrap(err, "setting dark style failed"))
return
}
return
},
Type: astilectron.MenuItemTypeRadio,
},
},
MenuOptions: []*astilectron.MenuItemOptions{{
Label: astilectron.PtrStr("File"),
SubMenu: []*astilectron.MenuItemOptions{
{Role: astilectron.MenuItemRoleClose},
},
},
MessageHandler: handleMessages,
OnWait: func(_ *astilectron.Astilectron, w *astilectron.Window, _ *astilectron.Menu, t *astilectron.Tray, _ *astilectron.Menu) error {
// Store global variables
window = w

// Add listeners on tray
t.On(astilectron.EventNameTrayEventClicked, func(e astilectron.Event) (deleteListener bool) { astilog.Info("Tray has been clicked!"); return })
}},
OnWait: func(_ *astilectron.Astilectron, iw *astilectron.Window, _ *astilectron.Menu, _ *astilectron.Tray, _ *astilectron.Menu) error {
w = iw
return nil
},
RestoreAssets: RestoreAssets,
TrayOptions: &astilectron.TrayOptions{
Image: astilectron.PtrStr("resources/tray.png"),
Tooltip: astilectron.PtrStr("Wow, what a beautiful tray!"),
},
MessageHandler: handleMessages,
RestoreAssets: RestoreAssets,
WindowOptions: &astilectron.WindowOptions{
BackgroundColor: astilectron.PtrStr("#333"),
BackgroundColor: astilectron.PtrStr("#000"),
Center: astilectron.PtrBool(true),
Height: astilectron.PtrInt(720),
Width: astilectron.PtrInt(370),
Height: astilectron.PtrInt(800),
Width: astilectron.PtrInt(320),
},
}); err != nil {
astilog.Fatal(errors.Wrap(err, "running bootstrap failed"))
Expand Down
51 changes: 39 additions & 12 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,56 @@ package main

import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"encoding/hex"

"github.com/asticode/go-astilectron"
"github.com/asticode/go-astilectron-bootstrap"
)

type AuthCredential struct {
Email string `json:"email"`
Password string `json:"password"`
type Auth struct {
Email string `json:"email"`
Hash string `json:"hash"`
}

// handleMessages handles messages
func handleMessages(w *astilectron.Window, m bootstrap.MessageIn) (payload interface{}, err error) {
func handleMessages(_ *astilectron.Window, m bootstrap.MessageIn) (payload interface{}, err error) {
switch m.Name {
case "user.auth":
case "hash":
// Unmarshal payload
var email string
if len(m.Payload) > 0 {
// Unmarshal payload
if err = json.Unmarshal(m.Payload, &email); err != nil {
payload = err.Error()
return
}
}

var a AuthCredential
_ = json.Unmarshal(m.Payload, &a)
// Explore
if payload, err = hash(email); err != nil {
payload = err.Error()
return
}

hash := md5.Sum([]byte(a.Email))
hashString := hex.EncodeToString(hash[:])

w.Send(bootstrap.MessageOut{Name: "list", Payload: hashString})
// start watching directory
go watchUploadFolder()
}
return
}

func hash(e string) (a Auth, err error) {
data := []byte(e)
dataHash := md5.Sum(data)

hashString := hex.EncodeToString(dataHash[:])
updemiaUser = Auth{Email: e, Hash: hashString}

return updemiaUser, nil
}

// PayloadDir represents a dir payload
type Dir struct {
Name string `json:"name"`
Path string `json:"path"`
}
3 changes: 0 additions & 3 deletions notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"github.com/deckarep/gosx-notifier"
"github.com/sadlil/go-trigger"
"io"
"log"
"net/http"
Expand All @@ -26,8 +25,6 @@ func notifyUserSuccess(url string) {
if err != nil {
log.Println("notification error")
}

trigger.Fire("user-newfile-success")
}

func notifyUserFail() {
Expand Down
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
This package is a demo of an [astilectron](https://github.com/asticode/go-astilectron) app that uses the [bootstrap](https://github.com/asticode/go-astilectron-bootstrap) and the [bundler](https://github.com/asticode/go-astilectron-bundler).

It's also the subject of this [blog post](https://medium.com/@social_57971/how-to-add-a-gui-to-your-golang-app-in-5-easy-steps-c25c99d4d8e0).

![screenshot](screenshot.png)

# Step 1: install the demo

Run the following commands:
Expand Down
41 changes: 22 additions & 19 deletions resources/app/index.html
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
<!DOCTYPE html>
<html lang="fr">
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="static/css/base.css"/>
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://blueimp.github.io/JavaScript-MD5/js/md5.js"></script>
<title>Updemia</title>
<link rel="stylesheet" href="static/lib/astiloader/astiloader.css">
<link rel="stylesheet" href="static/lib/astimodaler/astimodaler.css">
<link rel="stylesheet" href="static/lib/astinotifier/astinotifier.css">
<link rel="stylesheet" href="static/lib/font-awesome-4.7.0/css/font-awesome.min.css">
</head>
<body>
<div class="container">
<div class="HelloBox center">
<div id="authBox">
<form class="HelloBox center">
<img src="http://www.updemia.com/images/logo.png" alt="" />
<h1>Hello, you</h1>
<input type="text" value="test" placeholder="What's your user email?" autofocus="true" id="emailform" class="input-login" />
<input type="password" value="test" placeholder="Pass phrase" id="passwordform" class="input-login" />
<h1>
Hello, you
</h1>
<input id="email" type="email" placeholder="What's your user email?" autofocus="true" required="required" />
<input id="password" type="password" placeholder="Pass phrase" required="required" />
<button type="button" class="btn" id="authBtn">Start</button>

<p><br /></p>
<button class="btn" id="auth_btn">Start</button>

<p><br /></p>
<div id="msg" style="border:1px solid black">test</div>
</div>
<div id="imgListing">
<ul></ul>
</div>
<span id="userHash"></span>
</form>
</div>
<div id="listMedia"></div>

<input type="hidden" id="userHiddenHash">
<script src="static/js/index.js"></script>
<script src="static/lib/astiloader/astiloader.js"></script>
<script src="static/lib/astimodaler/astimodaler.js"></script>
<script src="static/lib/astinotifier/astinotifier.js"></script>
<script src="static/lib/chart/chart.min.js"></script>
<script type="text/javascript">
index.init();
</script>
Expand Down
Loading

0 comments on commit 1caecf7

Please sign in to comment.