forked from Grafikart/grafisearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
81 lines (68 loc) · 2.07 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"context"
"embed"
"fmt"
"grafikart/grafisearch/search"
"grafikart/grafisearch/server"
"grafikart/grafisearch/server/api"
"grafikart/grafisearch/utils"
"io/fs"
"log"
"net/http"
"os"
)
//go:embed all:public
var assets embed.FS
//go:embed install/com.grafisearch.plist
var macOSService string
//go:embed install/grafisearch.service
var linuxService string
const port = ":8042"
func main() {
// Handle "install" flag
if len(os.Args) >= 2 && os.Args[1] == "install" {
err := utils.InstallApp(linuxService, macOSService)
if err != nil {
panic(err)
}
return
}
publicFS, err := fs.Sub(assets, "public")
if err != nil {
panic(fmt.Sprintf("Cannot sub public directory %v", err))
}
viteAssets := server.NewViteAssets(publicFS)
frontMiddleware := createFrontEndMiddleware(*viteAssets)
publicServer := http.FileServer(http.FS(publicFS))
// Static Assets
http.HandleFunc("/sse", server.SSEHandler)
http.HandleFunc("/assets/", viteAssets.ServeAssets)
// API
http.HandleFunc("/api/ddg", api.SearchWithParser(search.GetDDGResults))
http.HandleFunc("/api/brave", api.SearchWithParser(search.GetBraveResults))
http.HandleFunc("/api/wallpaper", api.WallpaperHandler)
// FrontEnd URLs
http.HandleFunc("/weather", server.WeatherHandler)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Serve the root
if r.URL.Path == "/" {
frontMiddleware(server.SearchHandler)(w, r)
return
}
// Otherwise serve public files
publicServer.ServeHTTP(w, r)
})
fmt.Printf("Server is running on http://localhost%s\n", port)
log.Fatal(http.ListenAndServe(port, nil))
}
// Inject assets tags (as a string) in the context
func createFrontEndMiddleware(vite server.ViteAssets) func(func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request) {
html := vite.GetHeadHTML()
return func(next func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), "assets", html)
next(w, r.WithContext(ctx))
}
}
}