Skip to content

Commit

Permalink
improving the api backend
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickLaabs committed Sep 17, 2024
1 parent 2bbc376 commit c47bb2d
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 84 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* Just

## Get started

Start the API Backend:
```shell
eros server start
Expand All @@ -17,6 +16,9 @@ Start the Frontend Server:
eros frontend start
```

You can now either open up the frontend application via `http://localhost:8080`
or do direct queries against the api, like ` curl localhost:3000/kubernetes/local/create`.

## Documentation
### Backend API
I am using the Swagger Tooling to generate the needed API documentations.
Expand Down
17 changes: 6 additions & 11 deletions api/middleware/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,19 @@ Copyright © 2024 Patrick Laabs patrick.laabs@me.com
package middleware

import (
"github.com/PatrickLaabs/eros/api/routes"
_ "github.com/PatrickLaabs/eros/docs"
"net/http"
)

func Router(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Extract the URL path
path := r.URL.Path
w.Header().Set("Access-Control-Allow-Origin", "*") // Allows all origins; adjust as needed
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")

// Implement routing logic based on the URL path
switch path {
case "/version":
routes.Version(w, r)
case "/kubernetes":
routes.Kubernetes(w, r)
default:
http.Error(w, "Not found", http.StatusNotFound)
// Handle preflight requests
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusOK)
return
}

