small pixel drawing of a pufferfish gore

*
Jes Olson j3s@c3f.net
Mon, 09 Dec 2024 14:32:48 -0500
commit

a9b68bc4eafe5a6ca585726943574a12614b3b14

parent

d1ce3d8ee4de88f898180874a7523c1d3c8a9cb2

A .gitignore

@@ -0,0 +1,2 @@

+builddir +pkg
M internal/assets/header.tmplinternal/assets/header.tmpl

@@ -1,6 +1,6 @@

<!DOCTYPE html> <html lang="en"> -<title>{{ .Hostname }} — gore</title> +<title>gore</title> <link rel="stylesheet" href="/assets/bootstrap-3.3.7.min.css" /> <link rel="stylesheet" href="/assets/bootstrap-table-1.11.0.min.css" /> <link rel="icon" href="/assets/favicon.ico" type="image/x-icon" />

@@ -35,18 +35,12 @@ <div class="container-fluid">

<!-- Brand and toggle get grouped for better mobile display --> <div class="navbar-header"> <div style="clear: left;"> - <p style="float: left;"><img src = "assets/gokrazy-logo.svg" alt="the gokrazy logo: a mad gopher" width="70px"/></p> - <p style="width: 50ex; margin-top: 0.25em; font-size: 18px"><a href="/">gokrazy</a><br> - <small style="font-size: 11px" class="text-muted">built at {{ .BuildTimestamp }}</small></p> + <p style="width: 50ex; margin-top: 0.25em; font-size: 18px"><a href="/">gore</a></p> </div> </div> <div class="collapse navbar-collapse" id="navbar-collapse-1"> <table class="navbar-text navbar-right" style="border-spacing: 10px 0; border-collapse: separate"> - <tr> - <th>host</th> - <td>{{ .Hostname }}</td> - </tr> </table> </div><!-- /.navbar-collapse -->
M internal/assets/overview.tmplinternal/assets/overview.tmpl

@@ -3,6 +3,12 @@

<div class="row"> <div class="col-md-12"> +<p>Add a new service:</p> +<form action="/add" method="POST"> + <input type="text" name="path" placeholder="Go module URL" required style="width: 300px;"> + <button type="submit">Add</button> +</form> + <h1>services</h1> <table class="table">
M internal/module/module.gointernal/module/module.go

@@ -111,9 +111,9 @@ if err != nil {

return nil, fmt.Errorf("reading HTTP response: %v", err) } return &resolvedModule{ - module: importPath, - version: latest.Version, - goMod: b, + Module: importPath, + Version: latest.Version, + GoMod: b, }, nil }
M main.gomain.go

@@ -15,8 +15,11 @@

func main() { http.HandleFunc("GET /", indexHandler) http.Handle("GET /assets/", http.StripPrefix("/assets/", http.FileServer(http.FS(assets.Assets)))) - http.HandleFunc("POST /submit", submitHandler) + http.HandleFunc("POST /add", addHandler) + if err := SuperviseServices(services); err != nil { + log.Fatal(err) + } log.Println("Starting Gore server on :6043...") log.Fatal(http.ListenAndServe(":6043", nil)) }
M status.gostatus.go

@@ -11,6 +11,7 @@ "io"

"log" "net/http" "os" + "os/exec" "path/filepath" "runtime" "strconv"

@@ -21,7 +22,6 @@

"j3s.sh/gore/internal/assets" "j3s.sh/gore/internal/module" - "golang.org/x/mod/modfile" "golang.org/x/sys/unix" )

@@ -143,14 +143,6 @@ }

