-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
97 lines (70 loc) · 1.48 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
90
91
92
93
94
95
96
97
package main
import (
"fmt"
"log"
"otter-docs/internal/docs"
"otter-docs/internal/git"
"time"
"github.com/gofiber/fiber/v2"
"github.com/kelseyhightower/envconfig"
)
type Config struct {
URL string `envconfig:"GIT_URL" required:"true"`
Branch string `envconfig:"GIT_BRANCH" default:"main"`
Port string `envconfig:"PORT" default:"8080"`
}
func main() {
var cfg Config
if err := envconfig.Process("", &cfg); err != nil {
panic(err)
}
git, err := git.New("./vuepress/docs/", cfg.URL, cfg.Branch)
if err != nil {
panic(err)
}
docs, err := docs.New()
if err != nil {
panic(err)
}
var updateError error
go func() {
for {
updateError = update(git, docs)
if updateError != nil {
log.Printf("failed to update docs: %s", updateError)
}
time.Sleep(10 * time.Second)
}
}()
app := fiber.New()
app.Use(func(ctx *fiber.Ctx) error {
if updateError != nil {
return ctx.SendString(fmt.Sprintf("Failed to update docs: %s", updateError))
}
return ctx.Next()
})
app.Static("/", "./dist")
app.Listen(":8080")
}
func update(git *git.Git, docs *docs.Docs) error {
hasNewCommits, err := git.HasNewCommits()
if err != nil {
return err
}
if !hasNewCommits && !docs.LastBuilt.IsZero() {
return nil
}
if hasNewCommits {
if err := git.Pull(); err != nil {
return err
}
}
if err := docs.Install(); err != nil {
return err
}
if err := docs.Build(); err != nil {
return err
}
log.Print("successfully updated docs")
return nil
}