-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
37 lines (30 loc) · 768 Bytes
/
main.go
File metadata and controls
37 lines (30 loc) · 768 Bytes
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
36
37
package main
import (
"fmt"
"net/http"
"os"
"strconv"
"github.com/tinacious/static-server/utils"
)
func main() {
port := utils.GetRandomPort()
// Use configured port
portString := os.Getenv("PORT")
if portString != "" {
i, err := strconv.Atoi(portString)
if err != nil {
fmt.Printf("⛔️ invalid port %s - using random port %d\n", portString, port)
} else {
port = i
}
} else {
fmt.Printf("🎲 using random port %d - to configure, set the environment variable PORT\n", port)
}
cwd, err := os.Getwd()
if err != nil {
panic(err)
}
fmt.Printf("🚀 static server at: http://localhost:%d with contents from %s\n", port, cwd)
http.Handle("/", http.FileServer(http.Dir(".")))
http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
}