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

Vue 3 migration #109

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Embed static files using go embed pkg
  • Loading branch information
amimof committed Jun 13, 2024
commit da16840efc276ffaf02fc90614c803111c2fc45e
10 changes: 5 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
FROM golang:1.13-alpine AS go-build
FROM golang:1.21-alpine AS go-build
COPY . /build
WORKDIR /build/
RUN apk add --no-cache --update git make ca-certificates \
&& make

FROM node:10 AS npm-build
COPY web/ /build
FROM node:20 AS npm-build
COPY web2/ /build
WORKDIR /build/
RUN npm install \
&& NODE_ENV=production npm run build
Expand All @@ -14,5 +14,5 @@ FROM scratch
LABEL maintaner="@amimof ([email protected])"
COPY --from=go-build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=go-build /build/bin/* /
COPY --from=npm-build /build/dist /web/dist
ENTRYPOINT ["/logga"]
COPY --from=npm-build /build/dist /web2/dist
ENTRYPOINT ["/logga"]
10 changes: 6 additions & 4 deletions cmd/logga/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package main

import (
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"os"
"time"

ui "github.com/amimof/logga"
"github.com/amimof/logga/pkg/api"
"github.com/amimof/logga/pkg/server"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -41,7 +42,7 @@ func init() {
pflag.StringVar(&host, "host", "0.0.0.0", "The host address on which to listen for the --port port")
pflag.StringVar(&kubeconfigPath, "kubeconfig", fmt.Sprintf("%s%s", os.Getenv("HOME"), "/.kube/config"), "Absolute path to a kubeconfig file")
pflag.IntVar(&port, "port", 8080, "the port to listen on for insecure connections, defaults to 8080")
klog.SetOutput(ioutil.Discard) // Tell klog, which is used by client-go to log into /dev/null instead of file
klog.SetOutput(io.Discard) // Tell klog, which is used by client-go to log into /dev/null instead of file
}

func main() {
Expand Down Expand Up @@ -113,8 +114,9 @@ func main() {
r.Path("/api/namespaces/{namespace}/pods/{pod}/log").
HandlerFunc(a.GetPodLog)

// Serve the UI
r.PathPrefix("/").Handler(http.FileServer(http.Dir("/web2/dist/")))
// Serve the UI
uiHandler := ui.Handler{FS: ui.StaticFiles, RootDir: "web2/dist"}
r.PathPrefix("/").Handler(uiHandler)

s := server.NewServer()
s.Port = port
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/amimof/logga

go 1.19
go 1.21

require (
github.com/go-openapi/swag v0.19.9
Expand Down
43 changes: 43 additions & 0 deletions ui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package ui

import (
"embed"
"io/fs"
"net/http"
"os"
"path/filepath"
)

//go:embed all:web2/dist
var StaticFiles embed.FS

type Handler struct {
FS embed.FS
RootDir string
}

func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
dist := h.RootDir
f, err := h.FS.Open(filepath.Join(dist, r.URL.Path))
if os.IsNotExist(err) {
index, err := h.FS.ReadFile(filepath.Join(dist, "index.html"))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusAccepted)
w.Write(index)
return
} else if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer f.Close()

sub, err := fs.Sub(h.FS, dist)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}

http.FileServer(http.FS(sub)).ServeHTTP(w, r)
}
2 changes: 1 addition & 1 deletion web2/.env.development
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VUE_APP_API_URL=http://127.0.0.1:8080
VITE_API_URL=http://127.0.0.1:8080
2 changes: 1 addition & 1 deletion web2/.env.production
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VUE_APP_API_URL=""
VITE_API_URL=""
8 changes: 4 additions & 4 deletions web2/index.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="icon" href="/favicon.ico">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vite App</title>
<meta charset="UTF-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Logga</title>
</head>
<body>
<div id="app"></div>
Expand Down
24 changes: 24 additions & 0 deletions web2/src/components/NotFoundComponent.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<script setup>
import { defineProps } from 'vue';

const props = defineProps({
msg: {
type: String,
default() {
return "Ooops! Page not found";
}
}
})

</script>

<template>
<div class="container">
<div class="alert alert-warning" role="alert">
{{ msg }}
</div>
</div>
</template>

<style scoped>
</style>
12 changes: 10 additions & 2 deletions web2/src/router/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { createRouter, createWebHistory } from "vue-router";
// import NotFoundComponent from '../components/NotFoundComponent.vue'
import NotFoundComponent from "../components/NotFoundComponent.vue";
import Namespaces from "../components/Namespaces.vue";
import Pods from "../components/Pods.vue";
import Pod from "../components/Pod.vue";

const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
// history: createWebHistory(import.meta.env.BASE_URL),
history: createWebHistory(),
routes: [
{
path: "/",
Expand All @@ -26,7 +27,14 @@ const router = createRouter({
name: "Pod",
component: Pod,
},
{
path: "/:pathMatch(.*)*",
name: "not-found",
component: NotFoundComponent,
},
],
});

console.log(import.meta.env.BASE_URL);

export default router;
3 changes: 2 additions & 1 deletion web2/src/services/namespaces.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const apiUrl = "http://127.0.0.1:8080/api";
// const apiUrl = "http://127.0.0.1:8080/api";
const apiUrl = `${import.meta.env.VITE_API_URL}/api`;

async function getNamespace(namespace) {
return await fetch(`${apiUrl}/namespaces/${namespace}`);
Expand Down
3 changes: 2 additions & 1 deletion web2/src/services/pods.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const apiUrl = "http://127.0.0.1:8080/api";
// const apiUrl = "http://127.0.0.1:8080/api";
const apiUrl = `${import.meta.env.VITE_API_URL}/api`;

async function getPod(namespace, pod) {
return await fetch(`${apiUrl}/namespaces/${namespace}/pods/${pod}`);
Expand Down