implement the public index page

Signed-off-by: Xe Iaso <xe@tailscale.com>
This commit is contained in:
Xe Iaso 2022-12-15 17:37:35 +00:00
parent 24b7c5452c
commit a4b18e796a
3 changed files with 46 additions and 17 deletions

View file

@ -86,17 +86,26 @@ func (s *Server) TailnetIndex(w http.ResponseWriter, r *http.Request) {
}
func (s *Server) NotFound(w http.ResponseWriter, r *http.Request) {
ui, _ := upsertUserInfo(r.Context(), s.db, s.lc, r.RemoteAddr)
var up *tailcfg.UserProfile
if ui != nil {
up = ui.UserProfile
}
s.tmpls.ExecuteTemplate(w, "notfound.tmpl", struct {
UserInfo *tailcfg.UserProfile
Title string
}{
UserInfo: up,
UserInfo: nil,
Title: "Not found",
})
}
func (s *Server) PublicIndex(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
s.NotFound(w, r)
return
}
s.tmpls.ExecuteTemplate(w, "publicindex.tmpl", struct {
UserInfo *tailcfg.UserProfile
Title string
}{
UserInfo: nil,
Title: "Not found",
})
}
@ -219,14 +228,25 @@ WHERE p.id = ?1`
return
}
if len(sp) != 1 && sp[1] == "raw" {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", fname))
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(data)))
if len(sp) != 1 {
switch sp[1] {
case "raw":
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(data)))
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, data)
return
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, data)
return
case "dl":
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", fname))
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(data)))
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, data)
default:
s.NotFound(w, r)
}
}
err = s.tmpls.ExecuteTemplate(w, "showpaste.tmpl", struct {
@ -282,17 +302,21 @@ func main() {
log.Fatal(err)
}
tailnetMux := http.NewServeMux()
tmpls := template.Must(template.ParseFS(templateFiles, "tmpl/*.tmpl"))
srv := &Server{lc, db, tmpls}
tailnetMux := http.NewServeMux()
tailnetMux.Handle("/static/", http.FileServer(http.FS(staticFiles)))
tailnetMux.HandleFunc("/paste/", srv.ShowPost)
tailnetMux.HandleFunc("/api/post", srv.TailnetSubmitPaste)
tailnetMux.HandleFunc("/", srv.TailnetIndex)
funnelMux := http.NewServeMux()
funnelMux.Handle("/static/", http.FileServer(http.FS(staticFiles)))
funnelMux.HandleFunc("/", srv.PublicIndex)
funnelMux.HandleFunc("/paste/", srv.ShowPost)
log.Printf("listening on http://%s", *hostname)
log.Fatal(http.Serve(ln, tailnetMux))
}

View file

@ -0,0 +1,5 @@
{{template "header" .}}
<p>Hello! You have connceted to a private pastebin run by a Tailscale user.</p>
<p>If this is your tailnet and you are not expecting to see this page, please connect to your tailnet and try your query again.</p>
{{template "footer" .}}

View file

@ -6,5 +6,5 @@
{{.Data}}
</code></pre>
<a href="/paste/{{.ID}}">Permalink</a> - <a href="/paste/{{.ID}}/dl">Download</a>
<a href="/paste/{{.ID}}">Permalink</a> - <a href="/paste/{{.ID}}/dl">Download</a> - <a href="/paste/{{.ID}}/raw">Raw</a>
{{template "footer" .}}