<commit_before>package server

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"os"
	"runtime"
	"runtime/pprof"
	"time"

	"github.com/gorilla/mux"
	"github.com/gorilla/websocket"
	"github.com/nytlabs/streamtools/st/blocks"
	"github.com/nytlabs/streamtools/st/library"
	"github.com/nytlabs/streamtools/st/loghub"
	"github.com/nytlabs/streamtools/st/util"
)

var logStream = hub{
	Broadcast:   make(chan []byte),
	register:    make(chan *connection),
	unregister:  make(chan *connection),
	connections: make(map[*connection]bool),
}

var uiStream = hub{
	Broadcast:   make(chan []byte),
	register:    make(chan *connection),
	unregister:  make(chan *connection),
	connections: make(map[*connection]bool),
}

type Server struct {
	manager *BlockManager
	Port    string
	Domain  string
	Id      string
}

func NewServer() *Server {
	return &Server{
		manager: NewBlockManager(),
	}
}

var resourceType = map[string]string{
	"html": "text/html; charset=utf-8",
	"lib":  "application/javascript; charset=utf-8",
	"js":   "application/javascript; charset=utf-8",
	"json": "application/javascript; charset=utf-8",
	"css":  "text/css; charset=utf-8",
}

func (s *Server) rootHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	data, _ := Asset("gui/index.html")
	w.Write(data)
}

func (s *Server) exampleHandler(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	w.Header().Set("Content-Type", resourceType[vars["type"]])
	data, _ := Asset("examples/" + vars["file"])
	w.Write(data)
}

func (s *Server) staticHandler(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	w.Header().Set("Content-Type", resourceType[vars["type"]])
	data, _ := Asset("gui/static/" + vars["type"] + "/" + vars["file"])
	w.Write(data)
}

func (s *Server) libraryHandler(w http.ResponseWriter, r *http.Request) {
	lib, err := json.Marshal(library.BlockDefs)
	if err != nil {
		loghub.Log <- &loghub.LogMsg{
			Type: loghub.CREATE,
			Data: "Could not marshal library.",
			Id:   s.Id,
		}
	}
	s.apiWrap(w, r, 200, lib)
}

func (s *Server) versionHandler(w http.ResponseWriter, r *http.Request) {<commit_msg>apiwrap /examples responses to include headers<commit_after>package server

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"os"
	"runtime"
	"runtime/pprof"
	"time"

	"github.com/gorilla/mux"
	"github.com/gorilla/websocket"
	"github.com/nytlabs/streamtools/st/blocks"
	"github.com/nytlabs/streamtools/st/library"
	"github.com/nytlabs/streamtools/st/loghub"
	"github.com/nytlabs/streamtools/st/util"
)

var logStream = hub{
	Broadcast:   make(chan []byte),
	register:    make(chan *connection),
	unregister:  make(chan *connection),
	connections: make(map[*connection]bool),
}

var uiStream = hub{
	Broadcast:   make(chan []byte),
	register:    make(chan *connection),
	unregister:  make(chan *connection),
	connections: make(map[*connection]bool),
}

type Server struct {
	manager *BlockManager
	Port    string
	Domain  string
	Id      string
}

func NewServer() *Server {
	return &Server{
		manager: NewBlockManager(),
	}
}

var resourceType = map[string]string{
	"html": "text/html; charset=utf-8",
	"lib":  "application/javascript; charset=utf-8",
	"js":   "application/javascript; charset=utf-8",
	"json": "application/javascript; charset=utf-8",
	"css":  "text/css; charset=utf-8",
}

func (s *Server) rootHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	data, _ := Asset("gui/index.html")
	w.Write(data)
}

func (s *Server) exampleHandler(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	w.Header().Set("Content-Type", resourceType[vars["type"]])
	data, _ := Asset("examples/" + vars["file"])
	s.apiWrap(w, r, 200, data)
}

func (s *Server) staticHandler(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	w.Header().Set("Content-Type", resourceType[vars["type"]])
	data, _ := Asset("gui/static/" + vars["type"] + "/" + vars["file"])
	w.Write(data)
}

func (s *Server) libraryHandler(w http.ResponseWriter, r *http.Request) {
	lib, err := json.Marshal(library.BlockDefs)
	if err != nil {
		loghub.Log <- &loghub.LogMsg{
			Type: loghub.CREATE,
			Data: "Could not marshal library.",
			Id:   s.Id,
		}
	}
	s.apiWrap(w, r, 200, lib)
}

func (s *Server) versionHandler(w http.ResponseWriter, r *http.Request) {
{'fraction_non_alphanumeric': 0.17782805429864254, 'fraction_numerical': 0.004751131221719457, 'mean_word_length': 4.624681933842239, 'pattern_counts': {'":': 10, '<': 5, '<?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': '5064584', 'n_tokens_mistral': 1721, 'n_tokens_neox': 1674, 'n_words': 332}