Skip to content

Commit

Permalink
feat: show to which cache the server is bound
Browse files Browse the repository at this point in the history
Print it in the index.html and when starting the program.
  • Loading branch information
zimbatm committed Jul 28, 2024
1 parent b3543b5 commit 36fc4a0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
34 changes: 20 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package main

import (
"embed"
"context"
"io"
"log"
_ "embed"
"net/http"
"text/template"
"os"

"github.com/numtide/nar-serve/pkg/libstore"
Expand All @@ -15,20 +15,17 @@ import (
"github.com/go-chi/chi/v5/middleware"
)

//go:embed views/*
var viewsFS embed.FS
//go:embed views/index.html
var indexHTML string

func indexHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
f, _ := viewsFS.Open("views/index.html")
_, _ = io.Copy(w, f)
}
var indexHTMLTmpl = template.Must(template.New("index.html").Parse(indexHTML))

//go:embed views/robots.txt
var robotsTXT []byte

func robotsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
f, _ := viewsFS.Open("views/robots.txt")
_, _ = io.Copy(w, f)
f.Close()
_, _ = w.Write(robotsTXT)
}

func healthzHandler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -39,7 +36,7 @@ func main() {
var (
port = getEnv("PORT", "8383")
addr = getEnv("HTTP_ADDR", "")
nixCacheURL = getEnv("NAR_CACHE_URL", "https://cache.nixos.org")
nixCacheURL = getEnv("NIX_CACHE_URL", getEnv("NAR_CACHE_URL", "https://cache.nixos.org"))
)

if addr == "" {
Expand All @@ -62,11 +59,20 @@ func main() {
r.Use(middleware.CleanPath)
r.Use(middleware.GetHead)

r.Get("/", indexHandler)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
data := struct {
NixCacheURL string
}{ nixCacheURL }

if err := indexHTMLTmpl.Execute(w, data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
r.Get("/healthz", healthzHandler)
r.Get("/robots.txt", robotsHandler)
r.Method("GET", h.MountPath()+"*", h)

log.Println("nixCacheURL=", nixCacheURL)
log.Println("addr=", addr)
log.Fatal(http.ListenAndServe(addr, r))
}
Expand Down
2 changes: 1 addition & 1 deletion views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<div class="container-lg px-3 my-5 markdown-body">
<h1>nar-serve</h1>

<p>All the files in <a href="https://cache.nixos.org">cache.nixos.org</a> 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.</p>
<p>All the files in <a href="{{ .NixCacheURL }}">{{ .NixCacheURL }}</a> 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.</p>

<h2>Use cases</h2>

Expand Down

0 comments on commit 36fc4a0

Please sign in to comment.