aroz.go 2.6 KB

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