summaryrefslogblamecommitdiffstats
path: root/routes/routes.go
blob: f27a5549d6a00a28d94fbb2e57edc1e639e3ddc7 (plain) (tree)
1
2
3
4
5
6
7
8
9


              
                
             
                       

                  
            
                       
              
                 
              
 

                                     
                                     
                                       
                                           

                                            





                        




                                        
                                                              
                                                  





                                                        
                                             

                                  



                                            
                                                                    
                                             










                                                                       
                                        
                                





                                         
                              

                 
                                            
                                                              
 


                                                             

                                                           
                                            
                  

         




                                                            
 
                                                       



                                                     
                                       
 



                                                                   

 



















































                                                                                

                                                                  



                              
                                   







                                                       
 
                                     
                       




                                                   

         
                                    
                       
                             



                                
                                       
                                                



                                                    




                                                                                                 






                                                                                    







                                                          






                                                                 


                                                       

                                     

         

                                    




                                                            
                                
                                      

                                           
                                            
                               
                                      




                                                                  
 


              
                                                                 
                                               



                              
                                                  
                                             

                                   





                                                       
                                      
                       




                                                    

         
                                           
                       
                             



                                

                                    




                                                            

                                 
                                           
                                               
 
                                   


              

                                                                    



                              

                                                  
 
                                   





                                                       
                                      
                       




                                                    
         
 


                                                 




                                                            
                         
                                           
                               






                                                            



                              

                                             





                                                       

                                      




                                                    


                                    
                       
                             


                                
 
                                                       





                                                     




                                                            
                         
                                           
                          




                                                                 
 


                                                             



                              

                                             





                                                       

                                      




                                                    



                              
                             



                                
                                                       








                                                     




                                                            
                         
                                           





                                                                    


                                                             



                              
 





                                                       

                                     




                                                   














                                                                           
                                                       





                                                     




                                                            

                                   
                                           





                                                                  






                                                                    
package routes

import (
	"errors"
	"fmt"
	"html/template"
	"log"
	"net/http"
	"os"
	"path/filepath"
	"sort"
	"strings"
	"time"

	"git.icyphox.sh/legit/config"
	"git.icyphox.sh/legit/git"
	"github.com/alexedwards/flow"
	"github.com/dustin/go-humanize"
	gogit "github.com/go-git/go-git/v5"
	"github.com/microcosm-cc/bluemonday"
	"github.com/russross/blackfriday/v2"
)

type deps struct {
	c *config.Config
}

type info struct {
	Name, Path, Desc, Idle string
	d                      time.Time
}

func (d *deps) Index(w http.ResponseWriter, r *http.Request) {
	dirs, err := os.ReadDir(d.c.Repo.ScanPath)
	if err != nil {
		d.Write500(w)
		log.Printf("reading scan path: %s", err)
		return
	}

	categories := make(map[string][]info)

	for _, dir := range dirs {
		if d.isIgnored(dir.Name()) {
			continue
		}

		path := filepath.Join(d.c.Repo.ScanPath, dir.Name())
		gr, err := git.Open(path, "")
		if errors.Is(err, gogit.ErrRepositoryNotExists) {
			log.Printf("reading category: %s", dir.Name())
			infos, err := d.IndexCategory(path, dir.Name())
			if err != nil {
				log.Printf("reading category: %s", err)
			}
			if len(infos) > 0 {
				categories[dir.Name()] = infos
			}
			continue
		} else if err != nil {
			log.Println(err)
			continue
		}

		c, err := gr.LastCommit()
		if err != nil {
			d.Write500(w)
			log.Println(err)
			return
		}

		desc := getDescription(path)
		name := strings.TrimSuffix(dir.Name(), ".git")

		categories[""] = append(categories[""], info{
			Name: name,
			Path: name,
			Desc: desc,
			Idle: humanize.Time(c.Author.When),
			d:    c.Author.When,
		})
	}

	for _, infos := range categories {
		sort.Slice(infos, func(i, j int) bool {
			return infos[j].d.Before(infos[i].d)
		})
	}

	tpath := filepath.Join(d.c.Dirs.Templates, "*")
	t := template.Must(template.ParseGlob(tpath))

	data := make(map[string]interface{})
	data["meta"] = d.c.Meta
	data["categories"] = categories

	if err := t.ExecuteTemplate(w, "index", data); err != nil {
		log.Println(err)
		return
	}
}

