<commit_before>package main

import (
	"flag"
	"io"
	"log"
	"net/http"
	"os"
	"time"
)

var root string // root directory

const msg404 = `
<html lang="en">
 <head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>404 - not found</title>
<style>
* { padding: 0; margin: 0; }
html {
    -webkit-font-smoothing: antialiased;
    background-color: #fafafa;}
body {
    font-family: 'Helvetica Neue', Helvetica, Arial, 'lucida grande', tahoma, verdana, arial, sans-serif;
    color: #222222;
    margin: 0;
    line-height: 1.5em;}</style>
</head><body>
  <div style="padding:40px;"> 
  <h2>404 - not found</h2><hr size="1" width="30%"/>
  <br/><a href="/">go back</a></div>
</body></html>`

func notFoundHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	w.WriteHeader(http.StatusNotFound)
	io.WriteString(w, msg404)
}

type logServer struct {
	hdl http.Handler
}

func (l *logServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {

	w.Header().Add("Server", "nanoServ")
	fi, err := os.Stat(root + r.RequestURI)
	if fi == nil && err != nil {
		log.Printf("404: %s\n", r.RequestURI) //"stat /does/not/exist: no such file or directory"
		notFoundHandler(w, r)
		return
	}
	log.Println(r.Method, r.RequestURI)
	l.hdl.ServeHTTP(w, r)
}

func init() {
	const (
		defRoot = "."
		usage   = "root directory"
	)
	flag.StringVar(&root, "root", defRoot, usage)
	flag.StringVar(&root, "r", defRoot, usage+" (shorthand)")
}

func main() {

	addr := flag.String("addr", ":3000", "tcp4 host and port to listen")
	flag.Parse()

	srv := &http.Server{
		Addr:           *addr,
		Handler:        &logServer{hdl: http.FileServer(http.Dir(root))},
		ReadTimeout:    10 * time.Second,
		WriteTimeout:   10 * time.Second,
		MaxHeaderBytes: 1 << 15,
	}

	log.Printf("webServer started at %v with root=%v\n\n", srv.Addr, root)
	log.Fatal(srv.ListenAndServe())
}
<commit_msg>init func<commit_after>package main

import (
	"flag"
	"io"
	"log"
	"net/http"
	"os"
	"time"
)

var root string // root directory

const (
	sign = "nanoServ"

	msg404 = `
<html lang="en">
 <head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>404 - not found</title>
   <style>
	* { padding:0; margin:0; }
	html {
	    -webkit-font-smoothing: antialiased;
	    background-color:#fafaf0;}
	body {
	    font-family: Helvetica, Arial, Verdana;
	    color:#222222; line-height:1.5em;}
   </style>
  </head>
<body>
  <div style="padding:40px;"> 
  <h2>404 - not found</h2><hr size="1" width="30%"/>
  <br/><a href="/">go back</a></div>
</body></html>`
)

func notFoundHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	w.WriteHeader(http.StatusNotFound)
	io.WriteString(w, msg404)
}

type logServer struct {
	hdl http.Handler
}

func (l *logServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {

	w.Header().Add("Server", sign)
	fi, err := os.Stat(root + r.RequestURI)
	if fi == nil && err != nil {
		log.Printf("404: %s\n", r.RequestURI) //"stat /does/not/exist: no such file or directory"
		notFoundHandler(w, r)
		return
	}
	log.Println(r.Method, r.RequestURI)
	l.hdl.ServeHTTP(w, r)
}

func init() {
	const (
		defRoot = "."
		usage   = "root directory"
	)
	flag.StringVar(&root, "root", defRoot, usage)
	flag.StringVar(&root, "r", defRoot, usage+" (shorthand)")
}

func main() {

	addr := flag.String("addr", ":3000", "tcp4 host and port to listen")
	flag.Parse()

	srv := &http.Server{
		Addr:           *addr,
		Handler:        &logServer{hdl: http.FileServer(http.Dir(root))},
		ReadTimeout:    10 * time.Second,
		WriteTimeout:   10 * time.Second,
		MaxHeaderBytes: 1 << 15,
	}

	log.Printf("webServer started at %v with root=%v\n\n", srv.Addr, root)
	log.Fatal(srv.ListenAndServe())
}

{'fraction_non_alphanumeric': 0.18685031185031184, 'fraction_numerical': 0.02442827442827443, 'mean_word_length': 3.751851851851852, 'pattern_counts': {'":': 2, '<': 47, '<?xml version=': 0, '>': 43, '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': '1395813', 'n_tokens_mistral': 1612, 'n_tokens_neox': 1505, 'n_words': 333}