Skip to content

Instantly share code, notes, and snippets.

@jamsesso
Created August 21, 2013 18:57
Show Gist options
  • Save jamsesso/6298662 to your computer and use it in GitHub Desktop.
Save jamsesso/6298662 to your computer and use it in GitHub Desktop.
Very simple webserver for serving static HTML content.
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