12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- package myth
- import (
- "fmt"
- "log"
- "net/http"
- "os"
- "os/signal"
- "syscall"
- "time"
- )
- func newServe(addr string, router http.Handler) *http.Server {
- return &http.Server{
- Addr: addr,
- Handler: router,
- ReadTimeout: 30 * time.Second,
- WriteTimeout: 30 * time.Second,
- MaxHeaderBytes: 1 << 20,
- }
- }
- // ListenAndServe new server and start
- func ListenAndServe(addr string, router http.Handler) {
- s := newServe(addr, router)
- errc := make(chan error)
- go func() {
- c := make(chan os.Signal)
- signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
- errc <- fmt.Errorf("%s", <-c)
- }()
- go func() {
- log.Println("HTTP Server listen on", addr)
- errc <- s.ListenAndServe()
- log.Println("Exit HTTP server", "Quit")
- }()
- log.Println("Exit", <-errc)
- }
- // ListenAndServeTLS new server and start
- func ListenAndServeTLS(addr, certFile, keyFile string, router http.Handler) {
- s := newServe(addr, router)
- errc := make(chan error)
- go func() {
- c := make(chan os.Signal)
- signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
- errc <- fmt.Errorf("%s", <-c)
- }()
- go func() {
- log.Println("HTTP Server listen on", addr)
- errc <- s.ListenAndServeTLS(certFile, keyFile)
- log.Println("Exit HTTP server", "Quit")
- }()
- log.Println("Exit", <-errc)
- }
|