-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
89 lines (73 loc) · 1.74 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
82
83
84
85
86
87
88
89
package main
import (
"embed"
"encoding/json"
"io/fs"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/joho/godotenv"
)
func main() {
godotenv.Load()
startApi()
}
func startApi() {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Route("/api", func(r chi.Router) {
r.Use(middleware.NoCache)
r.Get("/hello", helloHandler)
})
r.Handle("/*", http.HandlerFunc(frontendHandler))
host := os.Getenv("HOST")
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
listenUrl := host + ":" + port
log.Printf("Starting server on %s", listenUrl)
err := http.ListenAndServe(listenUrl, r)
if err != nil {
log.Fatalf("Failed to start server: %v", err)
}
}
//go:embed all:frontend/dist
var distFS embed.FS
func frontendHandler(w http.ResponseWriter, r *http.Request) {
env := os.Getenv("ENV")
viteUrl := os.Getenv("VITE_URL")
if viteUrl == "" {
viteUrl = "http://localhost:5173"
}
if env == "dev" {
target, err := url.Parse(viteUrl)
if err != nil {
log.Fatalf("Could not parse VITE_URL: %s", viteUrl)
}
proxy := httputil.NewSingleHostReverseProxy(target)
proxy.ServeHTTP(w, r)
} else {
subFS, err := fs.Sub(distFS, "frontend/dist")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.FileServer(http.FS(subFS)).ServeHTTP(w, r)
}
}
func helloHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]string{"msg": "Hello, Gopher!"}
jsonResponse, err := json.Marshal(data)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(jsonResponse)
}