/
snapweb.go
113 lines (94 loc) · 2.25 KB
/
snapweb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"fmt"
"net"
"net/http"
"os"
"sync"
"gopkg.in/qml.v1"
"github.com/hydrogen18/stoppableListener"
)
const webview = `
import QtQuick 2.0
import QtWebKit 3.0
WebView {
width: 1600
height: 800
}
`
const serveAtPort = 52660
const storagePath = "/tmp/shit"
func main() {
stopServer := make(chan int)
serverResult := make(chan error)
go runAppServer(stopServer, serverResult)
if err := qml.Run(run); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
}
stopServer <- 0
err := <-serverResult
if err != nil && err != stoppableListener.StoppedError {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
}
}
func dataHandler(w http.ResponseWriter, r *http.Request) {
// TODO(perrito666) pass to appropriate handler for urls and respond in json
// TODO(perrito666) See how to handle POST
switch r.Method {
case "GET":
}
fmt.Fprintf(w, "Some data")
fmt.Print(r.URL.Path)
}
func handlePostNode() error {
// TODO(perrito666) make node id folder, inside node label json and node text file
// also add attachments somehow, most likely to the json and with resources folder.
return nil
}
func runAppServer(serverStopChan chan int, serverResultChan chan error) {
http.Handle("/app/", http.StripPrefix("/app/", http.FileServer(http.Dir("/home/hduran/Develop/menditor/resources"))))
http.HandleFunc("/data/", dataHandler) // homepage
originalListener, err := net.Listen("tcp", fmt.Sprintf(":%d", serveAtPort))
if err != nil {
panic(err)
}
sl, err := stoppableListener.New(originalListener)
if err != nil {
panic(err)
}
server := http.Server{}
var wg sync.WaitGroup
var serverError error
go func() {
wg.Add(1)
defer wg.Done()
serverError = server.Serve(sl)
}()
select {
case <-serverStopChan:
sl.Stop()
}
wg.Wait()
serverResultChan <- serverError
}
func run() error {
engine := qml.NewEngine()
component, err := engine.LoadString("webview.qml", webview)
if err != nil {
return err
}
ctrl := &Control{
done: make(chan error),
win: component.CreateWindow(nil),
}
engine.Context().SetVar("ctrl", ctrl)
root := ctrl.win.Root()
root.Set("url", fmt.Sprintf("http://localhost:%d/app/base.html", serveAtPort))
ctrl.win.Show()
ctrl.win.Wait()
return nil
}
type Control struct {
win *qml.Window
done chan error
}