blob: 8506f158cdbb38754569afe046b1f15b389f0480 [file] [log] [blame]
package main
import (
"flag"
"net/http"
"path/filepath"
"runtime"
"github.com/fiorix/go-web/autogzip"
"github.com/gorilla/mux"
"go.skia.org/infra/go/common"
"go.skia.org/infra/go/httputils"
"go.skia.org/infra/go/sklog"
)
// flags
var (
local = flag.Bool("local", false, "Running locally if true. As opposed to in production.")
port = flag.String("port", ":8000", "HTTP service address (e.g., ':8000')")
promPort = flag.String("prom_port", ":20000", "Metrics service address (e.g., ':10110')")
resourcesDir = flag.String("resources_dir", "", "The directory to find templates, JS, and CSS files. If blank the current directory will be used.")
)
func mainHandler(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "https://debugger.skia.org", http.StatusPermanentRedirect)
}
func makeResourceHandler() func(http.ResponseWriter, *http.Request) {
fileServer := http.FileServer(http.Dir(*resourcesDir))
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Cache-Control", "max-age=300")
w.Header().Set("Access-Control-Allow-Origin", "*")
fileServer.ServeHTTP(w, r)
}
}
func main() {
common.InitWithMust(
"debugger-assets",
common.PrometheusOpt(promPort),
common.MetricsLoggingOpt(),
)
if *resourcesDir == "" {
_, filename, _, _ := runtime.Caller(0)
*resourcesDir = filepath.Join(filepath.Dir(filename), "../..")
}
router := mux.NewRouter()
router.PathPrefix("/res/").HandlerFunc(autogzip.HandleFunc(makeResourceHandler()))
router.HandleFunc("/", mainHandler)
http.Handle("/", httputils.HealthzAndHTTPS(httputils.LoggingRequestResponse(router)))
sklog.Infoln("Ready to serve.")
sklog.Fatal(http.ListenAndServe(*port, nil))
}