main.go 978 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package main
  2. import (
  3. "embed"
  4. "encoding/json"
  5. "net/http"
  6. "github.com/tobychui/naffg"
  7. )
  8. //go:embed res/*
  9. var uiFiles embed.FS
  10. func main() {
  11. app := naffg.NewApplication(&naffg.Options{
  12. Title: "UI events example",
  13. Width: 400,
  14. Height: 300,
  15. Resizable: false,
  16. UiRes: &uiFiles, //Embed the UI resources
  17. UiResPreix: "res", //The folder where the resources is embeded from
  18. })
  19. //Print the embedded files
  20. app.Debug_PrintEmbeddedFiles()
  21. //Handle the API, for example /api/hello?name=John
  22. app.Mux().HandleFunc("/api/hello", func(w http.ResponseWriter, r *http.Request) {
  23. //Get the name from the query
  24. name := r.URL.Query().Get("name")
  25. if name == "" {
  26. name = "Who are you?"
  27. } else {
  28. name = "Hello, " + name
  29. }
  30. //Write the response
  31. js, _ := json.Marshal(name)
  32. w.Header().Set("Content-Type", "application/json")
  33. w.Write([]byte(js))
  34. })
  35. // Run the application
  36. app.Run()
  37. }