Finish new server
This commit is contained in:
@@ -1,4 +0,0 @@
|
||||
import pixctrl
|
||||
|
||||
proc doRefresh*() =
|
||||
refresh()
|
||||
@@ -2,7 +2,7 @@ import os, options, strformat
|
||||
import gintro/[glib, gobject, gtk, gio]
|
||||
import gintro/gdk except Window
|
||||
import argparse except run
|
||||
import providers, serverNew, common
|
||||
import providers, server, common
|
||||
|
||||
const
|
||||
css = slurp("resources/app.css")
|
||||
|
||||
0
src/resources/script.js
Normal file
0
src/resources/script.js
Normal file
@@ -1,60 +1,46 @@
|
||||
import net, json, strutils
|
||||
import asyncdispatch, strutils, json
|
||||
import jester
|
||||
import common
|
||||
|
||||
const
|
||||
index = slurp("resources/index.html")
|
||||
style = slurp("resources/site.css")
|
||||
script = slurp("resources/script.js")
|
||||
|
||||
type
|
||||
ServerArgs* = object of RootObj
|
||||
verbose: bool
|
||||
verbose*: bool
|
||||
port*: int
|
||||
|
||||
var
|
||||
chan*: Channel[CommandMessage]
|
||||
verbose: bool
|
||||
|
||||
proc newServerArgs*(verbose: bool): ServerArgs =
|
||||
ServerArgs(verbose: verbose)
|
||||
|
||||
proc log(things: varargs[string, `$`]) =
|
||||
if verbose:
|
||||
echo things.join()
|
||||
|
||||
proc closeServer*() =
|
||||
## Sends a "Close" command to the server
|
||||
var socket = newSocket()
|
||||
socket.connect("127.0.0.1", Port(defaultPort))
|
||||
let c = newCommand(cClose)
|
||||
socket.send(c.wrap)
|
||||
socket.close()
|
||||
router randopixRouter:
|
||||
get "/":
|
||||
log "Access from ", request.ip
|
||||
resp index
|
||||
|
||||
get "/style":
|
||||
resp(style, contentType="text/css")
|
||||
|
||||
get "/script":
|
||||
resp(script, contentType="text/javascript")
|
||||
|
||||
post "/":
|
||||
let json = request.body.parseJson
|
||||
let msg = json.to(CommandMessage)
|
||||
# Pass command from client to main applicaiton
|
||||
chan.send(msg)
|
||||
resp Http200
|
||||
|
||||
proc runServer*[ServerArgs](arg: ServerArgs) {.thread, nimcall.} =
|
||||
verbose = arg.verbose
|
||||
var server = net.newSocket()
|
||||
server.bindAddr(Port(defaultPort))
|
||||
server.listen()
|
||||
log "Control server is listening"
|
||||
|
||||
while true:
|
||||
# Process client requests
|
||||
var client = net.newSocket()
|
||||
server.accept(client)
|
||||
log "Client connected"
|
||||
try:
|
||||
var line = client.recvLine()
|
||||
if line == "":
|
||||
log "No data from client"
|
||||
continue
|
||||
|
||||
var jsonData = parseJson(line)
|
||||
let msg = jsonData.to(CommandMessage)
|
||||
case msg.command
|
||||
of cClose:
|
||||
log "Server recieved termination command. Exiting."
|
||||
break
|
||||
else:
|
||||
# Pass command from client to main applicaiton
|
||||
chan.send(msg)
|
||||
|
||||
except OSError:
|
||||
log "Server error: ", getCurrentExceptionMsg()
|
||||
except:
|
||||
log "Invalid command from client: ", getCurrentExceptionMsg()
|
||||
log repr(getCurrentException())
|
||||
server.close()
|
||||
let port = Port(arg.port)
|
||||
let settings = newSettings(port=port)
|
||||
var server = initJester(randopixRouter, settings=settings)
|
||||
server.serve()
|
||||
@@ -1,46 +0,0 @@
|
||||
import asyncdispatch, strutils, json
|
||||
import jester
|
||||
import common
|
||||
|
||||
const
|
||||
index = slurp("resources/index.html")
|
||||
style = slurp("resources/site.css")
|
||||
script = ""
|
||||
|
||||
type
|
||||
ServerArgs* = object of RootObj
|
||||
verbose*: bool
|
||||
port*: int
|
||||
|
||||
var
|
||||
chan*: Channel[CommandMessage]
|
||||
verbose: bool
|
||||
|
||||
proc log(things: varargs[string, `$`]) =
|
||||
if verbose:
|
||||
echo things.join()
|
||||
|
||||
router randopixRouter:
|
||||
get "/":
|
||||
log "Access from ", request.ip
|
||||
resp index
|
||||
|
||||
get "/style":
|
||||
resp(style, contentType="text/css")
|
||||
|
||||
get "/script":
|
||||
resp(script, contentType="text/javascript")
|
||||
|
||||
post "/":
|
||||
let json = request.body.parseJson
|
||||
let msg = json.to(CommandMessage)
|
||||
# Pass command from client to main applicaiton
|
||||
chan.send(msg)
|
||||
resp Http200
|
||||
|
||||
proc runServer*[ServerArgs](arg: ServerArgs) {.thread, nimcall.} =
|
||||
verbose = arg.verbose
|
||||
let port = Port(arg.port)
|
||||
let settings = newSettings(port=port)
|
||||
var server = initJester(randopixRouter, settings=settings)
|
||||
server.serve()
|
||||
@@ -1 +0,0 @@
|
||||
threads:on
|
||||
Reference in New Issue
Block a user