Dear Community User! We are updating our platform to a new system.
Read more: Important information on the platform change.

cancel
Showing results for 
Search instead for 
Did you mean: 
SOLVED

Go server using Unix socket

Go server using Unix socket

jand
Member

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?

2 REPLIES 2

JohannesA
Established Member

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

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

Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist