init
This commit is contained in:
commit
b732e9d7d4
10 changed files with 783 additions and 0 deletions
70
cmd/root.go
Normal file
70
cmd/root.go
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
/*
|
||||
Copyright © 2025 Ronmi Ren <ronmi.ren@gmail.com
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var cfgFile string
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "forgejo-pages",
|
||||
Short: "static page server for Forgejo",
|
||||
// Uncomment the following line if your bare application
|
||||
// has an action associated with it:
|
||||
// Run: func(cmd *cobra.Command, args []string) { },
|
||||
}
|
||||
|
||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||
func Execute() {
|
||||
err := rootCmd.Execute()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
cobra.OnInitialize(initConfig)
|
||||
|
||||
// Here you will define your flags and configuration settings.
|
||||
// Cobra supports persistent flags, which, if defined here,
|
||||
// will be global for your application.
|
||||
|
||||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.forgejo-pages.yaml)")
|
||||
|
||||
// Cobra also supports local flags, which will only run
|
||||
// when this action is called directly.
|
||||
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
}
|
||||
|
||||
// initConfig reads in config file and ENV variables if set.
|
||||
func initConfig() {
|
||||
if cfgFile != "" {
|
||||
// Use config file from the flag.
|
||||
viper.SetConfigFile(cfgFile)
|
||||
} else {
|
||||
// Find home directory.
|
||||
home, err := os.UserHomeDir()
|
||||
cobra.CheckErr(err)
|
||||
|
||||
// Search config in home directory with name ".forgejo-pages" (without extension).
|
||||
viper.AddConfigPath(home)
|
||||
viper.SetConfigType("yaml")
|
||||
viper.SetConfigName(".forgejo-pages")
|
||||
}
|
||||
|
||||
viper.AutomaticEnv() // read in environment variables that match
|
||||
|
||||
// If a config file is found, read it in.
|
||||
if err := viper.ReadInConfig(); err == nil {
|
||||
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
|
||||
}
|
||||
}
|
||||
78
cmd/serve.go
Normal file
78
cmd/serve.go
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
/*
|
||||
Copyright © 2025 Ronmi Ren <ronmi.ren@gmail.com
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"git.ronmi.tw/ronmi/forgejo-pages/lib"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// serveCmd represents the serve command
|
||||
var serveCmd = &cobra.Command{
|
||||
Use: "serve",
|
||||
Short: "Start the static page server.",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// check flags
|
||||
bind, _ := cmd.Flags().GetString("bind")
|
||||
server, _ := cmd.Flags().GetString("server")
|
||||
token, _ := cmd.Flags().GetString("token")
|
||||
branch, _ := cmd.Flags().GetString("branch")
|
||||
if bind == "" || server == "" || token == "" || branch == "" {
|
||||
fmt.Println("bind, server token and branch are required")
|
||||
return
|
||||
}
|
||||
serverUrl, err := url.Parse(server)
|
||||
if err != nil {
|
||||
fmt.Println("invalid server url: ", err)
|
||||
return
|
||||
}
|
||||
f := &lib.Forgejo{
|
||||
Server: *serverUrl,
|
||||
Token: token,
|
||||
Branch: branch,
|
||||
}
|
||||
|
||||
s, err := lib.Run(bind, f)
|
||||
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(serveCmd)
|
||||
|
||||
f := serveCmd.Flags()
|
||||
f.StringP("bind", "a", ":8080", "bind address")
|
||||
f.StringP("server", "s", "", "Forgejo server address")
|
||||
f.StringP("token", "k", "", "Forgejo api token")
|
||||
f.StringP("branch", "b", "static-pages", "branch to use")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue