notes-server/server/routes.go

56 lines
1.1 KiB
Go
Executable File

package server
import (
"fmt"
"local/notes-server/config"
"local/oauth2/oauth2client"
"local/router"
"log"
"net/http"
)
func (s *Server) Routes() error {
wildcard := router.Wildcard
endpoints := []struct {
path string
handler http.HandlerFunc
}{
{
path: fmt.Sprintf("notes/%s%s", wildcard, wildcard),
handler: s.authenticate(s.notes),
},
{
path: fmt.Sprintf("edit/%s%s", wildcard, wildcard),
handler: s.authenticate(s.edit),
},
{
path: fmt.Sprintf("submit/%s%s", wildcard, wildcard),
handler: s.authenticate(s.submit),
},
{
path: fmt.Sprintf("create/%s%s", wildcard, wildcard),
handler: s.authenticate(s.create),
},
}
for _, endpoint := range endpoints {
if err := s.Add(endpoint.path, endpoint.handler); err != nil {
return err
}
}
return nil
}
func (s *Server) authenticate(foo http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if config.OAuthServer != "" {
err := oauth2client.Authenticate(config.OAuthServer, w, r)
if err != nil {
log.Println(err)
return
}
}
foo(w, r)
}
}