add webhook mode
This commit is contained in:
parent
50a59ae109
commit
a82b00b191
3 changed files with 290 additions and 1 deletions
100
cmd/listen.go
Normal file
100
cmd/listen.go
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"git.ronmi.tw/ronmi/forgejo-pages/lib"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// listenCmd represents the listen command
|
||||
var listenCmd = &cobra.Command{
|
||||
Use: "listen",
|
||||
Short: "Start webhook listener",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
viper.BindPFlags(cmd.Flags())
|
||||
// check flags
|
||||
bind := viper.GetString("bind")
|
||||
server := viper.GetString("server")
|
||||
user := viper.GetString("user")
|
||||
token := viper.GetString("token")
|
||||
dir := viper.GetString("dir")
|
||||
branch := viper.GetString("branch")
|
||||
if bind == "" || server == "" || user == "" || token == "" || branch == "" || dir == "" {
|
||||
fmt.Println("bind, server, user, token, branch and dir are required")
|
||||
fmt.Println("dumping flags:")
|
||||
fmt.Println(" bind: ", bind)
|
||||
fmt.Println(" server: ", server)
|
||||
fmt.Println(" user: ", user)
|
||||
fmt.Println(" token: ", token)
|
||||
fmt.Println(" branch: ", branch)
|
||||
fmt.Println(" dir: ", dir)
|
||||
return
|
||||
}
|
||||
serverUrl, err := url.Parse(server)
|
||||
if err != nil {
|
||||
fmt.Println("invalid server url: ", err)
|
||||
return
|
||||
}
|
||||
|
||||
cfg := &lib.WebhookCFG{
|
||||
Forgejo: lib.Forgejo{
|
||||
Server: *serverUrl,
|
||||
Token: token,
|
||||
Branch: branch,
|
||||
},
|
||||
GitDir: filepath.Join(dir, "git"),
|
||||
PageDir: filepath.Join(dir, "pages"),
|
||||
GitUser: user,
|
||||
GitPass: token,
|
||||
}
|
||||
|
||||
s, err := lib.UseWebhook(bind, cfg)
|
||||
if err != nil {
|
||||
fmt.Println("cannot create server: ", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx, stop := signal.NotifyContext(
|
||||
context.TODO(),
|
||||
os.Interrupt,
|
||||
syscall.SIGTERM,
|
||||
os.Kill,
|
||||
)
|
||||
defer stop()
|
||||
|
||||
fmt.Println("starting server")
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
stop()
|
||||
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
|
||||
defer cancel()
|
||||
s.Shutdown(ctx)
|
||||
}()
|
||||
s.ListenAndServe()
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(listenCmd)
|
||||
|
||||
f := listenCmd.Flags()
|
||||
f.StringP("bind", "a", ":8080", "bind address")
|
||||
f.StringP("server", "s", "", "Forgejo server address")
|
||||
f.StringP("user", "u", "", "Forgejo user")
|
||||
f.StringP("token", "k", "", "Forgejo api token or password")
|
||||
f.StringP("branch", "b", "static-pages", "branch to use")
|
||||
f.StringP("dir", "d", "", "directory to store data, must be writable")
|
||||
}
|
||||
|
|
@ -23,6 +23,7 @@ var serveCmd = &cobra.Command{
|
|||
Use: "serve",
|
||||
Short: "Start the static page server.",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
viper.BindPFlags(cmd.Flags())
|
||||
// check flags
|
||||
bind := viper.GetString("bind")
|
||||
server := viper.GetString("server")
|
||||
|
|
@ -83,5 +84,4 @@ func init() {
|
|||
f.StringP("token", "k", "", "Forgejo api token")
|
||||
f.StringP("branch", "b", "static-pages", "branch to use")
|
||||
f.StringP("well-known", "w", "/.well-known", "well-known path, used by LetsEncrypt")
|
||||
viper.BindPFlags(f)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue