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..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 @@ -33,7 +34,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. @@ -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 421a9e4..0ed32b5 100644 --- a/main.go +++ b/main.go @@ -1,108 +1,51 @@ 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 ( + "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

- - - -

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() { + 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( @@ -112,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 +} + 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

+ + + +

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