1
0

aroz.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package aroz
  2. import (
  3. "encoding/json"
  4. "flag"
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "os"
  9. )
  10. type ArozHandler struct {
  11. Port string
  12. restfulEndpoint string
  13. }
  14. //Information required for registering this subservice to arozos
  15. type ServiceInfo struct {
  16. Name string //Name of this module. e.g. "Audio"
  17. Desc string //Description for this module
  18. Group string //Group of the module, e.g. "system" / "media" etc
  19. IconPath string //Module icon image path e.g. "Audio/img/function_icon.png"
  20. Version string //Version of the module. Format: [0-9]*.[0-9][0-9].[0-9]
  21. StartDir string //Default starting dir, e.g. "Audio/index.html"
  22. SupportFW bool //Support floatWindow. If yes, floatWindow dir will be loaded
  23. LaunchFWDir string //This link will be launched instead of 'StartDir' if fw mode
  24. SupportEmb bool //Support embedded mode
  25. LaunchEmb string //This link will be launched instead of StartDir / Fw if a file is opened with this module
  26. InitFWSize []int //Floatwindow init size. [0] => Width, [1] => Height
  27. InitEmbSize []int //Embedded mode init size. [0] => Width, [1] => Height
  28. SupportedExt []string //Supported File Extensions. e.g. ".mp3", ".flac", ".wav"
  29. }
  30. //This function will request the required flag from the startup paramters and parse it to the need of the arozos.
  31. func HandleFlagParse(info ServiceInfo) *ArozHandler {
  32. var infoRequestMode = flag.Bool("info", false, "Show information about this subservice")
  33. var port = flag.String("port", ":8000", "The default listening endpoint for this subservice")
  34. var restful = flag.String("rpt", "http://localhost:8080/api/ajgi/interface", "The RESTFUL Endpoint of the parent")
  35. //Parse the flags
  36. flag.Parse()
  37. if *infoRequestMode == true {
  38. //Information request mode
  39. jsonString, _ := json.Marshal(info)
  40. fmt.Println(string(jsonString))
  41. os.Exit(0)
  42. }
  43. return &ArozHandler{
  44. Port: *port,
  45. restfulEndpoint: *restful,
  46. }
  47. }
  48. //Get the username and resources access token from the request, return username, token
  49. func (a *ArozHandler) GetUserInfoFromRequest(w http.ResponseWriter, r *http.Request) (string, string) {
  50. username := r.Header.Get("aouser")
  51. token := r.Header.Get("aotoken")
  52. return username, token
  53. }
  54. func (a *ArozHandler) RequestGatewayInterface(token string, script string) (*http.Response, error) {
  55. resp, err := http.PostForm(a.restfulEndpoint,
  56. url.Values{"token": {token}, "script": {script}})
  57. if err != nil {
  58. // handle error
  59. return nil, err
  60. }
  61. return resp, nil
  62. }