this seems okay
This commit is contained in:
@@ -1,51 +1,84 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"local/s2sa/s2sa/logg"
|
||||
"local/s2sa/s2sa/server/router"
|
||||
"local/s2sa/s2sa/token"
|
||||
"errors"
|
||||
"local/rproxy3/config"
|
||||
"local/rproxy3/storage"
|
||||
"local/rproxy3/storage/packable"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type Router interface {
|
||||
Add(string, http.HandlerFunc) error
|
||||
ServeHTTP(http.ResponseWriter, *http.Request)
|
||||
}
|
||||
const nsRouting = "routing"
|
||||
|
||||
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 listenerScheme int
|
||||
|
||||
type Messenger interface {
|
||||
Produce(string, interface{}) error
|
||||
const (
|
||||
schemeHTTP listenerScheme = iota
|
||||
schemeHTTPS listenerScheme = iota
|
||||
)
|
||||
|
||||
func (ls listenerScheme) String() string {
|
||||
switch ls {
|
||||
case schemeHTTP:
|
||||
return "http"
|
||||
case schemeHTTPS:
|
||||
return "https"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
router Router
|
||||
db TokenDatabase
|
||||
authdb TokenDatabase
|
||||
messenger Messenger
|
||||
addr string
|
||||
db storage.DB
|
||||
addr string
|
||||
username string
|
||||
password 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) Route(src, dst string) error {
|
||||
log.Printf("Adding route %q -> %q...\n", src, dst)
|
||||
u, err := url.Parse(dst)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.db.Set(nsRouting, src, packable.NewURL(u))
|
||||
}
|
||||
|
||||
func (s *Server) Run() error {
|
||||
logg.Logf("Listening on %v...\n", s.addr)
|
||||
return http.ListenAndServe(s.addr, s)
|
||||
scheme := schemeHTTP
|
||||
if _, _, ok := config.GetSSL(); ok {
|
||||
scheme = schemeHTTPS
|
||||
}
|
||||
log.Printf("Listening for %v on %v...\n", scheme, s.addr)
|
||||
switch scheme {
|
||||
case schemeHTTP:
|
||||
return http.ListenAndServe(s.addr, s)
|
||||
case schemeHTTPS:
|
||||
c, k, _ := config.GetSSL()
|
||||
return http.ListenAndServeTLS(s.addr, c, k, s)
|
||||
}
|
||||
return errors.New("did not load server")
|
||||
}
|
||||
|
||||
func (s *Server) doAuth(foo http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
rusr, rpwd, ok := config.GetAuth()
|
||||
if ok {
|
||||
usr, pwd, ok := r.BasicAuth()
|
||||
if !ok || rusr != usr || rpwd != pwd {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
}
|
||||
foo(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) Pre(foo http.HandlerFunc) http.HandlerFunc {
|
||||
return s.doAuth(foo)
|
||||
}
|
||||
|
||||
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Printf("REQ: %s\n", r.URL.Path)
|
||||
s.router.ServeHTTP(w, r)
|
||||
s.Pre(s.Proxy)(w, r)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user