Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Fixes #679
- Loading branch information
Showing
36 changed files
with
333 additions
and
194 deletions.
There are no files selected for viewing
29 changes: 4 additions & 25 deletions
29
backend/api/admin/src/main/java/co/airy/core/api/config/ClientConfigController.java
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,46 +1,25 @@ | ||
package co.airy.core.api.config; | ||
|
||
import co.airy.core.api.admin.payload.ClientConfigResponsePayload; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@RestController | ||
public class ClientConfigController { | ||
|
||
private final ServiceDiscovery serviceDiscovery; | ||
private final String namespace; | ||
private final RestTemplate restTemplate; | ||
|
||
public ClientConfigController(ServiceDiscovery serviceDiscovery, @Value("${kubernetes.namespace}") String namespace, RestTemplate restTemplate) { | ||
public ClientConfigController(ServiceDiscovery serviceDiscovery) { | ||
this.serviceDiscovery = serviceDiscovery; | ||
this.namespace = namespace; | ||
this.restTemplate = restTemplate; | ||
} | ||
|
||
@PostMapping("/client.config") | ||
public ResponseEntity<ClientConfigResponsePayload> getConfig() { | ||
List<Map<String, Map<String, String>>> components = new ArrayList<>(); | ||
|
||
for (String service : serviceDiscovery.getServices()) { | ||
try { | ||
ResponseEntity<Object> response = restTemplate.exchange(String.format("http://%s.%s/actuator/health", service, namespace), HttpMethod.GET, null, Object.class); | ||
components.add(Map.of(service.replace("-connector", ""), Map.of("enabled", Boolean.toString(response.getStatusCode().is2xxSuccessful())))); | ||
} catch (Exception e) { | ||
components.add(Map.of(service.replace("-connector", ""), Map.of("enabled", Boolean.toString(false)))); | ||
} | ||
} | ||
|
||
return ResponseEntity.ok(ClientConfigResponsePayload.builder() | ||
.components(components) | ||
.features(List.of()) | ||
.components(serviceDiscovery.getComponents()) | ||
.features(Map.of()) | ||
.build()); | ||
} | ||
} |
7 changes: 3 additions & 4 deletions
7
.../payload/ClientConfigResponsePayload.java → ...i/config/ClientConfigResponsePayload.java
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,18 +1,17 @@ | ||
package co.airy.core.api.admin.payload; | ||
package co.airy.core.api.config; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Builder | ||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class ClientConfigResponsePayload { | ||
private List<Map<String, Map<String, String>>> components; | ||
private List<Map<String, String>> features; | ||
private Map<String, Map<String, Object>> components; | ||
private Map<String, String> features; | ||
} |
15 changes: 15 additions & 0 deletions
15
backend/api/admin/src/main/java/co/airy/core/api/config/ClientControllerConfig.java
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,15 @@ | ||
package co.airy.core.api.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.EnableScheduling; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@EnableScheduling | ||
@Configuration | ||
public class ClientControllerConfig { | ||
@Bean | ||
public RestTemplate restTemplate() { | ||
return new RestTemplate(); | ||
} | ||
} |
34 changes: 29 additions & 5 deletions
34
backend/api/admin/src/main/java/co/airy/core/api/config/ServiceDiscovery.java
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,26 +1,50 @@ | ||
package co.airy.core.api.config; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
@Component | ||
public class ServiceDiscovery { | ||
private final String namespace; | ||
private final RestTemplate restTemplate; | ||
|
||
private final Map<String, Map<String, Object>> components = new ConcurrentHashMap<>(); | ||
|
||
private static final List<String> services = List.of( | ||
"sources-chatplugin", | ||
"sources-facebook-connector", | ||
"sources-twilio-connector", | ||
"sources-google-connector" | ||
); | ||
|
||
@Bean | ||
public RestTemplate restTemplate() { | ||
return new RestTemplate(); | ||
public ServiceDiscovery(@Value("${kubernetes.namespace}") String namespace, RestTemplate restTemplate) { | ||
this.namespace = namespace; | ||
this.restTemplate = restTemplate; | ||
} | ||
|
||
public Map<String, Map<String, Object>> getComponents() { | ||
return components; | ||
} | ||
|
||
public List<String> getServices() { | ||
return services; | ||
@Scheduled(fixedRate = 1_000) | ||
private void updateComponentsStatus() { | ||
for (String service : services) { | ||
try { | ||
ResponseEntity<Object> response = restTemplate.exchange(String.format("http://%s.%s/actuator/health", service, namespace), HttpMethod.GET, null, Object.class); | ||
components.put(service.replace("-connector", ""), Map.of("enabled", response.getStatusCode().is2xxSuccessful())); | ||
} catch (Exception e) { | ||
components.put(service.replace("-connector", ""), Map.of("enabled",false)); | ||
} | ||
} | ||
} | ||
} |
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
10 changes: 7 additions & 3 deletions
10
infrastructure/cli/cmd/auth/BUILD → infrastructure/cli/cmd/api/BUILD
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,18 @@ | ||
package api | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// APICmd subcommand for Airy Core | ||
var APICmd = &cobra.Command{ | ||
Use: "api", | ||
TraverseChildren: true, | ||
Short: "Interacts with the Airy Core Platform HTTP API", | ||
Long: ``, | ||
} | ||
|
||
func init() { | ||
APICmd.AddCommand(signupCmd) | ||
APICmd.AddCommand(loginCmd) | ||
} |
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,42 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/airyhq/airy/lib/go/httpclient" | ||
"github.com/airyhq/airy/lib/go/httpclient/payloads" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var loginCmd = &cobra.Command{ | ||
Use: "login", | ||
Short: "Logs you in in the Airy Core Platform", | ||
Long: ``, | ||
Run: login, | ||
} | ||
|
||
func login(cmd *cobra.Command, args []string) { | ||
email, _ := cmd.Flags().GetString("email") | ||
password, _ := cmd.Flags().GetString("password") | ||
c := httpclient.NewClient(viper.GetString("apihost")) | ||
|
||
loginRequestPayload := payloads.LoginRequestPayload{Email: email, Password: password} | ||
res, err := c.Login(loginRequestPayload) | ||
if err != nil { | ||
fmt.Println("could not login:", err) | ||
os.Exit(1) | ||
} | ||
fmt.Printf("logged in correctly: %s\n", res.Token) | ||
|
||
viper.Set("apiJWTToken", res.Token) | ||
viper.WriteConfig() | ||
} | ||
|
||
func init() { | ||
var email, password string | ||
loginCmd.Flags().StringVarP(&email, "email", "e", "grace@hopper.com", "Email") | ||
loginCmd.Flags().StringVarP(&password, "password", "p", "the_answer_is_42", "Password") | ||
} |
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,43 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/airyhq/airy/lib/go/httpclient" | ||
"github.com/airyhq/airy/lib/go/httpclient/payloads" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var signupCmd = &cobra.Command{ | ||
Use: "signup", | ||
Short: "Signs users up in the Airy Core Platform", | ||
Long: ``, | ||
Run: signup, | ||
} | ||
|
||
func signup(cmd *cobra.Command, args []string) { | ||
firstName, _ := cmd.Flags().GetString("firstName") | ||
lastName, _ := cmd.Flags().GetString("lastName") | ||
email, _ := cmd.Flags().GetString("email") | ||
password, _ := cmd.Flags().GetString("password") | ||
c := httpclient.NewClient(viper.GetString("apihost")) | ||
|
||
signupRequestPayload := payloads.SignupRequestPayload{FirstName: firstName, LastName: lastName, Email: email, Password: password} | ||
res, err := c.Signup(signupRequestPayload) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
fmt.Printf("user created: %s\n", res.ID) | ||
} | ||
|
||
func init() { | ||
var firstName, lastName, email, password string | ||
signupCmd.Flags().StringVarP(&firstName, "firstName", "f", "Grace", "First name") | ||
signupCmd.Flags().StringVarP(&lastName, "lastName", "l", "Hopper", "Last name") | ||
signupCmd.Flags().StringVarP(&email, "email", "e", "grace@hopper.com", "Email") | ||
signupCmd.Flags().StringVarP(&password, "password", "p", "the_answer_is_42", "Password") | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.