created proxy for development
This commit is contained in:
@@ -5,6 +5,7 @@ function App() {
|
||||
return (
|
||||
<div>
|
||||
<h2>App</h2>
|
||||
<p>Some other stuff</p>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
2
main.go
2
main.go
@@ -25,6 +25,6 @@ func main() {
|
||||
|
||||
models.SeedDatabase()
|
||||
|
||||
server.Setup()
|
||||
server.Setup(prodMode)
|
||||
|
||||
}
|
||||
|
||||
@@ -2,22 +2,49 @@ package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
|
||||
"github.com/gin-contrib/static"
|
||||
"github.com/gin-gonic/gin"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"spahl.ddns.net/jasper/wok-able-backend/controllers"
|
||||
)
|
||||
|
||||
var httpServer *http.Server
|
||||
var httpRouter *gin.Engine
|
||||
|
||||
func Setup() {
|
||||
func proxy(c *gin.Context) {
|
||||
remote, err := url.Parse("http://localhost:3000")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
proxy := httputil.NewSingleHostReverseProxy(remote)
|
||||
|
||||
proxy.Director = func(req *http.Request) {
|
||||
req.Header = c.Request.Header
|
||||
req.Host = remote.Host
|
||||
req.URL.Scheme = remote.Scheme
|
||||
req.URL.Host = remote.Host
|
||||
req.URL.Path = c.Request.URL.Path
|
||||
}
|
||||
proxy.ServeHTTP(c.Writer, c.Request)
|
||||
}
|
||||
|
||||
func Setup(prodMode string) {
|
||||
httpRouter = gin.New()
|
||||
|
||||
httpRouter.Use(gin.Logger())
|
||||
httpRouter.Use(static.Serve("/", static.LocalFile("./assets/build", false)))
|
||||
|
||||
controllers.Setup(httpRouter)
|
||||
|
||||
if prodMode == "release" {
|
||||
httpRouter.Use(static.Serve("/", static.LocalFile("./assets/build", false)))
|
||||
} else {
|
||||
httpRouter.NoRoute(proxy)
|
||||
log.Info("using reverse proxy")
|
||||
}
|
||||
|
||||
httpRouter.Run()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user