package main import ( "embed" "mime" "net/http" "os" "path/filepath" "strconv" "strings" "log" "time" "github.com/wailsapp/wails/v3/pkg/application" ) // Wails uses Go's `embed` package to embed the frontend files into the binary. // Any files in the frontend/dist folder will be embedded into the binary and // made available to the frontend. // See https://pkg.go.dev/embed for more information. //go:embed all:frontend/dist var assets embed.FS func init() { // Register a custom event whose associated data type is string. // This is not required, but the binding generator will pick up registered events // and provide a strongly typed JS/TS API for them. application.RegisterEvent[string]("time") // Force register .pdf MIME type for WebView2 default PDF reader loading _ = mime.AddExtensionType(".pdf", "application/pdf") } // main function serves as the application's entry point. It initializes the application, creates a window, // and starts a goroutine that emits a time-based event every second. It subsequently runs the application and // logs any error that might occur. func main() { defaultHandler := application.AssetFileServerFS(assets) customAssetHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { path := r.URL.Path if strings.HasPrefix(path, "/assets/") { filename := strings.TrimPrefix(path, "/assets/") localPath := filepath.Join("assets", filename) if _, err := os.Stat(localPath); err == nil { http.ServeFile(w, r, localPath) return } } defaultHandler.ServeHTTP(w, r) }) quizService := NewQuizService() serverHost := os.Getenv("WAILS_SERVER_HOST") if serverHost == "" { serverHost = "0.0.0.0" } serverPortStr := os.Getenv("WAILS_SERVER_PORT") serverPort := 8080 if serverPortStr != "" { if p, err := strconv.Atoi(serverPortStr); err == nil { serverPort = p } } // Create a new Wails application by providing the necessary options. // Variables 'Name' and 'Description' are for application metadata. // 'Assets' configures the asset server with the 'FS' variable pointing to the frontend files. // 'Bind' is a list of Go struct instances. The frontend has access to the methods of these instances. // 'Mac' options tailor the application when running an macOS. app := application.New(application.Options{ Name: "Master CBT", Description: "Learn everything with me", Services: []application.Service{ application.NewService(quizService), }, Assets: application.AssetOptions{ Handler: customAssetHandler, }, Server: application.ServerOptions{ Host: serverHost, Port: serverPort, }, Mac: application.MacOptions{ ApplicationShouldTerminateAfterLastWindowClosed: true, }, }) quizService.SetApp(app) // Create a new window with the necessary options. // 'Title' is the title of the window. // 'Mac' options tailor the window when running on macOS. // 'BackgroundColour' is the background colour of the window. // 'URL' is the URL that will be loaded into the webview. app.Window.NewWithOptions(application.WebviewWindowOptions{ Title: "cbt", Mac: application.MacWindow{ InvisibleTitleBarHeight: 50, Backdrop: application.MacBackdropTranslucent, TitleBar: application.MacTitleBarHiddenInset, }, BackgroundColour: application.NewRGB(27, 38, 54), URL: "/", }) // Create a goroutine that emits an event containing the current time every second. // The frontend can listen to this event and update the UI accordingly. go func() { for { now := time.Now().Format(time.RFC1123) app.Event.Emit("time", now) time.Sleep(time.Second) } }() // Run the application. This blocks until the application has been exited. err := app.Run() // If an error occurred while running the application, log it and exit. if err != nil { log.Fatal(err) } }