<commit_before>package main

import (
	"encoding/json"
	"flag"
	"log"
	"net/http"
	"net/url"
	"os"
	"strings"
	"text/template"

	"github.com/rollbrettler/daily-stars/stars"
)

var port string

func init() {
	flag.StringVar(&port, "port", ":8001", "Port to listen on")
}

func main() {
	flag.Parse()

	envPort := os.Getenv("PORT")

	if envPort != "" {
		port = ":" + envPort
	}

	http.HandleFunc("/", showStar)
	fs := http.FileServer(http.Dir("assets"))
	http.Handle("/assets/", http.StripPrefix("/assets/", fs))
	http.HandleFunc("/favicon.ico", handleFavicon)
	http.ListenAndServe(port, nil)
}

func handleFavicon(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte(""))
}

func showStar(w http.ResponseWriter, r *http.Request) {

	username, suffix := username(r.URL)
	if username == "" {
		t, _ := template.ParseFiles("html/index.html")
		t.Execute(w, r.Host)
		return
	}
	log.Printf("Username: %v\n", username)

	s := stars.New(username)

	repos, err := s.Repos()
	if err != nil {
		w.Write([]byte("Wrong username"))
		return
	}

	t, _ := template.ParseFiles("html/result.html")

	if suffix {
		jsonResponse(w, repos)
	} else {
		t.Execute(w, repos)
	}

}

func jsonResponse(w http.ResponseWriter, r []stars.StaredRepos) {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")

	marshaledJSON, err := json.Marshal(r)
	if err != nil {
		w.Write([]byte("{'error': 'Wrong username'}"))
	}
	w.Write(marshaledJSON)
}

func username(s *url.URL) (string, bool) {
	u := strings.Split(s.Path, "/")
	i := strings.Index(u[len(u)-1], ".json")
	if i >= 0 {
		return u[len(u)-1][:i], true
	}
	return u[len(u)-1], false
}<commit_msg>Extract config parsing<commit_after>package main

import (
	"encoding/json"
	"flag"
	"log"
	"net/http"
	"net/url"
	"os"
	"strings"
	"text/template"

	"github.com/rollbrettler/daily-stars/stars"
)

var port string

func init() {
	flag.StringVar(&port, "port", ":8001", "Port to listen on")
}

func main() {
	parseConfigFlags()

	http.HandleFunc("/", showStar)
	fs := http.FileServer(http.Dir("assets"))
	http.Handle("/assets/", http.StripPrefix("/assets/", fs))
	http.HandleFunc("/favicon.ico", handleFavicon)
	http.ListenAndServe(port, nil)
}

func handleFavicon(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte(""))
}

func showStar(w http.ResponseWriter, r *http.Request) {

	username, suffix := username(r.URL)
	if username == "" {
		t, _ := template.ParseFiles("html/index.html")
		t.Execute(w, r.Host)
		return
	}
	log.Printf("Username: %v\n", username)

	s := stars.New(username)

	repos, err := s.Repos()
	if err != nil {
		w.Write([]byte("Wrong username"))
		return
	}

	t, _ := template.ParseFiles("html/result.html")

	if suffix {
		jsonResponse(w, repos)
	} else {
		t.Execute(w, repos)
	}

}

func jsonResponse(w http.ResponseWriter, r []stars.StaredRepos) {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")

	marshaledJSON, err := json.Marshal(r)
	if err != nil {
		w.Write([]byte("{'error': 'Wrong username'}"))
	}
	w.Write(marshaledJSON)
}

func username(s *url.URL) (string, bool) {
	u := strings.Split(s.Path, "/")
	i := strings.Index(u[len(u)-1], ".json")
	if i >= 0 {
		return u[len(u)-1][:i], true
	}
	return u[len(u)-1], false
}

func parseConfigFlags() {
	flag.Parse()

	envPort := os.Getenv("PORT")

	if envPort != "" {
		port = ":" + envPort
	}
}
{'fraction_non_alphanumeric': 0.20083932853717026, 'fraction_numerical': 0.00539568345323741, 'mean_word_length': 4.118098159509202, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 5, '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': '4297011', 'n_tokens_mistral': 1374, 'n_tokens_neox': 1314, 'n_words': 255}