diff --git a/examples/app/app.nim b/examples/app/app.nim index 242106d..5a61467 100644 --- a/examples/app/app.nim +++ b/examples/app/app.nim @@ -7,7 +7,6 @@ let settings = newSettings(appName = env.getOrDefault("appName", "Prologue"), debug = env.getOrDefault("debug", true), port = Port(env.getOrDefault("port", 8080)), - staticDirs = [], secretKey = env.getOrDefault("secretKey", "") ) diff --git a/examples/appconfig/.config/config.debug.json b/examples/appconfig/.config/config.debug.json new file mode 100644 index 0000000..7ed3c6b --- /dev/null +++ b/examples/appconfig/.config/config.debug.json @@ -0,0 +1,11 @@ +{ + "prologue": { + "address": "", + "port": 8080, + "debug": true, + "reusePort": true, + "appName": "", + "secretKey": "Set by yourself", + "bufSize": 40960 + } +} \ No newline at end of file diff --git a/examples/appconfig/.config/config.json b/examples/appconfig/.config/config.json new file mode 100644 index 0000000..7ed3c6b --- /dev/null +++ b/examples/appconfig/.config/config.json @@ -0,0 +1,11 @@ +{ + "prologue": { + "address": "", + "port": 8080, + "debug": true, + "reusePort": true, + "appName": "", + "secretKey": "Set by yourself", + "bufSize": 40960 + } +} \ No newline at end of file diff --git a/examples/appconfig/.config/config.production.json b/examples/appconfig/.config/config.production.json new file mode 100644 index 0000000..acbafdd --- /dev/null +++ b/examples/appconfig/.config/config.production.json @@ -0,0 +1,11 @@ +{ + "prologue": { + "address": "", + "port": 8080, + "debug": false, + "reusePort": true, + "appName": "", + "secretKey": "Set by yourself", + "bufSize": 40960 + } +} \ No newline at end of file diff --git a/examples/appconfig/app.nim b/examples/appconfig/app.nim new file mode 100644 index 0000000..0612791 --- /dev/null +++ b/examples/appconfig/app.nim @@ -0,0 +1,9 @@ +import prologue + +import ./urls + + +var app = newAppQueryEnv() +# Be careful with the routes. +app.addRoute(urls.urlPatterns, "") +app.run() diff --git a/examples/appconfig/urls.nim b/examples/appconfig/urls.nim new file mode 100644 index 0000000..afd373c --- /dev/null +++ b/examples/appconfig/urls.nim @@ -0,0 +1,8 @@ +import prologue + +import ./views + + +let urlPatterns* = @[ + pattern("/", hello) +] diff --git a/examples/appconfig/views.nim b/examples/appconfig/views.nim new file mode 100644 index 0000000..d9cee4f --- /dev/null +++ b/examples/appconfig/views.nim @@ -0,0 +1,5 @@ +import prologue + + +proc hello*(ctx: Context) {.async.} = + resp "

Hello, Prologue!

" diff --git a/src/loguepkg/create.nim b/src/loguepkg/create.nim index 71183ff..9c4d679 100644 --- a/src/loguepkg/create.nim +++ b/src/loguepkg/create.nim @@ -1,11 +1,46 @@ -import os, strformat -import tml / [app, urls, views] +import std / [os, strformat, json] +import tml / [app, urls, views, appconf] -proc getEnvContent(appName: string): string = +let + defaultConfig = %* {"prologue": { + "address": "", + "port": 8080, + "debug": true, + "reusePort": true, + "appName": "", + "secretKey": "Set by yourself", + "bufSize": 40960 + } + } + + defaultDebugConfig = %* {"prologue": { + "address": "", + "port": 8080, + "debug": true, + "reusePort": true, + "appName": "", + "secretKey": "Set by yourself", + "bufSize": 40960 + } + } + + defaultProductionConfig = %* {"prologue": { + "address": "", + "port": 8080, + "debug": false, + "reusePort": true, + "appName": "", + "secretKey": "Set by yourself", + "bufSize": 40960 + } + } + + +func getEnvContent(appName: string): string = result = fmt"""# Don't commit this to source control. # Eg. Make sure ".env" in your ".gitignore" file. -# If you want to release you programs, make sure debug=false. +# If you want to release you programs, make sure set debug=false. debug=true port=8080 appName={appName} @@ -13,22 +48,23 @@ secretKey=Pr435ol67ogue """ -proc initProject(projName: string) = +proc initProject(projName: string, useConfig: bool) = createDir(projName) - let - appFile = projName / "app.nim" - urlsFile = projName / "urls.nim" - viewsFile = projName / "views.nim" - envFile = projName / ".env" + writeFile(projName / "urls.nim", urlsStr) + writeFile(projName / "views.nim", viewsStr) - # let path = currentSourcePath().splitPath.head + if not useConfig: + writeFile(projName / "app.nim", appStr) + writeFile(projName / ".env", getEnvContent(projName)) + else: + writeFile(projName / "app.nim", appConfStr) + createDir(projName / ".config") + writeFile(projName / ".config" / "config.json", pretty defaultConfig) + writeFile(projName / ".config" / "config.debug.json", pretty defaultDebugConfig) + writeFile(projName / ".config" / "config.production.json", pretty defaultProductionConfig) - writeFile(appFile, appStr) - writeFile(urlsFile, urlsStr) - writeFile(viewsFile, viewsStr) - writeFile(envFile, getEnvContent(projName)) -proc init*(name: seq[string]) = +proc init*(name: seq[string], useConfig = false) = if name.len == 0: echo "Please give the name of your project!" else: @@ -36,4 +72,4 @@ proc init*(name: seq[string]) = if dirExists(projName): echo fmt"{projName} already exists!" else: - initProject(projName) + initProject(projName, useConfig) diff --git a/src/loguepkg/tml/app.nim b/src/loguepkg/tml/app.nim index dde68a8..0671495 100644 --- a/src/loguepkg/tml/app.nim +++ b/src/loguepkg/tml/app.nim @@ -8,7 +8,6 @@ let settings = newSettings(appName = env.getOrDefault("appName", "Prologue"), debug = env.getOrDefault("debug", true), port = Port(env.getOrDefault("port", 8080)), - staticDirs = [], secretKey = env.getOrDefault("secretKey", "") ) diff --git a/src/loguepkg/tml/appconf.nim b/src/loguepkg/tml/appconf.nim new file mode 100644 index 0000000..be056a1 --- /dev/null +++ b/src/loguepkg/tml/appconf.nim @@ -0,0 +1,11 @@ +const appConfStr* = """ +import prologue + +import ./urls + + +var app = newAppQueryEnv() +# Be careful with the routes. +app.addRoute(urls.urlPatterns, "") +app.run() +""" \ No newline at end of file