-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
30 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,35 @@ | ||
// A humble replacement to the famous Python SimpleHTTPServer | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
// HTTP Handler that logs the requested resources via the "log" module | ||
func logHandler(handler http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
log.Printf("%s %s ", r.Method, r.URL) | ||
handler.ServeHTTP(w, r) | ||
}) | ||
} | ||
|
||
func main() { | ||
fmt.Println("Serving files in the current directory on port 18000") | ||
http.Handle("/", http.FileServer(http.Dir("."))) | ||
err := http.ListenAndServe(":18000", nil) | ||
if err != nil { | ||
log.Fatal("ListenAndServe: ", err) | ||
} | ||
|
||
directoryOption := flag.String("directory", ".", "the directory to serve via HTTP (default is current directory)") | ||
portOption := flag.Int("port", 8080, "the listening port (default is 8080)") | ||
flag.Parse() | ||
|
||
var directory = http.Dir(*directoryOption) | ||
var fileServer = http.FileServer(directory) | ||
var port = *portOption | ||
|
||
var host = fmt.Sprintf(":%d", port) | ||
var handler = logHandler(fileServer) | ||
|
||
log.Printf("Staring HTTP server on http://127.0.0.1:%d/ in directory %v", port, directory) | ||
log.Fatal(http.ListenAndServe(host, handler)) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters