<commit_before>package main

import (
	"flag"
	"fmt"
	"log"
	"net"
	"net/http"
	"net/url"
	"os"

	"github.com/vincent-petithory/kraken"
	"github.com/vincent-petithory/kraken/admin"
	"github.com/vincent-petithory/kraken/fileserver"
)

var adminAddr string

const (
	// Environnement var for the addr of the admin service.
	// It takes precedence on the -http flag.
	envKrakenAddr = "KRAKEN_ADDR"
	// Environnement var for the base URL of the admin service.
	envKrakenURL = "KRAKEN_URL"
)

func init() {
	flag.StringVar(&adminAddr, "http", "localhost:4214", "The address on which the admin http api will listen on. Defaults to :4214")
	flag.Parse()
}

func main() {
	// Register fileservers
	fsf := make(fileserver.Factory)

	// Init server pool, run existing servers and listen for new ones
	serverPool := kraken.NewServerPool(fsf)
	go serverPool.ListenAndRun()

	// Start administration server
	sph := admin.NewServerPoolHandler(serverPool)

	if envAdminAddr := os.Getenv(envKrakenAddr); envAdminAddr != "" {
		adminAddr = envAdminAddr
	}
	ln, err := net.Listen("tcp", adminAddr)
	if err != nil {
		log.Fatal(err)
	}
	srv := &http.Server{
		Handler: sph,
	}

	var (
		adminURL *url.URL
		urlErr   error
	)
	adminURL, urlErr = url.Parse(fmt.Sprintf("http://%s", ln.Addr()))
	if envAdminURL := os.Getenv(envKrakenURL); envAdminURL != "" {
		adminURL, urlErr = url.Parse(envAdminURL)
	}
	if urlErr != nil {
		log.Fatal(urlErr)
	}
	sph.SetBaseURL(adminURL)

	log.Printf("[admin] Listening on %s", ln.Addr())
	log.Printf("[admin] Available on %s", sph.BaseURL())<commit_msg>krakend: write help Usage(), remove -http flag.<commit_after>package main

import (
	"flag"
	"fmt"
	"log"
	"net"
	"net/http"
	"net/url"
	"os"

	"github.com/vincent-petithory/kraken"
	"github.com/vincent-petithory/kraken/admin"
	"github.com/vincent-petithory/kraken/fileserver"
)

const (
	// Environnement var for the addr of the admin service.
	// It takes precedence on the -http flag.
	envKrakenAddr = "KRAKEN_ADDR"
	// Environnement var for the base URL of the admin service.
	envKrakenURL = "KRAKEN_URL"
	// Default value of KRAKEN_ADDR
	defaultAddr = "localhost:4214"
)

func init() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr,
			`Usage: krakend

krakend is an on-demand http server. It creates http servers at runtime through a RESTful API.
Those servers are meant to serve static files.

Environment vars
----------------

krakend takes no arguments and is configured through the following environment variables:

    %s: Address to bind to and port to listen to; defaults to %s
    %s: URL on which the API is accessible; defaults to http://{KRAKEN_ADDR}

See krakenctl for a command-line client of the API.
`, envKrakenAddr, defaultAddr, envKrakenURL)
	}
	flag.Parse()
}

func main() {
	log.SetOutput(os.Stdout)
	adminAddr := defaultAddr
	// Register fileservers
	fsf := make(fileserver.Factory)
	// Init server pool, run existing servers and listen for new ones
	serverPool := kraken.NewServerPool(fsf)
	go serverPool.ListenAndRun()

	// Start administration server
	sph := admin.NewServerPoolHandler(serverPool)

	if envAdminAddr := os.Getenv(envKrakenAddr); envAdminAddr != "" {
		adminAddr = envAdminAddr
	}
	ln, err := net.Listen("tcp", adminAddr)
	if err != nil {
		log.Fatal(err)
	}

	var (
		adminURL *url.URL
		urlErr   error
	)
	adminURL, urlErr = url.Parse(fmt.Sprintf("http://%s", ln.Addr()))
	if envAdminURL := os.Getenv(envKrakenURL); envAdminURL != "" {
		adminURL, urlErr = url.Parse(envAdminURL)
	}
	if urlErr != nil {
		log.Fatal(urlErr)
	}
	sph.SetBaseURL(adminURL)

	srv := &http.Server{
		Handler: sph,
	}
	log.Printf("[admin] Listening on %s", ln.Addr())
	log.Printf("[admin] Available on %s", sph.BaseURL())
{'fraction_non_alphanumeric': 0.13297442799461642, 'fraction_numerical': 0.003230148048452221, 'mean_word_length': 3.948069241011984, 'pattern_counts': {'":': 0, '<': 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': '4774830', 'n_tokens_mistral': 1389, 'n_tokens_neox': 1348, 'n_words': 357}