github.com/cloudfoundry/libcfbuildpack@v1.91.23/helper/application_routes.go (about) 1 /* 2 * Copyright 2018-2020 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package helper 18 19 import ( 20 "encoding/json" 21 "fmt" 22 "os" 23 ) 24 25 // ApplicationRoutes is a map of route name to ApplicationRoute. 26 type ApplicationRoutes map[string]ApplicationRoute 27 28 // ApplicationRoute represents a route exposed by the platform to an application. 29 type ApplicationRoute struct { 30 // Port is the port exposed as part of the route. 31 Port int `json:"port"` 32 33 // URI is the URI exposed by the route. 34 URI string `json:"uri"` 35 } 36 37 // DefaultApplicationRoutes creates a new instance of ApplicationRoutes, extracting the value from the 38 // CNB_APP_ROUTES environment variable. 39 func DefaultApplicationRoutes() (ApplicationRoutes, error) { 40 a, ok := os.LookupEnv("CNB_APP_ROUTES") 41 if !ok { 42 return nil, fmt.Errorf("CNB_APP_ROUTES not set") 43 } 44 45 var ar ApplicationRoutes 46 if err := json.Unmarshal([]byte(a), &ar); err != nil { 47 return nil, err 48 } 49 50 return ar, nil 51 }