Initial project setup with Go backend, React frontend, and Neutralino desktop shell
This commit is contained in:
8
backend/build.cmd
Normal file
8
backend/build.cmd
Normal file
@@ -0,0 +1,8 @@
|
||||
@echo off
|
||||
|
||||
set DST=nex.exe
|
||||
|
||||
echo Building %DST% ...
|
||||
go build -ldflags="-s -w" -o %DST% ./cmd/nex
|
||||
|
||||
echo DONE!
|
||||
8
backend/build.sh
Normal file
8
backend/build.sh
Normal file
@@ -0,0 +1,8 @@
|
||||
#!/bin/bash
|
||||
|
||||
DST=nex
|
||||
|
||||
echo "Building $DST ..."
|
||||
go build -ldflags="-s -w" -o $DST ./cmd/nex
|
||||
chmod +x $DST
|
||||
echo "DONE!"
|
||||
40
backend/cmd/nex/main.go
Normal file
40
backend/cmd/nex/main.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"nex/internal/neutralino"
|
||||
"time"
|
||||
)
|
||||
|
||||
const extDebug = true
|
||||
|
||||
var ext = new(neutralino.WSClient)
|
||||
|
||||
func processAppEvent(data neutralino.EventMessage) {
|
||||
if ext.IsEvent(data, "runGo") {
|
||||
if d, ok := data.Data.(map[string]interface{}); ok {
|
||||
|
||||
if d["function"] == "ping" {
|
||||
var out = make(map[string]interface{})
|
||||
out["result"] = fmt.Sprintf("Go says PONG in reply to '%s'", d["parameter"])
|
||||
ext.Send("pingResult", out)
|
||||
}
|
||||
|
||||
if d["function"] == "longRun" {
|
||||
go longRun()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func longRun() {
|
||||
for i := 1; i <= 10; i++ {
|
||||
s := fmt.Sprintf("Long running task progress %d / 10", i)
|
||||
ext.SendMessageString("pingResult", s)
|
||||
time.Sleep(time.Second * 1)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
ext.Run(processAppEvent, extDebug)
|
||||
}
|
||||
10
backend/go.mod
Normal file
10
backend/go.mod
Normal file
@@ -0,0 +1,10 @@
|
||||
module nex
|
||||
|
||||
go 1.22
|
||||
|
||||
require (
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/gorilla/websocket v1.5.1
|
||||
)
|
||||
|
||||
require golang.org/x/net v0.17.0 // indirect
|
||||
6
backend/go.sum
Normal file
6
backend/go.sum
Normal file
@@ -0,0 +1,6 @@
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
|
||||
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
|
||||
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
|
||||
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
|
||||
161
backend/internal/neutralino/client.go
Normal file
161
backend/internal/neutralino/client.go
Normal file
@@ -0,0 +1,161 @@
|
||||
package neutralino
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/signal"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
const Version = "1.0.6"
|
||||
|
||||
type Config struct {
|
||||
NlPort string `json:"nlPort"`
|
||||
NlToken string `json:"nlToken"`
|
||||
NlExtensionId string `json:"nlExtensionId"`
|
||||
NlConnectToken string `json:"nlConnectToken"`
|
||||
}
|
||||
|
||||
type EventMessage struct {
|
||||
Event string `json:"event"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
type DataPacket struct {
|
||||
Id string `json:"id"`
|
||||
Method string `json:"method"`
|
||||
AccessToken string `json:"accessToken"`
|
||||
Data EventMessage `json:"data"`
|
||||
}
|
||||
|
||||
type WSClient struct {
|
||||
url url.URL
|
||||
socket *websocket.Conn
|
||||
debug bool
|
||||
}
|
||||
|
||||
var ExtConfig = Config{}
|
||||
|
||||
func (wsclient *WSClient) Send(event string, data map[string]interface{}) {
|
||||
var msg = DataPacket{}
|
||||
msg.Id = uuid.New().String()
|
||||
msg.Method = "app.broadcast"
|
||||
msg.AccessToken = ExtConfig.NlToken
|
||||
msg.Data.Event = event
|
||||
msg.Data.Data = data
|
||||
|
||||
var d, err = json.Marshal(msg)
|
||||
if err != nil {
|
||||
fmt.Println("Error in marshaling data-packet.")
|
||||
return
|
||||
}
|
||||
|
||||
if wsclient.debug {
|
||||
fmt.Printf("%sSent: %s%s\n", "\u001B[32m", string(d), "\u001B[0m")
|
||||
}
|
||||
|
||||
err = wsclient.socket.WriteMessage(websocket.TextMessage, []byte(d))
|
||||
if err != nil {
|
||||
if wsclient.debug {
|
||||
fmt.Println("Error in Send(): ", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (wsclient *WSClient) SendMessageString(event string, data string) {
|
||||
msg := make(map[string]interface{})
|
||||
msg["result"] = data
|
||||
wsclient.Send(event, msg)
|
||||
}
|
||||
|
||||
func (wsclient *WSClient) Run(callback func(message EventMessage), debug bool) {
|
||||
wsclient.debug = debug
|
||||
|
||||
decoder := json.NewDecoder(os.Stdin)
|
||||
|
||||
err := decoder.Decode(&ExtConfig)
|
||||
if err != nil {
|
||||
if err != io.EOF {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
sigInt := make(chan os.Signal, 1)
|
||||
if debug {
|
||||
signal.Notify(sigInt, os.Interrupt)
|
||||
}
|
||||
|
||||
var addr = "127.0.0.1:" + ExtConfig.NlPort
|
||||
var path = "?extensionId=" + ExtConfig.NlExtensionId + "&connectToken=" + ExtConfig.NlConnectToken
|
||||
|
||||
wsclient.url = url.URL{Scheme: "ws", Host: addr, Path: path}
|
||||
if wsclient.debug {
|
||||
fmt.Printf("Connecting to %s\n", wsclient.url.String())
|
||||
}
|
||||
|
||||
wsclient.socket, _, err = websocket.DefaultDialer.Dial(wsclient.url.String(), nil)
|
||||
if err != nil {
|
||||
if wsclient.debug {
|
||||
fmt.Println("Connect: ", err)
|
||||
}
|
||||
}
|
||||
defer wsclient.socket.Close()
|
||||
|
||||
go func() {
|
||||
for {
|
||||
_, msg, err := wsclient.socket.ReadMessage()
|
||||
if err != nil {
|
||||
if wsclient.debug {
|
||||
fmt.Println("ERROR in read loop: ", err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
if wsclient.debug {
|
||||
fmt.Printf("%sReceived: %s%s\n", "\u001B[91m", msg, "\u001B[0m")
|
||||
}
|
||||
|
||||
var d EventMessage
|
||||
err = json.Unmarshal([]byte(msg), &d)
|
||||
if err != nil {
|
||||
if wsclient.debug {
|
||||
fmt.Println("ERROR in read loop, while unmarshalling JSON: ", err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if wsclient.IsEvent(d, "windowClose") || wsclient.IsEvent(d, "appClose") {
|
||||
wsclient.quit()
|
||||
continue
|
||||
}
|
||||
|
||||
callback(d)
|
||||
}
|
||||
}()
|
||||
|
||||
for {
|
||||
if <-sigInt != nil {
|
||||
fmt.Println("Interrupted by keyboard interaction ...")
|
||||
wsclient.quit()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (wsclient *WSClient) IsEvent(data EventMessage, event string) bool {
|
||||
return data.Event == event
|
||||
}
|
||||
|
||||
func (wsclient *WSClient) quit() {
|
||||
var pid = os.Getpid()
|
||||
fmt.Println("Killing own process with PID ", pid)
|
||||
process, _ := os.FindProcess(pid)
|
||||
err := process.Signal(os.Kill)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user