4267a229创建于 2024年8月2日历史提交
/*
    Copyright (c) [2023] [squallzhao]
    fountain is licensed under APACHE LICENSE, VERSION 2.0.
    You can use this software according to the terms and conditions of the APACHE LICENSE, VERSION 2.0.
    You may obtain a copy of APACHE LICENSE, VERSION 2.0 at: https://www.apache.org/licenses/LICENSE-2.0
*/
package microservice.web;
import microservice.web.server.*
import microservice.trace.*
import microservice.registry.*
import microservice.common.*

public open class Application{
   let router = DefaultRouter()
   let srv = CannonServer(router)
   var appName = "application"
   let defaultMaxConnections :Int32 = 10000
   
   public open func run(): Unit{
      srv.start(defaultMaxConnections)
      Logger.info("Application start.")
   }

   public func port(port: UInt16): Application{
       srv.setPort(port)
       return this
   }

   public func name(name: String): Application{
       this.appName = name
       return this
   }

   public func getRouter():IRouter {
      return router
   }

   public func getCannonServer(): CannonServer{
      return srv;
   }

   public func registry(r: IRegistry): Application{
        spawn { =>
            while(true) {
                Common.sleeps(Common.registryInterval)
                r.heartBeat(this.appName,Int64(srv.getPort()))
            }
        }
        Common.sleeps(Common.registryFirstInterval)
        r.register(this.appName,Int64(srv.getPort()))
        Logger.info("Application registed.")
        return this;
  }
}