1
0
mirror of https://github.com/mattermost/focalboard.git synced 2024-12-15 09:14:11 +02:00
focalboard/win/main.go

145 lines
3.8 KiB
Go
Raw Normal View History

2020-11-04 00:37:29 +02:00
package main
import (
"context"
2021-02-10 01:24:50 +02:00
"fmt"
2020-11-04 00:37:29 +02:00
"log"
"os"
"os/exec"
"runtime"
2020-11-04 00:37:29 +02:00
"github.com/gonutz/w32"
2021-02-09 22:51:53 +02:00
"github.com/google/uuid"
2020-11-04 00:37:29 +02:00
"github.com/zserge/lorca"
)
2021-02-10 01:24:50 +02:00
var sessionToken string = "su-" + uuid.New().String()
2021-02-09 22:51:53 +02:00
2021-01-26 20:52:42 +02:00
func runServer(ctx context.Context) *exec.Cmd {
// cmd := exec.CommandContext(ctx, "focalboard-server.exe", "--monitorpid", strconv.FormatInt(int64(os.Getpid()), 10), "-single-user")
cmd := exec.CommandContext(ctx, "focalboard-server.exe", "-single-user")
2021-01-29 00:23:52 +02:00
// cmd := exec.CommandContext(ctx, "cmd.exe", "/C", "start", "./bin/focalboard-server.exe", "--monitorpid", strconv.FormatInt(int64(os.Getpid()), 10))
// cmd := exec.CommandContext(ctx, "cmd.exe", "/C", "start", "./bin/focalboard-server.exe")
2020-11-04 00:37:29 +02:00
// cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
cmd.Env = []string{fmt.Sprintf("FOCALBOARD_SINGLE_USER_TOKEN=%s", sessionToken)}
2020-11-04 00:37:29 +02:00
cmd.Stdout = os.Stdout
go func() {
err := cmd.Run()
if err != nil {
2021-02-11 20:26:14 +02:00
log.Println("Failed to start server")
2020-11-04 00:37:29 +02:00
log.Fatal(err)
}
log.Printf("Just ran subprocess %d, exiting\n", cmd.Process.Pid)
}()
return cmd
}
func main() {
// log.Printf("PID: %s", strconv.FormatInt(int64(os.Getpid()), 10))
hideConsole()
2021-01-21 06:07:35 +02:00
// Try to find Chrome if Lorca can't find it
2021-01-21 06:28:43 +02:00
if len(lorca.ChromeExecutable()) == 0 {
2021-01-21 06:07:35 +02:00
chromePath := locateChrome()
log.Printf("chromePath: %s", chromePath)
if len(chromePath) > 0 {
2021-01-21 06:28:43 +02:00
os.Setenv("LORCACHROME", chromePath)
2021-01-21 06:07:35 +02:00
} else {
lorca.PromptDownload()
log.Fatal("Chrome not installed")
}
}
2020-11-04 00:37:29 +02:00
ctx, cancel := context.WithCancel(context.Background())
2021-01-26 20:52:42 +02:00
cmd := runServer(ctx)
2020-11-04 00:37:29 +02:00
2021-02-10 03:00:14 +02:00
defer func() {
2021-02-10 03:03:24 +02:00
log.Println("Cleanup")
2021-02-10 03:00:14 +02:00
cancel()
if err := cmd.Process.Kill(); err != nil {
log.Fatal("failed to kill server process: ", err)
}
if r := recover(); r != nil {
log.Fatal("ERROR: ", r)
}
}()
2021-02-10 01:24:50 +02:00
ui, err := lorca.New("", "", 1024, 768)
2020-11-04 00:37:29 +02:00
if err != nil {
2021-02-10 03:00:14 +02:00
log.Panic(err)
2020-11-04 00:37:29 +02:00
}
2021-02-10 03:00:14 +02:00
defer ui.Close()
2020-11-04 00:37:29 +02:00
2021-02-10 01:24:50 +02:00
if err := ui.Load("http://localhost:8088"); err != nil {
2021-02-10 03:00:14 +02:00
log.Panic(err)
2021-02-10 01:24:50 +02:00
}
script := fmt.Sprintf("localStorage.setItem('sessionId', '%s');", sessionToken)
value := ui.Eval(script)
if err := value.Err(); err != nil {
2021-02-10 21:11:12 +02:00
log.Panic(err)
2021-02-10 01:24:50 +02:00
}
2021-02-09 22:51:53 +02:00
2020-11-04 00:37:29 +02:00
log.Printf("Started")
<-ui.Done()
2021-02-10 03:03:24 +02:00
log.Println("App Closed")
2020-11-04 00:37:29 +02:00
}
func hideConsole() {
console := w32.GetConsoleWindow()
if console != 0 {
_, consoleProcID := w32.GetWindowThreadProcessId(console)
if w32.GetCurrentProcessId() == consoleProcID {
w32.ShowWindowAsync(console, w32.SW_HIDE)
}
}
}
// This duplicates the logic in Lorca, but adds Edge as an option for Windows, fallback to standard logic for other OSes
func locateChrome() string {
var paths []string
switch runtime.GOOS {
// case "darwin":
// paths = []string{
// "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
// "/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary",
// "/Applications/Chromium.app/Contents/MacOS/Chromium",
// "/usr/bin/google-chrome-stable",
// "/usr/bin/google-chrome",
// "/usr/bin/chromium",
// "/usr/bin/chromium-browser",
// }
case "windows":
paths = []string{
os.Getenv("LocalAppData") + "/Google/Chrome/Application/chrome.exe",
os.Getenv("ProgramFiles") + "/Google/Chrome/Application/chrome.exe",
os.Getenv("ProgramFiles(x86)") + "/Google/Chrome/Application/chrome.exe",
os.Getenv("LocalAppData") + "/Chromium/Application/chrome.exe",
os.Getenv("ProgramFiles") + "/Chromium/Application/chrome.exe",
os.Getenv("ProgramFiles(x86)") + "/Chromium/Application/chrome.exe",
os.Getenv("ProgramFiles(x86)") + "/Microsoft/Edge/Application/msedge.exe",
}
// default:
// paths = []string{
// "/usr/bin/google-chrome-stable",
// "/usr/bin/google-chrome",
// "/usr/bin/chromium",
// "/usr/bin/chromium-browser",
// "/snap/bin/chromium",
// }
}
for _, path := range paths {
if _, err := os.Stat(path); os.IsNotExist(err) {
continue
}
return path
}
return ""
}