HI,
I have the following settings in my REST API program packages:
In "routers.go"
type Route struct {
Name string
Method string
Pattern string
HandlerFunc http.HandlerFunc
}
var Router = NewRouter()
type Routes []Route
func NewRouter() *mux.Router {
router := mux.NewRouter().StrictSlash(true)
for _, route := range routes {
var handler http.Handler
handler = route.HandlerFunc
router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(handler)
}
return router
}
func Index(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello World!")
}
var routes = Routes{
Route{
"UserData",
strings.ToUpper("Get"),
"/{id}/user-data",
UserData,
},
Route{
"UserData",
strings.ToUpper("Post"),
"/{id}/user-data",
UserData,
},
Route{
"ExampleData",
strings.ToUpper("Post"),
"/{id}/example-data",
ExampleData,
},
Route{
"ExampleData",
strings.ToUpper("Get"),
"/{id}/example-data",
ExampleData,
},
}
In "main.go"
func main(){
var LISTENING_ADDR = GetListeningAddress()
...
server := NewServer(LISTENING_ADDR)
go func() {
err := server.ListenAndServe() // use if http
if err != nil && err != http.ErrServerClosed {
logger.Log.Errorf("Could not listen on %s: %v\n", LISTENING_ADDR, err)
os.Exit(0)
}
}()
}
// NewServer - Create a new server
func NewServer(LISTENING_ADDR string) *http.Server {
return &http.Server{
Addr: LISTENING_ADDR,
Handler: Router,
TLSConfig: TLSConfig(),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 120 * time.Second,
}
}
I have two other packages, example.go and user.go which contain the handler functions.
"example.go"
func ExampleDataPost(w http.Res..., r *http.Req) {
...
}
func ExampleDataGet(w http.Res..., r *http.Req) {
...
}
Also In "user.go" is
func UserDataPost(w http.Res..., r *http.Req) {
...
}
func UserDataGet(w http.Res..., r *http.Req) {
...
}
I would like to move their routes in their respective packages.
I have for example added to "user.go" what is shown below and the same for "example.go" but my problem is how to call the variables (group routers) "UserNewRouter ", "ExampleNewRouter" in their respective routers in the main.go or the "NewServer".
Any help on how to go about this?
"user.go"
func UserDataPost(w http.Res..., r *http.Req) {
...
}
func UserDataGet(w http.Res..., r *http.Req) {
...
}
type Route struct {
Name string
Method string
Pattern string
HandlerFunc http.HandlerFunc
}
var UserNewRouter = NewRouter()
type Routes []Route
func NewRouter() *mux.Router {
router := mux.NewRouter().StrictSlash(true)
for _, route := range routes {
var handler http.Handler
handler = route.HandlerFunc
router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(handler)
}
return router
}
var routes = Routes{
Route{
"UserDataGet",
strings.ToUpper("Get"),
"/{id}/user-data",
UserDataGet,
},
Route{
"UserDataPost",
strings.ToUpper("Post"),
"/{id}/user-data",
UserDataPost,
},
}
"example.go"
func ExampleDataPost(w http.Res..., r *http.Req) {
...
}
func ExampleDataGet(w http.Res..., r *http.Req) {
...
}
type Route struct {
Name string
Method string
Pattern string
HandlerFunc http.HandlerFunc
}
var ExampleNewRouter = NewRouter()
type Routes []Route
func NewRouter() *mux.Router {
router := mux.NewRouter().StrictSlash(true)
for _, route := range routes {
var handler http.Handler
handler = route.HandlerFunc
router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(handler)
}
return router
}
var routes = Routes{
Route{
"ExampleDataPost",
strings.ToUpper("Post"),
"/{id}/example-data",
ExampleDataPost,
},
Route{
"ExampleDataGet",
strings.ToUpper("Get"),
"/{id}/example-data",
ExampleDataGet,
},
}
BR
Fury