API Gatway updated, now communicates with frontend

This commit is contained in:
gaz8860 Gary 2023-04-15 23:21:15 +01:00
parent 6ad23b3e14
commit 9353c55d1d
4 changed files with 44 additions and 39 deletions

View File

@ -0,0 +1,10 @@
<div class="card">
<div class="card-body">
<p>@Data.Text</p>
</div>
</div>
@code {
[Parameter]
public Component Data { get; set; }
}

View File

@ -4,7 +4,7 @@
@using System.Text.Json; @using System.Text.Json;
@inject IHttpService httpService @inject IHttpService httpService
@* @if (data != null) @if (data != null)
{ {
@foreach (var component in data) { @foreach (var component in data) {
switch (component.Type) { switch (component.Type) {
@ -27,16 +27,16 @@
} }
} }
} *@ }
<pre>@response.Text</pre>
@code { @code {
private Component response; private Component[] data;
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
response = await httpService.GetAsync<Component>("http://localhost:8080/test"); var response = await httpService.GetAsync<Schema>("http://localhost:10000/localhost/schema");
data = response.components;
} }
} }

View File

@ -17,7 +17,7 @@
{ {
@foreach (var module in response){ @foreach (var module in response){
<div class="nav-item px-3"> <div class="nav-item px-3">
<NavLink class="nav-link" href="" Match="NavLinkMatch.All"> <NavLink class="nav-link" href=@module.Name Match="NavLinkMatch.All">
<span class="oi oi-home" aria-hidden="true"></span> @module.Name <span class="oi oi-home" aria-hidden="true"></span> @module.Name
</NavLink> </NavLink>
</div> </div>

View File

@ -31,7 +31,6 @@ type Module struct {
Container string `json:"Container"` Container string `json:"Container"`
} }
var Containers []Container
func homePage(w http.ResponseWriter, r *http.Request) { func homePage(w http.ResponseWriter, r *http.Request) {
@ -42,9 +41,8 @@ func getSchema(w http.ResponseWriter, r *http.Request){
vars := mux.Vars(r) vars := mux.Vars(r)
key := vars["container"] key := vars["container"]
for _, container := range Containers {
if container.Id == key { requestURL := fmt.Sprintf("http://%s:8080/schema", key)
requestURL := fmt.Sprintf("http://%s:%s/schema", container.Name, container.port)
resp, err := http.Get(requestURL) resp, err := http.Get(requestURL)
if err != nil { if err != nil {
log.Fatal("Request to container failed: %v", err) log.Fatal("Request to container failed: %v", err)
@ -57,19 +55,17 @@ func getSchema(w http.ResponseWriter, r *http.Request){
log.Fatal("Failed to print Body: %v", err) log.Fatal("Failed to print Body: %v", err)
} }
fmt.Fprintf(w,string(body)) w.Header().Set("Content-Type", "application/json")
} w.WriteHeader(resp.StatusCode)
} w.Write(body)
} }
func getStats(w http.ResponseWriter, r *http.Request){ func getStats(w http.ResponseWriter, r *http.Request){
vars := mux.Vars(r) vars := mux.Vars(r)
key := vars["container"] key := vars["container"]
for _, container := range Containers {
if container.Id == key { requestURL := fmt.Sprintf("http://%s:8080/stats", key)
requestURL := fmt.Sprintf("http://%s:%s/stats", container.Name, container.port)
resp, err := http.Get(requestURL) resp, err := http.Get(requestURL)
if err != nil { if err != nil {
log.Fatal("Request to container failed: %v", err) log.Fatal("Request to container failed: %v", err)
@ -82,10 +78,9 @@ func getStats(w http.ResponseWriter, r *http.Request){
log.Fatal("Failed to print Body: %v", err) log.Fatal("Failed to print Body: %v", err)
} }
fmt.Fprintf(w,string(body)) w.Header().Set("Content-Type", "application/json")
} w.WriteHeader(resp.StatusCode)
} w.Write(body)
} }