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.

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