| Davit Tabidze | 207ce08 | 2024-04-09 19:15:25 +0400 | [diff] [blame^] | 1 | package welcome |
| 2 | |
| 3 | import ( |
| 4 | "embed" |
| 5 | "fmt" |
| 6 | "html/template" |
| 7 | "log" |
| 8 | "net/http" |
| 9 | "strings" |
| 10 | ) |
| 11 | |
| 12 | //go:embed launcher-tmpl/launcher.html |
| 13 | var indexHTML embed.FS |
| 14 | |
| 15 | //go:embed static/* |
| 16 | var files embed.FS |
| 17 | |
| 18 | type AppLauncherInfo struct { |
| 19 | Name string |
| 20 | Description string |
| 21 | Icon template.HTML |
| 22 | Help []HelpDocument |
| 23 | Url string |
| 24 | } |
| 25 | |
| 26 | type HelpDocument struct { |
| 27 | Title string |
| 28 | Contents string |
| 29 | Children []HelpDocument |
| 30 | } |
| 31 | |
| 32 | type AppDirectory interface { |
| 33 | GetAllApps() ([]AppLauncherInfo, error) |
| 34 | } |
| 35 | |
| 36 | type LauncherServer struct { |
| 37 | port int |
| 38 | logoutUrl string |
| 39 | appDirectory AppDirectory |
| 40 | homeTmpl *template.Template |
| 41 | } |
| 42 | |
| 43 | func NewLauncherServer( |
| 44 | port int, |
| 45 | logoutUrl string, |
| 46 | appDirectory AppDirectory, |
| 47 | ) (*LauncherServer, error) { |
| 48 | tmpl, err := indexHTML.ReadFile("launcher-tmpl/launcher.html") |
| 49 | if err != nil { |
| 50 | return nil, fmt.Errorf("failed to parse template: %v", err) |
| 51 | } |
| 52 | t := template.New("index").Funcs(template.FuncMap{ |
| 53 | "GetUserInitials": getUserInitials, |
| 54 | "CleanAppName": cleanAppName, |
| 55 | }) |
| 56 | t, err = t.Parse(string(tmpl)) |
| 57 | if err != nil { |
| 58 | return nil, fmt.Errorf("failed to parse template: %v", err) |
| 59 | } |
| 60 | return &LauncherServer{ |
| 61 | port, |
| 62 | logoutUrl, |
| 63 | appDirectory, |
| 64 | t, |
| 65 | }, nil |
| 66 | } |
| 67 | |
| 68 | func getUserInitials(username string) string { |
| 69 | if username == "" { |
| 70 | return "" |
| 71 | } |
| 72 | return strings.ToUpper(username[:1]) |
| 73 | } |
| 74 | |
| 75 | func cleanAppName(name string) string { |
| 76 | cleanName := strings.ToLower(name) |
| 77 | cleanName = strings.ReplaceAll(cleanName, " ", "-") |
| 78 | return cleanName |
| 79 | } |
| 80 | |
| 81 | func (s *LauncherServer) Start() { |
| 82 | http.Handle("/static/", http.FileServer(http.FS(files))) |
| 83 | http.HandleFunc("/", s.homeHandler) |
| 84 | log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", s.port), nil)) |
| 85 | } |
| 86 | |
| 87 | type homeHandlerData struct { |
| 88 | LoggedInUsername string |
| 89 | AllAppsInfo []AppLauncherInfo |
| 90 | } |
| 91 | |
| 92 | func (s *LauncherServer) homeHandler(w http.ResponseWriter, r *http.Request) { |
| 93 | loggedInUsername := "longusername" |
| 94 | allAppsInfo, err := s.appDirectory.GetAllApps() |
| 95 | if err != nil { |
| 96 | http.Error(w, err.Error(), http.StatusBadRequest) |
| 97 | return |
| 98 | } |
| 99 | data := homeHandlerData{ |
| 100 | LoggedInUsername: loggedInUsername, |
| 101 | AllAppsInfo: allAppsInfo, |
| 102 | } |
| 103 | if err := s.homeTmpl.Execute(w, data); err != nil { |
| 104 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 105 | return |
| 106 | } |
| 107 | } |