Server

package revel

var (
    MainRouter         *Router
    MainTemplateLoader *TemplateLoader
    MainWatcher        *Watcher
    Server             *http.Server
)

// Run the server.
// This is called from the generated main file.
// If port is non-zero, use that.  Else, read the port from app.conf.
func Run(port int) {
    address := HttpAddr
    if port == 0 {
        port = HttpPort
    }
    // If the port equals zero, it means do not append port to the address.
    // It can use unix socket or something else.
    if port != 0 {
        address = fmt.Sprintf("%s:%d", address, port)
    }

    MainTemplateLoader = NewTemplateLoader(TemplatePaths)

    // The "watch" config variable can turn on and off all watching.
    // (As a convenient way to control it all together.)
    if Config.BoolDefault("watch", true) {
        MainWatcher = NewWatcher()
        Filters = append([]Filter{WatchFilter}, Filters...)
    }

    // If desired (or by default), create a watcher for templates and routes.
    // The watcher calls Refresh() on things on the first request.
    if MainWatcher != nil && Config.BoolDefault("watch.templates", true) {
        MainWatcher.Listen(MainTemplateLoader, MainTemplateLoader.paths...)
    } else {
        MainTemplateLoader.Refresh()
    }

    Server = &http.Server{
        Addr:    address,
        Handler: http.HandlerFunc(handle),
    }

    runStartupHooks()

    go func() {
        time.Sleep(100 * time.Millisecond)
        fmt.Printf("Listening on port %d...\n", port)
    }()

    if HttpSsl {
        ERROR.Fatalln("Failed to listen:",
            Server.ListenAndServeTLS(HttpSslCert, HttpSslKey))
    } else {
        ERROR.Fatalln("Failed to listen:", Server.ListenAndServe())
    }
}

func OnAppStart(f func()) {
    startupHooks = append(startupHooks, f)
}