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.config.*
import microservice.resttemplate.*
import microservice.common.*
import microservice.web.http11.*
import microservice.web.server.filter.*
import std.sync.*
import std.log.LogLevel
import microservice.registry.*
import std.convert.Parsable
import microservice.trace.*
import microservice.web.server.*

public class MicroBootApplication <: Application{
    var rgs = Option<IRegistry>.None;
    var rest = Option<RestTemplate>.None;

    public init(){
        this.name(ResourceFile.getProperty(Constant.SERVER_NAME).getOrThrow())
        this.port(UInt16.parse(ResourceFile.getProperty(Constant.SERVER_PORT).getOrThrow()))
        Http11DataProcessor.setHeaderLimit(Int64.parse(ResourceFile.getProperty(Constant.SERVER_MAX_HTTP_HEADER_SIZE).getOrThrow()))
        Http11DataProcessor.setBodyLimit(Int64.parse(ResourceFile.getProperty(Constant.SERVER_MAX_HTTP_BODY_SIZE).getOrThrow()))
        rgs = this.makeRegistry()
        rest = this.makeRestTemplate()
    }

    func makeRegistry(): IRegistry{
        var registryType = ResourceFile.getProperty(Constant.SERVER_REGISTRY_TYPE).getOrThrow()
        if (registryType == "eureka"){
            return EurekaRegistry();
        } else if (registryType == "consul"){
            return ConsulRegistry();
        } else if (registryType == "etcd"){
            return EtcdRegistry();
        } else if (registryType == "zookeeper"){
            return ZookeeperRegistry();
        } 
        return EurekaRegistry();
    }

    func makeRestTemplate(): RestTemplate{
        return RestTemplate(rgs.getOrThrow())
    }

    public func getRestTemplate(): RestTemplate{
        return rest.getOrThrow()
    }

    public override func run(): Unit{
        setLogging()
        FilterChain.addFilterToLast(FilterRouter(this.router));
        var cfg = Config()
        cfg.url = ResourceFile.getProperty(Constant.SERVER_REGISTRY_ADDRESS).getOrThrow()
        rgs.getOrThrow().config(cfg)
        registry(rgs.getOrThrow())
        Common.sleeps(Common.discoveryFirstInterval)
        rest.getOrThrow().start()
        spawn { =>
            var maxConnections:Int32 = 0
            var maxConnectionsText = ResourceFile.getProperty(Constant.SERVER_MAX_CONNECTIONS)
            if (let Some(v) <- maxConnectionsText){
                maxConnections = Int32.parse(v)
            }
            srv.start(maxConnections)  
        }
        Common.sleepms(10)
        Logger.info("MicroBootApplication start.")
    }

    private func setLogging(){
        var logging = ResourceFile.getProperty(Constant.SERVER_LOGGING_LEVEL).getOrThrow()
        if (logging == "trace"){
            Logger.setLevel(LogLevel.TRACE)
        } else if (logging == "warn"){
            Logger.setLevel(LogLevel.WARN)
        } else if (logging == "debug"){
            Logger.setLevel(LogLevel.DEBUG)
        } else if (logging == "error"){
            Logger.setLevel(LogLevel.ERROR)
        } else if (logging == "all"){
            Logger.setLevel(LogLevel.ALL)
        } else if (logging == "off"){
            Logger.setLevel(LogLevel.OFF)
        } else{
            Logger.setLevel(LogLevel.INFO)
        }
        
    }
}