Created
April 23, 2017 07:54
-
-
Save eamonnmcevoy/ec688d07675eee27991df3f577ed337b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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