Created
August 21, 2013 18:57
-
-
Save jamsesso/6298662 to your computer and use it in GitHub Desktop.
Very simple webserver for serving static HTML content.
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 ( | |
"fmt" | |
"strconv" | |
"net/http" | |
"io/ioutil" | |
) | |
const ( | |
SERVER_ROOT = "C:/xampp/htdocs/Go/Webserver/Pages" | |
PORT = 8080 | |
) | |
type Page struct { | |
Fname string | |
Body []byte | |
} | |
func loadPage(f string) *Page { | |
path := SERVER_ROOT + f | |
contents, _ := ioutil.ReadFile(path) | |
return &Page{Fname: path, Body: contents} | |
} | |
func handler(w http.ResponseWriter, r *http.Request) { | |
w.Header().Add("Content-type", "text/html") | |
page := loadPage(r.URL.Path[:]) | |
fmt.Fprintf(w, string(page.Body)) | |
} | |
func main() { | |
http.HandleFunc("/", handler) | |
defer http.ListenAndServe(":" + strconv.Itoa(PORT), nil) | |
fmt.Printf("Webserver running on any interface. Listening to port %d\n", PORT) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment