Skip to content

Commit

Permalink
Updated to use net.http instead of vweb
Browse files Browse the repository at this point in the history
  • Loading branch information
ulises-jeremias committed Oct 17, 2023
1 parent 356eaf8 commit 84d3330
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions plot/show.v
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module plot
import json
import net
import net.http
import os
import time

// port is the port to run the server on. If 0, it will run on the next available port.
Expand All @@ -29,19 +30,25 @@ fn (handler PlotlyHandler) handle(req http.Request) http.Response {
return r
}

// show starts a web server and opens a browser window to display the plot.
pub fn (plot Plot) show() ! {
$if test ? {
println('Ignoring plot.show() because we are running in test mode')
} $else {
ch := chan int{}
mut server := &http.Server{
accept_timeout: 1 * time.second
port: plot.port
handler: PlotlyHandler{
plot: plot
ch: ch
}
}
t := spawn server.listen_and_serve()
for server.status() != .running {
time.sleep(10 * time.millisecond)
}
os.open_uri('http://localhost:${plot.port}')!
_ := <-ch
server.close()
t.wait()
Expand Down

0 comments on commit 84d3330

Please sign in to comment.