1
0
mirror of https://github.com/mattermost/focalboard.git synced 2024-11-27 08:31:20 +02:00
focalboard/linux/main.go

138 lines
3.0 KiB
Go
Raw Normal View History

2020-10-29 08:50:25 +02:00
package main
import (
2021-02-10 01:46:49 +02:00
"fmt"
2020-10-29 08:50:25 +02:00
"log"
2021-04-23 10:38:24 +02:00
"net"
"os/exec"
"runtime"
2020-10-29 08:50:25 +02:00
2021-02-10 01:46:49 +02:00
"github.com/google/uuid"
2021-04-23 10:38:24 +02:00
"github.com/mattermost/focalboard/server/server"
"github.com/mattermost/focalboard/server/services/config"
2020-10-29 08:50:25 +02:00
"github.com/webview/webview"
"github.com/mattermost/mattermost-server/v6/shared/mlog"
2020-10-29 08:50:25 +02:00
)
2021-02-10 01:46:49 +02:00
var sessionToken string = "su-" + uuid.New().String()
2021-04-23 10:38:24 +02:00
func getFreePort() (int, error) {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
2021-04-13 20:20:21 +02:00
if err != nil {
2021-04-23 10:38:24 +02:00
return 0, err
2021-04-13 20:20:21 +02:00
}
2021-04-23 10:38:24 +02:00
l, err := net.ListenTCP("tcp", addr)
if err != nil {
return 0, err
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port, nil
}
2021-04-13 20:20:21 +02:00
2021-04-23 10:38:24 +02:00
func runServer(port int) (*server.Server, error) {
logger, _ := mlog.NewLogger()
config := &config.Configuration{
2021-04-23 10:38:24 +02:00
ServerRoot: fmt.Sprintf("http://localhost:%d", port),
Port: port,
DBType: "sqlite3",
DBConfigString: "./focalboard.db",
UseSSL: false,
SecureCookie: true,
WebPath: "./pack",
FilesDriver: "local",
2021-04-23 10:38:24 +02:00
FilesPath: "./focalboard_files",
Telemetry: true,
WebhookUpdate: []string{},
SessionExpireTime: 259200000000,
SessionRefreshTime: 18000,
LocalOnly: false,
EnableLocalMode: false,
LocalModeSocketLocation: "",
AuthMode: "native",
}
db, err := server.NewStore(config, logger)
if err != nil {
fmt.Println("ERROR INITIALIZING THE SERVER STORE", err)
return nil, err
}
2021-09-13 21:36:36 +02:00
params := server.Params{
Cfg: config,
SingleUserToken: sessionToken,
DBStore: db,
Logger: logger,
ServerID: "",
WSAdapter: nil,
NotifyBackends: nil,
}
server, err := server.New(params)
2020-10-29 08:50:25 +02:00
if err != nil {
2021-04-23 10:38:24 +02:00
fmt.Println("ERROR INITIALIZING THE SERVER", err)
return nil, err
}
err = server.Start()
if err != nil {
return nil, err
2020-10-29 08:50:25 +02:00
}
2021-04-23 10:38:24 +02:00
return server, nil
2020-10-29 08:50:25 +02:00
}
func openBrowser(url string) {
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
if err != nil {
log.Fatal(err)
}
}
2020-10-29 08:50:25 +02:00
func main() {
debug := true
w := webview.New(debug)
defer w.Destroy()
2021-04-23 10:38:24 +02:00
port, err := getFreePort()
if err != nil {
log.Println("Failed to open a free port")
log.Fatal(err)
}
server, err := runServer(port)
if err != nil {
log.Println("Failed to start the server")
log.Fatal(err)
}
2020-10-29 08:50:25 +02:00
2021-01-26 20:52:42 +02:00
w.SetTitle("Focalboard")
2020-10-29 08:50:25 +02:00
w.SetSize(1024, 768, webview.HintNone)
2021-02-10 01:46:49 +02:00
script := fmt.Sprintf("localStorage.setItem('focalboardSessionId', '%s');", sessionToken)
2021-02-10 01:46:49 +02:00
w.Init(script)
2021-04-23 10:38:24 +02:00
w.Navigate(fmt.Sprintf("http://localhost:%d", port))
w.Bind("openInNewBrowser", openBrowser)
w.Init(`
document.addEventListener('click', function (e) {
let a = e.target.closest('a[target="_blank"]');
if (a) {
openInNewBrowser(a.getAttribute('href'));
}
});
`)
2020-10-29 08:50:25 +02:00
w.Run()
2021-04-23 10:38:24 +02:00
server.Shutdown()
2020-10-29 08:50:25 +02:00
}