-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
using saved data for api calls, but wrong metadata on page
- Loading branch information
1 parent
5bc32f6
commit 1330441
Showing
10 changed files
with
232 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,31 @@ | ||
package app | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
) | ||
|
||
type SystemConfig struct { | ||
Schedule string `yaml:"schedule"` | ||
} | ||
|
||
func (app *Application) RegisterHandlers() { | ||
http.HandleFunc("/api/images", app.handleListImages) | ||
} | ||
|
||
func (app *Application) handleListImages(w http.ResponseWriter, r *http.Request) { | ||
images, err := app.Kubernetes.ListContainerImages("default") // Assume using "default" namespace | ||
if err != nil { | ||
http.Error(w, "Failed to get images", http.StatusInternalServerError) | ||
return | ||
refresh := r.URL.Query().Get("refresh") | ||
if refresh == "true" { | ||
// Trigger data refresh logic here | ||
} else { | ||
// Use saved data to respond to the request | ||
savedData, err := app.DataService.GetData(context.Background()) | ||
if err != nil { | ||
http.Error(w, "Failed to get saved data", http.StatusInternalServerError) | ||
return | ||
} | ||
json.NewEncoder(w).Encode(savedData) | ||
} | ||
|
||
json.NewEncoder(w).Encode(images) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package model | ||
|
||
type Data struct { | ||
ID string `json:"id"` | ||
Value string `json:"value"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"log" | ||
"net/http" | ||
"os" | ||
"slackwatch/backend/internal/model" | ||
"slackwatch/backend/internal/storage" | ||
"strings" | ||
) | ||
|
||
type DataService struct { | ||
Storage storage.Storage | ||
} | ||
|
||
func (ds *DataService) SaveData(ctx context.Context, data interface{}) error { | ||
file, err := os.OpenFile("data.json", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
encoder := json.NewEncoder(file) | ||
return encoder.Encode(data) | ||
} | ||
|
||
func (ds *DataService) GetData(ctx context.Context) ([]map[string]string, error) { | ||
file, err := os.Open("data.json") | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer file.Close() | ||
|
||
var data []map[string]string | ||
decoder := json.NewDecoder(file) | ||
err = decoder.Decode(&data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return data, nil | ||
} | ||
|
||
// HandleSaveData handles the HTTP request for saving data | ||
func (ds *DataService) HandleSaveData(w http.ResponseWriter, r *http.Request) { | ||
if r.Method != http.MethodPost { | ||
http.Error(w, "Only POST method is allowed", http.StatusMethodNotAllowed) | ||
return | ||
} | ||
|
||
var data model.Data | ||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
|
||
if err := ds.SaveData(context.Background(), data); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.WriteHeader(http.StatusCreated) | ||
} | ||
|
||
// HandleGetData handles the HTTP request for retrieving data by ID | ||
func (ds *DataService) HandleGetData(w http.ResponseWriter, r *http.Request) { | ||
if r.Method != http.MethodGet { | ||
http.Error(w, "Only GET method is allowed", http.StatusMethodNotAllowed) | ||
return | ||
} | ||
|
||
id := strings.TrimPrefix(r.URL.Path, "/api/data/") | ||
log.Printf("Getting data for ID: %s", id) | ||
data, err := ds.GetData(context.Background()) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusNotFound) | ||
return | ||
} | ||
|
||
if err := json.NewEncoder(w).Encode(data); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package storage | ||
|
||
import "context" | ||
import "slackwatch/backend/internal/model" | ||
|
||
type Storage interface { | ||
Save(ctx context.Context, data model.Data) error | ||
Get(ctx context.Context, id string) (model.Data, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package storage | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"os" | ||
"slackwatch/backend/internal/model" | ||
) | ||
|
||
type FileStorage struct { | ||
FilePath string | ||
} | ||
|
||
func (fs *FileStorage) Save(ctx context.Context, data model.Data) error { | ||
file, err := os.OpenFile(fs.FilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
encoder := json.NewEncoder(file) | ||
return encoder.Encode(data) | ||
} | ||
|
||
func (fs *FileStorage) Get(ctx context.Context, id string) (model.Data, error) { | ||
// Implementation for retrieving data by ID from file | ||
// This is a simplified example. A real implementation might need to read the entire file and decode each entry to find the matching ID. | ||
return model.Data{}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters