Skip to content
This repository was archived by the owner on Aug 27, 2025. It is now read-only.

Commit 49902d4

Browse files
committed
init
0 parents  commit 49902d4

4 files changed

Lines changed: 47 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea/
2+
*.exe

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module go-http-fileserver
2+
3+
go 1.19

go.sum

Whitespace-only changes.

main.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"log"
7+
"net"
8+
"net/http"
9+
)
10+
11+
var (
12+
listenPort *int
13+
listenHost *string
14+
)
15+
16+
func init() {
17+
listenPort = flag.Int("p", 0, "Listen to which port")
18+
listenHost = flag.String("h", "localhost", "Listen to which host")
19+
}
20+
21+
func main() {
22+
23+
// Parse arguments
24+
flag.Parse()
25+
26+
// Get listen port
27+
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", *listenHost, *listenPort))
28+
if err != nil {
29+
log.Fatalf("Failed to listen to target port: %d", listener.Addr().(*net.TCPAddr).Port)
30+
}
31+
32+
// Serve current dir
33+
fs := http.FileServer(http.Dir("."))
34+
35+
// Start HTTP server
36+
log.Printf("Starting HTTP server at http://%s", listener.Addr().String())
37+
err = http.Serve(listener, fs)
38+
if err != nil {
39+
log.Fatalf("Failed to start HTTP server with error: %v", err)
40+
}
41+
42+
}

0 commit comments

Comments
 (0)