github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/web/instances/contexts.go (about) 1 package instances 2 3 import ( 4 "net/http" 5 "net/url" 6 7 "github.com/cozy/cozy-stack/pkg/config/config" 8 "github.com/labstack/echo/v4" 9 ) 10 11 type contextAPI struct { 12 Config interface{} `json:"config"` 13 Context string `json:"context"` 14 Registries []string `json:"registries"` 15 Office *contextOffice `json:"office,omitempty"` 16 ClouderyEndpoint string `json:"cloudery_endpoint,omitempty"` 17 OIDC interface{} `json:"oidc,omitempty"` 18 } 19 20 type contextOffice struct { 21 OnlyOfficeURL string 22 } 23 24 func showContext(c echo.Context) error { 25 contextName := c.Param("name") 26 contexts := config.GetConfig().Contexts 27 cfg, ok := contexts[contextName].(map[string]interface{}) 28 if !ok { 29 registries := config.GetConfig().Registries 30 _, ok := registries[contextName] 31 if !ok { 32 return c.NoContent(http.StatusNotFound) 33 } 34 } 35 return c.JSON(http.StatusOK, getContextAPI(contextName, cfg)) 36 } 37 38 func lsContexts(c echo.Context) error { 39 contexts := config.GetConfig().Contexts 40 result := []contextAPI{} 41 for contextName, ctx := range contexts { 42 cfg, ok := ctx.(map[string]interface{}) 43 if !ok { 44 cfg = map[string]interface{}{} 45 } 46 result = append(result, getContextAPI(contextName, cfg)) 47 } 48 for contextName := range config.GetConfig().Registries { 49 if _, ok := contexts[contextName]; !ok { 50 result = append(result, getContextAPI(contextName, nil)) 51 } 52 } 53 return c.JSON(http.StatusOK, result) 54 } 55 56 func getContextAPI(contextName string, cfg map[string]interface{}) contextAPI { 57 configuration := config.GetConfig() 58 clouderies := configuration.Clouderies 59 registries := configuration.Registries 60 officeConfig := configuration.Office 61 62 // Clouderies 63 var clouderyEndpoint string 64 var cloudery config.ClouderyConfig 65 cloudery, ok := clouderies[contextName] 66 if !ok { 67 cloudery = clouderies[config.DefaultInstanceContext] 68 } 69 70 clouderyEndpoint = cloudery.API.URL 71 72 // Office 73 var office *contextOffice 74 if o, ok := officeConfig[contextName]; ok { 75 office = &contextOffice{OnlyOfficeURL: o.OnlyOfficeURL} 76 } else if o, ok := officeConfig[config.DefaultInstanceContext]; ok { 77 office = &contextOffice{OnlyOfficeURL: o.OnlyOfficeURL} 78 } 79 80 // Registries 81 var registriesList []string 82 var registryURLs []*url.URL 83 84 // registriesURLs contains context-specific urls and default ones 85 if registryURLs, ok = registries[contextName]; !ok { 86 registryURLs = registries[config.DefaultInstanceContext] 87 } 88 for _, url := range registryURLs { 89 registriesList = append(registriesList, url.String()) 90 } 91 92 // OIDC 93 var oidc map[string]interface{} 94 if full, ok := config.GetOIDC(contextName); ok { 95 oidc = make(map[string]interface{}) 96 for k, v := range full { 97 if k != "client_secret" { 98 oidc[k] = v 99 } 100 } 101 } 102 103 return contextAPI{ 104 Config: cfg, 105 Context: contextName, 106 Registries: registriesList, 107 Office: office, 108 ClouderyEndpoint: clouderyEndpoint, 109 OIDC: oidc, 110 } 111 }