mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-17 22:32:26 +08:00
Merge 45935cfac8766d012d8097500093afc929146bdd into a331dc6a31473b7208c57ec32e14bfcec3062dbb
This commit is contained in:
commit
3fc259ee56
76
README.md
76
README.md
@ -66,6 +66,7 @@ Gin is a web framework written in Go (Golang). It features a martini-like API wi
|
||||
- [Using BasicAuth() middleware](#using-basicauth-middleware)
|
||||
- [Goroutines inside a middleware](#goroutines-inside-a-middleware)
|
||||
- [Custom HTTP configuration](#custom-http-configuration)
|
||||
- [Support FCGI](#support-fcgi)
|
||||
- [Support Let's Encrypt](#support-lets-encrypt)
|
||||
- [Run multiple service using Gin](#run-multiple-service-using-gin)
|
||||
- [Graceful shutdown or restart](#graceful-shutdown-or-restart)
|
||||
@ -1590,6 +1591,81 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
### Support FCGI
|
||||
|
||||
Here is how to start Gin as a FCGI server that will listen to `os.Stdin`:
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func main() {
|
||||
r := gin.Default()
|
||||
|
||||
// Ping handler
|
||||
r.GET("/ping", func(c *gin.Context) {
|
||||
c.String(200, "pong")
|
||||
})
|
||||
|
||||
r.RunFCGI()
|
||||
}
|
||||
```
|
||||
|
||||
An alternative way of starting the FCGI server is:
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http/fcgi"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func main() {
|
||||
r := gin.Default()
|
||||
|
||||
// Ping handler
|
||||
r.GET("/ping", func(c *gin.Context) {
|
||||
c.String(200, "pong")
|
||||
})
|
||||
|
||||
fcgi.Serve(nil, r)
|
||||
}
|
||||
```
|
||||
|
||||
Please refer to [fcgi](https://golang.org/pkg/net/http/fcgi/) to see how to use a listener other that `os.Stdin`
|
||||
|
||||
For an Apache server, the configuration would look like this:
|
||||
|
||||
In the file `/etc/apache2/sites-available/your-project.conf`:
|
||||
|
||||
```
|
||||
DocumentRoot /var/www/your-project
|
||||
<Directory /var/www/your-project>
|
||||
Options FollowSymLinks ExecCGI
|
||||
AllowOverride All
|
||||
Require all granted
|
||||
</Directory>
|
||||
```
|
||||
|
||||
Then in the file `/var/www/your-project/.htaccess`:
|
||||
|
||||
```
|
||||
# Go serves all files, static and dynamic:
|
||||
|
||||
SetHandler fcgid-script
|
||||
|
||||
RewriteEngine on
|
||||
|
||||
RewriteRule ^(.*)$ your-project [QSA,L]
|
||||
```
|
||||
|
||||
If `your-project` is the name of your binary in the directory, as created by `go build` with no other parameters.
|
||||
|
||||
### Support Let's Encrypt
|
||||
|
||||
example for 1-line LetsEncrypt HTTPS servers.
|
||||
|
11
gin.go
11
gin.go
@ -9,6 +9,7 @@ import (
|
||||
"html/template"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/fcgi"
|
||||
"os"
|
||||
"path"
|
||||
"sync"
|
||||
@ -311,6 +312,16 @@ func (engine *Engine) Run(addr ...string) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// RunFCGI attaches the router to a fcgi.child and starts listening to os.Stdin for requests
|
||||
// It is a shortcut for fcgi.Serve(nil, router)
|
||||
func (engine *Engine) RunFCGI() (err error) {
|
||||
defer func() { debugPrintError(err) }()
|
||||
|
||||
debugPrint("Listening and serving HTTP on FCGI\n")
|
||||
err = fcgi.Serve(nil, engine)
|
||||
return
|
||||
}
|
||||
|
||||
// RunTLS attaches the router to a http.Server and starts listening and serving HTTPS (secure) requests.
|
||||
// It is a shortcut for http.ListenAndServeTLS(addr, certFile, keyFile, router)
|
||||
// Note: this method will block the calling goroutine indefinitely unless an error happens.
|
||||
|
Loading…
x
Reference in New Issue
Block a user