commit fe90b579d280dd7e2b5d7385d788b23c75bacf66
parent a35abd2c240ad3d1ef8f6a6caa90f168646a49e6
Author: Tyler Clark <tyler.clark@foxide.xyz>
Date: Mon, 29 Dec 2025 21:29:15 -0500
Refactoring to break up the codebase a bit. Will likely happen again as this it is still early days for the project.
Diffstat:
8 files changed, 96 insertions(+), 85 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,8 +1,8 @@
ALL:
- go124 build cmd/main.go cmd/buttons.go
+ go124 build cmd/main.go cmd/ui.go cmd/jail-mgr.go
test:
- go124 build cmd/main.go cmd/buttons.go cmd/test.go
+ go124 build cmd/main.go cmd/ui.go cmd/jail-mgr.go cmd/test.go
clean:
rm main
diff --git a/cmd/buttons.go b/cmd/buttons.go
@@ -1,10 +0,0 @@
-package main
-
-import (
- "html/template"
- "net/http"
-)
-func render_ui(w http.ResponseWriter, r *http.Request, j_name string) {
- tmpl := template.Must(template.ParseFiles("static/buttons.html"))
- tmpl.Execute(w, map[string]string{"j_name": j_name})
-}
diff --git a/cmd/jail-mgr.go b/cmd/jail-mgr.go
@@ -0,0 +1,59 @@
+/*
+ * This is the portion of the program that deals with jail management.
+ */
+package main
+
+import (
+ "fmt"
+ "os/exec"
+ "html/template"
+ "net/http"
+)
+
+func get_name(n int) (jail string) {
+ j_names := []string{"ca", "test-jail"}
+ jail = j_names[n]
+ return
+}
+
+func helloHandler(w http.ResponseWriter, r *http.Request) {
+ tmpl := template.Must(template.ParseFiles("static/hello.html"))
+ tmpl.Execute(w, map[string]string{"Name": "HTMX + Go"})
+}
+
+func j_status(w http.ResponseWriter, r *http.Request) {
+ tmpl := template.Must(template.ParseFiles("static/cmd.html"))
+ cmd := exec.Command("jls");
+ output, err := cmd.Output()
+ if err != nil {
+ fmt.Println("Error:", err)
+ return
+ }
+ out := string(output)
+ tmpl.Execute(w, map[string]string{"Output": out})
+}
+
+func j_start(w http.ResponseWriter, r *http.Request, j_name string) {
+ tmpl := template.Must(template.ParseFiles("static/cmd.html"))
+ cmd := exec.Command("doas", "service", "jail", "onestart", j_name);
+ output, err := cmd.Output()
+ if err != nil {
+ fmt.Println("Error:", err)
+ return
+ }
+ out := string(output)
+ tmpl.Execute(w, map[string]string{"Output": out})
+}
+
+func j_stop(w http.ResponseWriter, r *http.Request, j_name string) {
+ tmpl := template.Must(template.ParseFiles("static/cmd.html"))
+ cmd := exec.Command("doas", "service", "jail", "onestop", j_name);
+ output, err := cmd.Output()
+ if err != nil {
+ fmt.Println("Error:", err)
+ return
+ }
+ out := string(output)
+ tmpl.Execute(w, map[string]string{"Output": out})
+}
+
diff --git a/cmd/main.go b/cmd/main.go
@@ -1,78 +1,14 @@
+/*
+ * The 'main' program.
+ */
package main
import (
- "fmt"
- "html/template"
"net/http"
- "os/exec"
)
type naming func(num int) string
-func get_name(n int) (jail string) {
- j_names := []string{"ca", "test-jail"}
- jail = j_names[n]
- return
-}
-
-func helloHandler(w http.ResponseWriter, r *http.Request) {
- tmpl := template.Must(template.ParseFiles("static/hello.html"))
- tmpl.Execute(w, map[string]string{"Name": "HTMX + Go"})
-}
-
-func j_status(w http.ResponseWriter, r *http.Request) {
- tmpl := template.Must(template.ParseFiles("static/cmd.html"))
- cmd := exec.Command("jls");
- output, err := cmd.Output()
- if err != nil {
- fmt.Println("Error:", err)
- return
- }
- out := string(output)
- tmpl.Execute(w, map[string]string{"Output": out})
-}
-
-func j_start(w http.ResponseWriter, r *http.Request, j_name string) {
- tmpl := template.Must(template.ParseFiles("static/cmd.html"))
- cmd := exec.Command("doas", "service", "jail", "onestart", j_name);
- output, err := cmd.Output()
- if err != nil {
- fmt.Println("Error:", err)
- return
- }
- out := string(output)
- tmpl.Execute(w, map[string]string{"Output": out})
-}
-
-func j_stop(w http.ResponseWriter, r *http.Request, j_name string) {
- tmpl := template.Must(template.ParseFiles("static/cmd.html"))
- cmd := exec.Command("doas", "service", "jail", "onestop", j_name);
- output, err := cmd.Output()
- if err != nil {
- fmt.Println("Error:", err)
- return
- }
- out := string(output)
- tmpl.Execute(w, map[string]string{"Output": out})
-}
-
-/* Example function to be able to pass arguments through */
-/* The web server handler */
-func handler(w http.ResponseWriter, r *http.Request, random string) {
- tmpl := template.Must(template.ParseFiles("static/hello.html"))
- tmpl.Execute(w, map[string]string{"Name": random})
-}
-
-/* This function handles rendering the buttons for the *
-* various jails on the system, will be extented for *
-* VMs as well. */
-/*
-func render_ui(w http.ResponseWriter, r *http.Request, j_name string) {
- tmpl := template.Must(template.ParseFiles("static/buttons.html"))
- tmpl.Execute(w, map[string]string{"j_name": j_name})
-}
-*/
-
func main() {
//test := "Tyler"
@@ -95,15 +31,15 @@ func main() {
/* Build the UI */
http.HandleFunc("/buttons", func(w http.ResponseWriter, r *http.Request) {
- render_ui(w, r, jail_name[0])
- render_ui(w, r, jail_name[1])
+ for i:=0; i<len(jail_name); i++ {
+ render_ui(w, r, jail_name[i])
+ }
})
/*
* Commenting this for a test, the below one does work,
* but needs improvement
*
- * http.Handle("/", http.FileServer(http.Dir("./static")))
*/
/* Function for rendering the landing page */
http.Handle("/", http.FileServer(http.Dir("./static")))
diff --git a/cmd/test.go b/cmd/test.go
@@ -0,0 +1,2 @@
+/* This file exists purley to test out concepts in a way that is easy to remove */
+package main
diff --git a/cmd/ui.go b/cmd/ui.go
@@ -0,0 +1,25 @@
+/*
+ * Rendering UI elements for the application.
+ */
+package main
+
+import (
+ "html/template"
+ "net/http"
+)
+
+/*
+ * This function handles rendering the buttons for the
+ * various jails, and in future VMs on the system.
+ */
+func render_ui(w http.ResponseWriter, r *http.Request, j_name string) {
+ tmpl := template.Must(template.ParseFiles("static/buttons.html"))
+ tmpl.Execute(w, map[string]string{"j_name": j_name})
+}
+
+/* Example function to be able to pass arguments through */
+/* The web server handler */
+func handler(w http.ResponseWriter, r *http.Request, random string) {
+ tmpl := template.Must(template.ParseFiles("static/hello.html"))
+ tmpl.Execute(w, map[string]string{"Name": random})
+}
diff --git a/static/buttons.html b/static/buttons.html
@@ -1,4 +1,4 @@
{{ block "genButton" . }}
-<button hx-get="/start" hx-target="#result" hx-swap="innerHTML">Start {{ .j_name }}</button><br />
+<button hx-get="/start" hx-target="#result" hx-swap="innerHTML">Start {{ .j_name }}</button>
<button hx-get="/stop" hx-target="#result" hx-swap="innerHTML">Stop {{ .j_name }}</button><br />
{{end}}
diff --git a/static/index.html b/static/index.html
@@ -6,7 +6,7 @@
<body>
<h1>HTMX + Go Demo</h1>
<div id="buttons"></div>
- <button hx-get="/test" hx-target="#result" hx-swap="innerHTML">Test Button</button>
+ <button hx-get="/test" hx-target="#result" hx-swap="innerHTML">Test Button</button><br />
<!--
Probably don't need these buttons anymore
<button hx-get="/start" hx-target="#result" hx-swap="innerHTML">Start Jail</button><br />
@@ -15,7 +15,6 @@
<button hx-get="/status" hx-target="#result" hx-swap="innerHTML">Jail Status</button><br />
<button hx-get="/buttons" hx-target="#result" hx-swap "innerHTML">Generate Button</button><br />
<div id="result">
- {{ template "genButtons" . }}
</div>
</body>
</html>