Serving Files

Rafał Lorenz edited this page Feb 23, 2018 · 5 revisions
  1. Static Files

Static Files

package main

import (
    "fmt"
    "log"
    "net/http"
	
    "github.com/vardius/gorouter"
)

func Index(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Welcome!\n")
}

func Hello(w http.ResponseWriter, r *http.Request) {
    params, _ := gorouter.FromContext(r.Context())
    fmt.Fprintf(w, "hello, %s!\n", params.Value("name"))
}

func main() {
    router := gorouter.New()
    router.GET("/", http.HandlerFunc(Index))
    router.GET("/hello/{name}", http.HandlerFunc(Hello))
    // If route not found and the request method equals Get
    // router will serve files from directory
    // third parameter decide if prefix should be striped
    router.ServeFiles(http.Dir("static"), "static", false)

    log.Fatal(http.ListenAndServe(":8080", router))
}
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.