<commit_before>package main

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

	"github.com/julienschmidt/httprouter"
	"github.com/reshetylo/cmdexec"
)

var tpl *template.Template

type Confiuration struct {
	ServerAddress string
	StaticFolder  string
	StaticAddress string
	Templates     string
	Modules       string
}

type Context struct {
	Title string
	Data  string
}

var context Context
var config Confiuration

func Page(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
	log.Println("access", req.Host, req.RequestURI)

	pagename := ps.ByName("page")
	if len(pagename) == 0 {
		pagename = "index"
	}
	if _, err := os.Stat(strings.Replace(config.Templates, "*", pagename, 1)); err != nil {
		pagename = "not_found"
		log.Println("not_found", req.Host, req.RequestURI)
	}
	render(w, pagename+".gohtml")
}

func ApiModule(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
	runfile := strings.Replace(config.Modules, "*", ps.ByName("name"), 1)

	req_url, _ := url.ParseRequestURI(req.RequestURI)
	param, _ := url.ParseQuery(req_url.RawQuery)
	log.Println("api_module", req.Host, req.RequestURI)

	if _, err := os.Stat(runfile); err == nil {
		cmdexec.RenderFile(runfile, param, w)
		//response := executor.ExecFile(runfile, param)
		//responsejson := json.NewEncoder(reponse)
		//		log.Println(response)
		//		w.Header().Set("Content-Type", "application/json")
		//		w.Write([]byte(response))
		//		for _, result := range response {
		//			w.Write([]byte(result))
		//		}
	}
}

func init() {
	// get configuration
	cfg, _ := os.Open("config.json")
	decoder := json.NewDecoder(cfg)
	config = Confiuration{}
	err := decoder.Decode(&config)
	if err != nil {
		log.Fatal("Init: ", err)
	}

	// application init
	tpl = template.Must(template.ParseGlob(config.Templates))
	context = Context{
		Title: "Test Title",
		Data:  "Test Data",
	}
}

func render(w http.ResponseWriter, tpl_name string) {
	err := tpl.ExecuteTemplate(w, tpl_name, context)
	if err != nil {
		log.Fatalln("Render: ", err)
	}
}

func main() {
	router := httprouter.New()
	router.GET("/", Page)
	router.GET("/mod_:page", Page)
	router.GET("/api/:name", ApiModule)<commit_msg>code changes. added -config option for custom configuration files<commit_after>package main

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

	"github.com/julienschmidt/httprouter"
	"github.com/reshetylo/cmdexec"
)

var tpl *template.Template

type Confiuration struct {
	ServerAddress string
	StaticFolder  string
	StaticAddress string
	Templates     string
	Modules       string
}

type Context struct {
	Title string
	Data  string
}

var context = Context{}
var config = Confiuration{}

func Index(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
	render(w, "index.gohtml")
}

func Page(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
	log.Println("access", req.Host, req.RequestURI)

	pagename := ps.ByName("page")
	if _, err := os.Stat(strings.Replace(config.Templates, "*", pagename, 1)); err != nil {
		pagename = "not_found"
		log.Println("not_found", req.Host, req.RequestURI)
	}
	render(w, pagename+".gohtml")
}

func ApiModule(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
	runfile := strings.Replace(config.Modules, "*", ps.ByName("name"), 1)

	req_url, _ := url.ParseRequestURI(req.RequestURI)
	param, _ := url.ParseQuery(req_url.RawQuery)
	log.Println("api_module", req.Host, req.RequestURI)

	if _, err := os.Stat(runfile); err == nil {
		cmdexec.RenderFile(runfile, param, w)
		//response := executor.ExecFile(runfile, param)
		//responsejson := json.NewEncoder(reponse)
		//		log.Println(response)
		//		w.Header().Set("Content-Type", "application/json")
		//		w.Write([]byte(response))
		//		for _, result := range response {
		//			w.Write([]byte(result))
		//		}
	}
}

func init() {
	// read cli flags
	config_file := flag.String("config", "config.json", "configuration file")
	flag.Parse()

	// get configuration
	cfg, _ := os.Open(*config_file)
	decoder := json.NewDecoder(cfg)
	err := decoder.Decode(&config)
	if err != nil {
		log.Fatal("Init: ", err)
	}

	// application init
	tpl = template.Must(template.ParseGlob(config.Templates))
}

func render(w http.ResponseWriter, tpl_name string) {
	err := tpl.ExecuteTemplate(w, tpl_name, context)
	if err != nil {
		log.Fatalln("Render: ", err)
	}
}

func main() {
	router := httprouter.New()
	router.GET("/", Index)
	router.GET("/page/:page", Page)
	router.GET("/api/:name", ApiModule)
{'fraction_non_alphanumeric': 0.16138894955230398, 'fraction_numerical': 0.0010919414719371041, 'mean_word_length': 4.146067415730337, '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': '1326235', 'n_tokens_mistral': 1698, 'n_tokens_neox': 1668, 'n_words': 341}