Skip to content

Commit 010b8de

Browse files
committed
update handlers and registry adapters
Signed-off-by: bupd <[email protected]>
1 parent 98ce4d9 commit 010b8de

File tree

14 files changed

+128
-250
lines changed

14 files changed

+128
-250
lines changed

ground-control/internal/database/images.sql.go

Lines changed: 2 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ground-control/internal/server/handleResponse.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ func (e *AppError) Error() string {
1919
func WriteJSONResponse(w http.ResponseWriter, statusCode int, data interface{}) {
2020
w.Header().Set("Content-Type", "application/json")
2121
w.WriteHeader(statusCode)
22-
json.NewEncoder(w).Encode(data)
22+
_ = json.NewEncoder(w).Encode(data)
2323
}
2424

25-
// handle AppError and send structured JSON response.
25+
// handle AppError and senda structured JSON response.
2626
func HandleAppError(w http.ResponseWriter, err error) {
2727
if appErr, ok := err.(*AppError); ok {
2828
WriteJSONResponse(w, appErr.Code, appErr)

ground-control/internal/server/handlers.go

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@ import (
1010
"time"
1111

1212
"container-registry.com/harbor-satellite/ground-control/internal/database"
13+
"container-registry.com/harbor-satellite/ground-control/reg"
1314
"github.com/gorilla/mux"
1415
)
1516

17+
type RegListParams struct {
18+
Url string `json:"registry_url"`
19+
UserName string `json:"username"`
20+
Password string `json:"password"`
21+
}
1622
type GroupRequestParams struct {
1723
GroupName string `json:"group_name"`
1824
}
@@ -81,20 +87,24 @@ func (s *Server) healthHandler(w http.ResponseWriter, r *http.Request) {
8187
func (s *Server) createGroupHandler(w http.ResponseWriter, r *http.Request) {
8288
// Decode request body
8389
var req GroupRequestParams
84-
if err := DecodeRequestBody(r, req); err != nil {
90+
if err := DecodeRequestBody(r, &req); err != nil {
8591
HandleAppError(w, err)
8692
return
8793
}
8894

8995
params := database.CreateGroupParams{
9096
GroupName: req.GroupName,
91-
CreatedAt: time.Now(),
92-
UpdatedAt: time.Now(),
97+
CreatedAt: time.Now(),
98+
UpdatedAt: time.Now(),
9399
}
94100

95101
// Call the database query to create Group
96102
result, err := s.dbQueries.CreateGroup(r.Context(), params)
97103
if err != nil {
104+
err = &AppError{
105+
Message: err.Error(),
106+
Code: http.StatusBadRequest,
107+
}
98108
HandleAppError(w, err)
99109
return
100110
}
@@ -104,7 +114,7 @@ func (s *Server) createGroupHandler(w http.ResponseWriter, r *http.Request) {
104114

105115
func (s *Server) createLabelHandler(w http.ResponseWriter, r *http.Request) {
106116
var req LabelRequestParams
107-
if err := DecodeRequestBody(r, req); err != nil {
117+
if err := DecodeRequestBody(r, &req); err != nil {
108118
HandleAppError(w, err)
109119
return
110120
}
@@ -125,7 +135,7 @@ func (s *Server) createLabelHandler(w http.ResponseWriter, r *http.Request) {
125135

126136
func (s *Server) addImageHandler(w http.ResponseWriter, r *http.Request) {
127137
var req ImageAddParams
128-
if err := DecodeRequestBody(r, req); err != nil {
138+
if err := DecodeRequestBody(r, &req); err != nil {
129139
HandleAppError(w, err)
130140
return
131141
}
@@ -265,11 +275,11 @@ func (s *Server) assignImageToGroup(w http.ResponseWriter, r *http.Request) {
265275
}
266276

267277
func (s *Server) GetImagesForSatellite(w http.ResponseWriter, r *http.Request) {
268-
token, err := GetAuthToken(r)
269-
if err != nil {
270-
HandleAppError(w, err)
271-
return
272-
}
278+
token, err := GetAuthToken(r)
279+
if err != nil {
280+
HandleAppError(w, err)
281+
return
282+
}
273283
result, err := s.dbQueries.GetImagesForSatellite(r.Context(), token)
274284
if err != nil {
275285
log.Printf("Error: Failed to get image for satellite: %v", err)
@@ -283,11 +293,34 @@ func (s *Server) GetImagesForSatellite(w http.ResponseWriter, r *http.Request) {
283293
func (s *Server) listGroupHandler(w http.ResponseWriter, r *http.Request) {
284294
result, err := s.dbQueries.ListGroups(r.Context())
285295
if err != nil {
286-
HandleAppError(w, err)
296+
HandleAppError(w, err)
287297
return
288298
}
289299

290-
WriteJSONResponse(w, http.StatusOK, result)
300+
WriteJSONResponse(w, http.StatusOK, result)
301+
}
302+
303+
func (s *Server) regListHandler(w http.ResponseWriter, r *http.Request) {
304+
username := r.URL.Query().Get("username")
305+
password := r.URL.Query().Get("password")
306+
url := r.URL.Query().Get("url")
307+
308+
if url == "" {
309+
err := &AppError{
310+
Message: "Missing URL in Request",
311+
Code: http.StatusBadRequest,
312+
}
313+
HandleAppError(w, err)
314+
return
315+
}
316+
317+
result, err := reg.FetchRepos(username, password, url)
318+
if err != nil {
319+
HandleAppError(w, err)
320+
return
321+
}
322+
323+
WriteJSONResponse(w, http.StatusOK, result)
291324
}
292325

293326
func (s *Server) getGroupHandler(w http.ResponseWriter, r *http.Request) {
@@ -300,7 +333,7 @@ func (s *Server) getGroupHandler(w http.ResponseWriter, r *http.Request) {
300333
return
301334
}
302335

303-
WriteJSONResponse(w, http.StatusOK, result)
336+
WriteJSONResponse(w, http.StatusOK, result)
304337
}
305338

306339
// creates a unique random API token of the specified length in bytes.

ground-control/internal/server/routes.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ func (s *Server) RegisterRoutes() http.Handler {
1212
r.HandleFunc("/ping", s.Ping).Methods("GET")
1313
r.HandleFunc("/health", s.healthHandler).Methods("GET")
1414

15-
// Ground Control interface
15+
r.HandleFunc("/registry/list", s.regListHandler).Methods("GET")
16+
17+
// Ground Control interface
1618
r.HandleFunc("/group/list", s.listGroupHandler).Methods("GET")
1719
r.HandleFunc("/group/{group}", s.getGroupHandler).Methods("GET")
1820

@@ -29,7 +31,7 @@ func (s *Server) RegisterRoutes() http.Handler {
2931

3032
r.HandleFunc("/satellite/images", s.GetImagesForSatellite).Methods("GET")
3133

32-
// Satellite based routes
34+
// Satellite based routes
3335
// r.HandleFunc("/images", s.getImageListHandler).Methods("GET")
3436
// r.HandleFunc("/images", s.addImageListHandler).Methods("POST")
3537
// r.HandleFunc("/group", s.deleteGroupHandler).Methods("DELETE")

ground-control/internal/server/server.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ func NewServer() *http.Server {
4949
log.Fatalf("Error in sql: %v", err)
5050
}
5151

52-
5352
dbQueries := database.New(db)
5453

5554
NewServer := &Server{

ground-control/main

-8.11 MB
Binary file not shown.

ground-control/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
"log"
56

67
"container-registry.com/harbor-satellite/ground-control/internal/server"
78
_ "github.com/joho/godotenv/autoload"
@@ -13,6 +14,6 @@ func main() {
1314
fmt.Printf("Ground Control running on port %s\n", server.Addr)
1415
err := server.ListenAndServe()
1516
if err != nil {
16-
panic(fmt.Sprintf("cannot start server: %s", err))
17+
log.Fatalf("cannot start server: %s", err)
1718
}
1819
}

ground-control/reg/adapter.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package reg
2+
3+
import (
4+
"encoding/base64"
5+
"encoding/json"
6+
"fmt"
7+
"net/http"
8+
)
9+
10+
type AppError struct {
11+
Message string `json:"message"`
12+
Code int `json:"code"`
13+
}
14+
type Image struct {
15+
Digest string
16+
Name string
17+
}
18+
19+
type TagListResponse struct {
20+
Name string `json:"name"`
21+
Tags []string `json:"tags"`
22+
}
23+
24+
func FetchRepos(username, password, url string) ([]string, error) {
25+
// Sample Data
26+
// username := "admin"
27+
// password := "Harbor12345"
28+
// url := "https://demo.goharbor.io"
29+
30+
url = url + "/v2/_catalog?n=1000"
31+
32+
req, err := http.NewRequest("GET", url, nil)
33+
if err != nil {
34+
return nil, fmt.Errorf("failed to create request: %v", err)
35+
}
36+
37+
// Encode credentials for Basic Authentication
38+
auth := base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
39+
req.Header.Set("Authorization", "Basic "+auth)
40+
41+
client := &http.Client{}
42+
resp, err := client.Do(req)
43+
if err != nil {
44+
return nil, fmt.Errorf("failed to make request: %v", err)
45+
}
46+
defer resp.Body.Close()
47+
48+
// Check the response status code
49+
if resp.StatusCode != http.StatusOK {
50+
return nil, fmt.Errorf("failed to fetch catalog: %s", resp.Status)
51+
}
52+
53+
// Read the response body and decode JSON
54+
var result map[string][]string
55+
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
56+
return nil, fmt.Errorf("failed to unmarshal JSON response: %w", err)
57+
}
58+
59+
// Extract the list of repositories
60+
repos, ok := result["repositories"]
61+
if !ok {
62+
return nil, fmt.Errorf("repositories not found in response")
63+
}
64+
65+
return repos, nil
66+
}

ground-control/reg/adapter/adapter.go

Lines changed: 0 additions & 80 deletions
This file was deleted.

ground-control/reg/adapter/adapter_test.go

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
package main
1+
package reg
22

33
import (
4-
"context"
54
"testing"
65
)
76

8-
func BenchmarkListRepos(b *testing.B) {
7+
func BenchmarkFetchRepos(b *testing.B) {
98
for i := 0; i < b.N; i++ {
10-
ListRepos(context.Background())
9+
FetchRepos("admin", "Harbor12345", "https://demo.goharbor.io")
1110
}
1211
}
1312

@@ -19,4 +18,3 @@ func BenchmarkListRepos(b *testing.B) {
1918
// BenchmarkListRepos-12 1071 1028679 ns/op
2019
// PASS
2120
// ok container-registry.com/harbor-satellite/ground-control/reg 2.237s
22-

0 commit comments

Comments
 (0)