gomux

Go Static Files Serve giving 404 in Linux


I have a Simple Web Server in Go as below:

func webServerWay() {

    r := mux.NewRouter()
    
    //File Upload Code
    r.HandleFunc("/upload", uploadFile).Methods("POST")
    
    //Test Message
    r.HandleFunc("/test", test).Methods("GET")

    r.PathPrefix("/site/").Handler(http.StripPrefix("/site/", http.FileServer(http.Dir("site"))))


if err := http.ListenAndServe(":8010", r); err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

Simple Test Function looks as below: It's A Simple Function to see if the endpoint is reachable.

func test(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Welcome it Works!")
}

Site Structure is:

├── go.mod
├── go.sum
├── main.go
└── site
    ├── dist
    └── index.html

The Application Works Perfectly in my local dev environment i.e. Mac OS:

Ajay@apples-MacBook-Air:/$ curl localhost:8010/test
Welcome it Works!
Ajay@apples-MacBook-Air:/$ curl localhost:8010/site/
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>gAnalytics</title>
   
  </head>
  <body>
    <div id="root"></div>
  </body>
  <script defer src="./dist/appXYZ.min.js"></script></head>
</html>

But When I Create a Build for Linux(Ubuntu 20.04)x64

GOOS=linux GOARCH=386 go build -o gApp24v1.linux main.go

And run the code in Linux:

sudo ./gApp24v1.linux

The Static Site Serving Doesn't Work, But I can reach the Test Endpoint:

root@gapp24:/home/ajay/app# curl localhost:8010/test
Welcome it Works!
root@gapp24:/home/ajay/app#  curl localhost:8010/site/
404 page not found

I have tried multiple ways of code in static file handler seeing other answers and reading the document but it works in my macOS and not my Linux Machine.

Any Suggestions!

Also the netstat in my Linux(Ubuntu machine) is below:

root@gapp24:/home/ajay/app# netstat -plnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:21              0.0.0.0:*               LISTEN      671/vsftpd          
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      547/systemd-resolve 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      704/sshd: /usr/sbin 
tcp6       0      0 :::8010                 :::*                    LISTEN      1167/./gApp24v1.lin 
tcp6       0      0 :::22                   :::*                    LISTEN      704/sshd: /usr/sbin 

Solution

  • When Deploying the build file, the respective static files need to be also moved accordingly:

    Here gApp24v1.linux is the build for Linux. The static files containing folder site needs to be also placed in the respective path:

    .
    ├── gApp24v1.linux
    └── site
        ├── dist
        │   ├── appXYZ.min.js
        └── index.html