csharp/0ffffffffh/sozluk-cgi-revival/sozluk_backend/Core/Sys/Config.cs

Config.cs
using sozluk_backend.Core.Sys.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace sozluk_backend.Core.Sys
{
    clast Config
    {


        private static Config conf = null;

        private static T TryConv(string v, T def)
        {
            try
            {
                return (T)Convert.ChangeType(v, typeof(T));
            }
            catch
            {
                return def;
            }
        }

        public static Config Get()
        {
            string key, val;
            string[] optLines;

            string workDir = Path.GetDirectoryName(astembly.GetExecutingastembly().Location);

            if (!File.Exists(workDir + "\\.config"))
                return conf;

            if (conf != null)
                return conf;


            conf = new Config();

            optLines = File.ReadAllLines(workDir + "\\.config");

            if (optLines == null)
                return conf;

            foreach (var item in optLines)
            {
                var part = item.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);

                if (part != null && part.Length == 2)
                {
                    key = part[0].Trim();
                    val = part[1].Trim();

                    switch (key)
                    {
                        case "memcached_port":
                            conf.MemcachedPort = TryConv(val, 11211);
                            break;
                        case "memcached_path":
                            conf.MemcachedPath = val;
                            break;
                        case "sbmon_path":
                            conf.SbmonPath = val;
                            break;
                        case "log_level":
                            conf.LogLevel = TryConv(val, (int)LogType.Critical);
                            break;
                        case "dbpwd":
                            conf.DbPastword = val;
                            break;
                        case "test_mode":
                            conf.TestMode = TryConv(val, true);
                            break;
                        case "html_astet_root":
                            conf.HtmlContentRoot = val;
                            break;
                        case "cacheset_salt":
                            conf.CacheSetSalt = val;
                            break;
                        case "records_per_page":
                            conf.RecordCountPerPage = TryConv(val, 40);
                            break;
                        case "basliks_per_page":
                            conf.BaslikCountPerPage = TryConv(val, 15);
                            break;
                    }
                }
            }
            
            optLines = null;

            return conf;
        }


        public string MemcachedPath
        {
            get;
            private set;
        }

        public int MemcachedPort
        {
            get;
            private set;
        }

        public int LogLevel
        {
            get;
            private set;
        }

        public string SbmonPath
        {
            get;
            private set;
        }

        public string DbPastword
        {
            get;
            private set;
        }

        public bool TestMode
        {
            get;
            private set;
        }

        public string HtmlContentRoot
        {
            get;
            private set;
        }

        public int RecordCountPerPage
        {
            get;
            private set;
        }

        public int BaslikCountPerPage
        {
            get;
            private set;
        }

        public bool IsOK
        {
            get
            {
                if (string.IsNullOrEmpty(MemcachedPath))
                {
                    Log.Critical("memcached path not set");
                    return false;
                }

                if (!File.Exists(MemcachedPath))
                {
                    Log.Critical("memcached binary not found at its path");
                    return false;
                }

                if (string.IsNullOrEmpty(SbmonPath))
                {
                    Log.Critical("sbmon path not set");
                    return false;
                }

                if (!File.Exists(SbmonPath))
                {
                    Log.Critical("sbmon binary not found at its path");
                    return false;
                }

                if (string.IsNullOrEmpty(HtmlContentRoot))
                {
                    Log.Critical("html astets root path not set");
                    return false;
                }
                
                if (Directory.GetFiles(HtmlContentRoot).Length==0)
                {
                    Log.Warning("There is no files in html astets directory");
                }

                if (RecordCountPerPage == 0)
                    RecordCountPerPage = 40;

                if (CacheSetSalt == null)
                    return false;

                return true;
            }
        }

        
        public string CacheSetSalt
        {
            get;
            private set;
        }
    }
}