app.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. // Copyright 2014 beego Author. All Rights Reserved.
  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 beego
  15. import (
  16. "crypto/tls"
  17. "crypto/x509"
  18. "fmt"
  19. "io/ioutil"
  20. "net"
  21. "net/http"
  22. "net/http/fcgi"
  23. "os"
  24. "path"
  25. "strings"
  26. "time"
  27. "github.com/cnlh/nps/vender/github.com/astaxie/beego/grace"
  28. "github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
  29. "github.com/cnlh/nps/vender/github.com/astaxie/beego/utils"
  30. "golang.org/x/crypto/acme/autocert"
  31. )
  32. var (
  33. // BeeApp is an application instance
  34. BeeApp *App
  35. )
  36. func init() {
  37. // create beego application
  38. BeeApp = NewApp()
  39. }
  40. // App defines beego application with a new PatternServeMux.
  41. type App struct {
  42. Handlers *ControllerRegister
  43. Server *http.Server
  44. }
  45. // NewApp returns a new beego application.
  46. func NewApp() *App {
  47. cr := NewControllerRegister()
  48. app := &App{Handlers: cr, Server: &http.Server{}}
  49. return app
  50. }
  51. // MiddleWare function for http.Handler
  52. type MiddleWare func(http.Handler) http.Handler
  53. // Run beego application.
  54. func (app *App) Run(mws ...MiddleWare) {
  55. addr := BConfig.Listen.HTTPAddr
  56. if BConfig.Listen.HTTPPort != 0 {
  57. addr = fmt.Sprintf("%s:%d", BConfig.Listen.HTTPAddr, BConfig.Listen.HTTPPort)
  58. }
  59. var (
  60. err error
  61. l net.Listener
  62. endRunning = make(chan bool, 1)
  63. )
  64. // run cgi server
  65. if BConfig.Listen.EnableFcgi {
  66. if BConfig.Listen.EnableStdIo {
  67. if err = fcgi.Serve(nil, app.Handlers); err == nil { // standard I/O
  68. logs.Info("Use FCGI via standard I/O")
  69. } else {
  70. logs.Critical("Cannot use FCGI via standard I/O", err)
  71. }
  72. return
  73. }
  74. if BConfig.Listen.HTTPPort == 0 {
  75. // remove the Socket file before start
  76. if utils.FileExists(addr) {
  77. os.Remove(addr)
  78. }
  79. l, err = net.Listen("unix", addr)
  80. } else {
  81. l, err = net.Listen("tcp", addr)
  82. }
  83. if err != nil {
  84. logs.Critical("Listen: ", err)
  85. }
  86. if err = fcgi.Serve(l, app.Handlers); err != nil {
  87. logs.Critical("fcgi.Serve: ", err)
  88. }
  89. return
  90. }
  91. app.Server.Handler = app.Handlers
  92. for i := len(mws) - 1; i >= 0; i-- {
  93. if mws[i] == nil {
  94. continue
  95. }
  96. app.Server.Handler = mws[i](app.Server.Handler)
  97. }
  98. app.Server.ReadTimeout = time.Duration(BConfig.Listen.ServerTimeOut) * time.Second
  99. app.Server.WriteTimeout = time.Duration(BConfig.Listen.ServerTimeOut) * time.Second
  100. app.Server.ErrorLog = logs.GetLogger("HTTP")
  101. // run graceful mode
  102. if BConfig.Listen.Graceful {
  103. httpsAddr := BConfig.Listen.HTTPSAddr
  104. app.Server.Addr = httpsAddr
  105. if BConfig.Listen.EnableHTTPS || BConfig.Listen.EnableMutualHTTPS {
  106. go func() {
  107. time.Sleep(1000 * time.Microsecond)
  108. if BConfig.Listen.HTTPSPort != 0 {
  109. httpsAddr = fmt.Sprintf("%s:%d", BConfig.Listen.HTTPSAddr, BConfig.Listen.HTTPSPort)
  110. app.Server.Addr = httpsAddr
  111. }
  112. server := grace.NewServer(httpsAddr, app.Handlers)
  113. server.Server.ReadTimeout = app.Server.ReadTimeout
  114. server.Server.WriteTimeout = app.Server.WriteTimeout
  115. if BConfig.Listen.EnableMutualHTTPS {
  116. if err := server.ListenAndServeMutualTLS(BConfig.Listen.HTTPSCertFile, BConfig.Listen.HTTPSKeyFile, BConfig.Listen.TrustCaFile); err != nil {
  117. logs.Critical("ListenAndServeTLS: ", err, fmt.Sprintf("%d", os.Getpid()))
  118. time.Sleep(100 * time.Microsecond)
  119. endRunning <- true
  120. }
  121. } else {
  122. if BConfig.Listen.AutoTLS {
  123. m := autocert.Manager{
  124. Prompt: autocert.AcceptTOS,
  125. HostPolicy: autocert.HostWhitelist(BConfig.Listen.Domains...),
  126. Cache: autocert.DirCache(BConfig.Listen.TLSCacheDir),
  127. }
  128. app.Server.TLSConfig = &tls.Config{GetCertificate: m.GetCertificate}
  129. BConfig.Listen.HTTPSCertFile, BConfig.Listen.HTTPSKeyFile = "", ""
  130. }
  131. if err := server.ListenAndServeTLS(BConfig.Listen.HTTPSCertFile, BConfig.Listen.HTTPSKeyFile); err != nil {
  132. logs.Critical("ListenAndServeTLS: ", err, fmt.Sprintf("%d", os.Getpid()))
  133. time.Sleep(100 * time.Microsecond)
  134. endRunning <- true
  135. }
  136. }
  137. }()
  138. }
  139. if BConfig.Listen.EnableHTTP {
  140. go func() {
  141. server := grace.NewServer(addr, app.Handlers)
  142. server.Server.ReadTimeout = app.Server.ReadTimeout
  143. server.Server.WriteTimeout = app.Server.WriteTimeout
  144. if BConfig.Listen.ListenTCP4 {
  145. server.Network = "tcp4"
  146. }
  147. if err := server.ListenAndServe(); err != nil {
  148. logs.Critical("ListenAndServe: ", err, fmt.Sprintf("%d", os.Getpid()))
  149. time.Sleep(100 * time.Microsecond)
  150. endRunning <- true
  151. }
  152. }()
  153. }
  154. <-endRunning
  155. return
  156. }
  157. // run normal mode
  158. if BConfig.Listen.EnableHTTPS || BConfig.Listen.EnableMutualHTTPS {
  159. go func() {
  160. time.Sleep(1000 * time.Microsecond)
  161. if BConfig.Listen.HTTPSPort != 0 {
  162. app.Server.Addr = fmt.Sprintf("%s:%d", BConfig.Listen.HTTPSAddr, BConfig.Listen.HTTPSPort)
  163. } else if BConfig.Listen.EnableHTTP {
  164. BeeLogger.Info("Start https server error, conflict with http. Please reset https port")
  165. return
  166. }
  167. logs.Info("https server Running on https://%s", app.Server.Addr)
  168. if BConfig.Listen.AutoTLS {
  169. m := autocert.Manager{
  170. Prompt: autocert.AcceptTOS,
  171. HostPolicy: autocert.HostWhitelist(BConfig.Listen.Domains...),
  172. Cache: autocert.DirCache(BConfig.Listen.TLSCacheDir),
  173. }
  174. app.Server.TLSConfig = &tls.Config{GetCertificate: m.GetCertificate}
  175. BConfig.Listen.HTTPSCertFile, BConfig.Listen.HTTPSKeyFile = "", ""
  176. } else if BConfig.Listen.EnableMutualHTTPS {
  177. pool := x509.NewCertPool()
  178. data, err := ioutil.ReadFile(BConfig.Listen.TrustCaFile)
  179. if err != nil {
  180. BeeLogger.Info("MutualHTTPS should provide TrustCaFile")
  181. return
  182. }
  183. pool.AppendCertsFromPEM(data)
  184. app.Server.TLSConfig = &tls.Config{
  185. ClientCAs: pool,
  186. ClientAuth: tls.RequireAndVerifyClientCert,
  187. }
  188. }
  189. if err := app.Server.ListenAndServeTLS(BConfig.Listen.HTTPSCertFile, BConfig.Listen.HTTPSKeyFile); err != nil {
  190. logs.Critical("ListenAndServeTLS: ", err)
  191. time.Sleep(100 * time.Microsecond)
  192. endRunning <- true
  193. }
  194. }()
  195. }
  196. if BConfig.Listen.EnableHTTP {
  197. go func() {
  198. app.Server.Addr = addr
  199. logs.Info("http server Running on http://%s", app.Server.Addr)
  200. if BConfig.Listen.ListenTCP4 {
  201. ln, err := net.Listen("tcp4", app.Server.Addr)
  202. if err != nil {
  203. logs.Critical("ListenAndServe: ", err)
  204. time.Sleep(100 * time.Microsecond)
  205. endRunning <- true
  206. return
  207. }
  208. if err = app.Server.Serve(ln); err != nil {
  209. logs.Critical("ListenAndServe: ", err)
  210. time.Sleep(100 * time.Microsecond)
  211. endRunning <- true
  212. return
  213. }
  214. } else {
  215. if err := app.Server.ListenAndServe(); err != nil {
  216. logs.Critical("ListenAndServe: ", err)
  217. time.Sleep(100 * time.Microsecond)
  218. endRunning <- true
  219. }
  220. }
  221. }()
  222. }
  223. <-endRunning
  224. }
  225. // Router adds a patterned controller handler to BeeApp.
  226. // it's an alias method of App.Router.
  227. // usage:
  228. // simple router
  229. // beego.Router("/admin", &admin.UserController{})
  230. // beego.Router("/admin/index", &admin.ArticleController{})
  231. //
  232. // regex router
  233. //
  234. // beego.Router("/api/:id([0-9]+)", &controllers.RController{})
  235. //
  236. // custom rules
  237. // beego.Router("/api/list",&RestController{},"*:ListFood")
  238. // beego.Router("/api/create",&RestController{},"post:CreateFood")
  239. // beego.Router("/api/update",&RestController{},"put:UpdateFood")
  240. // beego.Router("/api/delete",&RestController{},"delete:DeleteFood")
  241. func Router(rootpath string, c ControllerInterface, mappingMethods ...string) *App {
  242. BeeApp.Handlers.Add(rootpath, c, mappingMethods...)
  243. return BeeApp
  244. }
  245. // UnregisterFixedRoute unregisters the route with the specified fixedRoute. It is particularly useful
  246. // in web applications that inherit most routes from a base webapp via the underscore
  247. // import, and aim to overwrite only certain paths.
  248. // The method parameter can be empty or "*" for all HTTP methods, or a particular
  249. // method type (e.g. "GET" or "POST") for selective removal.
  250. //
  251. // Usage (replace "GET" with "*" for all methods):
  252. // beego.UnregisterFixedRoute("/yourpreviouspath", "GET")
  253. // beego.Router("/yourpreviouspath", yourControllerAddress, "get:GetNewPage")
  254. func UnregisterFixedRoute(fixedRoute string, method string) *App {
  255. subPaths := splitPath(fixedRoute)
  256. if method == "" || method == "*" {
  257. for m := range HTTPMETHOD {
  258. if _, ok := BeeApp.Handlers.routers[m]; !ok {
  259. continue
  260. }
  261. if BeeApp.Handlers.routers[m].prefix == strings.Trim(fixedRoute, "/ ") {
  262. findAndRemoveSingleTree(BeeApp.Handlers.routers[m])
  263. continue
  264. }
  265. findAndRemoveTree(subPaths, BeeApp.Handlers.routers[m], m)
  266. }
  267. return BeeApp
  268. }
  269. // Single HTTP method
  270. um := strings.ToUpper(method)
  271. if _, ok := BeeApp.Handlers.routers[um]; ok {
  272. if BeeApp.Handlers.routers[um].prefix == strings.Trim(fixedRoute, "/ ") {
  273. findAndRemoveSingleTree(BeeApp.Handlers.routers[um])
  274. return BeeApp
  275. }
  276. findAndRemoveTree(subPaths, BeeApp.Handlers.routers[um], um)
  277. }
  278. return BeeApp
  279. }
  280. func findAndRemoveTree(paths []string, entryPointTree *Tree, method string) {
  281. for i := range entryPointTree.fixrouters {
  282. if entryPointTree.fixrouters[i].prefix == paths[0] {
  283. if len(paths) == 1 {
  284. if len(entryPointTree.fixrouters[i].fixrouters) > 0 {
  285. // If the route had children subtrees, remove just the functional leaf,
  286. // to allow children to function as before
  287. if len(entryPointTree.fixrouters[i].leaves) > 0 {
  288. entryPointTree.fixrouters[i].leaves[0] = nil
  289. entryPointTree.fixrouters[i].leaves = entryPointTree.fixrouters[i].leaves[1:]
  290. }
  291. } else {
  292. // Remove the *Tree from the fixrouters slice
  293. entryPointTree.fixrouters[i] = nil
  294. if i == len(entryPointTree.fixrouters)-1 {
  295. entryPointTree.fixrouters = entryPointTree.fixrouters[:i]
  296. } else {
  297. entryPointTree.fixrouters = append(entryPointTree.fixrouters[:i], entryPointTree.fixrouters[i+1:len(entryPointTree.fixrouters)]...)
  298. }
  299. }
  300. return
  301. }
  302. findAndRemoveTree(paths[1:], entryPointTree.fixrouters[i], method)
  303. }
  304. }
  305. }
  306. func findAndRemoveSingleTree(entryPointTree *Tree) {
  307. if entryPointTree == nil {
  308. return
  309. }
  310. if len(entryPointTree.fixrouters) > 0 {
  311. // If the route had children subtrees, remove just the functional leaf,
  312. // to allow children to function as before
  313. if len(entryPointTree.leaves) > 0 {
  314. entryPointTree.leaves[0] = nil
  315. entryPointTree.leaves = entryPointTree.leaves[1:]
  316. }
  317. }
  318. }
  319. // Include will generate router file in the router/xxx.go from the controller's comments
  320. // usage:
  321. // beego.Include(&BankAccount{}, &OrderController{},&RefundController{},&ReceiptController{})
  322. // type BankAccount struct{
  323. // beego.Controller
  324. // }
  325. //
  326. // register the function
  327. // func (b *BankAccount)Mapping(){
  328. // b.Mapping("ShowAccount" , b.ShowAccount)
  329. // b.Mapping("ModifyAccount", b.ModifyAccount)
  330. //}
  331. //
  332. // //@router /account/:id [get]
  333. // func (b *BankAccount) ShowAccount(){
  334. // //logic
  335. // }
  336. //
  337. //
  338. // //@router /account/:id [post]
  339. // func (b *BankAccount) ModifyAccount(){
  340. // //logic
  341. // }
  342. //
  343. // the comments @router url methodlist
  344. // url support all the function Router's pattern
  345. // methodlist [get post head put delete options *]
  346. func Include(cList ...ControllerInterface) *App {
  347. BeeApp.Handlers.Include(cList...)
  348. return BeeApp
  349. }
  350. // RESTRouter adds a restful controller handler to BeeApp.
  351. // its' controller implements beego.ControllerInterface and
  352. // defines a param "pattern/:objectId" to visit each resource.
  353. func RESTRouter(rootpath string, c ControllerInterface) *App {
  354. Router(rootpath, c)
  355. Router(path.Join(rootpath, ":objectId"), c)
  356. return BeeApp
  357. }
  358. // AutoRouter adds defined controller handler to BeeApp.
  359. // it's same to App.AutoRouter.
  360. // if beego.AddAuto(&MainContorlller{}) and MainController has methods List and Page,
  361. // visit the url /main/list to exec List function or /main/page to exec Page function.
  362. func AutoRouter(c ControllerInterface) *App {
  363. BeeApp.Handlers.AddAuto(c)
  364. return BeeApp
  365. }
  366. // AutoPrefix adds controller handler to BeeApp with prefix.
  367. // it's same to App.AutoRouterWithPrefix.
  368. // if beego.AutoPrefix("/admin",&MainContorlller{}) and MainController has methods List and Page,
  369. // visit the url /admin/main/list to exec List function or /admin/main/page to exec Page function.
  370. func AutoPrefix(prefix string, c ControllerInterface) *App {
  371. BeeApp.Handlers.AddAutoPrefix(prefix, c)
  372. return BeeApp
  373. }
  374. // Get used to register router for Get method
  375. // usage:
  376. // beego.Get("/", func(ctx *context.Context){
  377. // ctx.Output.Body("hello world")
  378. // })
  379. func Get(rootpath string, f FilterFunc) *App {
  380. BeeApp.Handlers.Get(rootpath, f)
  381. return BeeApp
  382. }
  383. // Post used to register router for Post method
  384. // usage:
  385. // beego.Post("/api", func(ctx *context.Context){
  386. // ctx.Output.Body("hello world")
  387. // })
  388. func Post(rootpath string, f FilterFunc) *App {
  389. BeeApp.Handlers.Post(rootpath, f)
  390. return BeeApp
  391. }
  392. // Delete used to register router for Delete method
  393. // usage:
  394. // beego.Delete("/api", func(ctx *context.Context){
  395. // ctx.Output.Body("hello world")
  396. // })
  397. func Delete(rootpath string, f FilterFunc) *App {
  398. BeeApp.Handlers.Delete(rootpath, f)
  399. return BeeApp
  400. }
  401. // Put used to register router for Put method
  402. // usage:
  403. // beego.Put("/api", func(ctx *context.Context){
  404. // ctx.Output.Body("hello world")
  405. // })
  406. func Put(rootpath string, f FilterFunc) *App {
  407. BeeApp.Handlers.Put(rootpath, f)
  408. return BeeApp
  409. }
  410. // Head used to register router for Head method
  411. // usage:
  412. // beego.Head("/api", func(ctx *context.Context){
  413. // ctx.Output.Body("hello world")
  414. // })
  415. func Head(rootpath string, f FilterFunc) *App {
  416. BeeApp.Handlers.Head(rootpath, f)
  417. return BeeApp
  418. }
  419. // Options used to register router for Options method
  420. // usage:
  421. // beego.Options("/api", func(ctx *context.Context){
  422. // ctx.Output.Body("hello world")
  423. // })
  424. func Options(rootpath string, f FilterFunc) *App {
  425. BeeApp.Handlers.Options(rootpath, f)
  426. return BeeApp
  427. }
  428. // Patch used to register router for Patch method
  429. // usage:
  430. // beego.Patch("/api", func(ctx *context.Context){
  431. // ctx.Output.Body("hello world")
  432. // })
  433. func Patch(rootpath string, f FilterFunc) *App {
  434. BeeApp.Handlers.Patch(rootpath, f)
  435. return BeeApp
  436. }
  437. // Any used to register router for all methods
  438. // usage:
  439. // beego.Any("/api", func(ctx *context.Context){
  440. // ctx.Output.Body("hello world")
  441. // })
  442. func Any(rootpath string, f FilterFunc) *App {
  443. BeeApp.Handlers.Any(rootpath, f)
  444. return BeeApp
  445. }
  446. // Handler used to register a Handler router
  447. // usage:
  448. // beego.Handler("/api", http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
  449. // fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
  450. // }))
  451. func Handler(rootpath string, h http.Handler, options ...interface{}) *App {
  452. BeeApp.Handlers.Handler(rootpath, h, options...)
  453. return BeeApp
  454. }
  455. // InsertFilter adds a FilterFunc with pattern condition and action constant.
  456. // The pos means action constant including
  457. // beego.BeforeStatic, beego.BeforeRouter, beego.BeforeExec, beego.AfterExec and beego.FinishRouter.
  458. // The bool params is for setting the returnOnOutput value (false allows multiple filters to execute)
  459. func InsertFilter(pattern string, pos int, filter FilterFunc, params ...bool) *App {
  460. BeeApp.Handlers.InsertFilter(pattern, pos, filter, params...)
  461. return BeeApp
  462. }