2022-06-03 19:39:22 +00:00
|
|
|
// Copyright (C) 2022 The pages authors
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-06-09 18:45:37 +00:00
|
|
|
"mime"
|
2022-06-03 19:39:22 +00:00
|
|
|
"net/http"
|
2022-10-06 17:04:39 +00:00
|
|
|
"time"
|
2022-06-03 19:39:22 +00:00
|
|
|
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
2022-07-03 03:01:26 +00:00
|
|
|
"code.pitz.tech/mya/pages/internal"
|
|
|
|
"code.pitz.tech/mya/pages/internal/git"
|
|
|
|
|
2022-10-25 15:19:25 +00:00
|
|
|
"github.com/mjpitz/myago/config"
|
2022-06-03 19:39:22 +00:00
|
|
|
"github.com/mjpitz/myago/flagset"
|
|
|
|
"github.com/mjpitz/myago/zaputil"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HostConfig struct {
|
2022-06-09 15:39:44 +00:00
|
|
|
internal.ServerConfig
|
2022-10-25 15:19:25 +00:00
|
|
|
Git git.Config `json:"git"`
|
|
|
|
SiteFile string `json:"site_file" usage:"configure multiple sites using a single file"`
|
2022-06-03 19:39:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
hostConfig = &HostConfig{
|
2022-06-09 15:39:44 +00:00
|
|
|
ServerConfig: internal.ServerConfig{
|
2022-06-03 19:39:22 +00:00
|
|
|
Public: internal.BindConfig{Address: "0.0.0.0:8080"},
|
|
|
|
Private: internal.BindConfig{Address: "0.0.0.0:8081"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
Host = &cli.Command{
|
|
|
|
Name: "host",
|
|
|
|
Usage: "Host the web page content",
|
|
|
|
UsageText: "pages host",
|
|
|
|
Flags: flagset.ExtractPrefix("pages", hostConfig),
|
|
|
|
Action: func(ctx *cli.Context) error {
|
|
|
|
log := zaputil.Extract(ctx.Context)
|
|
|
|
|
2022-06-09 20:00:23 +00:00
|
|
|
// additional mime-types that need to be explicitly registered
|
2022-06-09 18:45:37 +00:00
|
|
|
_ = mime.AddExtensionType(".woff2", "application/font-woff2")
|
|
|
|
_ = mime.AddExtensionType(".woff", "application/font-woff")
|
|
|
|
_ = mime.AddExtensionType(".ttf", "font/ttf")
|
2022-06-09 20:00:23 +00:00
|
|
|
_ = mime.AddExtensionType(".yaml", "application/yaml")
|
|
|
|
_ = mime.AddExtensionType(".yml", "application/yaml")
|
|
|
|
_ = mime.AddExtensionType(".json", "application/json")
|
2022-06-09 18:45:37 +00:00
|
|
|
|
2022-10-25 15:19:25 +00:00
|
|
|
server, err := internal.NewServer(ctx.Context, hostConfig.ServerConfig)
|
2022-06-30 03:46:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-25 15:19:25 +00:00
|
|
|
endpointConfig := git.EndpointConfig{
|
|
|
|
Sites: make(map[string]*git.Config),
|
2022-06-03 19:39:22 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 15:19:25 +00:00
|
|
|
if hostConfig.SiteFile == "" {
|
|
|
|
endpointConfig.Sites["*"] = &hostConfig.Git
|
|
|
|
} else {
|
|
|
|
err = config.Load(ctx.Context, &endpointConfig, hostConfig.SiteFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint, err := git.NewEndpoint(ctx.Context, endpointConfig)
|
2022-06-03 19:39:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-25 15:19:25 +00:00
|
|
|
defer endpoint.Close()
|
2022-06-03 19:39:22 +00:00
|
|
|
|
2022-10-25 15:19:25 +00:00
|
|
|
{ // git endpoints
|
|
|
|
server.AdminMux.HandleFunc("/sync", endpoint.Sync).Methods(http.MethodPost)
|
|
|
|
server.PublicMux.PathPrefix("/").HandlerFunc(endpoint.Lookup).Methods(http.MethodGet)
|
2022-06-03 19:39:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("serving",
|
2022-06-09 15:39:44 +00:00
|
|
|
zap.String("public", hostConfig.Public.Address),
|
|
|
|
zap.String("private", hostConfig.Private.Address))
|
2022-06-03 19:39:22 +00:00
|
|
|
|
|
|
|
group, c := errgroup.WithContext(ctx.Context)
|
|
|
|
group.Go(server.ListenAndServe)
|
2022-10-06 17:04:39 +00:00
|
|
|
group.Go(func() error {
|
2022-10-25 15:19:25 +00:00
|
|
|
return endpoint.SyncLoop(ctx.Context)
|
2022-10-06 17:04:39 +00:00
|
|
|
})
|
2022-06-03 19:39:22 +00:00
|
|
|
|
|
|
|
<-c.Done()
|
|
|
|
|
2022-10-25 15:19:25 +00:00
|
|
|
shutdownTimeout := 30 * time.Second
|
|
|
|
timeout, cancelTimeout := context.WithTimeout(context.Background(), shutdownTimeout)
|
|
|
|
defer cancelTimeout()
|
|
|
|
|
|
|
|
_ = server.Shutdown(timeout)
|
2022-06-03 19:39:22 +00:00
|
|
|
_ = group.Wait()
|
|
|
|
|
|
|
|
err = c.Err()
|
|
|
|
if err != context.Canceled {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
HideHelpCommand: true,
|
|
|
|
}
|
|
|
|
)
|