@@ -26,8 +26,7 @@ import (
2626// This is set at build time, see Makefile.
2727var 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
128129type RootResponse struct {
0 commit comments