| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "mime" |
| 5 | "net/http" |
| 6 | "path" |
| 7 | ) |
| 8 | |
| 9 | func (a *App) routes() *http.ServeMux { |
| 10 | mux := http.NewServeMux() |
| 11 | |
| 12 | mux.HandleFunc("GET /assets/kepler.css", func(w http.ResponseWriter, r *http.Request) { |
| 13 | data, err := appFS.ReadFile("static/styles.css") |
| 14 | if err != nil { |
| 15 | http.NotFound(w, r) |
| 16 | return |
| 17 | } |
| 18 | w.Header().Set("Content-Type", "text/css; charset=utf-8") |
| 19 | _, _ = w.Write(data) |
| 20 | }) |
| 21 | mux.HandleFunc("GET /assets/fonts/{name}", func(w http.ResponseWriter, r *http.Request) { |
| 22 | name := r.PathValue("name") |
| 23 | data, err := appFS.ReadFile("static/fonts/" + name) |
| 24 | if err != nil { |
| 25 | http.NotFound(w, r) |
| 26 | return |
| 27 | } |
| 28 | w.Header().Set("Content-Type", "font/otf") |
| 29 | w.Header().Set("Cache-Control", "public, max-age=31536000, immutable") |
| 30 | w.Header().Set("Access-Control-Allow-Origin", "*") |
| 31 | _, _ = w.Write(data) |
| 32 | }) |
| 33 | // Static icon/manifest assets. Registered as explicit literal paths so |
| 34 | // they stay more specific than the "/{repo}/..." routes (a wildcard |
| 35 | // "/assets/{name}" would be ambiguous against "/{repo}/refs" etc.). |
| 36 | staticAsset := func(w http.ResponseWriter, r *http.Request) { |
| 37 | name := path.Base(r.URL.Path) |
| 38 | data, err := appFS.ReadFile("static/" + name) |
| 39 | if err != nil { |
| 40 | http.NotFound(w, r) |
| 41 | return |
| 42 | } |
| 43 | ct := mime.TypeByExtension(path.Ext(name)) |
| 44 | if ct == "" { |
| 45 | ct = http.DetectContentType(data) |
| 46 | } |
| 47 | w.Header().Set("Content-Type", ct) |
| 48 | w.Header().Set("Cache-Control", "public, max-age=86400") |
| 49 | _, _ = w.Write(data) |
| 50 | } |
| 51 | for _, name := range []string{ |
| 52 | "og.png", "favicon.ico", "favicon-16x16.png", "favicon-32x32.png", |
| 53 | "apple-touch-icon.png", "android-chrome-192x192.png", |
| 54 | "android-chrome-512x512.png", "site.webmanifest", |
| 55 | } { |
| 56 | mux.HandleFunc("GET /assets/"+name, staticAsset) |
| 57 | } |
| 58 | |
| 59 | mux.HandleFunc("GET /api/repos", a.apiReposHandler) |
| 60 | |
| 61 | mux.HandleFunc("GET /{$}", a.indexHandler) |
| 62 | mux.HandleFunc("GET /{repo}", a.repoHandler) |
| 63 | mux.HandleFunc("GET /{repo}/refs", a.refsHandler) |
| 64 | mux.HandleFunc("GET /{repo}/atom.xml", a.atomHandler) |
| 65 | mux.HandleFunc("GET /{repo}/log/{ref}", a.logHandler) |
| 66 | mux.HandleFunc("GET /{repo}/commit/{sha}", a.commitHandler) |
| 67 | mux.HandleFunc("GET /{repo}/tree/{ref}", a.treeHandler) |
| 68 | mux.HandleFunc("GET /{repo}/tree/{ref}/{path...}", a.treeHandler) |
| 69 | mux.HandleFunc("GET /{repo}/blob/{ref}/{path...}", a.blobHandler) |
| 70 | mux.HandleFunc("GET /{repo}/raw/{ref}/{path...}", a.rawHandler) |
| 71 | mux.HandleFunc("GET /{repo}/archive/{name}", a.archiveHandler) |
| 72 | |
| 73 | return mux |
| 74 | } |