32 lines
592 B
Go
32 lines
592 B
Go
package http
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"sync"
|
|
)
|
|
|
|
// Main router
|
|
// Twilio handler
|
|
|
|
var server = &struct {
|
|
router *http.ServeMux
|
|
lock sync.Mutex
|
|
}{
|
|
router: http.NewServeMux(),
|
|
}
|
|
|
|
// AddRoute is used to set up routes.
|
|
//
|
|
// NB: no checking is done yet for duplicate patterns.
|
|
func AddRoute(pattern string, handler func(http.ResponseWriter, *http.Request)) {
|
|
server.lock.Lock()
|
|
defer server.lock.Unlock()
|
|
server.router.HandleFunc(pattern, handler)
|
|
}
|
|
|
|
func Start(addr string) {
|
|
log.Printf("starting HTTP server on %s", addr)
|
|
go log.Print(http.ListenAndServe(addr, server.router))
|
|
}
|