<commit_before>package main

import (
  "net/http"
  "log"
  "io/ioutil"
  "html/template"
  "os"
  "sort"
  "path"
  "flag"
)

var root string

type Entry struct {
  Name string
  FullName string
  IsDir bool
  IsParent bool
}

type ByIsDir []Entry

func (slice ByIsDir) Len() int {
  return len(slice)
}

func (slice ByIsDir) Less(i, j int) bool {
  if (slice[i].IsDir != slice[j].IsDir) {
    return slice[i].IsDir
  }

  return slice[i].Name < slice[j].Name
}

func (slice ByIsDir) Swap(i, j int) {
  slice[i], slice[j] = slice[j], slice[i]
}

func main() {
  flag.StringVar(&root, "root", "/", "Root folder.")
  flag.Parse()

  http.HandleFunc("/", indexHandler)
  log.Println("Server is started...")
  http.ListenAndServe(":8000", nil)
}

func indexHandler(w http.ResponseWriter, r *http.Request) {
  currentPath := r.URL.Query().Get("path")
  if (currentPath == "") {
    currentPath = root
  }

  stat, err := os.Stat(currentPath)

  if os.IsNotExist(err) {
    log.Println(err.Error())
    http.Error(w, http.StatusText(404), 404)
    return
  }

  if !stat.IsDir() {
    http.ServeFile(w, r, currentPath)
    return
  }

  t, err := template.ParseFiles("templates/fs.html")
  if err != nil {
    log.Println(err.Error())
    http.Error(w, http.StatusText(500), 500)
    return
  }

  t.Execute(w, getEntries(currentPath))
}

func getEntries(currentPath string) []Entry {
  entries := []Entry{}

  if (currentPath != root) {
    parent := Entry{
      Name: "..",
      FullName: path.Join(currentPath, ".."),
      IsDir: true,
      IsParent: true,
    }
    entries = append(entries, parent)
  }

  files, _ := ioutil.ReadDir(currentPath)
  for _, e := range files {
    entries = append(entries, Entry{
      Name: e.Name(),
      FullName: path.Join(currentPath, e.Name()),
      IsDir: e.IsDir(),
    })
  }

  sort.Sort(ByIsDir(entries))
  return entries
}
<commit_msg>Set default root to home dir<commit_after>package main

import (
  "net/http"
  "log"
  "io/ioutil"
  "html/template"
  "os"
  "os/user"
  "sort"
  "path"
  "flag"
)

var root string

type Entry struct {
  Name string
  FullName string
  IsDir bool
  IsParent bool
}

type ByIsDir []Entry

func (slice ByIsDir) Len() int {
  return len(slice)
}

func (slice ByIsDir) Less(i, j int) bool {
  if (slice[i].IsDir != slice[j].IsDir) {
    return slice[i].IsDir
  }

  return slice[i].Name < slice[j].Name
}

func (slice ByIsDir) Swap(i, j int) {
  slice[i], slice[j] = slice[j], slice[i]
}

func main() {
  currentUser, err := user.Current()
  if err != nil {
      log.Fatal(err)
  }

  flag.StringVar(&root, "root", currentUser.HomeDir, "Root folder. Default is hode dir.")
  flag.Parse()

  http.HandleFunc("/", indexHandler)
  log.Println("Server is started...")
  http.ListenAndServe(":8000", nil)
}

func indexHandler(w http.ResponseWriter, r *http.Request) {
  currentPath := r.URL.Query().Get("path")
  if (currentPath == "") {
    currentPath = root
  }

  stat, err := os.Stat(currentPath)

  if os.IsNotExist(err) {
    log.Println(err.Error())
    http.Error(w, http.StatusText(404), 404)
    return
  }

  if !stat.IsDir() {
    http.ServeFile(w, r, currentPath)
    return
  }

  t, err := template.ParseFiles("templates/fs.html")
  if err != nil {
    log.Println(err.Error())
    http.Error(w, http.StatusText(500), 500)
    return
  }

  t.Execute(w, getEntries(currentPath))
}

func getEntries(currentPath string) []Entry {
  entries := []Entry{}

  if (currentPath != root) {
    parent := Entry{
      Name: "..",
      FullName: path.Join(currentPath, ".."),
      IsDir: true,
      IsParent: true,
    }
    entries = append(entries, parent)
  }

  files, _ := ioutil.ReadDir(currentPath)
  for _, e := range files {
    entries = append(entries, Entry{
      Name: e.Name(),
      FullName: path.Join(currentPath, e.Name()),
      IsDir: e.IsDir(),
    })
  }

  sort.Sort(ByIsDir(entries))
  return entries
}

{'fraction_non_alphanumeric': 0.1632183908045977, 'fraction_numerical': 0.008173690932311623, 'mean_word_length': 3.0329557157569518, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1380560', 'n_tokens_mistral': 1496, 'n_tokens_neox': 1445, 'n_words': 326}