Last active
November 14, 2025 10:01
-
-
Save dbobrov/07d6f834e60c4578cf9076f0b4b9c3ff to your computer and use it in GitHub Desktop.
Simple static server for development
This file contains hidden or 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
| package main | |
| import ( | |
| "flag" | |
| "fmt" | |
| "log" | |
| "net/http" | |
| "path/filepath" | |
| "strconv" | |
| ) | |
| func dev(fs http.Handler) http.HandlerFunc { | |
| return func(w http.ResponseWriter, r *http.Request) { | |
| w.Header().Set("cache-control", "no-cache") | |
| w.Header().Set("X-dev-by", "Dmitry") | |
| fs.ServeHTTP(w, r) | |
| } | |
| } | |
| func main() { | |
| portPtr := flag.Int("port", 8080, "port number") | |
| dirPtr := flag.String("dir", ".", "directory of static files") | |
| flag.Parse() | |
| path, err := filepath.Abs(*dirPtr) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| port := strconv.Itoa(*portPtr) | |
| fs := http.FileServer(http.Dir(path)) | |
| http.Handle("/", dev(fs)) | |
| fmt.Printf("Static server for %s has started on http://localhost:%s\n", path, port) | |
| fmt.Println("Press Ctrl+C for shutdown") | |
| log.Fatal(http.ListenAndServe(":"+port, nil)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment