-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
193 lines (174 loc) · 4.44 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
import (
"bytes"
"errors"
"flag"
"fmt"
"io/fs"
"log"
"mime"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"github.com/gopxl/docgen/internal/bundler"
"github.com/joho/godotenv"
)
func init() {
err := mime.AddExtensionType(".css", "text/css")
if err != nil {
panic(fmt.Errorf("could not register mime type: %w", err))
}
err = mime.AddExtensionType(".svg", "image/svg+xml")
if err != nil {
panic(fmt.Errorf("could not register mime type: %w", err))
}
}
func newBundle(toolingFs fs.FS, config *Config) (*bundler.Bundle, error) {
b := bundler.NewBundler()
b.Add(
bundler.NewFsDirHandler(
toolingFs,
"public",
".",
),
)
b.Add(
bundler.NewFsGlobHandler(
toolingFs,
"node_modules/prismjs/components",
"*.min.js",
"vendor/prismjs/components",
),
)
b.Add(
bundler.NewFsFileHandler(
toolingFs,
"node_modules/prismjs/plugins/autoloader/prism-autoloader.min.js",
"vendor/prismjs/plugins/autoloader/prism-autoloader.min.js",
),
)
docsHandler, err := NewDocsHandler(toolingFs, config)
if err != nil {
return nil, err
}
b.Add(docsHandler)
return b.Compile()
}
func main() {
workingDir, err := os.Getwd()
if err != nil {
log.Fatalf("could not get the current working directory: %v", err)
}
log.Printf("current working directory: %s", workingDir)
err = godotenv.Load()
if err != nil && !errors.Is(err, os.ErrNotExist) {
log.Fatalf("error loading .env file: %v", err)
}
var serve bool
var debug bool
flag.BoolVar(&serve, "serve", false, "serve the site through a webserver for development")
flag.BoolVar(&debug, "debug", false, "print debugging information")
flag.Parse()
siteUrlStr := os.Getenv("SITE_URL")
githubUrl := os.Getenv("GITHUB_URL")
repoPath := filepath.Clean(os.Getenv("REPOSITORY_PATH"))
docsDir := filepath.Clean(os.Getenv("DOCS_DIR"))
outputDir := os.Getenv("OUTPUT_DIR")
mainBranch := os.Getenv("MAIN_BRANCH")
withWorkingDirStr := os.Getenv("WORKING_DIRECTORY")
withWorkingDir, err := strconv.ParseBool(withWorkingDirStr)
if err != nil {
withWorkingDir = false
}
siteUrl, err := url.Parse(siteUrlStr)
if err != nil {
log.Fatalf("could not parse root url %s: %v", siteUrlStr, err)
}
config := &Config{
siteUrl: siteUrl,
githubUrl: githubUrl,
repositoryPath: repoPath,
docsDir: docsDir,
outputDir: outputDir,
mainBranch: mainBranch,
withWorkingDir: withWorkingDir,
}
log.Printf("config:\n%v", config)
if debug {
bun, err := newBundle(embeddedFs, config)
if err != nil {
log.Fatalf("could not create bundle: %v", err)
}
fmt.Println(bun.Files())
}
if serve {
log.Println("Starting development server...")
// Override root url.
siteUrl, err = url.Parse("http://localhost:8080")
if err != nil {
log.Fatalf("could not parse root url: %v", err)
}
devConfig := *&config // shallow copy
devConfig.siteUrl = siteUrl
mux := http.NewServeMux()
mux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
b, err := newBundle(embeddedFs, devConfig)
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
_, _ = writer.Write([]byte(fmt.Sprintf("could not create bundle: %v", err)))
return
}
pth := path.Clean(strings.TrimLeft(request.URL.Path, "/"))
aliases := []string{
pth,
pth + ".html",
path.Join(pth, "index.html"),
}
var buf bytes.Buffer
var found bool
for _, pth := range aliases {
err = b.WriteFileTo(pth, &buf)
if errors.Is(err, fs.ErrNotExist) {
// Try an alias.
continue
}
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
_, _ = writer.Write([]byte(fmt.Sprintf("could not write file: %v", err)))
return
}
found = true
}
if !found {
writer.WriteHeader(http.StatusNotFound)
_, _ = writer.Write([]byte("Not Found"))
return
}
writer.Header().Add("Content-Type", mime.TypeByExtension(filepath.Ext(pth)))
_, _ = writer.Write(buf.Bytes())
})
s := &http.Server{
Addr: fmt.Sprintf(":%s", siteUrl.Port()),
Handler: mux,
}
log.Printf("listening on %v", siteUrl.String())
err = s.ListenAndServe()
if err != nil {
log.Fatalf("could not serve development server: %v", err)
}
} else {
log.Println("compiling...")
b, err := newBundle(embeddedFs, config)
if err != nil {
log.Fatalf("could not create bundle: %v", err)
}
err = b.StoreInDir(config.outputDir)
if err != nil {
log.Fatal(err)
}
}
}