Debian Codegraph @

server/server_web.go

raw
1 package server
2
3 import (
4 "encoding/json"
5 "fmt"
6 "io"
7 "net/http"
8
9 "heckel.io/ntfy/v2/util"
10 )
11
12 // handleWebApp serves the embedded web app's index for client-side (SPA) routes that the
13 // browser router resolves, so the app shell loads and the client-side router takes over.
14 func (s *Server) handleWebApp(w http.ResponseWriter, r *http.Request, v *visitor) error {
15 r.URL.Path = webAppIndex
16 return s.handleStatic(w, r, v)
17 }
18
19 // handleWebAppNoIndex serves the web app index for the magic-link landing pages, whose path
20 // carries a one-time token. The response is marked no-referrer (so the token can't leak to third
21 // parties via the Referer header) and noindex (so it never gets indexed).
22 func (s *Server) handleWebAppNoIndex(w http.ResponseWriter, r *http.Request, v *visitor) error {
23 w.Header().Set("Referrer-Policy", "no-referrer")
24 w.Header().Set("X-Robots-Tag", "noindex")
25 return s.handleWebApp(w, r, v)
26 }
27
28 func (s *Server) handleConfig(w http.ResponseWriter, _ *http.Request, _ *visitor) error {
29 w.Header().Set("Cache-Control", "no-cache")
30 return s.writeJSON(w, s.configResponse())
31 }
32
33 func (s *Server) handleWebConfig(w http.ResponseWriter, _ *http.Request, _ *visitor) error {
34 b, err := json.MarshalIndent(s.configResponse(), "", " ")
35 if err != nil {
36 return err
37 }
38 w.Header().Set("Content-Type", "text/javascript")
39 w.Header().Set("Cache-Control", "no-cache")
40 _, err = io.WriteString(w, fmt.Sprintf("// Generated server configuration\nvar config = %s;\n", string(b)))
41 return err
42 }
43
44 // handleWebManifest serves the web app manifest for the progressive web app (PWA)
45 func (s *Server) handleWebManifest(w http.ResponseWriter, _ *http.Request, _ *visitor) error {
46 response := &webManifestResponse{
47 Name: "ntfy",
48 Description: "ntfy lets you send push notifications via scripts from any computer or phone",
49 ShortName: "ntfy",
50 Scope: "/",
51 StartURL: s.config.WebRoot,
52 Display: "standalone",
53 BackgroundColor: "#ffffff",
54 ThemeColor: "#317f6f",
55 Icons: []*webManifestIcon{
56 {SRC: "/static/images/pwa-192x192.png", Sizes: "192x192", Type: "image/png"},
57 {SRC: "/static/images/pwa-512x512.png", Sizes: "512x512", Type: "image/png"},
58 },
59 }
60 return s.writeJSONWithContentType(w, response, "application/manifest+json")
61 }
62
63 // handleStatic returns all static resources (excluding the docs), including the web app
64 func (s *Server) handleStatic(w http.ResponseWriter, r *http.Request, _ *visitor) error {
65 r.URL.Path = webSiteDir + r.URL.Path
66 util.Gzip(http.FileServer(http.FS(webFsCached))).ServeHTTP(w, r)
67 return nil
68 }
69
70 // handleDocs returns static resources related to the docs
71 func (s *Server) handleDocs(w http.ResponseWriter, r *http.Request, _ *visitor) error {
72 util.Gzip(http.FileServer(http.FS(docsStaticCached))).ServeHTTP(w, r)
73 return nil
74 }
75
76 func (s *Server) configResponse() *apiConfigResponse {
77 return &apiConfigResponse{
78 BaseURL: "", // Will translate to window.location.origin
79 AppRoot: s.config.WebRoot,
80 EnableLogin: s.config.EnableLogin,
81 RequireLogin: s.config.RequireLogin,
82 EnableSignup: s.config.EnableSignup,
83 EnablePayments: s.config.StripeSecretKey != "",
84 EnableCalls: s.config.TwilioAccount != "",
85 EnableEmails: s.config.SMTPSenderFrom != "",
86 EnableResetPassword: s.config.SMTPSenderFrom != "" && s.config.BaseURL != "", // Reset links need SMTP + an absolute base-url
87 EnableReservations: s.config.EnableReservations,
88 EnableWebPush: s.config.WebPushPublicKey != "",
89 BillingContact: s.config.BillingContact,
90 WebPushPublicKey: s.config.WebPushPublicKey,
91 DisallowedTopics: s.config.DisallowedTopics,
92 ConfigHash: s.config.Hash(),
93 }
94 }