|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "net" |
| 7 | + "net/http" |
| 8 | + "os" |
| 9 | + |
| 10 | + "github.com/docker/dait/internal/models" |
| 11 | + "github.com/labstack/echo/v4" |
| 12 | + "github.com/labstack/echo/v4/middleware" |
| 13 | + "github.com/sirupsen/logrus" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + dataPath = "/data" |
| 18 | + logger = logrus.New() |
| 19 | +) |
| 20 | + |
| 21 | +func Run(socketPath string) error { |
| 22 | + |
| 23 | + logger.SetOutput(os.Stdout) |
| 24 | + |
| 25 | + logMiddleware := middleware.LoggerWithConfig(middleware.LoggerConfig{ |
| 26 | + Skipper: middleware.DefaultSkipper, |
| 27 | + Format: `{"time":"${time_rfc3339_nano}","id":"${id}",` + |
| 28 | + `"method":"${method}","uri":"${uri}",` + |
| 29 | + `"status":${status},"error":"${error}"` + |
| 30 | + `}` + "\n", |
| 31 | + CustomTimeFormat: "2006-01-02 15:04:05.00000", |
| 32 | + Output: logger.Writer(), |
| 33 | + }) |
| 34 | + |
| 35 | + logger.Infof("Starting listening on %s\n", socketPath) |
| 36 | + router := echo.New() |
| 37 | + router.HideBanner = true |
| 38 | + router.Use(logMiddleware) |
| 39 | + startURL := "" |
| 40 | + |
| 41 | + ln, err := listen(socketPath) |
| 42 | + if err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + router.Listener = ln |
| 46 | + |
| 47 | + router.GET("/catalog", catalog) |
| 48 | + router.GET("/newcatalog", newCatalog) |
| 49 | + router.GET("/config", config) |
| 50 | + router.POST("/events", events) |
| 51 | + router.POST("/refresh", refresh) |
| 52 | + router.GET("/assets/*", func(ctx echo.Context) error { |
| 53 | + return asset(ctx, ctx.Param("*")) |
| 54 | + }) |
| 55 | + router.GET("/clients", clients) |
| 56 | + |
| 57 | + routes := router.Routes() |
| 58 | + for _, route := range routes { |
| 59 | + logger.Infof("Registered route: %s %s", route.Method, route.Path) |
| 60 | + } |
| 61 | + |
| 62 | + return router.Start(startURL) |
| 63 | +} |
| 64 | + |
| 65 | +func listen(path string) (net.Listener, error) { |
| 66 | + return net.Listen("unix", path) |
| 67 | +} |
| 68 | + |
| 69 | +func asset(ctx echo.Context, path string) error { |
| 70 | + return ctx.File(dataPath + "/assets/" + path) |
| 71 | +} |
| 72 | + |
| 73 | +func newCatalog(ctx echo.Context) error { |
| 74 | + _, err := os.Stat(dataPath + "/catalog.json") |
| 75 | + if err != nil { |
| 76 | + return ctx.JSON(http.StatusNotFound, map[string]string{"error": "catalog.json does not exist"}) |
| 77 | + } |
| 78 | + return ctx.File(dataPath + "/catalog.json") |
| 79 | +} |
| 80 | + |
| 81 | +func catalog(ctx echo.Context) error { |
| 82 | + _, err := os.Stat(dataPath + "/catalog.yaml") |
| 83 | + if err != nil { |
| 84 | + return ctx.JSON(http.StatusNotFound, map[string]string{"error": "catalog.yaml does not exist"}) |
| 85 | + } |
| 86 | + return ctx.File(dataPath + "/catalog.yaml") |
| 87 | +} |
| 88 | + |
| 89 | +func config(ctx echo.Context) error { |
| 90 | + _, err := os.Stat(dataPath + "/config.json") |
| 91 | + if err != nil { |
| 92 | + return ctx.JSON(http.StatusNotFound, map[string]string{"error": "config.json does not exist"}) |
| 93 | + } |
| 94 | + return ctx.File(dataPath + "/config.json") |
| 95 | +} |
| 96 | + |
| 97 | +func clients(ctx echo.Context) error { |
| 98 | + _, err := os.Stat(dataPath + "/clients.json") |
| 99 | + if err != nil { |
| 100 | + return ctx.JSON(http.StatusNotFound, map[string]string{"error": "clients.json does not exist"}) |
| 101 | + } |
| 102 | + return ctx.File(dataPath + "/clients.json") |
| 103 | +} |
| 104 | + |
| 105 | +func events(ctx echo.Context) error { |
| 106 | + // Send events to Docker API |
| 107 | + url := "https://api.docker.com/events/v1/track" |
| 108 | + apiKey := "3EEvlMngcn3meCbpuYoyC4k8TSF0dYcB5XIVix lt" |
| 109 | + |
| 110 | + var records []models.Record |
| 111 | + if err := ctx.Bind(&records); err != nil { |
| 112 | + return ctx.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()}) |
| 113 | + } |
| 114 | + |
| 115 | + client := &http.Client{} |
| 116 | + jsonData, err := json.Marshal(records) |
| 117 | + if err != nil { |
| 118 | + return ctx.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) |
| 119 | + } |
| 120 | + req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) |
| 121 | + if err != nil { |
| 122 | + return ctx.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) |
| 123 | + } |
| 124 | + |
| 125 | + req.Header.Set("Content-Type", "application/json") |
| 126 | + req.Header.Set("x-api-key", apiKey) |
| 127 | + req.Header.Set("x-test-key", "test") |
| 128 | + |
| 129 | + resp, err := client.Do(req) |
| 130 | + if err != nil { |
| 131 | + return ctx.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) |
| 132 | + } |
| 133 | + defer resp.Body.Close() |
| 134 | + |
| 135 | + return ctx.JSON(resp.StatusCode, map[string]string{"status": "success"}) |
| 136 | +} |
| 137 | + |
| 138 | +func init() { |
| 139 | + // Skip initialization during tests |
| 140 | + if os.Getenv("GO_TEST") == "1" { |
| 141 | + return |
| 142 | + } |
| 143 | + if os.Getenv("DATA_PATH") != "" { |
| 144 | + dataPath = os.Getenv("DATA_PATH") |
| 145 | + } |
| 146 | + catalogURL := os.Getenv("CATALOG_URL") |
| 147 | + if catalogURL == "" { |
| 148 | + catalogURL = "https://raw.githubusercontent.com/docker/labs-ai-tools-for-devs/refs/heads/main/prompts/catalog.yaml" |
| 149 | + } |
| 150 | + |
| 151 | + err := initVolume() |
| 152 | + if err != nil { |
| 153 | + logger.Fatal(err) |
| 154 | + } |
| 155 | + // check if catalog.json exists |
| 156 | + _, err = os.Stat(dataPath + "/catalog.json") |
| 157 | + if err != nil { |
| 158 | + logger.Info("catalog.json does not exist, creating it") |
| 159 | + // load catalog.json |
| 160 | + err := models.RefreshCatalog(catalogURL, dataPath) |
| 161 | + if err != nil { |
| 162 | + logger.Fatal(err) |
| 163 | + } |
| 164 | + // refresh catalog.json |
| 165 | + tiles, err := models.ParseCatalog(dataPath) |
| 166 | + if err != nil { |
| 167 | + logger.Fatal(err) |
| 168 | + } |
| 169 | + // write catalog.json |
| 170 | + catalog, err := json.Marshal(tiles) |
| 171 | + if err != nil { |
| 172 | + logger.Fatal(err) |
| 173 | + } |
| 174 | + err = os.WriteFile(dataPath+"/catalog.json", catalog, 0644) |
| 175 | + if err != nil { |
| 176 | + logger.Fatal(err) |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | +} |
| 181 | + |
| 182 | +func refresh(ctx echo.Context) error { |
| 183 | + |
| 184 | + logger.Info("Refreshing catalog") |
| 185 | + catalogURL := os.Getenv("CATALOG_URL") |
| 186 | + if catalogURL == "" { |
| 187 | + catalogURL = "https://raw.githubusercontent.com/docker/labs-ai-tools-for-devs/refs/heads/main/prompts/catalog.yaml" |
| 188 | + } |
| 189 | + |
| 190 | + err := models.RefreshCatalog(catalogURL, dataPath) |
| 191 | + if err != nil { |
| 192 | + logger.Error(err) |
| 193 | + return ctx.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) |
| 194 | + } |
| 195 | + // refresh catalog.json |
| 196 | + tiles, err := models.ParseCatalog(dataPath) |
| 197 | + if err != nil { |
| 198 | + logger.Error(err) |
| 199 | + return ctx.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) |
| 200 | + } |
| 201 | + // write catalog.json |
| 202 | + catalog, err := json.Marshal(tiles) |
| 203 | + if err != nil { |
| 204 | + return ctx.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) |
| 205 | + } |
| 206 | + err = os.WriteFile(dataPath+"/catalog.json", catalog, 0644) |
| 207 | + if err != nil { |
| 208 | + return ctx.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) |
| 209 | + } |
| 210 | + return ctx.JSON(http.StatusOK, map[string]string{"status": "success"}) |
| 211 | +} |
| 212 | + |
| 213 | +type HTTPMessageBody struct { |
| 214 | + Message string |
| 215 | +} |
| 216 | + |
| 217 | +func initVolume() error { |
| 218 | + // check if /data/assets exists |
| 219 | + _, err := os.Stat(dataPath + "/assets") |
| 220 | + if err != nil { |
| 221 | + logger.Info("Creating /data/assets") |
| 222 | + err := os.MkdirAll(dataPath+"/assets", 0755) |
| 223 | + if err != nil { |
| 224 | + return err |
| 225 | + } |
| 226 | + } |
| 227 | + return nil |
| 228 | +} |
0 commit comments