June 14, 2016

golang serving dynamic and static files

http://www.alexedwards.net/blog/serving-static-sites-with-go


package main

import (
        "fmt"
        "net/http"
)

func index_handler(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "<html><body>Hi world.<img src=\"/img/kitten.jpg\"></body></html>")
}

func main() {
        fmt.Println("Running...")
        http.Handle("/img/", http.StripPrefix("/img/",http.FileServer(http.Dir("public/img"))))
        http.Handle("/js/",  http.StripPrefix("/js/", http.FileServer(http.Dir("public/js" ))))
        http.Handle("/css/", http.StripPrefix("/css/",http.FileServer(http.Dir("public/css"))))
        http.HandleFunc("/", index_handler)
        http.ListenAndServe(":9000", nil)
}

No comments:

Post a Comment