github.com/astaxie/beego@v1.12.3/app.go (about)

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