golang static stop index.html redirection - go

package main
import (
"log"
"net/http"
)
func main() {
fs := http.FileServer(http.Dir("."))
http.Handle("/", fs)
log.Println("Listening...")
http.ListenAndServe(":3000", nil)
}
So I have a index.html file and want server to stop showing it.

The docs for FileServer state that:
As a special case, the returned file server redirects any request
ending in "/index.html" to the same path, without the final
"index.html".
So /index.html is redirected to /, /foo/bar/index.html is redirected to /foo/bar/.
To avoid this register an additional handler for the special case.
http.HandleFunc("/index.html", func(w http.ResponseWriter, r *http.Request) {
f, err := os.Open("index.html")
if err != nil {
// handle error
return
}
http.ServeContent(w, r, "index.html", time.Now(), f)
})
Please note I'm using ServeContent insead of ServeFile because ServeFile handles /index.html requests the same way as FileServer.

There's no redirection going on, the default file to render when requesting a directory is index.html. The directory listing is a fallback for when this file isn't found, so you can't get a directory listing without removing the index.html file.
If you want a directory listing, you'll have to write it out yourself, which you can then format and style however you choose. The basic structure is very simple if you want to write it directly, take the internal dirList function for example:
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprintf(w, "<pre>\n")
for _, d := range dirs {
name := d.Name()
if d.IsDir() {
name += "/"
}
url := url.URL{Path: name}
fmt.Fprintf(w, "%s\n", url.String(), htmlReplacer.Replace(name))
}
fmt.Fprintf(w, "</pre>\n")

I think there are 3 ways:
Make a PR which allow accessing /index.html without redirect to golang's net/http/fs library. https://github.com/golang/go/issues/53870
Hack your code: replace /index.html with / to stop 301 redirect, e.g., https://github.com/ahuigo/go-lib/commit/1a99191d5c01cf9025136ce8ddb9668f123de05c#diff-67e8621fbb99281a50c089bae53d4874663d3d21ca5e90809ec207c070029351R44
Customize your own http fs handler instead of official tools.

Related

How to handle SPA urls correctly in Golang?

I am trying to embed and serve my frontend (nextjs with static export) with echo. I am currently using:
//go:embed all:frontend/out
var FrontendFS embed.FS
func BuildFrontendFS() http.FileSystem {
build, err := fs.Sub(FrontendFS, "frontend/out")
if err != nil {
log.Fatal(err)
}
return http.FS(build)
}
e := echo.New()
e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
Filesystem: BuildFrontendFS(),
HTML5: true,
}))
e.Logger.Fatal(e.Start(":1323"))
It works great however when I try to open a url like localhost:1323/example it routes back to index.html. The problem is that there is a page called example.html and I am expecting to see that page instead. If I call the url like localhost:1323/example.html it works.
Any ideas how I can solve this so that I can just use localhost:1323/example ?
I see an ./out directory being referenced, so it looks like you're exporting to static HTML. If that's the case, enable the trailingSlash setting in your next.config.js:
module.exports = {
trailingSlash: true,
}
./example.html will become ./example/index.html after the export.

How to end to end/integration test a Go app that use a reverse proxy to manage subdomain?

I have a Go app that use Gin gonic and a Nginx reverse proxy that send trafic to another app on domain.com and send all the *.domain.com subdomains traffic directly to my go app.
My Go app then has a middleware that will read the hostname that nginx passes to it from Context and allow my handlers to know what subdomain is being request and return the proper data and cookies for said subdomain.
It's a pretty simple setup and it seems to work fine from my test in postman as all my routes are the same across all my subdomains so this way i can only use one router for all of them instead of one router per subodmain.
Now my big problem come when i'm trying to do end to end testing.
I'm setting up my test like this :
router := initRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/api/login", bytes.NewBuffer(jsonLogin))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code)
with initRouter() returning a gin engine with all my routes and middlewares loaded and the rest as a basic test setup.
Obviously the test will fail as the gin Context won't ever receive a subdomain from context and act as if everything is coming from localhost:8000.
Is there a way to either :
"Mock" a subdomain so that the router think the call is coming from foo.localhost.com instead of localhost
Setup my test suit so that the test request are routed thought nginx.. i'd prefer solution 1 as this would be a mess to setup / maintain.
Edit :
As per the httptest doc i've tried to hard code foo.localhost as the param of the NewRequest but it doesn't really behave as i need it to behave :
NewRequest returns a new incoming server Request, suitable for passing to an http.Handler for testing.
The target is the RFC 7230 "request-target": it may be either a path or an absolute URL. If target is an absolute URL, the host name from the URL is used. Otherwise, "example.com" is used.
When hardcoding http://foo.localhost.com/api/login or foo.localhost.com/api/login as the request target it directly passes it to my router under "foo.localhost.com/api/login" while nginx would just hit the /api/login directly and parse from c.Request.Host
Edit 2:
I'm currently exploring setting the host manually using :
req.Header.Set("Host", "foo.localhost")
The request returned by http.NewRequest isn't suitable for passing directly to ServeHTTP. Use one returned by httptest.NewRequest instead.
Simply set the Host field directly:
package main
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestHelloWorld(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Host != "foobar" {
t.Errorf("Host is %q, want foobar", r.Host)
}
})
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/api/login", nil)
r.Host = "foobar"
mux.ServeHTTP(w, r)
}

