forked from RedHatOfficial/GoCourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpresent_server.go
More file actions
70 lines (59 loc) · 2.13 KB
/
present_server.go
File metadata and controls
70 lines (59 loc) · 2.13 KB
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
package main
import (
"fmt"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"os/exec"
"strings"
"time"
)
func main() {
port := "3999"
presentPort := "3998" // Go present будет работать на этом порту
if len(os.Args) > 1 {
port = os.Args[1]
}
// Запускаем Go present на внутреннем порту
fmt.Println("🚀 Starting Go present server on internal port", presentPort)
cmd := exec.Command("go", "tool", "present", "-http=:"+presentPort, "-play=false")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
go func() {
if err := cmd.Run(); err != nil {
log.Printf("Go present server error: %v", err)
}
}()
// Даем время Go present серверу запуститься
time.Sleep(2 * time.Second)
// Создаем прокси к Go present серверу
presentURL, _ := url.Parse("http://localhost:" + presentPort)
proxy := httputil.NewSingleHostReverseProxy(presentURL)
// Создаем кастомный обработчик
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Если запрашивается корень, показываем наш index.html
if r.URL.Path == "/" {
http.ServeFile(w, r, "index.html")
return
}
// Если запрашивается наш кастомный контент (presentations/), обрабатываем сами
if strings.HasPrefix(r.URL.Path, "/presentations/") {
http.ServeFile(w, r, "."+r.URL.Path)
return
}
// Для всех остальных запросов используем прокси к Go present
proxy.ServeHTTP(w, r)
})
fmt.Printf("🌐 Custom presentation server started on http://localhost:%s\n", port)
fmt.Println("")
fmt.Println("📖 Access presentations:")
fmt.Printf(" 🌐 Language Selection: http://localhost:%s/\n", port)
fmt.Printf(" 🇺🇸 English: http://localhost:%s/presentations/en/\n", port)
fmt.Printf(" 🇷🇺 Russian: http://localhost:%s/presentations/ru/\n", port)
fmt.Println("")
fmt.Println("🛑 Press Ctrl+C to stop the server")
fmt.Println("")
log.Fatal(http.ListenAndServe(":"+port, nil))
}