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/[glib, gobject, gtk, gio]
|
||||||
import gintro/gdk except Window
|
import gintro/gdk except Window
|
||||||
import argparse except run
|
import argparse except run
|
||||||
import providers, serverNew, common
|
import providers, server, common
|
||||||
|
|
||||||
const
|
const
|
||||||
css = slurp("resources/app.css")
|
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
|
import common
|
||||||
|
|
||||||
|
const
|
||||||
|
index = slurp("resources/index.html")
|
||||||
|
style = slurp("resources/site.css")
|
||||||
|
script = slurp("resources/script.js")
|
||||||
|
|
||||||
type
|
type
|
||||||
ServerArgs* = object of RootObj
|
ServerArgs* = object of RootObj
|
||||||
verbose: bool
|
verbose*: bool
|
||||||
|
port*: int
|
||||||
|
|
||||||
var
|
var
|
||||||
chan*: Channel[CommandMessage]
|
chan*: Channel[CommandMessage]
|
||||||
verbose: bool
|
verbose: bool
|
||||||
|
|
||||||
proc newServerArgs*(verbose: bool): ServerArgs =
|
|
||||||
ServerArgs(verbose: verbose)
|
|
||||||
|
|
||||||
proc log(things: varargs[string, `$`]) =
|
proc log(things: varargs[string, `$`]) =
|
||||||
if verbose:
|
if verbose:
|
||||||
echo things.join()
|
echo things.join()
|
||||||
|
|
||||||
proc closeServer*() =
|
router randopixRouter:
|
||||||
## Sends a "Close" command to the server
|
get "/":
|
||||||
var socket = newSocket()
|
log "Access from ", request.ip
|
||||||
socket.connect("127.0.0.1", Port(defaultPort))
|
resp index
|
||||||
let c = newCommand(cClose)
|
|
||||||
socket.send(c.wrap)
|
get "/style":
|
||||||
socket.close()
|
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.} =
|
proc runServer*[ServerArgs](arg: ServerArgs) {.thread, nimcall.} =
|
||||||
verbose = arg.verbose
|
verbose = arg.verbose
|
||||||
var server = net.newSocket()
|
let port = Port(arg.port)
|
||||||
server.bindAddr(Port(defaultPort))
|
let settings = newSettings(port=port)
|
||||||
server.listen()
|
var server = initJester(randopixRouter, settings=settings)
|
||||||
log "Control server is listening"
|
server.serve()
|
||||||
|
|
||||||
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()
|
|
||||||
@@ -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