You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.go 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package main
  2. import "errors"
  3. import "log"
  4. import "os"
  5. import "fmt"
  6. import "path"
  7. import "net/http"
  8. import "io/ioutil"
  9. import "encoding/json"
  10. import "github.com/go-vgo/robotgo"
  11. import "github.com/BurntSushi/toml"
  12. import "coffee.mort.mediator/screencap"
  13. type Config struct {
  14. BasePath string `toml:"base_path"`
  15. }
  16. type MousePosData struct {
  17. X int `json:"x"`
  18. Y int `json:"y"`
  19. }
  20. type ScreenSizeData struct {
  21. Width int `json:"width"`
  22. Height int `json:"height"`
  23. }
  24. type DirEntryData struct {
  25. Name string `json:"name"`
  26. Type string `json:"type"`
  27. }
  28. type ListDirData struct {
  29. Entries []DirEntryData `json:"entries"`
  30. }
  31. type Error struct {
  32. Error string `json:"error"`
  33. }
  34. func readConfig() (*Config, error) {
  35. confFile, err := os.Open("config.toml")
  36. if err != nil {
  37. return nil, err
  38. }
  39. defer confFile.Close()
  40. var conf Config
  41. _, err = toml.NewDecoder(confFile).Decode(&conf)
  42. if err != nil {
  43. return nil, err
  44. }
  45. return &conf, nil
  46. }
  47. type RW http.ResponseWriter
  48. type Req http.Request
  49. func handler(h func(w RW, req *Req) error) (
  50. func(w http.ResponseWriter, req *http.Request)) {
  51. return func(w http.ResponseWriter, req *http.Request) {
  52. err := h(RW(w), (*Req)(req))
  53. if err != nil {
  54. w.WriteHeader(400)
  55. err = json.NewEncoder(w).Encode(&Error{err.Error()})
  56. if err != nil {
  57. w.Write([]byte("Oh no, failed to encode error struct"))
  58. }
  59. }
  60. }
  61. }
  62. func main() {
  63. conf, err := readConfig()
  64. if err != nil {
  65. log.Fatal(err)
  66. }
  67. log.Printf("Config: %#v", conf)
  68. fs := http.FileServer(http.Dir("./web"))
  69. http.Handle("/", fs)
  70. http.HandleFunc("/api/remote/screen-size", handler(func(w RW, req *Req) error {
  71. if req.Method == "GET" {
  72. var size ScreenSizeData
  73. size.Width, size.Height = robotgo.GetScreenSize()
  74. return json.NewEncoder(w).Encode(&size)
  75. } else {
  76. return errors.New("Invalid method: " + req.Method)
  77. }
  78. }))
  79. http.HandleFunc("/api/remote/mouse-pos", handler(func(w RW, req *Req) error {
  80. if req.Method == "GET" {
  81. var pos MousePosData
  82. pos.X, pos.Y = robotgo.GetMousePos()
  83. return json.NewEncoder(w).Encode(&pos)
  84. } else if req.Method == "PUT" {
  85. var pos MousePosData
  86. err := json.NewDecoder(req.Body).Decode(&pos)
  87. if err != nil {
  88. return err
  89. }
  90. robotgo.MoveMouse(pos.X, pos.Y)
  91. return nil
  92. } else {
  93. return errors.New("Invalid method: " + req.Method)
  94. }
  95. }))
  96. http.HandleFunc("/api/remote/screencast", handler(func(w RW, req *Req) error {
  97. if req.Method == "GET" {
  98. w.Header().Add("Content-Type", "multipart/x-mixed-replace;boundary=MEDIATOR_FRAME_BOUNDARY")
  99. w.WriteHeader(200)
  100. for {
  101. img := <-screencap.Capture()
  102. log.Printf("Got image, %v bytes", img.Length)
  103. var err error
  104. _, err = w.Write([]byte(fmt.Sprintf(
  105. "--MEDIATOR_FRAME_BOUNDARY\r\n" +
  106. "Content-Type: image/jpeg\r\n" +
  107. "Content-Length: %d\r\n" +
  108. "\r\n", img.Length)))
  109. if err != nil {
  110. log.Printf("Write error: %v", err)
  111. return nil
  112. }
  113. _, err = w.Write(img.Data[0:img.Length])
  114. if err != nil {
  115. log.Printf("Write error: %v", err)
  116. return nil
  117. }
  118. _, err = w.Write([]byte("\r\n"))
  119. if err != nil {
  120. log.Printf("Write error: %v", err)
  121. return nil
  122. }
  123. }
  124. } else {
  125. return errors.New("Invalid method: " + req.Method)
  126. }
  127. }));
  128. http.HandleFunc("/api/dir/", handler(func(w RW, req *Req) error {
  129. if req.Method == "GET" {
  130. subPath := req.URL.Path[len("/api/dir/"):]
  131. dirEnts, err := ioutil.ReadDir(path.Join(conf.BasePath, subPath))
  132. if err != nil {
  133. return err
  134. }
  135. list := ListDirData{
  136. Entries: make([]DirEntryData, 0, len(dirEnts)),
  137. }
  138. for _, ent := range dirEnts {
  139. entType := "f"
  140. if ent.IsDir() {
  141. entType = "d"
  142. }
  143. list.Entries = append(list.Entries, DirEntryData{
  144. Name: ent.Name(),
  145. Type: entType,
  146. })
  147. }
  148. return json.NewEncoder(w).Encode(&list)
  149. } else {
  150. return errors.New("Invalid method: " + req.Method)
  151. }
  152. }))
  153. go screencap.Run()
  154. log.Println("Listening on :3000...")
  155. err = http.ListenAndServe("localhost:3000", nil)
  156. if err != nil {
  157. log.Fatal(err)
  158. }
  159. }