simpleHTTPServer

command
v1.15.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 29, 2021 License: MIT Imports: 4 Imported by: 0

README

simpleHTTPServer

many may have used the simple python web server SimpleHTTPServer:

python -m SimpleHTTPServer 8080

the same functionality can be recreated in Go with just a few lines of code:

package main

import (
    "log"
    "net/http"
    "os"
)

func Log(handler http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)
        handler.ServeHTTP(w, r)
    })
}


func main() {
    // default settings
    port := "8080"
    dir := os.Getenv("PWD")

    // get settings from command line
    if len(os.Args) > 1 {
        port = os.Args[1]
        if len(os.Args) > 2 {
            dir = os.Args[2]
        }
    }

    // start web server with logging
    log.Fatal(http.ListenAndServe(":"+port, Log(http.FileServer(http.Dir(dir)))))
}

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL