-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from colegion/extract_handlers_init
Add utils package to the skeleton application
- Loading branch information
Showing
2 changed files
with
41 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Package utils provides building blocks that are reused across | ||
// the application (e.g. in tests and main package) but are not related | ||
// to the business logics. Everything that is used for initialization and | ||
// start of the app but hasn't got its own package must be located here. | ||
package utils | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/colegion/goal/internal/skeleton/assets/handlers" | ||
"github.com/colegion/goal/internal/skeleton/routes" | ||
|
||
"github.com/colegion/contrib/configs/iniflag" | ||
) | ||
|
||
// InitHandler gets a number of INI configuration file paths. | ||
// It parses them, populates flags with the data extracted from the configs | ||
// and input arguments to the program, and returns an initialized | ||
// go HTTP handler that is based on the app's routes and handler functions | ||
// automatically generated from controllers. | ||
// Example: | ||
// h, err := InitHandler("basic.ini", "more/important.ini", "most/important.ini") | ||
// if err != nil { | ||
// panic(err) | ||
// } | ||
func InitHandler(configFiles ...string) (http.Handler, error) { | ||
// Parse configuration files and flags. | ||
err := iniflag.Parse(configFiles...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Initialize handler functions and controllers. | ||
handlers.Init() | ||
|
||
// Build a standard HTTP handler from routes and handler functions. | ||
return routes.List.Build() | ||
} |