func (d *deps) IndexCategory(scanpath string, category string) ([]info, error) {
	dirs, err := os.ReadDir(scanpath)
	if err != nil {
		return nil, fmt.Errorf("reading scan path: %s", err)
	}

	infos := []info{}

	for _, dir := range dirs {
		if d.isIgnored(dir.Name()) {
			continue
		}

		path := filepath.Join(scanpath, dir.Name())
		gr, err := git.Open(path, "")
		if errors.Is(err, gogit.ErrRepositoryNotExists) {
			log.Println(path)
			folder_infos, err := d.IndexCategory(path, category)
			if err != nil {
				log.Println(err)
				continue
			}
			infos = append(infos, folder_infos...)
			continue
		} else if err != nil {
			log.Println(err)
			continue
		}

		c, err := gr.LastCommit()
		if err != nil {
			return nil, err
		}

		desc := getDescription(path)
		repodir := filepath.Join(scanpath, dir.Name())
		repopath := strings.Split(repodir, category)
		name := strings.TrimSuffix(repopath[len(repopath)-1], ".git")
		name = strings.TrimPrefix(name, "/")

		infos = append(infos, info{
			Name: name,
			Path: category + "/" + name,
			Desc: desc,
			Idle: humanize.Time(c.Author.When),
			d:    c.Author.When,
		})
	}

	return infos, nil
}

func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) {
	name := flow.Param(r.Context(), "name")
	if d.isIgnored(name) {
		d.Write404(w)
		return
	}
	name = filepath.Clean(name)
	path := d.c.Repo.ScanPath
	category := flow.Param(r.Context(), "category")
	if category != "" {
		path = filepath.Join(path, category)
		log.Println(category)
	}
	path = filepath.Join(path, name)
	log.Println(path)

	gr, err := git.Open(path, "")
	if err != nil {
		gr, err = git.Open(path+".git", "")
		if err != nil {
			d.Write404(w)
			return
		}
	}

	commits, err := gr.Commits()
	if err != nil {
		d.Write500(w)
		log.Println(err)
		return
	}

	var readmeContent template.HTML
	for _, readme := range d.c.Repo.Readme {
		ext := filepath.Ext(readme)
		content, _ := gr.FileContent(readme)
		if len(content) > 0 {
			switch ext {
			case ".md", ".mkd", ".markdown":
				unsafe := blackfriday.Run(
					[]byte(content),
					blackfriday.WithExtensions(blackfriday.CommonExtensions),
				)
				html := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
				readmeContent = template.HTML(html)
			default:
				readmeContent = template.HTML(
					fmt.Sprintf(`<pre>%s</pre>`, content),
				)
			}
			break
		}
	}

	if readmeContent == "" {
		log.Printf("no readme found for %s", name)
	}

	mainBranch, err := gr.FindMainBranch(d.c.Repo.MainBranch)
	if err != nil {
		d.Write500(w)
		log.Println(err)
		return
	}

	tpath := filepath.Join(d.c.Dirs.Templates, "*")
	t := template.Must(template.ParseGlob(tpath))

	if len(commits) >= 3 {
		commits = commits[:3]
	}

	data := make(map[string]any)
	data["name"] = name
	if category != "" {
		data["repo"] = filepath.Join(category, name)
	} else {
		data["repo"] = name
	}
	data["ref"] = mainBranch
	data["readme"] = readmeContent
	data["commits"] = commits
	data["desc"] = getDescription(path)
	data["servername"] = d.c.Server.Name
	data["meta"] = d.c.Meta
	data["gomod"] = isGoModule(gr)

	if err := t.ExecuteTemplate(w, "repo", data); err != nil {
		log.Println(err)
		return
	}

	return
}

func (d *deps) RepoTree(w http.ResponseWriter, r *http.Request) {
	name := flow.Param(r.Context(), "name")
	if d.isIgnored(name) {
		d.Write404(w)
		return
	}
	treePath := flow.Param(r.Context(), "...")
	ref := flow.Param(r.Context(), "ref")

	name = filepath.Clean(name)
	path := d.c.Repo.ScanPath
	category := flow.Param(r.Context(), "category")
	if category != "" {
		path = filepath.Join(path, category)
	}
	path = filepath.Join(path, name)
	gr, err := git.Open(path, ref)
	if err != nil {
		gr, err = git.Open(path+".git", ref)
		if err != nil {
			d.Write404(w)
			return
		}
	}

	files, err := gr.FileTree(treePath)
	if err != nil {
		d.Write500(w)
		log.Println(err)
		return
	}

	data := make(map[string]any)
	data["name"] = name
	if category != "" {
		data["repo"] = filepath.Join(category, name)
	} else {
		data["repo"] = name
	}
	data["ref"] = ref
	data["parent"] = treePath
	data["desc"] = getDescription(path)
	data["dotdot"] = filepath.Dir(treePath)

	d.listFiles(files, data, w)
	return
}

