-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (54 loc) · 1.16 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
package main
import (
_ "embed"
"fmt"
"github.com/gin-gonic/gin"
_ "github.com/joho/godotenv/autoload"
"github.com/theplant/htmlgo"
"htmxgo/api"
"htmxgo/render"
"os"
"os/signal"
"strconv"
)
func main() {
gin.SetMode(os.Getenv("GIN_MODE"))
// 启动 API 服务器
apiPort, err := strconv.Atoi(os.Getenv("API_PORT"))
if err != nil {
panic(err)
}
go startAPIServer(apiPort)
// 启动 HTML 文件服务器
htmlPort, err := strconv.Atoi(os.Getenv("HTML_PORT"))
if err != nil {
panic(err)
}
go startHTMLServer(htmlPort)
// 留一个缓冲可以防止阻塞并且防止丢失信号
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
}
func startAPIServer(port int) {
api.NewSqliteDB()
engine := api.NewRouter()
addr := fmt.Sprintf(":%d", port)
fmt.Println("HTML Server listening on", addr)
err := engine.Run(addr)
if err != nil {
panic(err)
}
}
func startHTMLServer(port int) {
engine := gin.Default()
engine.Any("/", func(c *gin.Context) {
htmlgo.Fprint(c.Writer, render.Render(), c)
})
addr := fmt.Sprintf(":%d", port)
fmt.Println("HTML Server listening on", addr)
err := engine.Run(addr)
if err != nil {
panic(err)
}
}