44 lines
540 B
Go
44 lines
540 B
Go
package http
|
|
|
|
import (
|
|
"net"
|
|
"net/http"
|
|
)
|
|
|
|
const DefaultAddr = ":8080"
|
|
|
|
type Server struct {
|
|
Addr string
|
|
Handler *Mux
|
|
|
|
Err chan error
|
|
|
|
listener net.Listener
|
|
}
|
|
|
|
func NewServer() *Server {
|
|
return &Server{Addr: DefaultAddr,
|
|
Handler: NewMux()}
|
|
}
|
|
|
|
func (s *Server) Open() error {
|
|
ln, err := net.Listen("tcp", s.Addr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
s.listener = ln
|
|
|
|
go func() { http.Serve(s.listener, s.Handler) }()
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *Server) Close() error {
|
|
if s.listener != nil {
|
|
s.listener.Close()
|
|
}
|
|
|
|
return nil
|
|
}
|