Expand Down
5 changes: 3 additions & 2 deletions api/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ var (

func Start() {
mux := http.NewServeMux()
mux.Handle("/", middleware.Router(mux))
routes.RegisterRoutes(mux)

handler := middleware.Router(mux)

log.Printf("Running server on port %v", *backendAddr)
log.Fatal(http.ListenAndServe(*backendAddr, mux))
log.Fatal(http.ListenAndServe(*backendAddr, handler))
}
70 changes: 34 additions & 36 deletions coverage/coverage.out
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
mode: set
github.com/PatrickLaabs/eros/api/start.go:35.14,42.2 5 0
github.com/PatrickLaabs/eros/api/start.go:35.14,43.2 5 0
github.com/PatrickLaabs/eros/main.go:9.13,11.2 1 0
github.com/PatrickLaabs/eros/api/middleware/router.go:13.45,14.71 1 0
github.com/PatrickLaabs/eros/api/middleware/router.go:14.71,19.15 2 0
github.com/PatrickLaabs/eros/api/middleware/router.go:20.19,21.24 1 0
github.com/PatrickLaabs/eros/api/middleware/router.go:22.22,23.27 1 0
github.com/PatrickLaabs/eros/api/middleware/router.go:24.11,27.10 2 0
github.com/PatrickLaabs/eros/api/middleware/router.go:31.3,31.23 1 0
github.com/PatrickLaabs/eros/cmd/backendserver/server.go:18.47,20.3 1 0
github.com/PatrickLaabs/eros/cmd/backendserver/server.go:23.13,26.2 2 0
github.com/PatrickLaabs/eros/cmd/backendserver/start.go:17.47,26.3 2 0
github.com/PatrickLaabs/eros/cmd/backendserver/stop.go:16.47,24.3 1 0
github.com/PatrickLaabs/eros/cmd/root.go:22.16,24.16 2 0
github.com/PatrickLaabs/eros/cmd/root.go:24.16,26.3 1 0
github.com/PatrickLaabs/eros/cmd/root.go:29.13,32.2 2 0
github.com/PatrickLaabs/eros/pkg/testhelper/testhelper.go:14.59,17.2 2 0
github.com/PatrickLaabs/eros/pkg/testhelper/testhelper.go:19.48,21.17 2 0
github.com/PatrickLaabs/eros/pkg/testhelper/testhelper.go:21.17,23.3 1 0
github.com/PatrickLaabs/eros/pkg/testhelper/testhelper.go:26.57,28.17 2 0
github.com/PatrickLaabs/eros/pkg/testhelper/testhelper.go:28.17,30.3 1 0
github.com/PatrickLaabs/eros/api/middleware/router.go:12.45,13.71 1 0
github.com/PatrickLaabs/eros/api/middleware/router.go:13.71,19.37 4 0
github.com/PatrickLaabs/eros/api/middleware/router.go:19.37,22.4 2 0
github.com/PatrickLaabs/eros/api/middleware/router.go:25.3,25.23 1 0
github.com/PatrickLaabs/eros/docs/docs.go:43.13,45.2 1 0
github.com/PatrickLaabs/eros/cmd/frontendserver/server.go:18.47,20.3 1 0
github.com/PatrickLaabs/eros/cmd/frontendserver/server.go:23.13,26.2 2 0
github.com/PatrickLaabs/eros/cmd/frontendserver/start.go:16.47,18.3 1 0
github.com/PatrickLaabs/eros/cmd/frontendserver/stop.go:15.48,17.3 0 0
github.com/PatrickLaabs/eros/docs/docs.go:43.13,45.2 1 0
github.com/PatrickLaabs/eros/cmd/root.go:22.16,24.16 2 0
github.com/PatrickLaabs/eros/cmd/root.go:24.16,26.3 1 0
github.com/PatrickLaabs/eros/cmd/root.go:29.13,32.2 2 0
github.com/PatrickLaabs/eros/cmd/backendserver/server.go:18.47,20.3 1 0
github.com/PatrickLaabs/eros/cmd/backendserver/server.go:23.13,26.2 2 0
github.com/PatrickLaabs/eros/cmd/backendserver/start.go:17.47,26.3 2 0
github.com/PatrickLaabs/eros/cmd/backendserver/stop.go:16.47,24.3 1 0
github.com/PatrickLaabs/eros/frontend/views/footer_templ.go:19.31,20.131 1 0
github.com/PatrickLaabs/eros/frontend/views/footer_templ.go:20.131,23.31 3 0
github.com/PatrickLaabs/eros/frontend/views/footer_templ.go:23.31,24.17 1 0
Expand Down Expand Up @@ -62,22 +55,11 @@ github.com/PatrickLaabs/eros/frontend/views/landing_templ.go:50.32,52.4 1 0
github.com/PatrickLaabs/eros/frontend/views/landing_templ.go:53.3,54.32 2 0
github.com/PatrickLaabs/eros/frontend/views/landing_templ.go:54.32,56.4 1 0
github.com/PatrickLaabs/eros/frontend/views/landing_templ.go:57.3,57.28 1 0
github.com/PatrickLaabs/eros/pkg/preflight/checks.go:7.16,7.17 0 0
github.com/PatrickLaabs/eros/pkg/kind/cofiggen.go:12.25,46.16 3 1
github.com/PatrickLaabs/eros/pkg/kind/cofiggen.go:46.16,48.3 1 0
github.com/PatrickLaabs/eros/pkg/kind/cofiggen.go:50.2,50.17 1 1
github.com/PatrickLaabs/eros/pkg/kind/create.go:14.73,19.16 2 0
github.com/PatrickLaabs/eros/pkg/kind/create.go:19.16,21.3 1 0
github.com/PatrickLaabs/eros/pkg/kind/create.go:23.2,25.16 3 0
github.com/PatrickLaabs/eros/pkg/kind/create.go:25.16,27.3 1 0
github.com/PatrickLaabs/eros/pkg/kind/delete.go:12.45,15.16 3 0
github.com/PatrickLaabs/eros/pkg/kind/delete.go:15.16,18.3 2 0
github.com/PatrickLaabs/eros/pkg/kind/delete.go:19.2,19.12 1 0
github.com/PatrickLaabs/eros/pkg/kind/getclusters.go:14.58,18.16 3 0
github.com/PatrickLaabs/eros/pkg/kind/getclusters.go:18.16,20.3 1 0
github.com/PatrickLaabs/eros/pkg/kind/getclusters.go:23.2,25.16 3 0
github.com/PatrickLaabs/eros/pkg/kind/getclusters.go:25.16,27.3 1 0
github.com/PatrickLaabs/eros/pkg/kind/getclusters.go:28.2,28.34 1 0
github.com/PatrickLaabs/eros/pkg/testhelper/testhelper.go:14.59,17.2 2 0
github.com/PatrickLaabs/eros/pkg/testhelper/testhelper.go:19.48,21.17 2 0
github.com/PatrickLaabs/eros/pkg/testhelper/testhelper.go:21.17,23.3 1 0
github.com/PatrickLaabs/eros/pkg/testhelper/testhelper.go:26.57,28.17 2 0
github.com/PatrickLaabs/eros/pkg/testhelper/testhelper.go:28.17,30.3 1 0
github.com/PatrickLaabs/eros/frontend/start.go:27.14,36.2 6 0
github.com/PatrickLaabs/eros/frontend/views/footer_templ.go:19.31,20.131 1 0
github.com/PatrickLaabs/eros/frontend/views/footer_templ.go:20.131,23.31 3 0
Expand Down Expand Up @@ -117,6 +99,22 @@ github.com/PatrickLaabs/eros/frontend/views/landing_templ.go:50.32,52.4 1 0
github.com/PatrickLaabs/eros/frontend/views/landing_templ.go:53.3,54.32 2 0
github.com/PatrickLaabs/eros/frontend/views/landing_templ.go:54.32,56.4 1 0
github.com/PatrickLaabs/eros/frontend/views/landing_templ.go:57.3,57.28 1 0
github.com/PatrickLaabs/eros/pkg/preflight/checks.go:7.16,7.17 0 0
github.com/PatrickLaabs/eros/pkg/kind/cofiggen.go:12.25,46.16 3 1
github.com/PatrickLaabs/eros/pkg/kind/cofiggen.go:46.16,48.3 1 0
github.com/PatrickLaabs/eros/pkg/kind/cofiggen.go:50.2,50.17 1 1
github.com/PatrickLaabs/eros/pkg/kind/create.go:14.73,19.16 2 0
github.com/PatrickLaabs/eros/pkg/kind/create.go:19.16,21.3 1 0
github.com/PatrickLaabs/eros/pkg/kind/create.go:23.2,25.16 3 0
github.com/PatrickLaabs/eros/pkg/kind/create.go:25.16,27.3 1 0
github.com/PatrickLaabs/eros/pkg/kind/delete.go:12.45,15.16 3 0
github.com/PatrickLaabs/eros/pkg/kind/delete.go:15.16,18.3 2 0
github.com/PatrickLaabs/eros/pkg/kind/delete.go:19.2,19.12 1 0
github.com/PatrickLaabs/eros/pkg/kind/getclusters.go:14.58,18.16 3 0
github.com/PatrickLaabs/eros/pkg/kind/getclusters.go:18.16,20.3 1 0
github.com/PatrickLaabs/eros/pkg/kind/getclusters.go:23.2,25.16 3 0
github.com/PatrickLaabs/eros/pkg/kind/getclusters.go:25.16,27.3 1 0
github.com/PatrickLaabs/eros/pkg/kind/getclusters.go:28.2,28.34 1 0
github.com/PatrickLaabs/eros/api/routes/kubernetes.go:17.57,21.18 3 1
github.com/PatrickLaabs/eros/api/routes/kubernetes.go:21.18,24.3 2 1
github.com/PatrickLaabs/eros/api/routes/kubernetes.go:27.2,27.16 1 1
Expand Down
16 changes: 15 additions & 1 deletion frontend/views/landing.templ
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ templ LandingPage() {
<head>
<title>eros</title>
<link rel="stylesheet" href="/assets/style.css" />
<script>
function fetchVersion() {
fetch('http://localhost:3000/version')
.then(response => response.json())
.then(data => {
document.getElementById('version').textContent = 'API Version: ' + data;
})
.catch(error => {
console.error('Error fetching version:', error);
document.getElementById('version').textContent = 'Failed to fetch version.';
});
}
</script>
</head>
<body>
<div class="page-wrapper">
Expand All @@ -17,7 +30,8 @@ templ LandingPage() {
<div class="main-content">
<h1>Welcome to eros</h1>
<p>frontend app for eros backend api</p>
<button onclick="window.location.href='http://localhost:3000/version'">Click me!</button>
<button onclick="fetchVersion()">Click me!</button>
<p id="version"></p>
</div>
</main>
</div>
Expand Down
4 changes: 2 additions & 2 deletions frontend/views/landing_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 0 additions & 31 deletions pkg/testhelper/testhelper.go

This file was deleted.

0 comments on commit c47bb2d

Please sign in to comment.