From 9ecbef030a77de30db8271f39c1a57ffc25af248 Mon Sep 17 00:00:00 2001 From: zimbatm Date: Sun, 28 Jul 2024 12:08:59 +0200 Subject: [PATCH 1/3] chore: remove last now.sh references --- .nowignore | 8 -------- README.md | 2 +- main.go | 3 --- 3 files changed, 1 insertion(+), 12 deletions(-) delete mode 100644 .nowignore diff --git a/.nowignore b/.nowignore deleted file mode 100644 index f10039f..0000000 --- a/.nowignore +++ /dev/null @@ -1,8 +0,0 @@ -* -!go.mod -!go.sum -!api -!api/* -!api/*/*.go -!public -!public/* diff --git a/README.md b/README.md index 1346888..64a54eb 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ the fly. That's it. E.g.: -* https://nar-serve.zimbatm.now.sh/nix/store/barxv95b8arrlh97s6axj8k7ljn7aky1-go-1.12/share/go/doc/effective_go.html +* https://serve.ntd.one/nix/store/barxv95b8arrlh97s6axj8k7ljn7aky1-go-1.12/share/go/doc/effective_go.html NAR archives also contain information about the executable bit for each contained file. nar-serve uses a custom HTTP header named `NAR-executable` to indicate whether the fetched file would be executable. diff --git a/main.go b/main.go index 421a9e4..5f6e128 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,5 @@ package main -// this file is being used for local development. It reproduces more -// or less the behaviour of now.sh but all compiled into a single binary. - import ( "net/http" "os" From 6a8a267b8b0daa46c48cdd0b54cb67d431dfa3dd Mon Sep 17 00:00:00 2001 From: zimbatm Date: Sun, 28 Jul 2024 12:16:42 +0200 Subject: [PATCH 2/3] chore: move files out of go --- main.go | 78 +++++++----------------------------------------- views/index.html | 58 +++++++++++++++++++++++++++++++++++ views/robots.txt | 2 ++ 3 files changed, 70 insertions(+), 68 deletions(-) create mode 100644 views/index.html create mode 100644 views/robots.txt diff --git a/main.go b/main.go index 5f6e128..20284d6 100644 --- a/main.go +++ b/main.go @@ -1,91 +1,33 @@ package main import ( + "embed" "net/http" + "io" "os" unpack "github.com/numtide/nar-serve/api/unpack" "github.com/urfave/negroni" ) -const robotsTxt = ` -User-agent: * -Disallow: /nix/store -` - -const indexPage = ` - - - - - nar-serve - - - -
-

nar-serve

- -

All the files in cache.nixos.org are packed in NAR files which makes them not directly accessible. This service allows to dowload, decompress, unpack and serve any file in the cache on the fly.

- -

Use cases

- -
    -
  • Avoid publishing build artifacts to both the binary cache and - another service.
  • -
  • Allows to share build results easily.
  • -
  • Inspect the content of a NAR file.
  • -
- -

Usage

-
    -
  1. Pick a full store path in your filesystem.
  2. -
  3. Paste it in the form below.
  4. -
  5. Click submit. TADA!
  6. -
- -
-
-
- -
-
- - - -

Examples

- - - -
-

-Like this project? Star it on GitHub. - - -` +//go:embed views/* +var viewsFS embed.FS func indexHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") - w.Write([]byte(indexPage)) + f, _ := viewsFS.Open("views/index.html") + _, _ = io.Copy(w, f) } func robotsHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/plain") - w.Write([]byte(robotsTxt)) + f, _ := viewsFS.Open("views/robots.txt") + _, _ = io.Copy(w, f) + f.Close() } func healthzHandler(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("OK")) + _, _ = w.Write([]byte("OK")) } func main() { diff --git a/views/index.html b/views/index.html new file mode 100644 index 0000000..c988665 --- /dev/null +++ b/views/index.html @@ -0,0 +1,58 @@ + + + + + nar-serve + + + +

+

nar-serve

+ +

All the files in cache.nixos.org are packed in NAR files which makes them not directly accessible. This service allows to dowload, decompress, unpack and serve any file in the cache on the fly.

+ +

Use cases

+ +
    +
  • Avoid publishing build artifacts to both the binary cache and + another service.
  • +
  • Allows to share build results easily.
  • +
  • Inspect the content of a NAR file.
  • +
+ +

Usage

+
    +
  1. Pick a full store path in your filesystem.
  2. +
  3. Paste it in the form below.
  4. +
  5. Click submit. TADA!
  6. +
+ +
+
+
+ +
+
+ + + +

Examples

+ + + +
+

+Like this project? Star it on GitHub. + + diff --git a/views/robots.txt b/views/robots.txt new file mode 100644 index 0000000..2b6085d --- /dev/null +++ b/views/robots.txt @@ -0,0 +1,2 @@ +User-agent: * +Disallow: /nix/store From 730ae740fab4c65a4585f16896c17c01f101fe15 Mon Sep 17 00:00:00 2001 From: zimbatm Date: Sun, 28 Jul 2024 12:27:14 +0200 Subject: [PATCH 3/3] feat: add HTTP_ADDR env var Allow changing the bind address --- README.md | 4 +++- main.go | 23 ++++++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 64a54eb..4a32273 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,8 @@ Inside the provided nix shell run: This will create a small local server with live reload that emulates now.sh. -Currently, the default port is 8383. You can change it by setting the `PORT` environment variable. +Currently, the default port is 8383. You can change it by setting the `PORT` +environment variable, or `HTTP_ADDR` to also change the bind address. ## Usage @@ -45,6 +46,7 @@ You can use the following environment variables to configure nar-serve: | Name | Default value | Description | |:-- |:-- |:-- | | `PORT` | `8383` | Port number on which nar-service listens | +| `HTTP_ADDR` | `:$PORT` | HTTP address to bind the server to. When set, takes precedence over $PORT. | | `NAR_CACHE_URL` | `https://cache.nixos.org` | The URL of the Nix store from which NARs are fetched | ## Contributing diff --git a/main.go b/main.go index 20284d6..0ed32b5 100644 --- a/main.go +++ b/main.go @@ -31,17 +31,21 @@ func healthzHandler(w http.ResponseWriter, r *http.Request) { } func main() { + var ( + port = getEnv("PORT", "8383") + addr = getEnv("HTTP_ADDR", "") + ) + + if addr == "" { + addr = ":" + port + } + mux := http.NewServeMux() mux.HandleFunc("/", indexHandler) mux.HandleFunc("/healthz", healthzHandler) mux.HandleFunc("/robots.txt", robotsHandler) mux.HandleFunc(unpack.MountPath, unpack.Handler) - addr := ":8383" - if port := os.Getenv("PORT"); port != "" { - addr = ":" + port - } - // Includes some default middlewares // Serve static files from ./public n := negroni.New( @@ -51,3 +55,12 @@ func main() { n.UseHandler(mux) n.Run(addr) } + +func getEnv(name, def string) string { + value := os.Getenv(name) + if value == "" { + return def + } + return value +} +