extremely basic routing
This commit is contained in:
parent
080d1b42cf
commit
364f98cbce
47
main.go
47
main.go
@ -1,5 +1,50 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type SessionProvider interface {
|
||||
Create(user User, expr time.Duration) (string, error)
|
||||
Get(key string) (User, error)
|
||||
Refresh(key string, user User, expr time.Duration) error
|
||||
}
|
||||
|
||||
type PageProvider interface {
|
||||
Page(key string) (*Page, error)
|
||||
Save(key string, page *Page) error
|
||||
}
|
||||
|
||||
type RootHandler struct {
|
||||
Sessions SessionProvider
|
||||
Pages PageProvider
|
||||
}
|
||||
|
||||
func (h *RootHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/admin" {
|
||||
w.Write([]byte("admin route"))
|
||||
return
|
||||
}
|
||||
page, err := h.Pages.Page(r.URL.Path)
|
||||
if err == ErrPageNotFound {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte("not found"))
|
||||
return
|
||||
}
|
||||
w.Write(page.Contents)
|
||||
}
|
||||
|
||||
func main() {
|
||||
handler := &RootHandler{
|
||||
Sessions: &Sessions{},
|
||||
Pages: &Index{},
|
||||
}
|
||||
handler.Pages.Save("foo", &Page{
|
||||
Contents: []byte("foobar"),
|
||||
})
|
||||
|
||||
err := http.ListenAndServe(":8080", handler)
|
||||
log.Fatalf("server error: %v", err)
|
||||
}
|
||||
|
@ -10,11 +10,6 @@ import (
|
||||
|
||||
var ErrInvalidSession = fmt.Errorf("session not found")
|
||||
|
||||
type User struct {
|
||||
Name string
|
||||
Roles []string
|
||||
}
|
||||
|
||||
type Sess struct {
|
||||
User User
|
||||
expr time.Time
|
||||
|
Loading…
Reference in New Issue
Block a user