Saturday, March 26, 2016

Serving static content files in Go Gorilla

package main

import (
  "fmt"
  "net/http"
  "log"
  "github.com/gorilla/mux"
)

const (
  PORT       = ":80"
  STATIC_DIR = "/static/"
)

func main() {
  mx := mux.NewRouter()
  mx.PathPrefix(STATIC_DIR).Handler(http.StripPrefix(STATIC_DIR, http.FileServer(http.Dir("." + STATIC_DIR))))

  err := http.ListenAndServe(PORT, mx)

  if err != nil {
    fmt.Printf("main(): %s\n", err)
    log.Fatal("ListenAndServe: ", err)
  }
}

Reference:

http://stackoverflow.com/questions/15834278/serving-static-content-with-a-root-url-with-the-gorilla-toolkit

No comments: