Merge 45935cfac8766d012d8097500093afc929146bdd into a331dc6a31473b7208c57ec32e14bfcec3062dbb

This commit is contained in:
Thomas Godart 2021-03-27 15:38:32 +08:00 committed by GitHub
commit 3fc259ee56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 87 additions and 0 deletions

View File

@ -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) - [Using BasicAuth() middleware](#using-basicauth-middleware)
- [Goroutines inside a middleware](#goroutines-inside-a-middleware) - [Goroutines inside a middleware](#goroutines-inside-a-middleware)
- [Custom HTTP configuration](#custom-http-configuration) - [Custom HTTP configuration](#custom-http-configuration)
- [Support FCGI](#support-fcgi)
- [Support Let's Encrypt](#support-lets-encrypt) - [Support Let's Encrypt](#support-lets-encrypt)
- [Run multiple service using Gin](#run-multiple-service-using-gin) - [Run multiple service using Gin](#run-multiple-service-using-gin)
- [Graceful shutdown or restart](#graceful-shutdown-or-restart) - [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 ### Support Let's Encrypt
example for 1-line LetsEncrypt HTTPS servers. example for 1-line LetsEncrypt HTTPS servers.

11
gin.go
View File

@ -9,6 +9,7 @@ import (
"html/template" "html/template"
"net" "net"
"net/http" "net/http"
"net/http/fcgi"
"os" "os"
"path" "path"
"sync" "sync"
@ -311,6 +312,16 @@ func (engine *Engine) Run(addr ...string) (err error) {
return 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. // 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) // It is a shortcut for http.ListenAndServeTLS(addr, certFile, keyFile, router)
// Note: this method will block the calling goroutine indefinitely unless an error happens. // Note: this method will block the calling goroutine indefinitely unless an error happens.