forked from cztomczak/cef2go
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhttp_server_windows.go
113 lines (96 loc) · 3.24 KB
/
http_server_windows.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
// Copyright (c) 2014 The cef2go authors. All rights reserved.
// License: BSD 3-clause.
// Website: https://github.com/CzarekTomczak/cef2go
// DESCRIPTION:
// ----------------------------------------------------------------------------
// This example shows how to run an internal web server while
// embedding Chromium browser. You can communicate with Go from
// javascript using XMLHttpRequests.
// ----------------------------------------------------------------------------
package main
import (
"fmt"
"net/http"
"runtime"
)
// Imports from "main_windows.go"
import (
"cef"
"github.com/op/go-logging"
"os"
"syscall"
"time"
"unsafe"
"wingui"
)
func handler(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprintf(w, "This is Go server talking.<br>")
fmt.Fprintf(w, "Time on the server: %v<br>",
time.Now().Format("Jan 2, 2006 at 3:04pm (MST)"))
fmt.Fprintf(w, "Go version: %v<br>", runtime.Version())
fmt.Fprintf(w, "<br>")
if req.URL.Path == "/" {
fmt.Fprintf(w, "Try <a href=/test.go>/test.go</a><br>")
} else if req.URL.Path == "/test.go" {
fmt.Fprintf(w, "<b>You accessed /test.go</b><br>")
}
}
func run_http_server() {
http.HandleFunc("/", handler)
listen_at := "127.0.0.1:54007"
fmt.Printf("Running http server at %s\n", listen_at)
http.ListenAndServe(listen_at, nil)
}
// ----------------------------------------------------------------------------
// The code below copied from "main_windows.go" with the following changes:
// 1. Added a call to run_http_server() at the beginning of main.
// 2. Changed url at browser creation to "http://127.0.0.1:54007/"
// 3. Imports were moved to the top of the file
// ----------------------------------------------------------------------------
var log = logging.MustGetLogger("cef2go")
func main() {
go run_http_server()
hInstance, e := wingui.GetModuleHandle(nil)
if e != nil {
wingui.AbortErrNo("GetModuleHandle", e)
}
cef.ExecuteProcess(unsafe.Pointer(hInstance))
settings := cef.Settings{}
settings.CachePath = "webcache" // Set to empty to disable
settings.LogSeverity = cef.LOGSEVERITY_DEFAULT // LOGSEVERITY_VERBOSE
cef.Initialize(settings)
wndproc := syscall.NewCallback(WndProc)
log.Info("CreateWindow")
hwnd := wingui.CreateWindow("cef2go example", wndproc)
browserSettings := cef.BrowserSettings{}
// TODO: It should be executable's directory used
// rather than working directory.
url, _ := os.Getwd()
url = "file://" + url + "/example.html"
url = "http://127.0.0.1:54007/"
cef.CreateBrowser(unsafe.Pointer(hwnd), browserSettings, url)
// It should be enough to call WindowResized after 10ms,
// though to be sure let's extend it to 100ms.
time.AfterFunc(time.Millisecond*100, func() {
cef.WindowResized(unsafe.Pointer(hwnd))
})
cef.RunMessageLoop()
cef.Shutdown()
os.Exit(0)
}
func WndProc(hwnd syscall.Handle, msg uint32, wparam, lparam uintptr) (rc uintptr) {
switch msg {
case wingui.WM_CREATE:
rc = wingui.DefWindowProc(hwnd, msg, wparam, lparam)
case wingui.WM_SIZE:
cef.WindowResized(unsafe.Pointer(hwnd))
case wingui.WM_CLOSE:
wingui.DestroyWindow(hwnd)
case wingui.WM_DESTROY:
cef.QuitMessageLoop()
default:
rc = wingui.DefWindowProc(hwnd, msg, wparam, lparam)
}
return
}