<commit_before>package main

import (
	"bytes"
	"crypto/subtle"
	"encoding/json"
	"fmt"
	"io"
	"mime"
	"net/http"
	"path/filepath"
	"strings"
	"time"

	"github.com/flynn/flynn/Godeps/_workspace/src/github.com/badgerodon/ioutil"
	"github.com/flynn/flynn/Godeps/_workspace/src/github.com/go-martini/martini"
	"github.com/flynn/flynn/Godeps/_workspace/src/github.com/martini-contrib/binding"
	"github.com/flynn/flynn/Godeps/_workspace/src/github.com/martini-contrib/render"
	"github.com/flynn/flynn/pkg/cors"
	"github.com/flynn/flynn/pkg/status"
)

type LoginInfo struct {
	Token string `json:"token" form:"token"`
}

func AssetReader(path string) (io.ReadSeeker, time.Time, error) {
	t := time.Time{}
	data, err := Asset(path)
	if err != nil {
		return nil, t, err
	}
	if fi, err := AssetInfo(path); err != nil {
		t = fi.ModTime()
	}
	return bytes.NewReader(data), t, nil
}

func APIHandler(conf *Config) http.Handler {
	r := martini.NewRouter()
	m := martini.New()
	m.Use(martini.Logger())
	m.Use(martini.Recovery())
	m.Use(render.Renderer(render.Options{}))
	m.Action(r.Handle)

	m.Map(conf)

	httpInterfaceURL := conf.InterfaceURL
	if strings.HasPrefix(conf.InterfaceURL, "https") {
		httpInterfaceURL = "http" + strings.TrimPrefix(conf.InterfaceURL, "https")
	}

	m.Use(corsHandler(&cors.Options{
		AllowOrigins:     []string{conf.InterfaceURL, httpInterfaceURL},
		AllowMethods:     []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD"},
		AllowHeaders:     []string{"Authorization", "Accept", "Content-Type", "If-Match", "If-None-Match"},
		ExposeHeaders:    []string{"ETag"},
		AllowCredentials: true,
		MaxAge:           time.Hour,
	}))

	r.Get(status.Path, status.HealthyHandler)

	r.Group(conf.PathPrefix, func(r martini.Router) {
		m.Use(reqHelperMiddleware)
		r.Post("/user/sessions", binding.Bind(LoginInfo{}), login)
		r.Delete("/user/session", logout)

		r.Get("/config", getConfig)
		r.Get("/cert", getCert)

		r.Any("/assets/dashboard.*.js", serveDashboardJs)

		r.Any("/assets.*", serveAsset)

		r.Get("/ping", pingHandler)

		r.Get("/.*", serveTemplate)
	})

	return m
}

func corsHandler(corsOptions *cors.Options) http.HandlerFunc {
	defaultCorsHandler := cors.Allow(corsOptions)
	return func(w http.ResponseWriter, req *http.Request) {
		origin := req.Header.Get("Origin")
		if req.URL.Path == "/ping" && strings.HasPrefix(origin, "http://localhost:") {
			cors.Allow(&cors.Options{
				AllowOrigins:     []string{origin},
				AllowMethods:     []string{"GET"},
				AllowHeaders:     corsOptions.AllowHeaders,<commit_msg>dashboard: Fix status handler<commit_after>package main

import (
	"bytes"
	"crypto/subtle"
	"encoding/json"
	"fmt"
	"io"
	"mime"
	"net/http"
	"path/filepath"
	"strings"
	"time"

	"github.com/flynn/flynn/Godeps/_workspace/src/github.com/badgerodon/ioutil"
	"github.com/flynn/flynn/Godeps/_workspace/src/github.com/go-martini/martini"
	"github.com/flynn/flynn/Godeps/_workspace/src/github.com/martini-contrib/binding"
	"github.com/flynn/flynn/Godeps/_workspace/src/github.com/martini-contrib/render"
	"github.com/flynn/flynn/pkg/cors"
	"github.com/flynn/flynn/pkg/status"
)

type LoginInfo struct {
	Token string `json:"token" form:"token"`
}

func AssetReader(path string) (io.ReadSeeker, time.Time, error) {
	t := time.Time{}
	data, err := Asset(path)
	if err != nil {
		return nil, t, err
	}
	if fi, err := AssetInfo(path); err != nil {
		t = fi.ModTime()
	}
	return bytes.NewReader(data), t, nil
}

func APIHandler(conf *Config) http.Handler {
	r := martini.NewRouter()
	m := martini.New()
	m.Use(martini.Logger())
	m.Use(martini.Recovery())
	m.Use(render.Renderer(render.Options{}))
	m.Action(r.Handle)

	m.Map(conf)

	httpInterfaceURL := conf.InterfaceURL
	if strings.HasPrefix(conf.InterfaceURL, "https") {
		httpInterfaceURL = "http" + strings.TrimPrefix(conf.InterfaceURL, "https")
	}

	m.Use(corsHandler(&cors.Options{
		AllowOrigins:     []string{conf.InterfaceURL, httpInterfaceURL},
		AllowMethods:     []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD"},
		AllowHeaders:     []string{"Authorization", "Accept", "Content-Type", "If-Match", "If-None-Match"},
		ExposeHeaders:    []string{"ETag"},
		AllowCredentials: true,
		MaxAge:           time.Hour,
	}))

	r.Get(status.Path, status.HealthyHandler.ServeHTTP)

	r.Group(conf.PathPrefix, func(r martini.Router) {
		m.Use(reqHelperMiddleware)
		r.Post("/user/sessions", binding.Bind(LoginInfo{}), login)
		r.Delete("/user/session", logout)

		r.Get("/config", getConfig)
		r.Get("/cert", getCert)

		r.Any("/assets/dashboard.*.js", serveDashboardJs)

		r.Any("/assets.*", serveAsset)

		r.Get("/ping", pingHandler)

		r.Get("/.*", serveTemplate)
	})

	return m
}

func corsHandler(corsOptions *cors.Options) http.HandlerFunc {
	defaultCorsHandler := cors.Allow(corsOptions)
	return func(w http.ResponseWriter, req *http.Request) {
		origin := req.Header.Get("Origin")
		if req.URL.Path == "/ping" && strings.HasPrefix(origin, "http://localhost:") {
			cors.Allow(&cors.Options{
				AllowOrigins:     []string{origin},
				AllowMethods:     []string{"GET"},
				AllowHeaders:     corsOptions.AllowHeaders,
{'fraction_non_alphanumeric': 0.17702227432590856, 'fraction_numerical': 0.0, 'mean_word_length': 4.4054910242872225, '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': '3899186', 'n_tokens_mistral': 2001, 'n_tokens_neox': 1897, 'n_words': 304}