Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Commit

Permalink
fix /stop error
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Dec 21, 2017
1 parent e184d03 commit 541e9b7
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bufio"
"bytes"
"context"
"encoding/binary"
"encoding/json"
"os/user"
Expand Down Expand Up @@ -537,9 +538,13 @@ func ServeHTTP(lis net.Listener, tunnel *TunnelProxy) error {
m.HandleFunc("/stop", func(w http.ResponseWriter, r *http.Request) {
log.Println("stop all service")
service.StopAll()
log.Println("server stopped")
log.Println("service stopped")
io.WriteString(w, "Finished!")
go httpServer.Shutdown(nil)
go func() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel() // The document says need to call cancel(), but I donot known why.
httpServer.Shutdown(ctx)
}()
})

m.HandleFunc("/uiautomator", func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -947,9 +952,9 @@ func ServeHTTP(lis net.Listener, tunnel *TunnelProxy) error {

target, _ := url.Parse("http://127.0.0.1:9008")
uiautomatorProxy := httputil.NewSingleHostReverseProxy(target)
http.Handle("/jsonrpc/0", uiautomatorProxy)
http.Handle("/ping", uiautomatorProxy)
http.HandleFunc("/screenshot/0", func(w http.ResponseWriter, r *http.Request) {
m.Handle("/jsonrpc/0", uiautomatorProxy)
m.Handle("/ping", uiautomatorProxy)
m.HandleFunc("/screenshot/0", func(w http.ResponseWriter, r *http.Request) {
if r.FormValue("minicap") == "false" || strings.ToLower(getProperty("ro.product.manufacturer")) == "meizu" {
uiautomatorProxy.ServeHTTP(w, r)
return
Expand All @@ -962,9 +967,11 @@ func ServeHTTP(lis net.Listener, tunnel *TunnelProxy) error {
http.ServeFile(w, r, screenshotFilename)
}
})
http.Handle("/", m)
http.Handle("/assets/", http.StripPrefix("/assets", http.FileServer(Assets)))
return http.Serve(lis, nil)

m.Handle("/assets/{(.*)}", http.StripPrefix("/assets", http.FileServer(Assets)))

httpServer = &http.Server{Handler: m} // url(/stop) need it.
return httpServer.Serve(lis)
}

func runDaemon() {
Expand Down

0 comments on commit 541e9b7

Please sign in to comment.