From 181f652880387179f646650d3a35c57f3f78d731 Mon Sep 17 00:00:00 2001 From: Andrus Diaz Date: Thu, 10 Oct 2019 17:46:05 -0400 Subject: [PATCH 1/2] DOC: example is added with a server written in Go --- docs-gitbook/de/essentials/history-mode.md | 26 ++++++++++++++++++++ docs-gitbook/es/essentials/history-mode.md | 28 +++++++++++++++++++++- docs-gitbook/fr/essentials/history-mode.md | 27 +++++++++++++++++++++ docs-gitbook/ja/essentials/history-mode.md | 28 ++++++++++++++++++++++ docs-gitbook/kr/essentials/history-mode.md | 27 +++++++++++++++++++++ docs-gitbook/ru/essentials/history-mode.md | 27 +++++++++++++++++++++ docs/fr/guide/essentials/history-mode.md | 26 ++++++++++++++++++++ docs/guide/essentials/history-mode.md | 27 +++++++++++++++++++++ docs/ja/guide/essentials/history-mode.md | 27 +++++++++++++++++++++ docs/kr/guide/essentials/history-mode.md | 27 +++++++++++++++++++++ docs/ru/guide/essentials/history-mode.md | 27 +++++++++++++++++++++ docs/zh/guide/essentials/history-mode.md | 26 ++++++++++++++++++++ 12 files changed, 322 insertions(+), 1 deletion(-) diff --git a/docs-gitbook/de/essentials/history-mode.md b/docs-gitbook/de/essentials/history-mode.md index 4f4c7c681..f5530b583 100644 --- a/docs-gitbook/de/essentials/history-mode.md +++ b/docs-gitbook/de/essentials/history-mode.md @@ -44,6 +44,32 @@ location / { Für Node.js/Express benutz du am besten [connect-history-api-fallback middleware](https://github.com/bripkens/connect-history-api-fallback). +#### Golang (gorilla/mux) + +```go +package main +import ( + "net/http" + "os" + "github.com/gorilla/mux" +) +var httpPort = "80" +var indexFile = "index.html" +func serverHandler(w http.ResponseWriter, r *http.Request) { + if _, err := os.Stat(indexFile); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + http.ServeFile(w, r, indexFile) +} +func main() { + r := mux.NewRouter() + r.NotFoundHandler = r.NewRoute().HandlerFunc(serverHandler).GetHandler() + http.Handle("/", r) + http.ListenAndServe(":"+httpPort, nil) +} +``` + ## Warnung Es gibt einen kleinen Nachteil: Der Server wird keine 404-Fehler mehr melden, da alle nicht gefundenen Pfade zur `index.html` führen. Um das zu beheben, solltest du eine Sammel-Route in der Vue-App für die 404-Seite definieren. diff --git a/docs-gitbook/es/essentials/history-mode.md b/docs-gitbook/es/essentials/history-mode.md index f89b473ae..94bdbae59 100644 --- a/docs-gitbook/es/essentials/history-mode.md +++ b/docs-gitbook/es/essentials/history-mode.md @@ -40,10 +40,36 @@ location / { } ``` -#### Node.js (Express) +#### Node.js utilizando (Express) Para Node.js/Express, considera utilizar el middleware [connect-history-api-fallback](https://github.com/bripkens/connect-history-api-fallback). +#### Golang utilizando (gorilla/mux) + +```go +package main +import ( + "net/http" + "os" + "github.com/gorilla/mux" +) +var httpPort = "80" +var indexFile = "index.html" +func serverHandler(w http.ResponseWriter, r *http.Request) { + if _, err := os.Stat(indexFile); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + http.ServeFile(w, r, indexFile) +} +func main() { + r := mux.NewRouter() + r.NotFoundHandler = r.NewRoute().HandlerFunc(serverHandler).GetHandler() + http.Handle("/", r) + http.ListenAndServe(":"+httpPort, nil) +} +``` + ## Deventajas Hay una deventaja para esto: tu servidor ya no reportará errores 404 dado que todas las rutas no encontradas serán redireccionadas al archivo `index.html`. Para solucionar este problema debes implementar dentro de la aplicación Vue una ruta por defecto para mostrar una página de error 404: diff --git a/docs-gitbook/fr/essentials/history-mode.md b/docs-gitbook/fr/essentials/history-mode.md index 1d803e14b..b52fdd108 100644 --- a/docs-gitbook/fr/essentials/history-mode.md +++ b/docs-gitbook/fr/essentials/history-mode.md @@ -68,6 +68,33 @@ http.createServer((req, res) => { Pour Node.js avec Express, vous pouvez utiliser le [middleware connect-history-api-fallback](https://github.com/bripkens/connect-history-api-fallback). + +#### Golang avec (gorilla/mux) + +```go +package main +import ( + "net/http" + "os" + "github.com/gorilla/mux" +) +var httpPort = "80" +var indexFile = "index.html" +func serverHandler(w http.ResponseWriter, r *http.Request) { + if _, err := os.Stat(indexFile); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + http.ServeFile(w, r, indexFile) +} +func main() { + r := mux.NewRouter() + r.NotFoundHandler = r.NewRoute().HandlerFunc(serverHandler).GetHandler() + http.Handle("/", r) + http.ListenAndServe(":"+httpPort, nil) +} +``` + #### Internet Information Services (IIS) 1. Instaler [IIS UrlRewrite](https://www.iis.net/downloads/microsoft/url-rewrite) diff --git a/docs-gitbook/ja/essentials/history-mode.md b/docs-gitbook/ja/essentials/history-mode.md index 26081d159..0ba7a7509 100644 --- a/docs-gitbook/ja/essentials/history-mode.md +++ b/docs-gitbook/ja/essentials/history-mode.md @@ -70,6 +70,34 @@ http.createServer((req, res) => { Node.js/Express では [connect-history-api-fallback middleware](https://github.com/bripkens/connect-history-api-fallback) の利用を検討してください。 + +#### Golang (gorilla/mux) + +```go +package main +import ( + "net/http" + "os" + "github.com/gorilla/mux" +) +var httpPort = "80" +var indexFile = "index.html" +func serverHandler(w http.ResponseWriter, r *http.Request) { + if _, err := os.Stat(indexFile); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + http.ServeFile(w, r, indexFile) +} +func main() { + r := mux.NewRouter() + r.NotFoundHandler = r.NewRoute().HandlerFunc(serverHandler).GetHandler() + http.Handle("/", r) + http.ListenAndServe(":"+httpPort, nil) +} +``` + + #### Internet Information Services (IIS) 1. [IIS UrlRewrite](https://www.iis.net/downloads/microsoft/url-rewrite) をインストール diff --git a/docs-gitbook/kr/essentials/history-mode.md b/docs-gitbook/kr/essentials/history-mode.md index 8645c45e1..4e4d3c1a0 100644 --- a/docs-gitbook/kr/essentials/history-mode.md +++ b/docs-gitbook/kr/essentials/history-mode.md @@ -69,6 +69,33 @@ http.createServer((req, res) => { Node.js/Express의 경우 [connect-history-api-fallback 미들웨어](https://github.com/bripkens/connect-history-api-fallback)를 고려해보세요. +#### Golang (gorilla/mux) + +```go +package main +import ( + "net/http" + "os" + "github.com/gorilla/mux" +) +var httpPort = "80" +var indexFile = "index.html" +func serverHandler(w http.ResponseWriter, r *http.Request) { + if _, err := os.Stat(indexFile); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + http.ServeFile(w, r, indexFile) +} +func main() { + r := mux.NewRouter() + r.NotFoundHandler = r.NewRoute().HandlerFunc(serverHandler).GetHandler() + http.Handle("/", r) + http.ListenAndServe(":"+httpPort, nil) +} +``` + + #### Internet Information Services (IIS) ``` diff --git a/docs-gitbook/ru/essentials/history-mode.md b/docs-gitbook/ru/essentials/history-mode.md index a988ace89..7c0e2ec79 100644 --- a/docs-gitbook/ru/essentials/history-mode.md +++ b/docs-gitbook/ru/essentials/history-mode.md @@ -68,6 +68,33 @@ http.createServer((req, res) => { При использовании Node.js/Express, мы рекомендуем пользоваться [connect-history-api-fallback middleware](https://github.com/bripkens/connect-history-api-fallback). + +#### Golang использованием (gorilla/mux) + +```go +package main +import ( + "net/http" + "os" + "github.com/gorilla/mux" +) +var httpPort = "80" +var indexFile = "index.html" +func serverHandler(w http.ResponseWriter, r *http.Request) { + if _, err := os.Stat(indexFile); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + http.ServeFile(w, r, indexFile) +} +func main() { + r := mux.NewRouter() + r.NotFoundHandler = r.NewRoute().HandlerFunc(serverHandler).GetHandler() + http.Handle("/", r) + http.ListenAndServe(":"+httpPort, nil) +} +``` + #### Internet Information Services (IIS) 1. Установить [IIS UrlRewrite](https://www.iis.net/downloads/microsoft/url-rewrite) diff --git a/docs/fr/guide/essentials/history-mode.md b/docs/fr/guide/essentials/history-mode.md index 2cd21c043..8b1a3179c 100644 --- a/docs/fr/guide/essentials/history-mode.md +++ b/docs/fr/guide/essentials/history-mode.md @@ -70,6 +70,32 @@ http.createServer((req, res) => { Pour Node.js avec Express, vous pouvez utiliser le [middleware connect-history-api-fallback](https://github.com/bripkens/connect-history-api-fallback). +#### Golang avec (gorilla/mux) + +```go +package main +import ( + "net/http" + "os" + "github.com/gorilla/mux" +) +var httpPort = "80" +var indexFile = "index.html" +func serverHandler(w http.ResponseWriter, r *http.Request) { + if _, err := os.Stat(indexFile); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + http.ServeFile(w, r, indexFile) +} +func main() { + r := mux.NewRouter() + r.NotFoundHandler = r.NewRoute().HandlerFunc(serverHandler).GetHandler() + http.Handle("/", r) + http.ListenAndServe(":"+httpPort, nil) +} +``` + ### Internet Information Services (IIS) 1. Instaler [IIS UrlRewrite](https://www.iis.net/downloads/microsoft/url-rewrite) diff --git a/docs/guide/essentials/history-mode.md b/docs/guide/essentials/history-mode.md index 5eeaad033..e716d65c6 100644 --- a/docs/guide/essentials/history-mode.md +++ b/docs/guide/essentials/history-mode.md @@ -72,6 +72,33 @@ http.createServer((req, res) => { For Node.js/Express, consider using [connect-history-api-fallback middleware](https://github.com/bripkens/connect-history-api-fallback). + +#### Golang with (gorilla/mux) + +```go +package main +import ( + "net/http" + "os" + "github.com/gorilla/mux" +) +var httpPort = "80" +var indexFile = "index.html" +func serverHandler(w http.ResponseWriter, r *http.Request) { + if _, err := os.Stat(indexFile); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + http.ServeFile(w, r, indexFile) +} +func main() { + r := mux.NewRouter() + r.NotFoundHandler = r.NewRoute().HandlerFunc(serverHandler).GetHandler() + http.Handle("/", r) + http.ListenAndServe(":"+httpPort, nil) +} +``` + #### Internet Information Services (IIS) 1. Install [IIS UrlRewrite](https://www.iis.net/downloads/microsoft/url-rewrite) diff --git a/docs/ja/guide/essentials/history-mode.md b/docs/ja/guide/essentials/history-mode.md index 26081d159..00eee71c1 100644 --- a/docs/ja/guide/essentials/history-mode.md +++ b/docs/ja/guide/essentials/history-mode.md @@ -70,6 +70,33 @@ http.createServer((req, res) => { Node.js/Express では [connect-history-api-fallback middleware](https://github.com/bripkens/connect-history-api-fallback) の利用を検討してください。 + +#### Golang (gorilla/mux) + +```go +package main +import ( + "net/http" + "os" + "github.com/gorilla/mux" +) +var httpPort = "80" +var indexFile = "index.html" +func serverHandler(w http.ResponseWriter, r *http.Request) { + if _, err := os.Stat(indexFile); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + http.ServeFile(w, r, indexFile) +} +func main() { + r := mux.NewRouter() + r.NotFoundHandler = r.NewRoute().HandlerFunc(serverHandler).GetHandler() + http.Handle("/", r) + http.ListenAndServe(":"+httpPort, nil) +} +``` + #### Internet Information Services (IIS) 1. [IIS UrlRewrite](https://www.iis.net/downloads/microsoft/url-rewrite) をインストール diff --git a/docs/kr/guide/essentials/history-mode.md b/docs/kr/guide/essentials/history-mode.md index 8645c45e1..4ac336f29 100644 --- a/docs/kr/guide/essentials/history-mode.md +++ b/docs/kr/guide/essentials/history-mode.md @@ -69,6 +69,33 @@ http.createServer((req, res) => { Node.js/Express의 경우 [connect-history-api-fallback 미들웨어](https://github.com/bripkens/connect-history-api-fallback)를 고려해보세요. + +#### (gorilla/mux)와 Golang + +```go +package main +import ( + "net/http" + "os" + "github.com/gorilla/mux" +) +var httpPort = "80" +var indexFile = "index.html" +func serverHandler(w http.ResponseWriter, r *http.Request) { + if _, err := os.Stat(indexFile); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + http.ServeFile(w, r, indexFile) +} +func main() { + r := mux.NewRouter() + r.NotFoundHandler = r.NewRoute().HandlerFunc(serverHandler).GetHandler() + http.Handle("/", r) + http.ListenAndServe(":"+httpPort, nil) +} +``` + #### Internet Information Services (IIS) ``` diff --git a/docs/ru/guide/essentials/history-mode.md b/docs/ru/guide/essentials/history-mode.md index 1128c3b79..4f4c7000d 100644 --- a/docs/ru/guide/essentials/history-mode.md +++ b/docs/ru/guide/essentials/history-mode.md @@ -70,6 +70,33 @@ http.createServer((req, res) => { При использовании Node.js/Express, мы рекомендуем пользоваться [connect-history-api-fallback middleware](https://github.com/bripkens/connect-history-api-fallback). + +#### Golang использованием (gorilla/mux) + +```go +package main +import ( + "net/http" + "os" + "github.com/gorilla/mux" +) +var httpPort = "80" +var indexFile = "index.html" +func serverHandler(w http.ResponseWriter, r *http.Request) { + if _, err := os.Stat(indexFile); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + http.ServeFile(w, r, indexFile) +} +func main() { + r := mux.NewRouter() + r.NotFoundHandler = r.NewRoute().HandlerFunc(serverHandler).GetHandler() + http.Handle("/", r) + http.ListenAndServe(":"+httpPort, nil) +} +``` + #### Internet Information Services (IIS) 1. Установить [IIS UrlRewrite](https://www.iis.net/downloads/microsoft/url-rewrite) diff --git a/docs/zh/guide/essentials/history-mode.md b/docs/zh/guide/essentials/history-mode.md index ee27f0c56..8e6ec3a4a 100644 --- a/docs/zh/guide/essentials/history-mode.md +++ b/docs/zh/guide/essentials/history-mode.md @@ -70,6 +70,32 @@ http.createServer((req, res) => { 对于 Node.js/Express,请考虑使用 [connect-history-api-fallback 中间件](https://github.com/bripkens/connect-history-api-fallback)。 +#### Golang 的 (gorilla/mux) + +```go +package main +import ( + "net/http" + "os" + "github.com/gorilla/mux" +) +var httpPort = "80" +var indexFile = "index.html" +func serverHandler(w http.ResponseWriter, r *http.Request) { + if _, err := os.Stat(indexFile); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + http.ServeFile(w, r, indexFile) +} +func main() { + r := mux.NewRouter() + r.NotFoundHandler = r.NewRoute().HandlerFunc(serverHandler).GetHandler() + http.Handle("/", r) + http.ListenAndServe(":"+httpPort, nil) +} +``` + #### Internet Information Services (IIS) 1. 安装 [IIS UrlRewrite](https://www.iis.net/downloads/microsoft/url-rewrite) From 79732261e4a9ca06e9229a2c63552931a1c7ae59 Mon Sep 17 00:00:00 2001 From: Andrus Diaz German <30560543+AndrusGerman@users.noreply.github.com> Date: Sat, 12 Oct 2019 17:46:14 -0400 Subject: [PATCH 2/2] docs: static file detection added If the main page calls a static file it will work. If it is not a file it will be detected as a link history mode --- docs-gitbook/de/essentials/history-mode.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/docs-gitbook/de/essentials/history-mode.md b/docs-gitbook/de/essentials/history-mode.md index f5530b583..56e04e989 100644 --- a/docs-gitbook/de/essentials/history-mode.md +++ b/docs-gitbook/de/essentials/history-mode.md @@ -55,12 +55,14 @@ import ( ) var httpPort = "80" var indexFile = "index.html" +var folder = "./" + func serverHandler(w http.ResponseWriter, r *http.Request) { - if _, err := os.Stat(indexFile); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + if _, err := os.Stat(folder + r.URL.Path); err != nil { + http.ServeFile(w, r, folder+"/index.html") return } - http.ServeFile(w, r, indexFile) + http.ServeFile(w, r, folder+r.URL.Path) } func main() { r := mux.NewRouter()