How to use swaggo (swagger doc) in GoLang lang with http.ServeMux?

In documentation https://github.com/swaggo/swag is using gin to initialize server, but in my application i'm using http.ServeMux and how initialize swaggo without use gin server
in Docs use
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
how can I use something like ...
mu.Handle("/swagger/*any", swaggerFiles.Handler)
......
follows as my initial idea, but don't work... rsrs
func Server() *http.ServeMux {
docs.SwaggerInfo.Title = "Swagger Example API"
docs.SwaggerInfo.Description = "This is a sample server Petstore server."
docs.SwaggerInfo.Version = "1.0"
docs.SwaggerInfo.Host = "petstore.swagger.io"
mu := http.NewServeMux()
mu.Handle("/metrics", promhttp.Handler())
mu.Handle("/swagger/*any", swaggerFiles.Handler)
mu.HandleFunc("/helloWorld", handlers.NewHelloWorldHandler().HelloWorldHandler)
mu.HandleFunc("/production/", handlers.NewProductionHandler().ProductionHandler)
return mu
}
If you have your swagger files built for distribution (i.e. static files) and are in say the directory: /some/dir/path/static/swagger
This should work with go's http router:
staticFilesPath := "/some/dir/path/static"
staticRoute := "/static/"
h := http.NewServeMux()
// static file handler for route '/static/*'
h.Handle(
staticRoute,
http.StripPrefix(
staticRoute,
http.FileServer(http.Dir(staticFilesPath)),
),
)
I find it helpful to add this also:
// (redirect a route if not recognized - remove in production)
//
// unrecognized routes will redirect to Swagger API docs
h.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, staticRoute + "swagger/", http.StatusSeeOther)
})

Is there no way to redirect from https://www.. to https://.. in Go?

I saw this post by someone here but there are no answers: Redirecting https://www.domain.com to https://domain.com in Go
I tried to see if I could find a way to check if the request was made with a www url by checking the variables in *http.Request variable but all I got was relative paths and empty strings "".
I tried to fmt.Println() these variables:
func handleFunc(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.URL.string())
fmt.Println(r.Host)
fmt.Println(r.RequestURI)
}
But none of these variables contained the absolute path with the www part. How can I check if a request was made from a www url? I want to figure this out so that I can redirect from www to non-www.
Is this really not even possible in Go? Some people suggested putting nginx in front of Go, but there has to be a way without nginx right? Do I really need to install and use nginx in front of Go just to do a simple redirect from www to non-www? This does not seem like a good solution to a seemingly small problem.
Is there no way to achieve this?
Wrap your handlers with a redirector:
func wwwRedirect(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if host := strings.TrimPrefix(r.Host, "www."); host != r.Host {
// Request host has www. prefix. Redirect to host with www. trimmed.
u := *r.URL
u.Host = host
u.Scheme = "https"
http.Redirect(w, r, u.String(), http.StatusFound)
return
}
h.ServeHTTP(w, r)
})
}
Use the redirector like this:
log.Fatal(http.ListenAndServeTLS(addr, certFile, keyFile, wwwRedirect(handler))

Selectively Follow Redirects in Go

I'm trying to write a twitter reader that resolves the final URLs of link shorteners etc, but gives me a URL along the way for a list of manually defined host patterns. The reason to do this is that i don't want to end up with the paywall URL but the one before.
As far as i can tell the way to do this is write my own client based on the default RoundTripper because returning an error from a custom CheckRedirect function aborts the client without yielding a response.
Is there a way to use the default client and record a list of URLs/specific URL from a custom checkRedirect function?
The client request will actually still return the last valid Response in cases where your custom CheckResponse yields an error (As mentioned in the comments).
http://golang.org/pkg/net/http/#Client
If CheckRedirect returns an error, the Client's Get method returns both the previous Response and CheckRedirect's error (wrapped in a url.Error) instead of issuing the Request req.
If you maintain a list of "known" paywall-urls, you can abort the paywall-redirect in your CheckResponse with a custom error type (Paywalled in the example below).
Your error handling code later has to consider that error type as a special (non-erroneous) case.
Example:
package main
import (
"errors"
"fmt"
"net/http"
"net/url"
)
var Paywalled = errors.New("next redirect would hit a paywall")
var badHosts = map[string]error{
"registration.ft.com": Paywalled,
}
var client = &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
// N.B.: when used in production, also check for redirect loops
return badHosts[req.URL.Host]
},
}
func main() {
resp, err := client.Get("http://on.ft.com/14pQBYE")
// ignore non-nil err if it's a `Paywalled` wrapped in url.Error
if e, ok := err.(*url.Error); (ok && e.Err != Paywalled) || (!ok && err != nil) {
fmt.Println("error: ", err)
return
}
resp.Body.Close()
fmt.Println(resp.Request.URL)
}

Resources