| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | |
| 6 | "github.com/stevedylandev/andromeda/pkg/darkmatter" |
| 7 | "github.com/stevedylandev/andromeda/pkg/web" |
| 8 | ) |
| 9 | |
| 10 | func (a *App) routes() *http.ServeMux { |
| 11 | mux := http.NewServeMux() |
| 12 | |
| 13 | requireSession := func(next http.HandlerFunc) http.HandlerFunc { |
| 14 | return a.Sessions.RequireSession("/admin/login", next) |
| 15 | } |
| 16 | cors := func(next http.HandlerFunc) http.HandlerFunc { |
| 17 | return func(w http.ResponseWriter, r *http.Request) { |
| 18 | w.Header().Set("Access-Control-Allow-Origin", "*") |
| 19 | w.Header().Set("Access-Control-Allow-Methods", "GET") |
| 20 | next(w, r) |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | // Public |
| 25 | mux.HandleFunc("GET /", a.indexHandler) |
| 26 | mux.HandleFunc("GET /feed.xml", a.rssFeed) |
| 27 | mux.HandleFunc("GET /wines/{short_id}", a.wineDetail) |
| 28 | mux.HandleFunc("GET /wines/{short_id}/image", a.wineImage) |
| 29 | mux.HandleFunc("GET /wishlist", a.wishlistHandler) |
| 30 | |
| 31 | // API |
| 32 | mux.HandleFunc("GET /api/wines", cors(a.apiListWines)) |
| 33 | mux.HandleFunc("GET /api/wines/{short_id}", cors(a.apiGetWine)) |
| 34 | mux.HandleFunc("GET /api/wines/{short_id}/pentagon.svg", cors(a.apiPentagonSVG)) |
| 35 | mux.HandleFunc("GET /api/wines/{short_id}/bars.svg", cors(a.apiBarsSVG)) |
| 36 | |
| 37 | // Admin auth |
| 38 | mux.HandleFunc("GET /admin/login", a.loginGet) |
| 39 | mux.HandleFunc("POST /admin/login", a.loginPost) |
| 40 | mux.HandleFunc("GET /admin/logout", a.logout) |
| 41 | |
| 42 | // Admin protected |
| 43 | mux.HandleFunc("GET /admin", requireSession(a.adminIndex)) |
| 44 | mux.HandleFunc("GET /admin/new", requireSession(a.newWineGet)) |
| 45 | mux.HandleFunc("POST /admin/new", requireSession(a.newWinePost)) |
| 46 | mux.HandleFunc("GET /admin/edit/{short_id}", requireSession(a.editWineGet)) |
| 47 | mux.HandleFunc("POST /admin/edit/{short_id}", requireSession(a.editWinePost)) |
| 48 | mux.HandleFunc("POST /admin/delete/{short_id}", requireSession(a.deleteWinePost)) |
| 49 | mux.HandleFunc("GET /admin/wishlist/new", requireSession(a.newWishlistGet)) |
| 50 | mux.HandleFunc("POST /admin/wishlist/new", requireSession(a.newWishlistPost)) |
| 51 | mux.HandleFunc("GET /admin/wishlist/edit/{short_id}", requireSession(a.editWishlistGet)) |
| 52 | mux.HandleFunc("POST /admin/wishlist/edit/{short_id}", requireSession(a.editWishlistPost)) |
| 53 | mux.HandleFunc("POST /admin/wishlist/delete/{short_id}", requireSession(a.deleteWishlistPost)) |
| 54 | mux.HandleFunc("POST /admin/wishlist/promote/{short_id}", requireSession(a.promoteWinePost)) |
| 55 | mux.HandleFunc("POST /admin/analyze-image", requireSession(a.analyzeImage)) |
| 56 | |
| 57 | // Static |
| 58 | mux.HandleFunc("GET /static/", web.EmbeddedHandler(appFS, "static")) |
| 59 | darkmatter.Mount(mux, "/assets") |
| 60 | return mux |
| 61 | } |