일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- Jupyter Notebook
- ftp
- postgresql
- 28015
- appium
- nmap
- nohup
- perfect
- port forwarding
- centos
- GoCD
- create table
- Materials
- openpyxl
- mysql
- PYTHON
- ubuntu
- SWIFT
- sshpass
- appium server
- ssh
- 실행권한
- postgres
- rethinkdb
- kitura
- insert
- Jupyter
- STF
- nGrinder
- STF_PortForwarding
- Today
- Total
don't stop believing
json 파일로 config를 관리해 봅시다. 본문
빠르게 가겠습니다.
swift 버전을 확인합니다.
123$ swift -versionApple Swift version 4.0.3 (swiftlang-900.0.74.1 clang-900.0.39.2)Target: x86_64-apple-macosx10.9
프로젝트 폴더로 이동해 swift package init으로 초기화 합니다.
123456789$ cd ~/Documents/Test_Perfect/nGleServer008$ swift package init --type executableCreating executable package: nGleServer008Creating Package.swiftCreating README.mdCreating .gitignoreCreating Sources/Creating Sources/nGleServer008/main.swiftCreating Tests/
swift package 기본 폴더들이 생성된 것을 볼 수 있습니다.
xcodeproj 파일을 생성하고 열어줍니다.
12$ swift package generate-xcodeproj$ open nGleServer008.xcodeproj
xcode가 실행되면 Package.swift 파일에 Config를 사용할 만한 Package들을 잔득 넣습니다.
123456789101112131415161718192021// swift-tools-version:4.0import PackageDescriptionlet package = Package(name: "nGleServer008",dependencies: [.package(url: "https://github.com/PerfectlySoft/Perfect-HTTPServer.git", from: "3.0.0"),.package(url: "https://github.com/PerfectlySoft/Perfect-RequestLogger.git", from: "3.0.0"),.package(url: "https://github.com/PerfectlySoft/Perfect-Mustache.git", from: "3.0.0"),.package(url: "https://github.com/SwiftORM/Postgres-StORM.git", from: "3.0.0"),.package(url: "https://github.com/PerfectlySoft/Perfect-Session-Redis.git", from: "3.0.0"),],targets: [.target(name: "nGleServer008",dependencies: ["PerfectHTTPServer","PostgresStORM","PerfectMustache","PerfectSessionRedis"]),])
저장하고 swift build를 해줍니다.
그리고 빌드가 완료되면 Source/nGleServer008 폴더로 이동해 controller.swift 파일도 만들어 줍니다.
123$ swift build$ cd Sources/nGleServer008/$ touch controller.swift
xcodeproj를 다시 생성해주고 열어 봅니다.
12$ swift package generate-xcodeproj$ open nGleServer008.xcodeproj
xcode가 실행되면 main.swift파일로 이동해 아래와 같이 설정해 봅니다.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970import PerfectLibimport PerfectHTTPimport PerfectHTTPServerimport PerfectMustacheimport PerfectSessionimport PerfectSessionRedisimport PerfectRedisimport StORMimport PostgresStORMRedisSessionConnector.host = "192.168.1.12"RedisSessionConnector.password = "12345678"RedisSessionConnector.port = redisDefaultPortSessionConfig.name = "MacaronRedisDrivers"SessionConfig.idle = 60// OptionalSessionConfig.cookieDomain = "localhost"SessionConfig.IPAddressLock = trueSessionConfig.userAgentLock = trueSessionConfig.CSRF.checkState = trueSessionConfig.CORS.enabled = trueSessionConfig.CORS.acceptableHostnames.append("http://www.ngle.co.kr")SessionConfig.CORS.maxAge = 60let sessionDriver = SessionRedisDriver()let server = HTTPServer()server.setRequestFilters([sessionDriver.requestFilter])server.setResponseFilters([sessionDriver.responseFilter])// PostgreSQLPostgresConnector.host = "192.168.1.12"PostgresConnector.username = "macaron"PostgresConnector.password = "12345678"PostgresConnector.database = "macaron"PostgresConnector.port = 5432// PostgreSQL table setuplet setupWords = Words()try? setupWords.setup()try StudyController().putWordMaxCount()let setupPatterns = Patterns()try? setupPatterns.setup()try StudyController().putPatternMaxCount()let setupDialogues = Dialogues()try? setupDialogues.setup()try StudyController().putDialogueMaxCount()let setupEBS = EBS()try? setupEBS.setup()try StudyController().putEBSMaxCount()server.serverPort = 1026server.documentRoot = "webroot"let controller = Controller()server.addRoutes(Routes(controller.routes))let RC = RedisController()do {try server.start()} catch PerfectError.networkError(let err, let msg) {print("Network error thrown: \(err) \(msg)")}
실제 서버를 실행하려면 postgreSQL과 redis는 설치되어 있어야 합니다. 만약 없다면 기본 서버 정보만으로 진행할 수 있습니다.
기본 설정은 아래와 같습니다. 이 Post를 따라한다면 기본 설정으로 하시기 바랍니다.
1234567891011121314151617import PerfectLibimport PerfectHTTPimport PerfectHTTPServerlet server = HTTPServer()server.serverPort = 1026server.documentRoot = "webroot"let controller = Controller()server.addRoutes(Routes(controller.routes))do {try server.start()} catch PerfectError.networkError(let err, let msg) {print("Network error thrown: \(err) \(msg)")}
그 다음 controller.swift에서 아래와 같이 작성합니다.
123456789101112131415161718192021222324252627282930313233import Foundationimport PerfectLibimport PerfectHTTPimport PerfectHTTPServerfinal class Controller {var routes: [Route] {return [Route(method: .get, uri: "/heartbeat", handler: heartbeat)]}func heartbeat(request: HTTPRequest, response: HTTPResponse) {let data = ["message": "macaron api is alive."] as [String: Any]do {try response.setBody(json: ["result": 0, "data": data, "ts": Date().ticks]).setHeader(.contentType, value: "application/json").completed()} catch {response.setBody(string: "Error handling request: \(error)").completed(status: .internalServerError)}}}extension Date {var ticks: UInt64 {return UInt64((self.timeIntervalSince1970 + 62_135_596_800) * 10_000_000)}}
xcode의 Edit Scheme...에서 Run의 Options에 Working Directory를 프로젝트 폴더로 잡아줍니다.
그리고 실행을 하면 아래와 같이 서버 실행 로그가 출력 됩니다.
브라우저를 열고 http://localhost:1026/heartheat을 호출하면 아래처럼 json 데이터가 리턴됩니다.
이제 json 파일을 이용한 configuration을 시작해 봅시다.
프로젝트 폴더에서 Source/nGleServer008로 이동 후 Config.swift 파일을 생성합니다.
그리고 config 파일로 사용할 json 파일도 만들어 보겠습니다. 프로젝트 폴더 root에 Config라고 폴더를 만듭니다. 그리고 Config 폴더 안에 아래 두 json 파일을 만들어 줍니다.
ServerConfiguration.json
ServerConfigurationLinux.json
1234567$ cd Sources/nGleServer008/$ touch Config.swift$ cd ../..$ mkdir Config$ cd Config$ touch ServerConfiguration.json$ touch ServerConfigurationLinux.json
이제 다시 xcodeproj를 다시 생성해 줍니다.
12$ swift package generate-xcodeproj$ open nGleServer008.xcodeproj/
Config.swift 파일에 아래와 같이 작성합니다.
123456789101112131415161718192021222324252627282930313233343536import Foundationimport PerfectLibpublic struct JSONConfig {public enum ConfigError: Error {case FileNotWrittencase FileDoesNotExist}let name: Stringpublic init?(name:String) {self.name = namelet thisFile = File(name)if thisFile.exists == false {print("Settings file does not exist")}}public func getValues() -> Dictionary?{let thisFile = File(name)do {try thisFile.open(.read, permissions: .readUser)defer { thisFile.close() }let txt = try thisFile.readString()let dict = try txt.jsonDecode() as! Dictionaryreturn dict} catch {print(error)return .none}}}
그리고 바로 밑에 json 파일에서 데이터를 불러오는 class를 만들어 줍니다.
1234567891011121314151617181920212223class Config {func set() {#if os(Linux)let configPath = "/Home/macaron/Config/ServerConfigurationLinux.json"#elselet configPath = "./Config/ServerConfiguration.json"#endifif let configData = JSONConfig(name: configPath) {if let conf = configData.getValues() {let serverConf = conf["server"] as![String: Any]print("\(serverConf)")// Required Valuesserver.serverPort = UInt16(serverConf["serverPort"] as! Int)server.documentRoot = serverConf["documentRoot"] as! String}} else {print("Unable to get Configuration")}}}
이제 json 파일에 config 정보를 추가할 차례입니다.
ServerConfiguration.json과 ServerConfigurationLinux.json 두 파일에 동일하게 아래와 같이 추가해 줍니다.
123456{"server" : {"serverPort": 1026,"documentRoot": "webroot"}}
마지막으로 main.swift 파일에서 설정부분을 주석처리하고 Config.set()을 추가합니다.
1234567891011121314151617181920import PerfectLibimport PerfectHTTPimport PerfectHTTPServerlet server = HTTPServer()let config = Config()config.set()// server.serverPort = 1026// server.documentRoot = "webroot"let controller = Controller()server.addRoutes(Routes(controller.routes))do {try server.start()} catch PerfectError.networkError(let err, let msg) {print("Network error thrown: \(err) \(msg)")}
xcode를 실행하면 아래와 같이 정상적으로 서버 포트를 불러 오는 걸 확인할 수 있습니다.
session, redis, postgresql 등 설정이 많은 경우 아래와 같이 json 파일로 설정할 수 있습니다.
12345678910111213141516171819202122232425{"server": {"serverPort": 10260,"documentRoot": "webroot"},"RedisSessionConnector": {"host": "192.168.1.12","password": "12345678","port": 6379},"SessionConfig": {"name": "MacaronRedisDrivers","idle": 60,"cookieDomain": "localhost","IPAddressLock": true,"userAgentLock": true},"PostgresConnector": {"host": "192.168.1.12","username": "macaron","password": "12345678","database": "macaron","port": 5432}}
configuration.swift 파일의 Config 클레스에는 아래와 같이 json 데이터를 불러와 설정해 주고 Log.info()로 확인해 줄 수 있습니다.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687class Config {func set() {#if os(Linux)let configPath = "/home/macaron/macaron-api/Config/ServerConfigurationLinux.json"#elselet configPath = "./Config/ServerConfiguration.json"#endifif let configData = JSONConfig(name: configPath) {if let conf = configData.getValues() {Log.info(message: "Macaron API Server Configuration Info \n")// Server Infolet serverConf = conf["server"] as![String: Any]server.serverPort = UInt16(serverConf["serverPort"] as! Int)Log.info(message: "server.serverPort = \(String(describing: serverConf["serverPort"]!))")server.documentRoot = serverConf["documentRoot"] as! StringLog.info(message: "server.documentRoot = \(String(describing: serverConf["documentRoot"]!)) \n")// RedisSessionConnector Infolet redisSessionConf = conf["RedisSessionConnector"] as![String: Any]RedisSessionConnector.host = redisSessionConf["host"] as! StringLog.info(message: "RedisSessionConnector.host = \(String(describing: redisSessionConf["host"]!))")RedisSessionConnector.password = redisSessionConf["password"] as! StringLog.info(message: "RedisSessionConnector.password = \(String(describing: redisSessionConf["password"]!))")RedisSessionConnector.port = redisSessionConf["port"] as! IntLog.info(message: "RedisSessionConnector.port = \(String(describing: redisSessionConf["port"]!)) \n")// SessionConfig Infolet SessionConf = conf["SessionConfig"] as![String: Any]SessionConfig.name = SessionConf["name"] as! StringLog.info(message: "SessionConfig.name = \(String(describing: SessionConf["name"]!))")SessionConfig.idle = SessionConf["idle"] as! IntLog.info(message: "SessionConfig.idle = \(String(describing: SessionConf["idle"]!))")SessionConfig.cookieDomain = SessionConf["cookieDomain"] as! StringLog.info(message: "SessionConfig.cookieDomain = \(String(describing: SessionConf["cookieDomain"]!))")SessionConfig.IPAddressLock = SessionConf["IPAddressLock"] as! BoolLog.info(message: "SessionConfig.IPAddressLock = \(String(describing: SessionConf["IPAddressLock"]!))")SessionConfig.userAgentLock = SessionConf["userAgentLock"] as! BoolLog.info(message: "SessionConfig.userAgentLock = \(String(describing: SessionConf["userAgentLock"]!))")SessionConfig.CSRF.checkState = trueLog.info(message: "SessionConfig.CSRF.checkState = true")SessionConfig.CORS.enabled = trueLog.info(message: "SessionConfig.CORS.enabled = true")SessionConfig.CORS.acceptableHostnames.append("http://www.test-cors.org")Log.info(message: "SessionConfig.CORS.acceptableHostnames.append(\"http://www.test-cors.org\")")SessionConfig.CORS.maxAge = 60Log.info(message: "SessionConfig.CORS.maxAge = 60 \n")// PostgreSQLlet PostgresConnConf = conf["PostgresConnector"] as![String: Any]PostgresConnector.host = PostgresConnConf["host"] as! StringLog.info(message: "PostgresConnector.host = \(String(describing: PostgresConnConf["host"]!))")PostgresConnector.username = PostgresConnConf["username"] as! StringLog.info(message: "PostgresConnector.username = \(String(describing: PostgresConnConf["username"]!))")PostgresConnector.password = PostgresConnConf["password"] as! StringLog.info(message: "PostgresConnector.password = \(String(describing: PostgresConnConf["password"]!))")PostgresConnector.database = PostgresConnConf["database"] as! StringLog.info(message: "PostgresConnector.database = \(String(describing: PostgresConnConf["database"]!))")PostgresConnector.port = PostgresConnConf["port"] as! IntLog.info(message: "PostgresConnector.port = \(String(describing: PostgresConnConf["port"]!)) \n")}} else {print("Unable to get Configuration")}}}
여기까지 json 파일을 이용한 서버 configuration 이었습니다.
'Swift > Perfect' 카테고리의 다른 글
Perfect에서 여러 방법으로 데이터 전달하기 (HTTPRequest) (0) | 2018.06.09 |
---|---|
이미지 업로드와 static file (이미지, html) 보기 (0) | 2018.02.07 |
Perfect로 redis에 연결해 봅시다. 2 (0) | 2018.01.22 |
Perfect로 redis에 연결해 봅시다. 1 (0) | 2018.01.17 |
[macaron 2] 빌드와 배포 환경 만들기 (Gitlab, Jenkins, ssh) (0) | 2018.01.08 |