Skip to content

Commit a6b48e6

Browse files
committed
update
1 parent a3bece4 commit a6b48e6

13 files changed

+117
-190
lines changed

config.go

-105
This file was deleted.

main.go

+7-9
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,22 @@ func main() {
3333
flag.StringVar(&root, "root", root, "repos root dir")
3434
flag.Parse()
3535

36-
config := NewConfig()
37-
config.Git.Root = root
38-
app := gin.Default()
39-
err := config.LoadAllRepositories()
40-
templ, err := loadTemplates(config)
36+
sc := NewSmithy()
37+
sc.LoadAllRepositories(root)
38+
templ, err := loadTemplates()
4139
if err != nil {
4240
log.Fatal("Failed to load templates:", err)
4341
return
4442
}
43+
app := gin.Default()
4544
app.SetHTMLTemplate(templ)
46-
app.Use(AddConfigMiddleware(config))
45+
app.Use(AddConfigMiddleware(sc))
4746
routes := CompileRoutes()
4847
app.Any("*path", func(ctx *gin.Context) {
4948
Dispatch(ctx, routes, http.FileServer(http.FS(staticfiles)))
5049
})
51-
err = app.Run(":" + fmt.Sprint(config.Port))
50+
err = app.Run(":" + fmt.Sprint(sc.Port))
5251
if err != nil {
53-
log.Fatal("ERROR:", err, config.Port)
52+
log.Fatal("ERROR:", err, sc.Port)
5453
}
55-
return
5654
}

server.go

+20-25
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"html/template"
88
"io"
9-
"io/ioutil"
109
"log"
1110
"net/http"
1211
"path/filepath"
@@ -26,8 +25,6 @@ import (
2625
highlighting "github.com/yuin/goldmark-highlighting"
2726

2827
"embed"
29-
30-
"github.com/song940/gitgo/githttp"
3128
)
3229

3330
//go:embed templates
@@ -172,6 +169,9 @@ func RenderSyntaxHighlighting(file *object.File) (string, error) {
172169
)
173170

174171
iterator, err := lexer.Tokenise(nil, contents)
172+
if err != nil {
173+
return "", err
174+
}
175175

176176
buf := bytes.NewBuffer(nil)
177177
err = formatter.Format(buf, style, iterator)
@@ -184,17 +184,17 @@ func RenderSyntaxHighlighting(file *object.File) (string, error) {
184184
}
185185

186186
func Http404(ctx *gin.Context) {
187-
smithyConfig := ctx.MustGet("config").(SmithyConfig)
187+
smithyConfig := ctx.MustGet("config").(Smithy)
188188
ctx.HTML(http.StatusNotFound, "404.html", makeTemplateContext(smithyConfig, gin.H{}))
189189
}
190190

191191
func Http500(ctx *gin.Context) {
192-
smithyConfig := ctx.MustGet("config").(SmithyConfig)
192+
smithyConfig := ctx.MustGet("config").(Smithy)
193193
ctx.HTML(http.StatusInternalServerError, "500.html",
194194
makeTemplateContext(smithyConfig, gin.H{}))
195195
}
196196

