github.com/ckxng/wakeup@v0.0.0-20190105202853-90356a5f5a15/src/http_server_windows.go (about) 1 // Copyright (c) 2014 The cef2go authors. All rights reserved. 2 // License: BSD 3-clause. 3 // Website: https://github.com/CzarekTomczak/cef2go 4 5 // DESCRIPTION: 6 // ---------------------------------------------------------------------------- 7 // This example shows how to run an internal web server while 8 // embedding Chromium browser. You can communicate with Go from 9 // javascript using XMLHttpRequests. 10 // ---------------------------------------------------------------------------- 11 12 package main 13 14 import ( 15 "fmt" 16 "net/http" 17 "runtime" 18 ) 19 20 // Imports from "main_windows.go" 21 import ( 22 "cef" 23 "wingui" 24 "os" 25 "syscall" 26 "unsafe" 27 "log" 28 "time" 29 ) 30 31 func handler(w http.ResponseWriter, req *http.Request) { 32 w.Header().Set("Content-Type", "text/html; charset=utf-8") 33 fmt.Fprintf(w, "This is Go server talking.<br>") 34 fmt.Fprintf(w, "Time on the server: %v<br>", 35 time.Now().Format("Jan 2, 2006 at 3:04pm (MST)")) 36 fmt.Fprintf(w, "Go version: %v<br>", runtime.Version()) 37 fmt.Fprintf(w, "<br>") 38 if req.URL.Path == "/" { 39 fmt.Fprintf(w, "Try <a href=/test.go>/test.go</a><br>") 40 } else if req.URL.Path == "/test.go" { 41 fmt.Fprintf(w, "<b>You accessed /test.go</b><br>") 42 } 43 } 44 45 func run_http_server() { 46 http.HandleFunc("/", handler) 47 listen_at := "127.0.0.1:54007" 48 fmt.Printf("Running http server at %s\n", listen_at) 49 http.ListenAndServe(listen_at, nil) 50 } 51 52 // ---------------------------------------------------------------------------- 53 // The code below copied from "main_windows.go" with the following changes: 54 // 1. Added a call to run_http_server() at the beginning of main. 55 // 2. Changed url at browser creation to "http://127.0.0.1:54007/" 56 // 3. Imports were moved to the top of the file 57 // ---------------------------------------------------------------------------- 58 59 var Logger *log.Logger = log.New(os.Stdout, "[main] ", log.Lshortfile) 60 61 func main() { 62 go run_http_server() 63 64 hInstance, e := wingui.GetModuleHandle(nil) 65 if e != nil { wingui.AbortErrNo("GetModuleHandle", e) } 66 67 cef.ExecuteProcess(unsafe.Pointer(hInstance)) 68 69 settings := cef.Settings{} 70 settings.CachePath = "webcache" // Set to empty to disable 71 settings.LogSeverity = cef.LOGSEVERITY_DEFAULT // LOGSEVERITY_VERBOSE 72 cef.Initialize(settings) 73 74 wndproc := syscall.NewCallback(WndProc) 75 Logger.Println("CreateWindow") 76 hwnd := wingui.CreateWindow("cef2go example", wndproc) 77 78 browserSettings := cef.BrowserSettings{} 79 // TODO: It should be executable's directory used 80 // rather than working directory. 81 url, _ := os.Getwd() 82 url = "file://" + url + "/example.html" 83 url = "http://127.0.0.1:54007/" 84 cef.CreateBrowser(unsafe.Pointer(hwnd), browserSettings, url) 85 86 // It should be enough to call WindowResized after 10ms, 87 // though to be sure let's extend it to 100ms. 88 time.AfterFunc(time.Millisecond * 100, func(){ 89 cef.WindowResized(unsafe.Pointer(hwnd)) 90 }) 91 92 cef.RunMessageLoop() 93 cef.Shutdown() 94 os.Exit(0) 95 } 96 97 func WndProc(hwnd syscall.Handle, msg uint32, wparam, lparam uintptr) (rc uintptr) { 98 switch msg { 99 case wingui.WM_CREATE: 100 rc = wingui.DefWindowProc(hwnd, msg, wparam, lparam) 101 case wingui.WM_SIZE: 102 cef.WindowResized(unsafe.Pointer(hwnd)) 103 case wingui.WM_CLOSE: 104 wingui.DestroyWindow(hwnd) 105 case wingui.WM_DESTROY: 106 cef.QuitMessageLoop() 107 default: 108 rc = wingui.DefWindowProc(hwnd, msg, wparam, lparam) 109 } 110 return 111 }