<commit_before>package main

import (
  "html/template"
  "log"
  "fmt"
  "net/http"
  "os"
  "path"
  "time"
)

type Middleware struct {
  Next http.Handler
}

func withMiddleware(handler http.Handler) Middleware {
  return Middleware{handler}
}

func withMiddlewareFunc(fn http.HandlerFunc) Middleware {
  return withMiddleware(http.HandlerFunc(fn))
}

func (m Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  t1 := time.Now()
  m.Next.ServeHTTP(w, r)
  t2 := time.Now()
  log.Printf("[%s] %q %v\n", r.Method, r.URL.String(), t2.Sub(t1))
}

func handler(w http.ResponseWriter, r *http.Request) {
  fmt.Fprintf(w, "path - %s", r.URL.Path[1:])
}

func main() {
  fs := http.FileServer(http.Dir("static"))

  http.Handle("/static/", withMiddleware(http.StripPrefix("/static/", fs)))
  http.Handle("/", withMiddlewareFunc(serveTemplate))
  http.Handle("/api/", withMiddlewareFunc(handler))


  log.Println("Listening on 8080...")
  http.ListenAndServe(":8080", nil)
}

func serveTemplate(w http.ResponseWriter, r *http.Request) {
  filePath := r.URL.Path

  if filePath == "/" {
    filePath = "index"
  }
  filePath = filePath + ".html"

  lp := path.Join("templates", "layout.html")
  fp := path.Join("templates", filePath)

  // Return a 404 if the template doesn't exist
  info, err := os.Stat(fp)
  if err != nil {
    if os.IsNotExist(err) {
      http.NotFound(w, r)
      return
    }
  }

  // Return a 404 if the request is for a directory
  if info.IsDir() {
    http.NotFound(w, r)
    return
  }

  tmpl, err := template.ParseFiles(lp, fp)
  if err != nil {
    // Log the detailed error
    log.Println(err.Error())
    // Return a generic "Internal Server Error" message
    http.Error(w, http.StatusText(500), 500)
    return
  }

  if err := tmpl.ExecuteTemplate(w, "layout", nil); err != nil {
    log.Println(err.Error())
    http.Error(w, http.StatusText(500), 500)
  }
}
<commit_msg>Simple json listing of the books<commit_after>package main

import (
  "html/template"
  "log"
  "net/http"
  "os"
  "path"
  "time"
  "encoding/json"
)

type Middleware struct {
  Next http.Handler
}

func withMiddleware(handler http.Handler) Middleware {
  return Middleware{handler}
}

func withMiddlewareFunc(fn http.HandlerFunc) Middleware {
  return withMiddleware(http.HandlerFunc(fn))
}

func (m Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  t1 := time.Now()
  m.Next.ServeHTTP(w, r)
  t2 := time.Now()
  log.Printf("[%s] %q %v\n", r.Method, r.URL.String(), t2.Sub(t1))
}

type Book struct {
  Id          string  `json:"id"`
  Title       string  `json:"title"`
  Description string  `json:"description"`
  Color       string  `json:"color"`
}

func main() {
  fs := http.FileServer(http.Dir("static"))

  http.Handle("/static/", withMiddleware(http.StripPrefix("/static/", fs)))
  http.Handle("/", withMiddlewareFunc(serveTemplate))
  http.Handle("/api/", withMiddlewareFunc(serveApi))

  log.Println("Listening on 8080...")
  http.ListenAndServe(":8080", nil)
}

func serveApi(w http.ResponseWriter, r *http.Request) {
  book := Book{"1", "Dark river", "The story of ...", "blue"}
  books := []Book{book}

  json.NewEncoder(w).Encode(books)
}

func serveTemplate(w http.ResponseWriter, r *http.Request) {
  filePath := r.URL.Path

  if filePath == "/" {
    filePath = "index"
  }
  filePath = filePath + ".html"

  lp := path.Join("templates", "layout.html")
  fp := path.Join("templates", filePath)

  // Return a 404 if the template doesn't exist
  info, err := os.Stat(fp)
  if err != nil {
    if os.IsNotExist(err) {
      http.NotFound(w, r)
      return
    }
  }

  // Return a 404 if the request is for a directory
  if info.IsDir() {
    http.NotFound(w, r)
    return
  }

  tmpl, err := template.ParseFiles(lp, fp)
  if err != nil {
    // Log the detailed error
    log.Println(err.Error())
    // Return a generic "Internal Server Error" message
    http.Error(w, http.StatusText(500), 500)
    return
  }

  if err := tmpl.ExecuteTemplate(w, "layout", nil); err != nil {
    log.Println(err.Error())
    http.Error(w, http.StatusText(500), 500)
  }
}

{'fraction_non_alphanumeric': 0.16201117318435754, 'fraction_numerical': 0.015059509351469517, 'mean_word_length': 3.3438818565400843, 'pattern_counts': {'":': 2, '<': 3, '<?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': '1800923', 'n_tokens_mistral': 1514, 'n_tokens_neox': 1465, 'n_words': 345}