-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
35 lines (30 loc) · 892 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package main
import (
"log"
"net/http"
"path/filepath"
)
func main() {
// Define the directory to serve.
const staticFilesDir = "./"
const port = "8080"
// Setup route to serve static files.
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Generate the absolute path of the static directory.
absPath, err := filepath.Abs(staticFilesDir)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
log.Printf("Error generating absolute path: %v", err)
return
}
// Serve the file directly using http.ServeFile.
path := filepath.Join(absPath, r.URL.Path)
http.ServeFile(w, r, path)
})
// Start the server.
address := ":" + port
log.Printf("Serving %s on HTTP port: %s\n", staticFilesDir, port)
if err := http.ListenAndServe(address, nil); err != nil {
log.Fatalf("Failed to start server: %v", err)
}
}