197-
func makeTemplateContext(config SmithyConfig, extra gin.H) gin.H {
197+
func makeTemplateContext(config Smithy, extra gin.H) gin.H {
198198
results := gin.H{
199199
"Site": gin.H{
200200
"Title": config.Title,
@@ -209,7 +209,7 @@ func makeTemplateContext(config SmithyConfig, extra gin.H) gin.H {
209209
}
210210

211211
func IndexView(ctx *gin.Context, urlParts []string) {
212-
smithyConfig := ctx.MustGet("config").(SmithyConfig)
212+
smithyConfig := ctx.MustGet("config").(Smithy)
213213
repos := smithyConfig.GetRepositories()
214214

215215
ctx.HTML(http.StatusOK, "index.html", makeTemplateContext(smithyConfig, gin.H{
@@ -230,7 +230,7 @@ func findMainBranch(ctx *gin.Context, repo *git.Repository) (string, *plumbing.H
230230

231231
func RepoIndexView(ctx *gin.Context, urlParts []string) {
232232
repoName := urlParts[0]
233-
smithyConfig := ctx.MustGet("config").(SmithyConfig)
233+
smithyConfig := ctx.MustGet("config").(Smithy)
234234
repo, exists := smithyConfig.FindRepo(repoName)
235235

236236
if !exists {
@@ -293,14 +293,14 @@ func RepoIndexView(ctx *gin.Context, urlParts []string) {
293293
}
294294

295295
func RepoGitView(ctx *gin.Context, urlParts []string) {
296-
smithyConfig := ctx.MustGet("config").(SmithyConfig)
297-
git := githttp.New(smithyConfig.Git.Root)
298-
git.ServeHTTP(ctx.Writer, ctx.Request)
296+
// smithyConfig := ctx.MustGet("config").(Smithy)
297+
// git := githttp.New(smithyConfig.Git.Root)
298+
// git.ServeHTTP(ctx.Writer, ctx.Request)
299299
}
300300

301301
func RefsView(ctx *gin.Context, urlParts []string) {
302302
repoName := urlParts[0]
303-
smithyConfig := ctx.MustGet("config").(SmithyConfig)
303+
smithyConfig := ctx.MustGet("config").(Smithy)
304304
repo, exists := smithyConfig.FindRepo(repoName)
305305

306306
if !exists {
@@ -328,7 +328,7 @@ func RefsView(ctx *gin.Context, urlParts []string) {
328328

329329
func TreeView(ctx *gin.Context, urlParts []string) {
330330
repoName := urlParts[0]
331-
smithyConfig := ctx.MustGet("config").(SmithyConfig)
331+
smithyConfig := ctx.MustGet("config").(Smithy)
332332
repo, exists := smithyConfig.FindRepo(repoName)
333333

334334
if !exists {
@@ -438,7 +438,7 @@ func TreeView(ctx *gin.Context, urlParts []string) {
438438

439439
func LogView(ctx *gin.Context, urlParts []string) {
440440
repoName := urlParts[0]
441-
smithyConfig := ctx.MustGet("config").(SmithyConfig)
441+
smithyConfig := ctx.MustGet("config").(Smithy)
442442
repo, exists := smithyConfig.FindRepo(repoName)
443443
if !exists {
444444
Http404(ctx)
@@ -485,7 +485,7 @@ func LogView(ctx *gin.Context, urlParts []string) {
485485

486486
func LogViewDefault(ctx *gin.Context, urlParts []string) {
487487
repoName := urlParts[0]
488-
smithyConfig := ctx.MustGet("config").(SmithyConfig)
488+
smithyConfig := ctx.MustGet("config").(Smithy)
489489
repo, exists := smithyConfig.FindRepo(repoName)
490490
if !exists {
491491
Http404(ctx)
@@ -540,7 +540,7 @@ func FormatChanges(changes object.Changes) (string, error) {
540540
func PatchView(ctx *gin.Context, urlParts []string) {
541541
const commitFormatDate = "Mon, 2 Jan 2006 15:04:05 -0700"
542542
repoName := urlParts[0]
543-
smithyConfig := ctx.MustGet("config").(SmithyConfig)
543+
smithyConfig := ctx.MustGet("config").(Smithy)
544544
repo, exists := smithyConfig.FindRepo(repoName)
545545
if !exists {
546546
Http404(ctx)
@@ -600,7 +600,7 @@ func PatchView(ctx *gin.Context, urlParts []string) {
600600

601601
func CommitView(ctx *gin.Context, urlParts []string) {
602602
repoName := urlParts[0]
603-
smithyConfig := ctx.MustGet("config").(SmithyConfig)
603+
smithyConfig := ctx.MustGet("config").(Smithy)
604604
repo, exists := smithyConfig.FindRepo(repoName)
605605
if !exists {
606606
Http404(ctx)
@@ -639,7 +639,7 @@ func CommitView(ctx *gin.Context, urlParts []string) {
639639
}
640640

641641
// Make the config available to every request
642-
func AddConfigMiddleware(cfg SmithyConfig) gin.HandlerFunc {
642+
func AddConfigMiddleware(cfg Smithy) gin.HandlerFunc {
643643
return func(c *gin.Context) {
644644
c.Set("config", cfg)
645645
}
@@ -705,7 +705,6 @@ func Dispatch(ctx *gin.Context, routes []Route, fileSystemHandler http.Handler)
705705
if !route.Pattern.MatchString(urlPath) {
706706
continue
707707
}
708-
709708
urlParts := []string{}
710709
for i, match := range route.Pattern.FindStringSubmatch(urlPath) {
711710
if i != 0 {
@@ -715,15 +714,11 @@ func Dispatch(ctx *gin.Context, routes []Route, fileSystemHandler http.Handler)
715714

716715
route.View(ctx, urlParts)
717716
return
718-
719717
}
720-
721718
Http404(ctx)
722-
723719
}
724720

725-
func loadTemplates(smithyConfig SmithyConfig) (*template.Template, error) {
726-
721+
func loadTemplates() (*template.Template, error) {
727722
funcs := template.FuncMap{
728723
// "css": func() string {
729724
// return cssPath
@@ -743,7 +738,7 @@ func loadTemplates(smithyConfig SmithyConfig) (*template.Template, error) {
743738
if err != nil {
744739
return t, err
745740
}
746-
contents, err := ioutil.ReadAll(f)
741+
contents, err := io.ReadAll(f)
747742
if err != nil {
748743
return t, err
749744
}

smithy.go

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"path"
6+
"sort"
7+
8+
"github.com/go-git/go-git/v5"
9+
)
10+
11+
type Smithy struct {
12+
Title string `yaml:"title"`
13+
Description string `yaml:"description"`
14+
Host string `yaml:"host"`
15+
Port int `yaml:"port"`
16+
repos map[string]RepositoryWithName
17+
}
18+
19+
func NewSmithy() Smithy {
20+
return Smithy{
21+
Title: "Liu Song’s Projects",
22+
Description: "Publish your git repositories with ease",
23+
Port: 3456,
24+
Host: "localhost",
25+
}
26+
}
27+
28+
func (sc *Smithy) LoadAllRepositories(root string) error {
29+
files, err := os.ReadDir(root)
30+
if err != nil {
31+
return err
32+
}
33+
sc.repos = make(map[string]RepositoryWithName)
34+
for _, f := range files {
35+
repoPath := path.Join(root, f.Name())
36+
r, err := git.PlainOpen(repoPath)
37+
if err != nil {
38+
// Ignore directories that aren't git repositories
39+
continue
40+
}
41+
key := f.Name()
42+
rwn := RepositoryWithName{Name: f.Name(), Repository: r}
43+
sc.repos[key] = rwn
44+
}
45+
return nil
46+
}
47+
48+
func (sc *Smithy) GetRepositories() []RepositoryWithName {
49+
var repos []RepositoryWithName
50+
for _, repo := range sc.repos {
51+
repos = append(repos, repo)
52+
}
53+
sort.Sort(RepositoryByName(repos))
54+
return repos
55+
}
56+
57+
func (sc *Smithy) FindRepo(slug string) (RepositoryWithName, bool) {
58+
value, exists := sc.repos[slug]
59+
return value, exists
60+
}

static/style.css

+5
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,8 @@
99
.table {
1010
width: 100%;
1111
}
12+
13+
pre {
14+
width: 100%;
15+
overflow: scroll;
16+
}

0 commit comments

Comments
 (0)