Skip to content

Instantly share code, notes, and snippets.

Created January 21, 2017 00:38
Show Gist options
  • Save anonymous/af1e6d922ce22597099521a4b2cfa16f to your computer and use it in GitHub Desktop.
Save anonymous/af1e6d922ce22597099521a4b2cfa16f to your computer and use it in GitHub Desktop.
package main
import (
"net/http"
"time"
"github.com/gorilla/mux"
)
func main() {
// fs := http.FileServer(http.Dir("./docs/html"))
// http.Handle("/", fs)
//
// log.Println("Listening...")
// http.ListenAndServe(":3000", nil)
r := NewStack()
svr := http.Server{
Addr: "0.0.0.0:3000",
Handler: r,
}
svr.ListenAndServe()
}
// NewStack returns a mux router with all the routes/middleware for the service attached to it
func NewStack() *mux.Router {
// Create a new mux router
r := mux.NewRouter().StrictSlash(true)
// Range over the Routes array and add a new endpoint to the mux router with the proper values
for _, route := range Routes {
var handler http.Handler
handler = route.HandlerFunc
r.Methods(route.Method).Path(route.Pattern).Name(route.Name).Handler(
handler,
)
}
return r
}
// Route is an object that represents a route for the service
type Route struct {
Name string `json:"name"`
Method string `json:"method"`
Pattern string `json:"path"`
Timestamp string `json:"timestamp"`
HandlerFunc http.HandlerFunc
}
// Routes array will hold all the routes for the service
var Routes = []Route{
Route{
"Docs",
"GET",
"/",
time.Now().Format(time.RFC3339),
ServeDocs,
},
}
// ServeDocs returns a static html file to the ResponseWriter
// that contains the documentation for the microservice
func ServeDocs(w http.ResponseWriter, r *http.Request) {
// Get the path for the docs
path := "./docs/html"
// Create the file server
fs := http.FileServer(http.Dir(path))
fs.ServeHTTP(w, r)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment