目录
目录README.md

JSONConfig 简体中文

This is a simple json based configuration library for Perfect.

Rather than presenting a flat json file for your configuration needs, this library lets you organize it a bit more.

Setup:

Include the JSONConfig dependency in your project’s Package.swift file:

.package(url: "https://github.com/AutomatonTec/JSONConfig.git", from: "1.1.3")

// section dependencies
dependencies: ["JSONConfig"]

Rebuild your Xcode project after changing your Package.swift file.

swift package generate-xcodeproj

Example usage:

import JSONConfig
// somewhere, perhaps in main.swift, determine the path to your config file

let configServerDefaults = "./config/server/common.json"
#if os(Linux)
    let configServer = "./config/server/linux.json"
#else
    let configServer = "./config/server/macOS.json"
#endif

let config = JSONConfig(withJsonAt:configServer, defaultsInJsonAt:configServerDefaults)

// somewhere, anywhere
func setupDatabase(withConfig config:JSONConfig) {
    MySQLConnector.host     = config.string(forKeyPath: "database.host", otherwise: "127.0.0.1")
    MySQLConnector.username = config.string(forKeyPath: "database.username", otherwise: "db_user")
    MySQLConnector.password = config.string(forKeyPath: "database.password", otherwise: "best_password")
    MySQLConnector.database = config.string(forKeyPath: "database.database", otherwise: "db_user")
}

func setupServer(withConfig config:JSONConfig, server:HTTPServer) {
    server.serverName = config.string(forKeyPath: "server.name", otherwise: "sub.your-domain.com")
    server.serverPort = UInt16(config.integer(forKeyPath: "server.port", otherwise: 8080))
}

In your common json configuration file, you might have something like:

{
    "server": {
        "name" : "api.your-domain.com",
        "port" : 80
    },
    "database": {
        "host":     "127.0.0.1",
        "username": "db_bob",
        "password": "bob_password",
        "database": "db_bob"
    }
}

And in your macOS json configuration file, you might have something like:

{
    "server": {
        "name" : "localhost",
        "port" : 8181
    }
}
关于
52.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

©Copyright 2023 CCF 开源发展委员会
Powered by Trustie& IntelliDE 京ICP备13000930号