FORUM CTRLX AUTOMATION
ctrlX World Partner Apps for ctrlX AUTOMATION
03-16-2021 04:11 PM
I'm trying to change the Go example to use a Unix socket as it is recommended. The main.go looks something like this (shortened):
import "github.com/gorilla/mux" var router *mux.Router = mux.NewRouter() func main() { router.HandleFunc("/my-snap/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello World") }) socketPath := path.Join(os.Getenv("SNAP_DATA"), "/package-run/my-snap/my-snap.web.sock") conn, err := net.Listen("unix", socketPath) if err != nil { log.Fatal("Could not listen on socket: ", err) return } defer conn.Close() log.Println("Webserver started...") log.Fatal(http.Serve(conn, router)) }
And the snapcraft.yml like this (also shortened):
name: my-snap apps: my-server: command: ./bin/webserver daemon: simple restart-condition: always plugs: [network-bind] parts: simple-webserver: source: webserver plugin: go configs: source: ./configs plugin: dump organize: 'package-assets/*': package-assets/${SNAPCRAFT_PROJECT_NAME}/ slots: package-assets: interface: content content: package-assets source: read: - $SNAP/package-assets/${SNAPCRAFT_PROJECT_NAME} package-run: interface: content content: package-run source: write: - $SNAP_DATA/package-run/${SNAPCRAFT_PROJECT_NAME} plugs: active-solution: interface: content content: solutions target: $SNAP_COMMON/solutions
However, when I look at the logbook, I see that the snap could not bind to the socket:
Could not listen on socket: listen unix /var/snap/my-snap/x1/package-run/my-snap/my-snap.web.sock: bind: permission denied
What am I missing here?
Solved! Go to Solution.
03-18-2021 12:22 PM
Hello jand,
i did not face the same issue, i try to build the same snap with a simple unix socket, and all worked fine.
You can find the test project in the attachments.
It also contains the snap for the virtual control.
Maybe you can share the difference with your project, thanks.
Regards Johannes
03-23-2021 01:43 PM
Okay, this took me a while to figure out, but the change that made my snap work was in the snapcraft.yaml file; adding the 5s restart delay.
apps: my-server: command: ./bin/webserver daemon: simple restart-condition: always passthrough: restart-delay: 5s plugs: [network-bind]
The first log entry is still
Could not listen on socket: listen unix /var/snap/my-snap/x1/package-run/my-snap/my-snap.web.sock: bind: no such file or directory
but after the 5 second delay the webserver starts and binds to the Unix socket...