derekG
(Derek Goslav)
August 29, 2017, 3:40am
1
Hi everybody,
Hoiw are you?
I was wondering, how would we implement the following scenario:
A curl POST call from an external computer/cell to triggering an event on a GO server.
This event makes GO open chrome or VLC and play a video file?
Thanks!!
geosoft1
(George Calianu)
August 29, 2017, 5:07am
2
A simple web server can listen the request (you decide the request format) and do exec.Command
with VLC or whatever
Code example (on Linux)
package main
import (
"net/http"
"os/exec"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
exec.Command("/opt/google/chrome/google-chrome", "http://google.com").Start()
println("ok")
})
http.ListenAndServe(":8080", nil)
}
1 Like
derekG
(Derek Goslav)
August 29, 2017, 6:24am
3
Awesome, thanks!!!
I would like to put the chrome’s address plus parameters and ask GO to open other apps, like VLC dynamically from another computer.
Like a HTML page with a form.
address to open chrome or vlc on Windows = data 1
parameters, to open fullscreen, webpage address, etc = data 2
This is something I was wondering for a long time…
for instance, in your example:
exec.Command({{data 1}}}}, {{“data 2”)}} ? is this like databinding? Sorry I don’t know the names.
exec.Command("/opt/google/chrome/google-chrome", “http://google.com ”)
Thanks.
geosoft1
(George Calianu)
August 29, 2017, 7:16am
4
You can pass any parameters you want. Of course you will need some frontend. Look over templates for this
derekG
(Derek Goslav)
August 29, 2017, 11:18pm
5
Thanks for your help George.
I understand the temp[late part.
I am just wondering how I would implement the link between the two html form fields with GO.
For example:
HTML Form Field 1: THE USER will input the the addrees of the application.
like so: “C:\Program Files (x86)\Google\Chrome\Application\chrome.exe”
HTML Form Field 2: THE USER will input the the address of on the web and some other parameters to open in fullscreen mode, like so:.
like so: “http://google.com ” “–kiosk”
As you demonstrated with pure GO code, I totally understand but I don’t know how to link both.
What should I put on those spots on Go code: on data 1 and data 2?
exec.Command({{data 1}}}}, {{“data 2”)}}
form your pure Go code: exec.Command("/opt/google/chrome/google-chrome", “http://google.com ”)
Thanks.
geosoft1
(George Calianu)
August 30, 2017, 5:20am
6
Like this?
package main
import (
"net/http"
"os/exec"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
exec.Command("/opt/google/chrome/google-chrome", r.FormValue("url")).Start()
println("ok")
})
http.ListenAndServe(":8080", nil)
}
Tested with:
curl -X POST -F 'url=google.com' http://localhost:8080
Send the form from frontend. Note that if you send the form in Json format it’s more complicated.
1 Like
derekG
(Derek Goslav)
August 30, 2017, 3:41pm
7
Thanks George.
That didn’t open Chrome on Windows 10. Does not need to insert where chrome is installed in my computer?
I would like to understand the logic behind it on how to achieve the following.
A user open a web page with two form fields.
On the first form field he inputs the application he wants to open on server, where Go is running.
Second, parameters, like a webpage URL if the application chosen was a browser.
Thanks.
geosoft1
(George Calianu)
August 31, 2017, 5:56am
8
Ok, a more comprehensive example below. This work on Windows (not may favorite system ).
package main
import (
"html/template"
"net/http"
"os/exec"
)
func main() {
t, _ := template.New("templ").ParseGlob("*.html")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
t.ExecuteTemplate(w, "form.html", nil)
})
http.HandleFunc("/start", func(w http.ResponseWriter, r *http.Request) {
exec.Command(r.FormValue("browser"), r.FormValue("url")).Start()
println("ok")
})
http.ListenAndServe(":8080", nil)
}
and template file (form.html)
<html>
<body>
<form action="/start">
<input type="text" name="browser" value="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe">
<input type="text" name="url" value="google.com">
<input type="submit" value="OK">
</form>
</body>
</html>
2 Likes
derekG
(Derek Goslav)
August 31, 2017, 7:08am
9
Hi George,
It works!! Thank you so much I totally understand all the code and started messing around to fully understand it!!
I was wondering how can we input some parameters to chrome, some command line parameters, like --kiosk, to open in fullscreen mode.
https://peter.sh/experiments/chromium-command-line-switches/
I created another form field but it doesn’t work to input this command but it doesn’t work.
I’ve tried opening a .exe and it works as well!!
Thanks again.
geosoft1
(George Calianu)
August 31, 2017, 10:02am
10
In a simplest way you can add another input in form with a parameter like this
<input type="text" name="p1" value="--kiosk">
and handle it in backend
exec.Command(r.FormValue("browser"), r.FormValue("p1"),r.FormValue("url")).Start()
Also, in the same manner you can add more parameters (p2,p3, and so on).
1 Like
derekG
(Derek Goslav)
September 1, 2017, 3:25am
11
Hi George,
Thank you so much for all your help.
There is no way to comunicate with the WINAPI and make the apps open in certain size and location on screen, right?
Thanks again.
derekG
(Derek Goslav)
September 1, 2017, 10:30pm
12
Hi George,
You think is possible to create several exec.Command , like 10 and all be trigger by one button?
Do you think is better to create a GO routine/channel to each Osc.exec?
Will the computer perform better opening the applications?
Thanks.
geosoft1
(George Calianu)
September 2, 2017, 8:10am
13
is no way to comunicate with the WINAPI
Maybe not native but are some solutions .
Sure is posible. Extend the interface as you wish.
Not really, i imagine that if you launch instantly many applications this will not happen too quick.
1 Like
derekG
(Derek Goslav)
September 2, 2017, 6:59pm
14
Thanks!
Go will use all threads/CPU cores of the computer automatically with that code or I need to create go routines?
Thanks.
geosoft1
(George Calianu)
September 2, 2017, 7:59pm
15
A more detailed explanation about using cores that will clarify you more is here
multithreading, go
derekG
(Derek Goslav)
September 5, 2017, 6:33pm
16
Hi George,
Sorry for this, I’ve tried several things, see some tutorials but I don’t know how I would solve this problem.
Problem:
4 forms like yours, with only on main difference all forms linked to one button. One button will trigger everything.
This button will trigger 4 funcs ( exec. command ).
`
I understand this must be done with interface so I could implement more features and grow in the future.
Each form must have one exec.command , this group(form+exec.command) x 4 will have one button to trigger all the actions.
Thanks.
`
geosoft1
(George Calianu)
September 6, 2017, 10:57am
17
You can send one form with many inputs.
derekG
(Derek Goslav)
September 6, 2017, 3:07pm
18
Hi George,
I’ve tried that but Chrome complains opening multiple files this way…
http.HandleFunc("/start", func(w http.ResponseWriter, r *http.Request) {
exec.Command(r.FormValue("browser"), r.FormValue("url"), r.FormValue("browsers"), r.FormValue("urls")).Start()
derekG
(Derek Goslav)
September 6, 2017, 3:09pm
19
<input type="text" name="browser" value="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe">
<input type="text" name="url" value="google.com">
<input type="text" name="browsers" value="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe">
<input type="text" name="urls" value="google.com">
geosoft1
(George Calianu)
September 7, 2017, 5:07am
20
Use browser addr1 addr2...
not browser addr browser addr...