-
Notifications
You must be signed in to change notification settings - Fork 0
/
micronaut_detector.go
99 lines (88 loc) · 2.74 KB
/
micronaut_detector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*******************************************************************************
* Copyright (c) 2021 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat, Inc.
******************************************************************************/
package enricher
import (
"context"
"os"
"github.com/mike-hoang/alizer-copy/go/pkg/apis/model"
"github.com/mike-hoang/alizer-copy/go/pkg/utils"
"gopkg.in/yaml.v3"
)
type MicronautDetector struct{}
type MicronautApplicationProps struct {
Micronaut struct {
Server struct {
Port int `yaml:"port,omitempty"`
SSL struct {
Enabled bool `yaml:"enabled,omitempty"`
Port int `yaml:"port,omitempty"`
} `yaml:"ssl,omitempty"`
} `yaml:"server,omitempty"`
} `yaml:"micronaut,omitempty"`
}
func (m MicronautDetector) GetSupportedFrameworks() []string {
return []string{"Micronaut"}
}
// DoFrameworkDetection uses the groupId to check for the framework name
func (m MicronautDetector) DoFrameworkDetection(language *model.Language, config string) {
if hasFwk, _ := hasFramework(config, "io.micronaut", ""); hasFwk {
language.Frameworks = append(language.Frameworks, "Micronaut")
}
}
// DoPortsDetection searches for the port in src/main/resources/application.yaml
func (m MicronautDetector) DoPortsDetection(component *model.Component, ctx *context.Context) {
// check if port is set on env var
ports := getMicronautPortsFromEnvs()
if len(ports) > 0 {
component.Ports = ports
return
}
bytes, err := utils.ReadAnyApplicationFile(component.Path, []model.ApplicationFileInfo{
{
Dir: "src/main/resources",
File: "application.yml",
},
{
Dir: "src/main/resources",
File: "application.yaml",
},
}, ctx)
if err != nil {
return
}
ports = getMicronautPortsFromBytes(bytes)
if len(ports) > 0 {
component.Ports = ports
}
}
func getMicronautPortsFromBytes(bytes []byte) []int {
var ports []int
var data MicronautApplicationProps
err := yaml.Unmarshal(bytes, &data)
if err != nil {
return []int{}
}
if data.Micronaut.Server.SSL.Enabled && utils.IsValidPort(data.Micronaut.Server.SSL.Port) {
ports = append(ports, data.Micronaut.Server.SSL.Port)
}
if utils.IsValidPort(data.Micronaut.Server.Port) {
ports = append(ports, data.Micronaut.Server.Port)
}
return ports
}
func getMicronautPortsFromEnvs() []int {
sslEnabled := os.Getenv("MICRONAUT_SERVER_SSL_ENABLED")
envs := []string{"MICRONAUT_SERVER_PORT"}
if sslEnabled == "true" {
envs = append(envs, "MICRONAUT_SERVER_SSL_PORT")
}
return utils.GetValidPortsFromEnvs(envs)
}