dashboard.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2016 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package server
  15. import (
  16. "fmt"
  17. "net"
  18. "net/http"
  19. "time"
  20. "github.com/fatedier/frp/src/assets"
  21. )
  22. var (
  23. httpServerReadTimeout = 10 * time.Second
  24. httpServerWriteTimeout = 10 * time.Second
  25. )
  26. func RunDashboardServer(addr string, port int64) (err error) {
  27. // url router
  28. mux := http.NewServeMux()
  29. // api, see dashboard_api.go
  30. mux.HandleFunc("/api/reload", apiReload)
  31. mux.HandleFunc("/api/proxies", apiProxies)
  32. // view, see dashboard_view.go
  33. mux.Handle("/favicon.ico", http.FileServer(assets.FileSystem))
  34. mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(assets.FileSystem)))
  35. mux.HandleFunc("/", viewDashboard)
  36. address := fmt.Sprintf("%s:%d", addr, port)
  37. server := &http.Server{
  38. Addr: address,
  39. Handler: mux,
  40. ReadTimeout: httpServerReadTimeout,
  41. WriteTimeout: httpServerWriteTimeout,
  42. }
  43. if address == "" {
  44. address = ":http"
  45. }
  46. ln, err := net.Listen("tcp", address)
  47. if err != nil {
  48. return err
  49. }
  50. go server.Serve(ln)
  51. return
  52. }