extremely basic routing

This commit is contained in:
David 2022-05-24 22:25:42 -04:00
parent 080d1b42cf
commit 364f98cbce
3 changed files with 51 additions and 6 deletions

47
main.go
View File

@ -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)
}

View File

@ -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

View File

@ -8,6 +8,11 @@ import (
var ErrUserNotFound = fmt.Errorf("user not found")
type User struct {
Name string
Roles []string
}
type storeUser struct {
name string
roles []string