This repository has been archived on 2022-11-30. You can view files and clone it, but cannot push or open issues or pull requests.
tacitus/http/server.go

44 lines
540 B
Go
Raw Normal View History

2017-09-08 10:57:39 -07:00
package http
import (
"net"
"net/http"
)
const DefaultAddr = ":8080"
type Server struct {
Addr string
2017-09-09 10:14:50 -07:00
Handler *Mux
2017-09-08 10:57:39 -07:00
Err chan error
listener net.Listener
}
func NewServer() *Server {
2017-09-09 10:14:50 -07:00
return &Server{Addr: DefaultAddr,
Handler: NewMux()}
2017-09-08 10:57:39 -07:00
}
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
}