Skip to content

Commit 4145617

Browse files
authored
Merge pull request #34 from factorysh/features/autodoc
Add documentation
2 parents 8923dd7 + 9bcb451 commit 4145617

File tree

3 files changed

+79
-13
lines changed

3 files changed

+79
-13
lines changed

application/application.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,12 @@ func New(cfg *conf.Conf) (*Application, error) {
117117
logger.Error("JWT or OAth2 middleware crash", zap.Error(err))
118118
return nil, err
119119
}
120-
r.Get("/", HomeHandler)
120+
r.Get("/", a.HomeHandler())
121121
r.Get("/metrics", promhttp.Handler().ServeHTTP)
122122
r.Get("/oauth/callback", oauth.CallbackHandler(&cfg.OAuth, &sessions))
123123

124124
r.Get("/services", a.ServicesHandler)
125-
r.Route("/service/{serviceID}", func(r chi.Router) {
126-
r.Use(authMiddleware.Middleware())
127-
r.Use(a.ServiceMiddleware)
128-
r.Get("/", a.ReadmeHandler)
129-
r.Get("/-{taskID}", a.TaskIDHandler)
130-
})
125+
r.Get("/service/{serviceID}", a.ReadmeHandler)
131126
r.Route("/service/{serviceID}/{project}", func(r chi.Router) {
132127
r.Use(authMiddleware.Middleware())
133128
r.Use(a.ServiceMiddleware)

application/home.go

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,82 @@
11
package application
22

33
import (
4+
"bytes"
5+
"embed"
46
"fmt"
7+
"html/template"
58
"net/http"
9+
"strings"
610

711
"github.com/factorysh/microdensity/version"
12+
"github.com/yuin/goldmark"
13+
"go.uber.org/zap"
814
)
915

10-
// HomeHandle display the home page
11-
func HomeHandler(w http.ResponseWriter, _ *http.Request) {
12-
w.Header().Set("content-type", "text/plain")
13-
w.Write([]byte(`
16+
var (
17+
// used to ensure embed import
18+
_ embed.FS
19+
//go:embed templates/HELP.md
20+
helpTemplate string
21+
)
22+
23+
func acceptsHTML(r *http.Request) bool {
24+
accepts, found := r.Header["Accept"]
25+
if !found {
26+
return false
27+
}
28+
29+
for _, h := range accepts {
30+
if strings.Contains(h, "text/html") {
31+
return true
32+
}
33+
}
34+
35+
return false
36+
}
37+
38+
// HomeHandler display the home page
39+
func (a *Application) HomeHandler() http.HandlerFunc {
40+
return func(w http.ResponseWriter, r *http.Request) {
41+
l := a.logger.With(
42+
zap.String("path", "home"),
43+
)
44+
45+
// if you ask for something that is not html
46+
// nice geeky ascii art
47+
if !acceptsHTML(r) {
48+
w.Header().Set("content-type", "text/plain")
49+
w.Write([]byte(`
1450
_ _ _
1551
___| |__ ___ ___| | __ _ __ ___ _ _ __ _____| |__
1652
/ __| '_ \ / _ \/ __| |/ / | '_ ' _ \| | | | \ \ /\ / / _ \ '_ \
1753
| (__| | | | __/ (__| < | | | | | | |_| | \ V V / __/ |_) |
1854
\ ___|_| |_|\___|\___|_|\_\ |_| |_| |_|\__, | \_/\_/ \___|_.__/
19-
|___/
55+
|___/
2056
`))
21-
fmt.Fprintf(w, "Version: %s", version.Version())
57+
fmt.Fprintf(w, "Version: %s", version.Version())
58+
return
59+
}
60+
61+
// text/html requests sends a documentation file
62+
template, err := template.New("help").Parse(helpTemplate)
63+
if err != nil {
64+
l.Error("template", zap.Error(err))
65+
w.WriteHeader(http.StatusInternalServerError)
66+
return
67+
}
68+
69+
var buffer bytes.Buffer
70+
71+
template.Execute(&buffer, a.Services)
72+
73+
err = goldmark.Convert(buffer.Bytes(), w)
74+
if err != nil {
75+
l.Error("markdown convert", zap.Error(err))
76+
w.WriteHeader(http.StatusInternalServerError)
77+
return
78+
}
79+
80+
w.Header().Set("content-type", "text/html")
81+
}
2282
}

application/templates/HELP.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Check My Web
2+
3+
Check you website using various sercices directly inside your CI.
4+
5+
## Available Services
6+
7+
{{ range $key, $service := . }}
8+
- [{{$key}}](service/{{$key}})
9+
{{ end }}
10+
11+
_Each service serve it's own documentation/examples_

0 commit comments

Comments
 (0)