<commit_before>package main

import (
	"encoding/json"
	"fmt"
	"net/http"
	"strings"

	"github.com/NebulousLabs/Sia-Block-Explorer/api"
)

// A structure to store any state of the server. Should remain
// relatively unpopulated, mostly constants which will eventually be
// broken off
type ExploreServer struct {
	// The explorer must know where to send the API calls
	siad *api.ApiLink

	// Used to store the server muxer
	serveMux *http.ServeMux
}

// writeJSON writes the object to the ResponseWriter. If the encoding fails, an
// error is written instead.
func writeJSON(w http.ResponseWriter, obj interface{}) {
	if json.NewEncoder(w).Encode(obj) != nil {
		http.Error(w, "Failed to encode response", http.StatusInternalServerError)
	}
}

func (srv *ExploreServer) overviewPage(w http.ResponseWriter, r *http.Request) {
	// First query the local instance of siad for the status
	explorerState, err := srv.siad.ExplorerState()
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}
	blocklist, err := srv.siad.GetBlockData(0, explorerState.Height)
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	// Attempt to make a page out of it
	page, err := parseOverview(overviewRoot{
		Explorer: explorerState,
		Blocks: blocklist,
	})
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	w.Write(page)
}

// Handles the root page being requested. Is responsible for
// differentiating between api calls and pages
func (srv *ExploreServer) rootHandler(w http.ResponseWriter, r *http.Request) {

	if (strings.Contains(r.URL.Path, ".")) {
		srv.serveMux.ServeHTTP(w, r)
	} else {
		srv.overviewPage(w, r)
	}
}

// TODO: parse port as a command line option

func main() {
	// Initilize the server
	var srv = &ExploreServer{
		siad: api.New("9000"),
		serveMux: http.NewServeMux(),
	}

	srv.serveMux.Handle("/", http.FileServer(http.Dir("./webroot/")))
	http.HandleFunc("/", srv.rootHandler)
	http.ListenAndServe(":9003", nil)
	fmt.Println("Done serving")
}
<commit_msg>Add Command line flags for port numbers<commit_after>package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"net/http"
	"strings"

	"github.com/NebulousLabs/Sia-Block-Explorer/api"
)

// A structure to store any state of the server. Should remain
// relatively unpopulated, mostly constants which will eventually be
// broken off
type ExploreServer struct {
	// The explorer must know where to send the API calls
	siad *api.ApiLink

	// Used to store the server muxer
	serveMux *http.ServeMux
}

// writeJSON writes the object to the ResponseWriter. If the encoding fails, an
// error is written instead.
func writeJSON(w http.ResponseWriter, obj interface{}) {
	if json.NewEncoder(w).Encode(obj) != nil {
		http.Error(w, "Failed to encode response", http.StatusInternalServerError)
	}
}

func (srv *ExploreServer) overviewPage(w http.ResponseWriter, r *http.Request) {
	// First query the local instance of siad for the status
	explorerState, err := srv.siad.ExplorerState()
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}
	blocklist, err := srv.siad.GetBlockData(0, explorerState.Height)
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	// Attempt to make a page out of it
	page, err := parseOverview(overviewRoot{
		Explorer: explorerState,
		Blocks: blocklist,
	})
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	w.Write(page)
}

// Handles the root page being requested. Is responsible for
// differentiating between api calls and pages
func (srv *ExploreServer) rootHandler(w http.ResponseWriter, r *http.Request) {
	if (strings.Contains(r.URL.Path, ".")) {
		srv.serveMux.ServeHTTP(w, r)
	} else {
		srv.overviewPage(w, r)
	}
}

// TODO: parse port as a command line option

func main() {
	// Parse command line flags
	apiPort := flag.String("a", "9980", "Api port")
	hostPort := flag.String("p", "9983", "HTTP host port")
	flag.Parse()


	// Initilize the server
	var srv = &ExploreServer{
		siad: api.New(*apiPort),
		serveMux: http.NewServeMux(),
	}

	srv.serveMux.Handle("/", http.FileServer(http.Dir("./webroot/")))
	http.HandleFunc("/", srv.rootHandler)
	http.ListenAndServe(":"+*hostPort, nil)
	fmt.Println("Done serving")
}

{'fraction_non_alphanumeric': 0.13189964157706094, 'fraction_numerical': 0.008602150537634409, 'mean_word_length': 3.9479905437352247, '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': '2424467', 'n_tokens_mistral': 1537, 'n_tokens_neox': 1437, 'n_words': 423}