apps/jotts/routes.go 1.6 K raw
1
package main
2
3
import (
4
	"net/http"
5
6
	"github.com/stevedylandev/andromeda/pkg/auth"
7
	"github.com/stevedylandev/andromeda/pkg/darkmatter"
8
	"github.com/stevedylandev/andromeda/pkg/web"
9
)
10
11
func (a *App) routes() *http.ServeMux {
12
	mux := http.NewServeMux()
13
14
	mux.HandleFunc("GET /static/", web.EmbeddedHandler(appFS, "static"))
15
	darkmatter.Mount(mux, "/assets")
16
17
	requireSession := func(next http.HandlerFunc) http.HandlerFunc {
18
		return a.Sessions.RequireSession("/login", next)
19
	}
20
	requireAPIKey := func(next http.HandlerFunc) http.HandlerFunc {
21
		return auth.RequireAPIKey(a.APIKey, next)
22
	}
23
24
	mux.HandleFunc("GET /login", a.loginGetHandler)
25
	mux.HandleFunc("POST /login", a.loginPostHandler)
26
	mux.HandleFunc("GET /logout", a.logoutHandler)
27
28
	mux.HandleFunc("GET /{$}", requireSession(a.indexHandler))
29
	mux.HandleFunc("GET /notes/new", requireSession(a.newNoteGetHandler))
30
	mux.HandleFunc("POST /notes", requireSession(a.createNoteHandler))
31
	mux.HandleFunc("GET /notes/{short_id}", requireSession(a.viewNoteHandler))
32
	mux.HandleFunc("GET /notes/{short_id}/edit", requireSession(a.editNoteGetHandler))
33
	mux.HandleFunc("POST /notes/{short_id}", requireSession(a.updateNoteHandler))
34
	mux.HandleFunc("POST /notes/{short_id}/delete", requireSession(a.deleteNoteHandler))
35
36
	mux.HandleFunc("GET /api/notes", requireAPIKey(a.apiListNotes))
37
	mux.HandleFunc("POST /api/notes", requireAPIKey(a.apiCreateNote))
38
	mux.HandleFunc("GET /api/notes/{short_id}", requireAPIKey(a.apiGetNote))
39
	mux.HandleFunc("PUT /api/notes/{short_id}", requireAPIKey(a.apiUpdateNote))
40
	mux.HandleFunc("DELETE /api/notes/{short_id}", requireAPIKey(a.apiDeleteNote))
41
42
	return mux
43
}