Last active
November 13, 2023 11:59
-
-
Save hudson-newey/787c4db7a05bbc769a2544d0bc99f7cf to your computer and use it in GitHub Desktop.
A simple golang server for serving a single file
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
package main | |
import ( | |
"io" | |
"log" | |
"net/http" | |
"io/ioutil" | |
"os" | |
) | |
func main() { | |
// Set routing rules | |
http.HandleFunc("/", home) | |
//Use the default DefaultServeMux. | |
err := http.ListenAndServe(":8080", nil) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} | |
func home(w http.ResponseWriter, r *http.Request) { | |
programName := os.Args[1] | |
io.WriteString(w, readFile(programName)) | |
} | |
func readFile(fileName string) string { | |
data, err := ioutil.ReadFile(fileName) | |
if err != nil { | |
return "Error 404, File not Found!" | |
} | |
return string(data) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment