-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from B-urb/development
Development
- Loading branch information
Showing
33 changed files
with
756 additions
and
171 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -47,7 +47,7 @@ jobs: | |
run: go get ./... | ||
- name: Build | ||
working-directory: backend | ||
run: go build -o build/kubevoyage ./cmd/kubevoyage | ||
run: GOOS=linux GOARCH=amd64 go build -o build/kubevoyage ./cmd/kubevoyage | ||
#- name: Test with the Go CLI | ||
# run: go test | ||
- name: Archive production artifacts | ||
|
@@ -86,7 +86,7 @@ jobs: | |
push: true | ||
tags: ${{ secrets.DOCKERHUB_USERNAME }}/kubevoyage:${{ env.BRANCH_NAME }} | ||
release: | ||
needs: build-docker | ||
needs: helm-release | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: write | ||
|
@@ -108,4 +108,35 @@ jobs: | |
- name: Release | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.PAT }} | ||
run: npx semantic-release --debug | ||
run: npx semantic-release --debug | ||
helm-release: | ||
needs: build-docker | ||
# depending on default permission settings for your org (contents being read-only or read-write for workloads), you will have to add permissions | ||
# see: https://docs.github.com/en/actions/security-guides/automatic-token-authentication#modifying-the-permissions-for-the-github_token | ||
permissions: | ||
contents: write | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Configure Git | ||
run: | | ||
git config user.name "$GITHUB_ACTOR" | ||
git config user.email "[email protected]" | ||
- name: Install Helm | ||
uses: azure/setup-helm@v3 | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.PAT }} | ||
|
||
- name: Run chart-releaser | ||
uses: helm/[email protected] | ||
with: | ||
charts_dir: deploy/ | ||
env: | ||
registryImage: ${{ secrets.DOCKERHUB_USERNAME }}/kubevoyage | ||
imageTag: ${{ github.head_ref || github.ref_name }} | ||
CR_TOKEN: ${{ secrets.PAT }} |
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 |
---|---|---|
|
@@ -41,7 +41,7 @@ jobs: | |
run: go get ./... | ||
- name: Build | ||
working-directory: backend | ||
run: go build -o build/kubevoyage ./cmd/kubevoyage | ||
run: GOOS=linux GOARCH=amd64 go build -o build/kubevoyage ./cmd/kubevoyage | ||
#- name: Test with the Go CLI | ||
# run: go test | ||
- name: Archive production artifacts | ||
|
@@ -79,7 +79,8 @@ jobs: | |
push: true | ||
tags: ${{ secrets.DOCKERHUB_USERNAME }}/kubevoyage:${{ env.BRANCH_NAME }} | ||
|
||
release: | ||
helm-release: | ||
needs: build-docker | ||
# depending on default permission settings for your org (contents being read-only or read-write for workloads), you will have to add permissions | ||
# see: https://docs.github.com/en/actions/security-guides/automatic-token-authentication#modifying-the-permissions-for-the-github_token | ||
permissions: | ||
|
@@ -98,6 +99,8 @@ jobs: | |
- name: Install Helm | ||
uses: azure/setup-helm@v3 | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.PAT }} | ||
|
||
- name: Run chart-releaser | ||
uses: helm/[email protected] | ||
|
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
.idea | ||
*.db | ||
build | ||
build | ||
|
||
deploy/charts/values-local.yaml |
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
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,109 +1,135 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/B-Urb/KubeVoyage/internal/app" | ||
"github.com/B-Urb/KubeVoyage/internal/handlers" | ||
"github.com/B-Urb/KubeVoyage/internal/models" | ||
"github.com/rs/cors" | ||
"gorm.io/driver/sqlite" | ||
"gorm.io/gorm" | ||
"log" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
// or "gorm.io/driver/postgres" for PostgreSQL | ||
) | ||
|
||
var db *gorm.DB | ||
|
||
var frontendPathLocal = "./public" //"../frontend/public" | ||
|
||
type loggingResponseWriter struct { | ||
http.ResponseWriter | ||
statusCode int | ||
length int | ||
} | ||
|
||
func (lrw *loggingResponseWriter) Write(b []byte) (int, error) { | ||
if lrw.statusCode == 0 { | ||
// Default status code is 200 OK. | ||
lrw.statusCode = http.StatusOK | ||
} | ||
size, err := lrw.ResponseWriter.Write(b) | ||
lrw.length += size | ||
return size, err | ||
} | ||
|
||
func (lrw *loggingResponseWriter) WriteHeader(statusCode int) { | ||
lrw.statusCode = statusCode | ||
lrw.ResponseWriter.WriteHeader(statusCode) | ||
} | ||
func logMiddleware(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
lrw := &loggingResponseWriter{ResponseWriter: w} | ||
start := time.Now() | ||
|
||
next.ServeHTTP(lrw, r) | ||
|
||
duration := time.Since(start) | ||
log.Printf( | ||
"Method: %s, Path: %s, RemoteAddr: %s, Duration: %s, StatusCode: %d, ResponseSize: %d bytes\n", | ||
r.Method, | ||
r.URL.Path, | ||
r.RemoteAddr, | ||
duration, | ||
lrw.statusCode, | ||
lrw.length, | ||
) | ||
}) | ||
} | ||
func main() { | ||
var err error | ||
db, err = gorm.Open(sqlite.Open("test.db"), &gorm.Config{}) | ||
app, err := application.NewApp() // Assuming NewApp is in the same package | ||
if err != nil { | ||
panic("failed to connect database") | ||
log.Fatalf(err.Error()) | ||
} | ||
|
||
handler := handlers.NewHandler(app.DB) | ||
err = app.Init() | ||
if err != nil { | ||
log.Fatalf(err.Error()) | ||
} | ||
mux := http.NewServeMux() | ||
|
||
// Migrate the schema | ||
db.AutoMigrate(&models.User{}, &models.Site{}, &models.UserSite{}) | ||
//generateTestData() | ||
mux := setupServer(handler) | ||
|
||
log.Println("Starting server on :8080") | ||
log.Fatal(http.ListenAndServe(":8080", mux)) | ||
} | ||
func setupServer(handle *handlers.Handler) http.Handler { | ||
mux := http.NewServeMux() | ||
|
||
handler := cors.Default().Handler(mux) | ||
|
||
// Serve static files | ||
fs := http.FileServer(http.Dir("../frontend/public/")) // Adjust the path based on your directory structure | ||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
fs := http.FileServer(http.Dir(frontendPathLocal)) // Adjust the path based on your directory structure | ||
mux.Handle("/", logMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
// Check if it's an API route first | ||
if isAPIRoute(r.URL.Path) { | ||
// Handle API routes separately | ||
return | ||
} | ||
|
||
path := "../frontend/public" + r.URL.Path | ||
log.Println(path) | ||
_, err := os.Stat(path) | ||
path := frontendPathLocal + r.URL.Path | ||
absolutePath, err := filepath.Abs(path) | ||
if err != nil { | ||
fmt.Println("Error getting absolute path:", err) | ||
return | ||
} | ||
fmt.Println("Absolute Path:", absolutePath) | ||
_, err = os.Stat(path) | ||
|
||
// If the file exists, serve it | ||
if !os.IsNotExist(err) { | ||
fs.ServeHTTP(w, r) | ||
return | ||
} else { | ||
log.Println(err) | ||
} | ||
|
||
// Otherwise, serve index.html | ||
http.ServeFile(w, r, "../frontend/public/index.html") | ||
}) | ||
http.ServeFile(w, r, frontendPathLocal+"/index.html") | ||
}))) | ||
|
||
mux.HandleFunc("/api/requests", func(w http.ResponseWriter, r *http.Request) { | ||
mux.Handle("/api/requests", logMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
handlers.HandleRequests(w, r, db) | ||
}) | ||
mux.HandleFunc("/api/register", func(w http.ResponseWriter, r *http.Request) { | ||
handlers.HandleRegister(w, r, db) | ||
}) | ||
mux.HandleFunc("/api/login", func(w http.ResponseWriter, r *http.Request) { | ||
handlers.HandleLogin(w, r, db) | ||
}) | ||
mux.HandleFunc("/api/authenticate", func(w http.ResponseWriter, r *http.Request) { | ||
handlers.HandleAuthenticate(w, r, db) | ||
}) | ||
mux.HandleFunc("/api/request", func(w http.ResponseWriter, r *http.Request) { | ||
handlers.HandleRequestSite(w, r, db) | ||
}) | ||
// Start the server on port 8081 | ||
log.Println("Starting server on :8080") | ||
|
||
log.Fatal(http.ListenAndServe(":8080", handler)) | ||
}))) | ||
mux.Handle("/api/register", logMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
handle.HandleRegister(w, r) | ||
}))) | ||
mux.Handle("/api/login", logMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
handle.HandleLogin(w, r) | ||
}))) | ||
mux.Handle("/api/authenticate", logMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
handle.HandleAuthenticate(w, r) | ||
}))) | ||
mux.Handle("/api/redirect", logMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
handle.HandleRedirect(w, r) | ||
}))) | ||
mux.Handle("/api/request", logMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
handle.HandleRequestSite(w, r, db) | ||
}))) | ||
|
||
// ... setup your routes and start your server | ||
return handler | ||
} | ||
|
||
func isAPIRoute(path string) bool { | ||
return len(path) >= 4 && path[0:4] == "/api" | ||
} | ||
func generateTestData() { | ||
// Insert test data for Users | ||
users := []models.User{ | ||
{Email: "[email protected]", Password: "password1", Role: "admin"}, | ||
{Email: "[email protected]", Password: "password2", Role: "user"}, | ||
{Email: "[email protected]", Password: "password3", Role: "user"}, | ||
} | ||
for _, user := range users { | ||
db.Create(&user) | ||
} | ||
|
||
// Insert test data for Sites | ||
sites := []models.Site{ | ||
{URL: "https://site1.com"}, | ||
{URL: "https://site2.com"}, | ||
{URL: "https://site3.com"}, | ||
} | ||
for _, site := range sites { | ||
db.Create(&site) | ||
} | ||
|
||
// Insert test data for UserSite | ||
userSites := []models.UserSite{ | ||
{UserID: 1, SiteID: 1, State: "authorized"}, | ||
{UserID: 2, SiteID: 2, State: "requested"}, | ||
{UserID: 3, SiteID: 3, State: "authorized"}, | ||
} | ||
for _, userSite := range userSites { | ||
db.Create(&userSite) | ||
} | ||
} |
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
Oops, something went wrong.