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.

161 lines
3.5 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package osmanthuswine
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/go-chi/chi"
  6. "github.com/go-chi/chi/middleware"
  7. "github.com/ouxuanserver/osmanthuswine/src/core"
  8. "github.com/ouxuanserver/osmanthuswine/src/session"
  9. "github.com/wailovet/overseer"
  10. "github.com/wailovet/overseer/fetcher"
  11. "log"
  12. "net"
  13. "net/http"
  14. "os"
  15. "os/exec"
  16. "osmanthuswine/src/helper"
  17. "path/filepath"
  18. "runtime"
  19. "runtime/debug"
  20. "strings"
  21. "time"
  22. )
  23. var chiRouter *chi.Mux
  24. func GetChiRouter() *chi.Mux {
  25. if chiRouter == nil {
  26. chiRouter = chi.NewRouter()
  27. chiRouter.Use(middleware.RequestID)
  28. chiRouter.Use(middleware.RealIP)
  29. chiRouter.Use(middleware.Logger)
  30. chiRouter.Use(middleware.Recoverer)
  31. chiRouter.Use(middleware.Timeout(60 * time.Second))
  32. }
  33. return chiRouter
  34. }
  35. func Run() {
  36. path, _ := GetCurrentPath()
  37. os.Chdir(path)
  38. log.Println("工作目录:", path)
  39. cc := core.GetInstanceConfig()
  40. if runtime.GOOS == "windows" || cc.UpdatePath == "" {
  41. listener, err := net.Listen("tcp", cc.Host+":"+cc.Port)
  42. if err != nil {
  43. log.Fatal(err.Error())
  44. }
  45. RunProg(overseer.State{
  46. Listener: listener,
  47. })
  48. } else {
  49. overseer.Run(overseer.Config{
  50. Program: RunProg,
  51. Address: cc.Host + ":" + cc.Port,
  52. Fetcher: &fetcher.File{
  53. Path: cc.UpdateDir + cc.UpdatePath,
  54. Interval: time.Second * 10,
  55. },
  56. })
  57. }
  58. }
  59. func RunProg(state overseer.State) {
  60. cc := core.GetInstanceConfig()
  61. helper.GetInstanceLog().Out("开始监听:", cc.Host+":"+cc.Port)
  62. r := GetChiRouter()
  63. apiRouter := cc.ApiRouter
  64. r.HandleFunc(apiRouter, func(writer http.ResponseWriter, request *http.Request) {
  65. requestData := core.Request{}
  66. sessionMan := session.New(request, writer)
  67. requestData.REQUEST = make(map[string]string)
  68. //GET
  69. requestData.SyncGetData(request)
  70. //POST
  71. requestData.SyncPostData(request, cc.PostMaxMemory)
  72. //HEADER
  73. requestData.SyncHeaderData(request)
  74. //COOKIE
  75. requestData.SyncCookieData(request)
  76. //SESSION
  77. requestData.SyncSessionData(sessionMan)
  78. responseHandle := core.Response{OriginResponseWriter: writer, Session: sessionMan}
  79. defer func() {
  80. errs := recover()
  81. if errs == nil {
  82. return
  83. }
  84. errtxt := fmt.Sprintf("%v", errs)
  85. if errtxt != "" {
  86. responseHandle.DisplayByError(errtxt, 500, strings.Split(string(debug.Stack()), "\n\t")...)
  87. }
  88. }()
  89. core.GetInstanceRouterManage().RouterSend(request.URL.Path, requestData, responseHandle, cc.CrossDomain)
  90. })
  91. r.Handle("/*", http.FileServer(http.Dir("html")))
  92. //r.HandleFunc("/html/*", func(writer http.ResponseWriter, request *http.Request) {
  93. // path := request.URL.Path
  94. // if path == "/html/" {
  95. // path = "/index.html"
  96. // }
  97. //
  98. // path=strings.TrimLeft(path,"/")
  99. // helper.GetInstanceLog().Out("静态文件:", path)
  100. //
  101. // f, err := os.Stat(path)
  102. // if err == nil {
  103. // if f.IsDir() {
  104. // path += "/index.html"
  105. // }
  106. // data, err := ioutil.ReadFile(path)
  107. // if err == nil {
  108. // writer.Write(data)
  109. // return
  110. // }
  111. // }
  112. //
  113. // writer.WriteHeader(404)
  114. // writer.Write([]byte(err.Error()))
  115. //
  116. //})
  117. if err := http.Serve(state.Listener, r); err != nil {
  118. log.Fatal(err)
  119. }
  120. //http.ListenAndServe(cc.Host+":"+cc.Port, r)
  121. }
  122. func GetCurrentPath() (string, error) {
  123. file, err := exec.LookPath(os.Args[0])
  124. if err != nil {
  125. return "", err
  126. }
  127. path, err := filepath.Abs(file)
  128. if err != nil {
  129. return "", err
  130. }
  131. i := strings.LastIndex(path, "/")
  132. if i < 0 {
  133. i = strings.LastIndex(path, "\\")
  134. }
  135. if i < 0 {
  136. return "", errors.New(`error: Can't find "/" or "\".`)
  137. }
  138. return string(path[0 : i+1]), nil
  139. }