func (d *deps) FileContent(w http.ResponseWriter, r *http.Request) {
	name := flow.Param(r.Context(), "name")
	if d.isIgnored(name) {
		d.Write404(w)
		return
	}
	treePath := flow.Param(r.Context(), "...")
	ref := flow.Param(r.Context(), "ref")

	name = filepath.Clean(name)
	path := d.c.Repo.ScanPath
	category := flow.Param(r.Context(), "category")
	if category != "" {
		path = filepath.Join(path, category)
	}
	path = filepath.Join(path, name)
	gr, err := git.Open(path, ref)
	if err != nil {
		gr, err = git.Open(path+".git", ref)
		if err != nil {
			d.Write404(w)
			return
		}
	}

	contents, err := gr.FileContent(treePath)
	data := make(map[string]any)
	data["name"] = name
	if category != "" {
		data["repo"] = filepath.Join(category, name)
	} else {
		data["repo"] = name
	}
	data["ref"] = ref
	data["desc"] = getDescription(path)
	data["path"] = treePath

	d.showFile(contents, data, w)
	return
}

func (d *deps) Log(w http.ResponseWriter, r *http.Request) {
	name := flow.Param(r.Context(), "name")
	if d.isIgnored(name) {
		d.Write404(w)
		return
	}
	ref := flow.Param(r.Context(), "ref")

	path := d.c.Repo.ScanPath
	category := flow.Param(r.Context(), "category")
	if category != "" {
		path = filepath.Join(path, category)
	}
	path = filepath.Join(path, name)
	gr, err := git.Open(path, ref)
	if err != nil {
		gr, err = git.Open(path+".git", ref)
		if err != nil {
			d.Write404(w)
			return
		}
	}

	commits, err := gr.Commits()
	if err != nil {
		d.Write500(w)
		log.Println(err)
		return
	}

	tpath := filepath.Join(d.c.Dirs.Templates, "*")
	t := template.Must(template.ParseGlob(tpath))

	data := make(map[string]interface{})
	data["commits"] = commits
	data["meta"] = d.c.Meta
	data["name"] = name
	if category != "" {
		data["repo"] = filepath.Join(category, name)
	} else {
		data["repo"] = name
	}
	data["ref"] = ref
	data["desc"] = getDescription(path)
	data["log"] = true

	if err := t.ExecuteTemplate(w, "log", data); err != nil {
		log.Println(err)
		return
	}
}

func (d *deps) Diff(w http.ResponseWriter, r *http.Request) {
	name := flow.Param(r.Context(), "name")
	if d.isIgnored(name) {
		d.Write404(w)
		return
	}
	ref := flow.Param(r.Context(), "ref")

	path := d.c.Repo.ScanPath
	category := flow.Param(r.Context(), "category")
	if category != "" {
		path = filepath.Join(path, category)
	}
	path = filepath.Join(path, name)
	gr, err := git.Open(path, ref)
	if err != nil {
		gr, err = git.Open(path+".git", ref)
		if err != nil {
			d.Write404(w)
			return
		}
	}

	diff, err := gr.Diff()
	if err != nil {
		d.Write500(w)
		log.Println(err)
		return
	}

	tpath := filepath.Join(d.c.Dirs.Templates, "*")
	t := template.Must(template.ParseGlob(tpath))

	data := make(map[string]interface{})

	data["commit"] = diff.Commit
	data["stat"] = diff.Stat
	data["diff"] = diff.Diff
	data["meta"] = d.c.Meta
	data["name"] = name
	if category != "" {
		data["repo"] = filepath.Join(category, name)
	} else {
		data["repo"] = name
	}
	data["ref"] = ref
	data["desc"] = getDescription(path)

	if err := t.ExecuteTemplate(w, "commit", data); err != nil {
		log.Println(err)
		return
	}
}

func (d *deps) Refs(w http.ResponseWriter, r *http.Request) {
	name := flow.Param(r.Context(), "name")
	if d.isIgnored(name) {
		d.Write404(w)
		return
	}

	path := d.c.Repo.ScanPath
	category := flow.Param(r.Context(), "category")
	if category != "" {
		path = filepath.Join(path, category)
	}
	path = filepath.Join(path, name)
	gr, err := git.Open(path, "")
	if err != nil {
		gr, err = git.Open(path+".git", "")
		if err != nil {
			d.Write404(w)
			return
		}
	}

	tags, err := gr.Tags()
	if err != nil {
		// Non-fatal, we *should* have at least one branch to show.
		log.Println(err)
	}

	branches, err := gr.Branches()
	if err != nil {
		log.Println(err)
		d.Write500(w)
		return
	}

	tpath := filepath.Join(d.c.Dirs.Templates, "*")
	t := template.Must(template.ParseGlob(tpath))

	data := make(map[string]interface{})

	data["meta"] = d.c.Meta
	data["name"] = name
	if category != "" {
		data["repo"] = filepath.Join(category, name)
	} else {
		data["repo"] = name
	}
	data["branches"] = branches
	data["tags"] = tags
	data["desc"] = getDescription(path)

	if err := t.ExecuteTemplate(w, "refs", data); err != nil {
		log.Println(err)
		return
	}
}

func (d *deps) ServeStatic(w http.ResponseWriter, r *http.Request) {
	f := flow.Param(r.Context(), "file")
	f = filepath.Clean(filepath.Join(d.c.Dirs.Static, f))

	http.ServeFile(w, r, f)
}