Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
ringabout committed Oct 10, 2020
1 parent c1eef2b commit 57af739
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 19 deletions.
1 change: 0 additions & 1 deletion examples/app/app.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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", "")
)

Expand Down
11 changes: 11 additions & 0 deletions examples/appconfig/.config/config.debug.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"prologue": {
"address": "",
"port": 8080,
"debug": true,
"reusePort": true,
"appName": "",
"secretKey": "Set by yourself",
"bufSize": 40960
}
}
11 changes: 11 additions & 0 deletions examples/appconfig/.config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"prologue": {
"address": "",
"port": 8080,
"debug": true,
"reusePort": true,
"appName": "",
"secretKey": "Set by yourself",
"bufSize": 40960
}
}
11 changes: 11 additions & 0 deletions examples/appconfig/.config/config.production.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"prologue": {
"address": "",
"port": 8080,
"debug": false,
"reusePort": true,
"appName": "",
"secretKey": "Set by yourself",
"bufSize": 40960
}
}
9 changes: 9 additions & 0 deletions examples/appconfig/app.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import prologue

import ./urls


var app = newAppQueryEnv()
# Be careful with the routes.
app.addRoute(urls.urlPatterns, "")
app.run()
8 changes: 8 additions & 0 deletions examples/appconfig/urls.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import prologue

import ./views


let urlPatterns* = @[
pattern("/", hello)
]
5 changes: 5 additions & 0 deletions examples/appconfig/views.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import prologue


proc hello*(ctx: Context) {.async.} =
resp "<h1>Hello, Prologue!</h1>"
70 changes: 53 additions & 17 deletions src/loguepkg/create.nim
Original file line number Diff line number Diff line change
@@ -1,39 +1,75 @@
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}
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:
let projName = name[0]
if dirExists(projName):
echo fmt"{projName} already exists!"
else:
initProject(projName)
initProject(projName, useConfig)
1 change: 0 additions & 1 deletion src/loguepkg/tml/app.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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", "")
)
Expand Down
11 changes: 11 additions & 0 deletions src/loguepkg/tml/appconf.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const appConfStr* = """
import prologue
import ./urls
var app = newAppQueryEnv()
# Be careful with the routes.
app.addRoute(urls.urlPatterns, "")
app.run()
"""

0 comments on commit 57af739

Please sign in to comment.