var templates = template.Must(template.New("root"). Funcs(map[string]interface{}{ - "printSBOMHash": func(sbomHash string) string { - const sbomHashLen = 10 - if len(sbomHash) < sbomHashLen { - return sbomHash - } - return sbomHash[:sbomHashLen] - }, - "shortenSHA256": func(hash string) string { if len(hash) > 10 { return hash[:10]

@@ -260,8 +252,8 @@ status := "started"

if svc.Stopped() { status = "stopped" } - w.Header().Set("X-Gokrazy-Status", status) - w.Header().Set("X-Gokrazy-GOARCH", runtime.GOARCH) + w.Header().Set("X-Gore-Status", status) + w.Header().Set("X-Gore-GOARCH", runtime.GOARCH) io.Copy(w, &buf) })

@@ -326,7 +318,6 @@ Services []*service

BuildTimestamp string Meminfo map[string]int64 Hostname string - Kernel string }{ Services: services.S, Meminfo: parseMeminfo(),

@@ -353,7 +344,7 @@ }

io.Copy(w, &buf) } -func submitHandler(w http.ResponseWriter, r *http.Request) { +func addHandler(w http.ResponseWriter, r *http.Request) { formInput := r.FormValue("path") if formInput == "" { http.Error(w, "Module URL is required (e.g. git.j3s.sh/vore@latest)", http.StatusBadRequest)

@@ -369,8 +360,9 @@

// First: we resolve da module using le proxies resolved, err := module.Resolve(context.TODO(), path, version) if err != nil { - // TODO: error to user - log.Println(err) + msg := fmt.Sprintf("Failed to resolve module: %v", err) + fmt.Fprintf(w, msg) + log.Println(msg) return }

@@ -378,7 +370,7 @@ log.Printf(`Adding the following package to gore:

Go package : %s in Go module: %s`, path, resolved.Module) - // Next: we build da binary, based on le module data + // Next: we install da binary, based on le module data buildDir := filepath.Join("builddir", resolved.Module) if _, err := os.Stat(buildDir); err != nil { log.Printf("Creating builddir for module %s", resolved.Module)

@@ -388,34 +380,49 @@ return

} } - if _, err := os.Stat(filepath.Join(buildDir, "go.mod")); err == nil { - log.Printf("Adding require line to existing go.mod") - } else { - log.Printf("Creating go.mod based on upstream go.mod") - modf, err := modfile.Parse("go.mod", resolved.GoMod, nil) - if err != nil { - log.Printf("parsing old go.mod: %v", err) - return - } - if err := modf.AddModuleStmt("gokrazy/build/" + resolved.Module); err != nil { - log.Println(err) - return - } + // if _, err := os.Stat(filepath.Join(buildDir, "go.mod")); err == nil { + // log.Printf("Adding require line to existing go.mod") + // } else { + // log.Printf("Creating go.mod based on upstream go.mod") + // modf, err := modfile.Parse("go.mod", resolved.GoMod, nil) + // if err != nil { + // log.Printf("parsing old go.mod: %v", err) + // return + // } + // if err := modf.AddModuleStmt("gore/build/" + resolved.Module); err != nil { + // log.Println(err) + // return + // } + // + // b, err := modf.Format() + // if err != nil { + // log.Println(err) + // return + // } - b, err := modf.Format() - if err != nil { - log.Println(err) - return - } + // if err := os.WriteFile(filepath.Join(buildDir, "go.mod"), b, 0600); err != nil { + // log.Println(err) + // return + // } + // } - if err := os.WriteFile(filepath.Join(buildDir, "go.mod"), b, 0600); err != nil { - log.Println(err) - return - } + get := exec.Command("go", "install", resolved.Module+"@"+resolved.Version) + absoluteInstallPath, err := filepath.Abs(filepath.Join(buildDir + "@" + resolved.Version)) + if err != nil { + fmt.Fprintf(w, "oopsie woopsie, we made a fucky wu- actually, you know what? 500 internal server error. fuck you.") + log.Println(err) + return + } + get.Env = append(os.Environ(), "CGO_ENABLED=0", "GOBIN="+absoluteInstallPath) + get.Stdout = os.Stdout + get.Stderr = os.Stderr + if err := get.Run(); err != nil { + fmt.Fprintf(w, "oopsie woopsie, we made a fucky wu- actually, you know what? 500 internal server error. fuck you.") + log.Println(err) + return } - // attempt fetch - // attempt new service? + // add to service list http.Redirect(w, r, "/", http.StatusSeeOther) }