Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic landing page #48

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ services:
- STORAGE_LOCATION=/app/files
volumes:
- scratch:/app/files
- /var/run/docker.sock:/var/run/docker.sock

mariadb-10-5:
image: uselagoon/mariadb-10.5:latest
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/uselagoon/machinery v0.0.29
github.com/vanng822/go-solr v0.10.0
go.mongodb.org/mongo-driver v1.16.1
gopkg.in/yaml.v3 v3.0.1
)

require (
Expand All @@ -26,5 +27,5 @@ require (
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/text v0.15.0 // indirect
)
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,13 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
28 changes: 28 additions & 0 deletions landingTemplate.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!-- template.html -->
<!DOCTYPE html>
<html>
<head>
<title>Internal Services Test</title>
</head>
<body style="background: black;font-family: monospace;color: white;">
<h1>Internal Services Test</h1>
<p>This project was designed to perform a functional test on a given native Lagoon service.</p>
<p>It works by:</p>
<ul>
<li>Receiving a request from the Lagoon service</li>
<li>Performing a test on the service</li>
<li>Returning the result of the test</li>
</ul>
<p>To check a service, use this format http://0.0.0.0:3000/{service-type}?service={service-name}</p>
<h2>Available Services:</h2>
<ul>
{{range .Services}}
{{if (eq .Type "storage")}}
<li><a href="/{{.Type}}?path={{.Name}}">storage:{{.Name}}</a></li>
{{else}}
<li><a href="/{{.Type}}?service={{.Name}}">{{.Name}}</a></li>
{{end}}
{{end}}
</ul>
</body>
</html>
78 changes: 74 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,25 @@ import (
"bytes"
"fmt"
"github.com/gorilla/mux"
machinery "github.com/uselagoon/machinery/utils/variables"
"gopkg.in/yaml.v3"
"html/template"
"log"
"net/http"
"os"
"regexp"
"strings"
"time"
)

type funcType func() map[string]string
type LandingPageData struct {
Services []Service
}

type Service struct {
Type string
Name string
}

func main() {
r := mux.NewRouter()
Expand All @@ -28,6 +40,53 @@ func main() {
log.Fatal(http.ListenAndServe(":3000", handler(r)))
}

// getServices Parses docker-compose.yml and returns a list of services
func getServices() []Service {
type BuildConfig struct {
Context string `yaml:"context,omitempty"`
Dockerfile string `yaml:"dockerfile,omitempty"`
}

type ServiceConfig struct {
Image string `yaml:"image,omitempty"`
Labels map[string]string `yaml:"labels,omitempty"`
Build BuildConfig `yaml:"build,omitempty"`
Ports []string `yaml:"ports,omitempty"`
Volumes []string `yaml:"volumes,omitempty"`
}

type DockerCompose struct {
Services map[string]ServiceConfig `yaml:"services"`
Volumes map[string]interface{} `yaml:"volumes,omitempty"`
}

compose := &DockerCompose{}
data, err := os.ReadFile("docker-compose.yml")
if err != nil {
log.Fatalf("Error reading docker-compose.yml: %v", err)
}

err = yaml.Unmarshal(data, compose)
if err != nil {
log.Fatalf("Error parsing docker-compose.yml: %v", err)
}

var serviceList []Service
typeRegexp := regexp.MustCompile(`^\w*`)
for service := range compose.Services {
if service != "web" {
serviceType := typeRegexp.FindString(service)
serviceList = append(serviceList, Service{Type: serviceType, Name: service})
}
}
storage := machinery.GetEnv("STORAGE_LOCATION", "")
if storage != "" {
serviceList = append(serviceList, Service{Type: "storage", Name: storage})
}

return serviceList
}

func handler(m *mux.Router) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
driver := strings.ReplaceAll(r.URL.Path, "/", "")
Expand All @@ -44,9 +103,20 @@ func handler(m *mux.Router) http.HandlerFunc {
}

func handleReq(w http.ResponseWriter, r *http.Request) {
var funcToCall []funcType
for _, conFunc := range funcToCall {
fmt.Fprintf(w, dbConnectorPairs(conFunc(), ""))
tmpl, err := template.ParseFiles("landingTemplate.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

serviceList := getServices()
data := LandingPageData{
Services: serviceList,
}

err = tmpl.Execute(w, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}

Expand Down
Loading