tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/net/webstatic/main.go (about)

     1  // This example is an HTTP server serving up a static file system
     2  //
     3  // Note: It may be necessary to increase the stack size when using "net/http".
     4  // Use the -stack-size=4KB command line option.
     5  
     6  //go:build ninafw || wioterminal
     7  
     8  package main
     9  
    10  import (
    11  	"embed"
    12  	"log"
    13  	"net/http"
    14  	"time"
    15  
    16  	"tinygo.org/x/drivers/netlink"
    17  	"tinygo.org/x/drivers/netlink/probe"
    18  )
    19  
    20  var (
    21  	ssid string
    22  	pass string
    23  	port string = ":80"
    24  )
    25  
    26  //go:embed index.html main.go images
    27  var fs embed.FS
    28  
    29  func main() {
    30  	// wait a bit for console
    31  	time.Sleep(2 * time.Second)
    32  
    33  	link, _ := probe.Probe()
    34  
    35  	err := link.NetConnect(&netlink.ConnectParams{
    36  		Ssid:       ssid,
    37  		Passphrase: pass,
    38  	})
    39  	if err != nil {
    40  		log.Fatal(err)
    41  	}
    42  
    43  	hfs := http.FileServer(http.FS(fs))
    44  	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    45  		hfs.ServeHTTP(w, r)
    46  	})
    47  
    48  	log.Fatal(http.ListenAndServe(port, nil))
    49  }