/*
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 net.http.*
import std.time.*
import microservice.registry.*
import microservice.common.*
public class WebServer{
var srv : Server
var appName = "webserver"
public init(port: UInt16, readTimeout: Duration, writeTimeout: Duration){
srv = ServerBuilder()
.port(port)
.readTimeout(readTimeout)
.writeTimeout(writeTimeout)
.build()
}
public func start(port: UInt16): WebServer{
//srv.readTimeout = (3*Duration.second)
//srv.writeTimeout = (5*Duration.second)
srv.serve()
return this;
}
public func stop():WebServer{
srv.close()
return this;
}
public func handle(pattern: String, funcHandler: FuncHandler): WebServer{
srv.distributor.register(pattern,funcHandler);
return this;
}
public func setAppName(appName: String): WebServer{
this.appName = appName;
return this;
}
public func registry(r: IRegistry): WebServer{
spawn { =>
Common.sleeps(Common.registryFirstInterval)
r.register(this.appName,Int64(srv.port))
while(true){
Common.sleeps(Common.registryInterval)
r.heartBeat(this.appName,Int64(srv.port))
}
}
return this;
}
}