Skip to content

golang route for static files

pikachule edited this page Aug 7, 2017 · 2 revisions
package main

import (
	"fmt"
	"net/http"

	"github.com/gorilla/mux"
)

func HomepageHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "%s", "hello world!")
}

func ArticleHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "%s", "article here.")
}

func ArticleListHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "%s", "article list")
}

func main() {
	r := mux.NewRouter()

	r.HandleFunc("/", HomepageHandler)

	s := r.PathPrefix("/article").Subrouter()
	s.HandleFunc("/", ArticleListHandler)
	s.HandleFunc("/{article_id}/", ArticleHandler)

	r.PathPrefix("/{file}").Handler(http.FileServer(http.Dir("F:\\go_workspaces\\public\\")))
	http.ListenAndServe(":1234", r)
	fmt.Println("it works!")
}

Clone this wiki locally