<commit_before>package main

import (
	"bytes"
	"encoding/json"
	"errors"
	"flag"
	"fmt"
	"io"
	"log"
	"math/rand"
	"net/http"
	"os"
	"os/signal"
	"path/filepath"
	"runtime"
	"sort"
	"strconv"
	"strings"
	"time"

	"github.com/omniscale/magnacarto/mml"

	"golang.org/x/net/websocket"

	"github.com/omniscale/magnacarto"
	"github.com/omniscale/magnacarto/builder"
	mapnikBuilder "github.com/omniscale/magnacarto/builder/mapnik"
	"github.com/omniscale/magnacarto/builder/mapserver"
	"github.com/omniscale/magnacarto/config"
	"github.com/omniscale/magnacarto/maps"
	mssPkg "github.com/omniscale/magnacarto/mss"
	"github.com/omniscale/magnacarto/render"

	"github.com/gorilla/handlers"
	"github.com/gorilla/mux"
)

type magnaserv struct {
	config            *config.Magnacarto
	builderCache      *builder.Cache
	mapnikMaker       builder.MapMaker
	defaultMaker      builder.MapMaker
	mapnikRenderer    *render.Mapnik
	mapserverRenderer *render.MapServer
}

func (s *magnaserv) styleParams(r *http.Request) (mml string, mss []string) {
	baseDir := s.config.StylesDir
	base := r.FormValue("base")
	if base != "" {
		baseDir = filepath.Join(baseDir, base)
	}

	mml = r.FormValue("mml")
	if mml != "" {
		mml = filepath.Join(baseDir, mml)
	}

	mssList := r.FormValue("mss")
	if mssList != "" {
		for _, f := range strings.Split(mssList, ",") {
			mss = append(mss, filepath.Join(baseDir, f))
		}
	}

	return mml, mss
}

