Skip to content

Instantly share code, notes, and snippets.

@eamonnmcevoy
Created April 23, 2017 07:54
Show Gist options
  • Save eamonnmcevoy/ec688d07675eee27991df3f577ed337b to your computer and use it in GitHub Desktop.
Save eamonnmcevoy/ec688d07675eee27991df3f577ed337b to your computer and use it in GitHub Desktop.
package server
import (
"go_rest_api/pkg"
"log"
"net/http"
"os"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)
type Server struct {
router *mux.Router
}
func NewServer(u root.UserService) *Server {
s := Server{router: mux.NewRouter()}
userRouter := NewUserRouter(u, s.getSubrouter("/user"))
s.router.Handle("/user", userRouter)
return &s
}
func (s *Server) Start() {
log.Println("Listening on port 8080")
if err := http.ListenAndServe(":8080", handlers.LoggingHandler(os.Stdout, s.router)); err != nil {
log.Fatal("http.ListenAndServe: ", err)
}
}
func (s *Server) getSubrouter(path string) *mux.Router {
return s.router.PathPrefix(path).Subrouter()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment