Curl and Go manipulating windows apps

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!!

A simple web server can listen the request (you decide the request format) and do exec.Command with VLC or whatever :slight_smile:

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

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.

  1. address to open chrome or vlc on Windows = data 1
  2. 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.

You can pass any parameters you want. Of course you will need some frontend. Look over templates for this :wink:

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.

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

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.

Ok, a more comprehensive example below. This work on Windows (not may favorite system :stuck_out_tongue_winking_eye:).

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

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.

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

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.

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.

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

Thanks!

Go will use all threads/CPU cores of the computer automatically with that code or I need to create go routines?

Thanks.

A more detailed explanation about using cores that will clarify you more is here

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.
`

You can send one form with many inputs.

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()
   <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">

:man_facepalming:
Use browser addr1 addr2... not browser addr browser addr...