lotus/cmd/lotus-provider/web/srv.go

58 lines
1.6 KiB
Go
Raw Normal View History

2023-12-12 05:16:57 +00:00
// Package web defines the HTTP web server for static files and endpoints.
package web
import (
"context"
"embed"
"net"
"net/http"
2023-12-20 00:58:55 +00:00
"os"
2023-12-12 05:16:57 +00:00
"strings"
2023-12-20 00:58:55 +00:00
"time"
2023-12-12 05:16:57 +00:00
2023-12-19 15:12:45 +00:00
"github.com/gorilla/mux"
"go.opencensus.io/tag"
2023-12-12 05:16:57 +00:00
"github.com/filecoin-project/lotus/cmd/lotus-provider/deps"
"github.com/filecoin-project/lotus/cmd/lotus-provider/web/api"
"github.com/filecoin-project/lotus/cmd/lotus-provider/web/hapi"
2023-12-12 05:16:57 +00:00
"github.com/filecoin-project/lotus/metrics"
)
// go:embed static
var static embed.FS
2023-12-20 00:58:55 +00:00
// An dev mode hack for no-restart changes to static and templates.
// You still need to recomplie the binary for changes to go code.
var webDev = os.Getenv("LOTUS_WEB_DEV") == "1"
2023-12-12 05:16:57 +00:00
func GetSrv(ctx context.Context, deps *deps.Deps) (*http.Server, error) {
mux := mux.NewRouter()
api.Routes(mux.PathPrefix("/api").Subrouter(), deps)
err := hapi.Routes(mux.PathPrefix("/hapi").Subrouter(), deps)
if err != nil {
return nil, err
}
2023-12-12 05:16:57 +00:00
mux.NotFoundHandler = http.FileServer(http.FS(static))
2023-12-20 00:58:55 +00:00
if webDev {
mux.NotFoundHandler = http.FileServer(http.Dir("cmd/lotus-provider/web/static"))
}
2023-12-12 05:16:57 +00:00
return &http.Server{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "/") {
r.URL.Path = r.URL.Path + "index.html"
return
}
mux.ServeHTTP(w, r)
}),
BaseContext: func(listener net.Listener) context.Context {
ctx, _ := tag.New(context.Background(), tag.Upsert(metrics.APIInterface, "lotus-provider"))
return ctx
},
2023-12-20 00:58:55 +00:00
Addr: deps.Cfg.Subsystems.GuiAddress,
ReadTimeout: time.Minute * 3,
ReadHeaderTimeout: time.Minute * 3, // lint
2023-12-12 05:16:57 +00:00
}, nil
}