main.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package main
  2. import (
  3. "io/ioutil"
  4. "log"
  5. "net/http"
  6. "os"
  7. "os/signal"
  8. "syscall"
  9. aroz "imuslab.com/arozos/demo/aroz"
  10. )
  11. var (
  12. handler *aroz.ArozHandler
  13. )
  14. /*
  15. Demo for showing the implementation of ArOZ Online Subservice Structure
  16. Proxy url is get from filepath.Dir(StartDir) of the serviceInfo.
  17. In this example, the proxy path is demo/*
  18. */
  19. //Kill signal handler. Do something before the system the core terminate.
  20. func SetupCloseHandler() {
  21. c := make(chan os.Signal, 2)
  22. signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  23. go func() {
  24. <-c
  25. log.Println("\r- Shutting down demo module.")
  26. //Do other things like close database or opened files
  27. os.Exit(0)
  28. }()
  29. }
  30. func main() {
  31. //If you have other flags, please add them here
  32. //Start the aoModule pipeline (which will parse the flags as well). Pass in the module launch information
  33. handler = aroz.HandleFlagParse(aroz.ServiceInfo{
  34. Name: "Demo Subservice",
  35. Desc: "A simple subservice code for showing how subservice works in ArOZ Online",
  36. Group: "Development",
  37. IconPath: "demo/icon.png",
  38. Version: "0.0.1",
  39. //You can define any path before the actualy html file. This directory (in this case demo/ ) will be the reverse proxy endpoint for this module
  40. StartDir: "demo/home.html",
  41. SupportFW: true,
  42. LaunchFWDir: "demo/home.html",
  43. SupportEmb: true,
  44. LaunchEmb: "demo/embedded.html",
  45. InitFWSize: []int{720, 480},
  46. InitEmbSize: []int{720, 480},
  47. SupportedExt: []string{".txt", ".md"},
  48. })
  49. //Register the standard web services urls
  50. fs := http.FileServer(http.Dir("./web"))
  51. http.HandleFunc("/api_test", apiTestDemo)
  52. http.Handle("/", fs)
  53. //To receive kill signal from the System core, you can setup a close handler to catch the kill signal
  54. //This is not nessary if you have no opened files / database running
  55. SetupCloseHandler()
  56. //Any log println will be shown in the core system via STDOUT redirection. But not STDIN.
  57. log.Println("Demo module started. Listening on " + handler.Port)
  58. err := http.ListenAndServe(handler.Port, nil)
  59. if err != nil {
  60. log.Fatal(err)
  61. }
  62. }
  63. //API Test Demo. This showcase how can you access arozos resources with RESTFUL API CALL
  64. func apiTestDemo(w http.ResponseWriter, r *http.Request) {
  65. //Get username and token from request
  66. username, token := handler.GetUserInfoFromRequest(w, r)
  67. log.Println("Received request from: ", username, " with token: ", token)
  68. //Create an AGI Call that get the user desktop files
  69. script := `
  70. if (requirelib("filelib")){
  71. var filelist = filelib.glob("user:/Desktop/*")
  72. sendJSONResp(JSON.stringify(filelist));
  73. }else{
  74. sendJSONResp(JSON.stringify({
  75. error: "Filelib require failed"
  76. }));
  77. }
  78. `
  79. //Execute the AGI request on server side
  80. resp, err := handler.RequestGatewayInterface(token, script)
  81. if err != nil {
  82. //Something went wrong when performing POST request
  83. log.Println(err)
  84. } else {
  85. //Try to read the resp body
  86. bodyBytes, err := ioutil.ReadAll(resp.Body)
  87. if err != nil {
  88. log.Println(err)
  89. w.Write([]byte(err.Error()))
  90. return
  91. }
  92. resp.Body.Close()
  93. //Relay the information to the request using json header
  94. //Or you can process the information within the go program
  95. w.Header().Set("Content-Type", "application/json")
  96. w.Write(bodyBytes)
  97. }
  98. }