52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"local/s2sa/s2sa/logg"
|
|
"local/s2sa/s2sa/server/router"
|
|
"local/s2sa/s2sa/token"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
type Router interface {
|
|
Add(string, http.HandlerFunc) error
|
|
ServeHTTP(http.ResponseWriter, *http.Request)
|
|
}
|
|
|
|
type TokenDatabase interface {
|
|
Register(string) error
|
|
New(string, string) (token.Basic, error)
|
|
Get(string, string) (token.Basic, error)
|
|
Revoke(string, string) error
|
|
Lookup(string, string) (token.Basic, error)
|
|
}
|
|
|
|
type Messenger interface {
|
|
Produce(string, interface{}) error
|
|
}
|
|
|
|
type Server struct {
|
|
router Router
|
|
db TokenDatabase
|
|
authdb TokenDatabase
|
|
messenger Messenger
|
|
addr string
|
|
}
|
|
|
|
func (s *Server) Add(path string, handler http.HandlerFunc) error {
|
|
logg.Logf("Adding path %v...\n", path)
|
|
path = strings.Replace(path, wildcard, router.Wildcard, -1)
|
|
return s.router.Add(path, handler)
|
|
}
|
|
|
|
func (s *Server) Run() error {
|
|
logg.Logf("Listening on %v...\n", s.addr)
|
|
return http.ListenAndServe(s.addr, s)
|
|
}
|
|
|
|
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Printf("REQ: %s\n", r.URL.Path)
|
|
s.router.ServeHTTP(w, r)
|
|
}
|