コード例 #1
0
ファイル: main.go プロジェクト: readthecodes/go-oauth2-server
func runServer(cnf *config.Config, db *gorm.DB) {
	// Initialise the oauth service
	oauthService := oauth.NewService(cnf, db)

	// Initialise the accounts service
	_ = accounts.NewService(cnf, db, oauthService)

	// Initialise the web service
	_ = web.NewService(cnf, oauthService)

	// Start a classic negroni app
	webApp := negroni.Classic()

	// Create a router instance
	router := mux.NewRouter().StrictSlash(true)

	// Add routes for the oauth package (REST tokens endpoint)
	routes.AddRoutes(oauth.Routes, router.PathPrefix("/api/v1/oauth").Subrouter())

	// Add routes for the web package (register, login authorize web pages)
	routes.AddRoutes(web.Routes, router.PathPrefix("/web").Subrouter())

	// Set the router
	webApp.UseHandler(router)

	// Run the server on port 8080
	webApp.Run(":8080")
}
コード例 #2
0
ファイル: main.go プロジェクト: jmheidly/go-oauth2-server
func runServer(cnf *config.Config, db *gorm.DB) {
	// Initialise the oauth service
	oauthService := oauth.NewService(cnf, db)

	// Initialise the accounts service
	_ = accounts.NewService(cnf, db, oauthService)

	// Initialise the web service
	_ = web.NewService(cnf, oauthService)

	// Start a classic negroni app
	webApp := negroni.Classic()

	// Create a router instance
	router := mux.NewRouter().StrictSlash(true)

	var subRouter *mux.Router

	// Add routes for the oauth service
	subRouter = router.PathPrefix("/oauth/api/v1").Subrouter()
	for _, route := range oauth.Routes {
		var handler http.Handler
		if len(route.Middlewares) > 0 {
			n := negroni.New()
			for _, middleware := range route.Middlewares {
				n.Use(middleware)
			}
			n.Use(negroni.Wrap(route.HandlerFunc))
			handler = n
		} else {
			handler = route.HandlerFunc
		}

		subRouter.Methods(route.Methods...).
			Path(route.Pattern).
			Name(route.Name).
			Handler(handler)
	}

	// Add routes for web pages
	subRouter = router.PathPrefix("/web").Subrouter()
	for _, route := range web.Routes {
		var handler http.Handler
		if len(route.Middlewares) > 0 {
			n := negroni.New()
			for _, middleware := range route.Middlewares {
				n.Use(middleware)
			}
			n.Use(negroni.Wrap(route.HandlerFunc))
			handler = n
		} else {
			handler = route.HandlerFunc
		}

		subRouter.Methods(route.Methods...).
			Path(route.Pattern).
			Name(route.Name).
			Handler(handler)
	}

	// Set the router
	webApp.UseHandler(router)

	// Run the server on port 8080
	webApp.Run(":8080")
}