func (s *magnaserv) render(w http.ResponseWriter, r *http.Request) {
	mapReq, err := maps.ParseMapRequest(r)
	if err != nil {
		log.Println(err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	var maker builder.MapMaker
	renderer := mapReq.Query.Get("RENDERER")
	switch renderer {
	case "mapnik":
		maker = s.mapnikMaker
	case "mapserver":
		maker = mapserver.Maker
	default:
		maker = s.defaultMaker
		if mapserver.Maker == s.defaultMaker {
			renderer = "mapserver"
		}
	}

	styleFile := mapReq.Query.Get("FILE")
	if styleFile == "" {
		mml, mss := s.styleParams(r)
		if mml == "" {
			log.Println("missing mml param in request")
			http.Error(w, "missing mml param", http.StatusBadRequest)
			return
		}

		styleFile, err = s.builderCache.StyleFile(maker, mml, mss)
		if err != nil {
			log.Println(err)
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
	}

	w.Header().Add("Content-Type", "image/png")
	if renderer == "mapserver" {
		mapReq.Format = mapReq.Query.Get("FORMAT") // use requested format, not internal mapnik format
		if s.mapserverRenderer == nil {
			err = errors.New("mapserver not initialized")
		}
		err = s.mapserverRenderer.Render(styleFile, w, renderReq(mapReq))
	} else {
		if s.mapnikRenderer == nil {
			err = errors.New("mapnik not initialized")
		} else {
			err = s.mapnikRenderer.Render(styleFile, w, renderReq(mapReq))
		}
	}
	s.internalError(w, r, errors.New("text"))

	if err != nil {
		s.internalError(w, r, err)
		return
	}
}

func (s *magnaserv) projects(w http.ResponseWriter, r *http.Request) {
	projects, err := findProjects(s.config.StylesDir)
	if err != nil {
		s.internalError(w, r, err)
		return
	}

	sort.Sort(sort.Reverse(byLastChange(projects)))
	w.Header().Add("Content-Type", "application/json")

	enc := json.NewEncoder(w)
	err = enc.Encode(struct {
		Projects []project `json:"projects"`
	}{Projects: projects})
	if err != nil {
		s.internalError(w, r, err)
		return
	}<commit_msg>remove test error<commit_after>package main

import (
	"bytes"
	"encoding/json"
	"errors"
	"flag"
	"fmt"
	"io"
	"log"
	"math/rand"
	"net/http"
	"os"
	"os/signal"
	"path/filepath"
	"runtime"
	"sort"
	"strconv"
	"strings"
	"time"

	"github.com/omniscale/magnacarto/mml"

	"golang.org/x/net/websocket"

	"github.com/omniscale/magnacarto"
	"github.com/omniscale/magnacarto/builder"
	mapnikBuilder "github.com/omniscale/magnacarto/builder/mapnik"
	"github.com/omniscale/magnacarto/builder/mapserver"
	"github.com/omniscale/magnacarto/config"
	"github.com/omniscale/magnacarto/maps"
	mssPkg "github.com/omniscale/magnacarto/mss"
	"github.com/omniscale/magnacarto/render"

	"github.com/gorilla/handlers"
	"github.com/gorilla/mux"
)

type magnaserv struct {
	config            *config.Magnacarto
	builderCache      *builder.Cache
	mapnikMaker       builder.MapMaker
	defaultMaker      builder.MapMaker
	mapnikRenderer    *render.Mapnik
	mapserverRenderer *render.MapServer
}

func (s *magnaserv) styleParams(r *http.Request) (mml string, mss []string) {
	baseDir := s.config.StylesDir
	base := r.FormValue("base")
	if base != "" {
		baseDir = filepath.Join(baseDir, base)
	}

	mml = r.FormValue("mml")
	if mml != "" {
		mml = filepath.Join(baseDir, mml)
	}

	mssList := r.FormValue("mss")
	if mssList != "" {
		for _, f := range strings.Split(mssList, ",") {
			mss = append(mss, filepath.Join(baseDir, f))
		}
	}

	return mml, mss
}

func (s *magnaserv) render(w http.ResponseWriter, r *http.Request) {
	mapReq, err := maps.ParseMapRequest(r)
	if err != nil {
		log.Println(err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	var maker builder.MapMaker
	renderer := mapReq.Query.Get("RENDERER")
	switch renderer {
	case "mapnik":
		maker = s.mapnikMaker
	case "mapserver":
		maker = mapserver.Maker
	default:
		maker = s.defaultMaker
		if mapserver.Maker == s.defaultMaker {
			renderer = "mapserver"
		}
	}

	styleFile := mapReq.Query.Get("FILE")
	if styleFile == "" {
		mml, mss := s.styleParams(r)
		if mml == "" {
			log.Println("missing mml param in request")
			http.Error(w, "missing mml param", http.StatusBadRequest)
			return
		}

		styleFile, err = s.builderCache.StyleFile(maker, mml, mss)
		if err != nil {
			log.Println(err)
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
	}

	w.Header().Add("Content-Type", "image/png")
	if renderer == "mapserver" {
		mapReq.Format = mapReq.Query.Get("FORMAT") // use requested format, not internal mapnik format
		if s.mapserverRenderer == nil {
			err = errors.New("mapserver not initialized")
		}
		err = s.mapserverRenderer.Render(styleFile, w, renderReq(mapReq))
	} else {
		if s.mapnikRenderer == nil {
			err = errors.New("mapnik not initialized")
		} else {
			err = s.mapnikRenderer.Render(styleFile, w, renderReq(mapReq))
		}
	}

	if err != nil {
		s.internalError(w, r, err)
		return
	}
}

func (s *magnaserv) projects(w http.ResponseWriter, r *http.Request) {
	projects, err := findProjects(s.config.StylesDir)
	if err != nil {
		s.internalError(w, r, err)
		return
	}

	sort.Sort(sort.Reverse(byLastChange(projects)))
	w.Header().Add("Content-Type", "application/json")

	enc := json.NewEncoder(w)
	err = enc.Encode(struct {
		Projects []project `json:"projects"`
	}{Projects: projects})
	if err != nil {
		s.internalError(w, r, err)
		return
	}
{'fraction_non_alphanumeric': 0.15040710584752034, 'fraction_numerical': 0.0, 'mean_word_length': 3.906318082788671, 'pattern_counts': {'":': 4, '<': 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': '4301759', 'n_tokens_mistral': 2782, 'n_tokens_neox': 2638, 'n_words': 493}