Skip to content

Commit b9a4586

Browse files
committed
refactor: break up router setup into multiple functions
1 parent 0bbf298 commit b9a4586

File tree

4 files changed

+30
-24
lines changed

4 files changed

+30
-24
lines changed

main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,14 @@ func main() {
5454
log.Fatal().Msg(err.Error())
5555
}
5656

57-
r, err := router.Router()
57+
r, err := router.Config()
5858
if err != nil {
5959
log.Fatal().Msg(err.Error())
6060
}
6161

62+
// Attach the routes to the root URL
63+
router.AttachRoutes(r.Group("/"))
64+
6265
// Set the port to the env variable, default to 8080
6366
port := os.Getenv("PORT")
6467
if port == "" {

pkg/router/router.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ import (
2626
// This is set at build time, see Makefile.
2727
var version = "0.0.0"
2828

29-
// Router controls the routes for the API.
30-
func Router() (*gin.Engine, error) {
29+
func Config() (*gin.Engine, error) {
3130
// Set up the router and middlewares
3231
r := gin.New()
3332

@@ -78,23 +77,14 @@ func Router() (*gin.Engine, error) {
7877
// therefore we don’t need to trust anyone here.
7978
_ = r.SetTrustedProxies([]string{})
8079

81-
/*
82-
* Route setup
83-
*/
84-
r.GET("", GetRoot)
85-
r.OPTIONS("", OptionsRoot)
86-
r.GET("/version", GetVersion)
87-
88-
r.OPTIONS("/version", OptionsVersion)
89-
9080
apiURL, ok := os.LookupEnv("API_URL")
9181
if !ok {
92-
return nil, errors.New("Environment variable API_URL must be set")
82+
return nil, errors.New("environment variable API_URL must be set")
9383
}
9484

9585
url, err := url.Parse(apiURL)
9686
if err != nil {
97-
return nil, errors.New("Environment variable API_URL must be a valid URL")
87+
return nil, errors.New("environment variable API_URL must be a valid URL")
9888
}
9989

10090
log.Debug().Str("API Base URL", url.String()).Str("Host", url.Host).Str("Path", url.Path).Msg("Router")
@@ -105,10 +95,23 @@ func Router() (*gin.Engine, error) {
10595
docs.SwaggerInfo.Version = version
10696
docs.SwaggerInfo.Description = "The backend for Envelope Zero, a zero based envelope budgeting solution. Check out the source code at https://github.com/envelope-zero/backend."
10797

108-
r.GET("/docs/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
98+
return r, nil
99+
}
100+
101+
// AttachRoutes attaches the API routes to the router group that is passed in
102+
// Separating this from RouterConfig() allows us to attach it to different
103+
// paths for different use cases, e.g. the standalone version.
104+
func AttachRoutes(group *gin.RouterGroup) {
105+
group.GET("", GetRoot)
106+
group.OPTIONS("", OptionsRoot)
107+
group.GET("/version", GetVersion)
108+
109+
group.OPTIONS("/version", OptionsVersion)
110+
111+
group.GET("/docs/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
109112

110113
// API v1 setup
111-
v1 := r.Group("/v1")
114+
v1 := group.Group("/v1")
112115
{
113116
v1.GET("", GetV1)
114117
v1.DELETE("", controllers.DeleteAll)
@@ -121,8 +124,6 @@ func Router() (*gin.Engine, error) {
121124
controllers.RegisterCategoryRoutes(v1.Group("/categories"))
122125
controllers.RegisterEnvelopeRoutes(v1.Group("/envelopes"))
123126
controllers.RegisterAllocationRoutes(v1.Group("/allocations"))
124-
125-
return r, nil
126127
}
127128

128129
type RootResponse struct {

pkg/router/router_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ func TestGinMode(t *testing.T) {
1717
os.Setenv("GIN_MODE", "debug")
1818
os.Setenv("API_URL", "http://example.com")
1919

20-
_, err := router.Router()
20+
r, err := router.Config()
21+
router.AttachRoutes(r.Group("/"))
2122

2223
assert.Nil(t, err, "%T: %v", err, err)
2324
assert.True(t, gin.IsDebugging())
@@ -27,14 +28,14 @@ func TestGinMode(t *testing.T) {
2728
}
2829

2930
func TestEnvUnset(t *testing.T) {
30-
_, err := router.Router()
31+
_, err := router.Config()
3132

3233
assert.NotNil(t, err, "API_URL is unset, this must lead to an error")
3334
}
3435

3536
func TestEnvNoURL(t *testing.T) {
3637
os.Setenv("API_URL", "\\:veryMuchNotAURL")
37-
_, err := router.Router()
38+
_, err := router.Config()
3839

3940
assert.NotNil(t, err, "API_URL is not an URL, this must lead to an error")
4041
}
@@ -45,7 +46,7 @@ func TestCorsSetting(t *testing.T) {
4546
os.Setenv("CORS_ALLOW_ORIGINS", "http://localhost:3000 https://example.com")
4647
os.Setenv("API_URL", "http://example.com")
4748

48-
_, err := router.Router()
49+
_, err := router.Config()
4950

5051
assert.Nil(t, err)
5152
os.Unsetenv("CORS_ALLOW_ORIGINS")

test/helpers.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ func Request(t *testing.T, method, url string, body any, headers ...map[string]s
4141
}
4242
}
4343

44-
router, err := router.Router()
44+
r, err := router.Config()
4545
if err != nil {
4646
assert.FailNow(t, "Router could not be initialized")
4747
}
48+
router.AttachRoutes(r.Group("/"))
4849

4950
recorder := httptest.NewRecorder()
5051
req, _ := http.NewRequest(method, url, bytes.NewBuffer(byteStr))
@@ -55,7 +56,7 @@ func Request(t *testing.T, method, url string, body any, headers ...map[string]s
5556
}
5657
}
5758

58-
router.ServeHTTP(recorder, req)
59+
r.ServeHTTP(recorder, req)
5960

6061
return *recorder
6162
}

0 commit comments

Comments
 (0)