54 lines
838 B
Go
54 lines
838 B
Go
package http
|
|
|
|
import (
|
|
"net"
|
|
"net/http"
|
|
|
|
"git.kevincotugno.com/kcotugno/tacitus"
|
|
)
|
|
|
|
const DefaultAddr = ":8080"
|
|
|
|
type Server struct {
|
|
Addr string
|
|
Handler *Mux
|
|
|
|
Err chan error
|
|
|
|
listener net.Listener
|
|
logger tacitus.Logger
|
|
}
|
|
|
|
func NewServer() *Server {
|
|
return &Server{Addr: DefaultAddr,
|
|
Handler: NewMux()}
|
|
}
|
|
|
|
func (s *Server) Open() error {
|
|
ln, err := net.Listen("tcp", s.Addr)
|
|
if err != nil {
|
|
s.logger.Info(`Error opening socket on %v. error="%v"`, s.Addr, err)
|
|
|
|
return err
|
|
}
|
|
s.logger.Info("Listening on %v", s.Addr)
|
|
|
|
s.listener = ln
|
|
|
|
go func() { http.Serve(s.listener, s.Handler) }()
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *Server) Close() {
|
|
if s.listener != nil {
|
|
s.listener.Close()
|
|
s.logger.Info("Socket closed")
|
|
}
|
|
}
|
|
|
|
func (s *Server) SetLogger(logger tacitus.Logger) {
|
|
s.logger = logger
|
|
s.Handler.SetLogger(logger)
|
|
}
|