Frontier Software

Server

Simple file server

// Simple static webserver:
package main

import (
	"log"
	"net/http"
)

func main() {
	log.Fatal(http.ListenAndServe("localhost:8000", http.FileServer(http.Dir("/usr/share/doc"))))
}

FileServer returns a handler that serves HTTP requests with the contents of the file system rooted at root. As a special case, the returned file server redirects any request ending in “/index.html” to the same path, without the final “index.html”.

ServeFile doesn’t return a handler, but rather is a function that can be used in a handler to return the contents of a file.

// Serve test.html
package main

import (
	"log"
	"net/http"
)

// each request calls handler
func main() {
	http.HandleFunc("/", handler)
	log.Fatal(http.ListenAndServe("localhost:8000", nil))
}

func handler(w http.ResponseWriter, r *http.Request) {
	http.ServeFile(w, r, "./test.html")
}