playground: add support to run inside docker

- fix the address bind to be configurable with cli
- add Dockerfile to run playground server

fixes #93
This commit is contained in:
jangko 2021-07-04 11:41:08 +07:00
parent ea4d15e25a
commit 5ed7fc0af1
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
4 changed files with 95 additions and 15 deletions

11
Dockerfile Normal file
View File

@ -0,0 +1,11 @@
FROM nimlang/nim:1.4.8
WORKDIR /app
COPY graphql.nimble .
RUN nimble install --depsOnly -y --verbose
COPY . .
RUN nim c -d:release playground/swserver
EXPOSE 8547
ENTRYPOINT ./playground/swserver starwars --bind:0.0.0.0:8547

View File

@ -53,6 +53,12 @@ or
$ nim c -r playground/swserver ethereum
```
To run the playground server with docker, you can execute this commands:
```bash
$ docker build -t graphql .
$ docker run -p 8547:8547 -t graphql
```
The server is accessible at this address `http://127.0.0.1:8547/graphql`
and the web user interface at `http://127.0.0.1:8547/graphql/ui`.

64
playground/config.nim Normal file
View File

@ -0,0 +1,64 @@
# nim-graphql
# Copyright (c) 2021 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.
import
std/[parseopt, strutils, uri],
stew/results, chronos
type
Schema* = enum
starwars
ethereum
Configuration* = ref object
bindAddress*: TransportAddress
schema*: Schema
var testConfig {.threadvar.}: Configuration
const
defaultAddress = initTAddress("127.0.0.1:8547")
proc initConfiguration(): Configuration =
result = new Configuration
result.bindAddress = defaultAddress
result.schema = starwars
proc getConfiguration*(): Configuration {.gcsafe.} =
if isNil(testConfig):
testConfig = initConfiguration()
result = testConfig
proc processArguments*(): Result[Configuration, string] =
var
opt = initOptParser()
config = getConfiguration()
for kind, key, value in opt.getopt():
case kind
of cmdArgument:
try:
config.schema = parseEnum[Schema](key)
except Exception as e:
return err(e.msg)
of cmdLongOption, cmdShortOption:
case key.toLowerAscii()
of "bind", "b":
try:
config.bindAddress = initTAddress(value)
except Exception as e:
return err(e.msg)
else:
var msg = "Unknown option " & key
if value.len > 0: msg = msg & " : " & value
return err(msg)
of cmdEnd:
doAssert(false)
ok(config)

View File

@ -10,14 +10,9 @@
import
std/[os, strutils], chronos, chronicles,
../graphql, ../graphql/httpserver,
./swapi, ./ethapi
./swapi, ./ethapi, ./config
type
Schema = enum
starwars
ethereum
proc loadSchema(ctx: GraphqlRef, schema: Schema): GraphqlResult =
proc loadSchema(ctx: GraphqlRef, schema: config.Schema): GraphqlResult =
notice "loading graphql api", name = schema
var conf = defaultParserConf()
@ -30,22 +25,26 @@ proc loadSchema(ctx: GraphqlRef, schema: Schema): GraphqlResult =
ctx.initStarWarsApi()
ctx.parseSchemaFromFile("tests" / "schemas" / "star_wars_schema.ql", conf = conf)
const
address = initTAddress("127.0.0.1:8547")
proc main() =
var message: string
## Processing command line arguments
let r = processArguments()
if r.isErr:
debugEcho r.error
quit(QuitFailure)
let conf = r.get
let socketFlags = {ServerFlags.TcpNoDelay, ServerFlags.ReuseAddr}
var ctx = GraphqlRef.new()
let schema = if paramCount() == 0: starwars
else: parseEnum[Schema](paramStr(1))
let res = ctx.loadSchema(schema)
let res = ctx.loadSchema(conf.schema)
if res.isErr:
debugEcho res.error
return
let sres = GraphqlHttpServerRef.new(ctx, address, socketFlags = socketFlags)
let sres = GraphqlHttpServerRef.new(ctx, conf.bindAddress, socketFlags = socketFlags)
if sres.isErr():
debugEcho sres.error
return