177 lines
4.0 KiB
Go
177 lines
4.0 KiB
Go
package main
|
|
|
|
import (
|
|
"embed"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
|
|
"go.wit.com/dev/alexflint/arg"
|
|
"go.wit.com/gui"
|
|
"go.wit.com/lib/protobuf/forgepb"
|
|
"go.wit.com/log"
|
|
)
|
|
|
|
// are sent via -ldflags at buildtime
|
|
var VERSION string
|
|
var BUILDTIME string
|
|
var ARGNAME string = "forged"
|
|
|
|
//go:embed resources/*
|
|
var resources embed.FS
|
|
|
|
var HOSTNAME string = "forge.wit.com"
|
|
var LIBDIR string = "/var/lib/forged/"
|
|
var FORGEDIR string = "/home/forge"
|
|
|
|
func main() {
|
|
me = new(mainType)
|
|
gui.InitArg()
|
|
me.pp = arg.MustParse(&argv)
|
|
|
|
if argv.Bash {
|
|
argv.doBash()
|
|
os.Exit(0)
|
|
}
|
|
if len(argv.BashAuto) != 0 {
|
|
argv.doBashAuto()
|
|
os.Exit(0)
|
|
}
|
|
|
|
if argv.Hostname != "" {
|
|
HOSTNAME = argv.Hostname
|
|
}
|
|
|
|
// the default forged dir is /home/forge
|
|
if os.Getenv("FORGE_GOSRC") == "" {
|
|
os.Setenv("FORGE_GOSRC", "/home/forge")
|
|
}
|
|
|
|
if os.Getenv("FORGE_PATCHDIR") == "" {
|
|
os.Setenv("FORGE_PATCHDIR", "/var/lib/forged")
|
|
}
|
|
|
|
me.forge = forgepb.RawInitPB()
|
|
|
|
if err := me.forge.InitPatchsets(); err != nil {
|
|
log.Info("patches failed to open", err)
|
|
}
|
|
|
|
if argv.List != nil {
|
|
doList()
|
|
okExit("")
|
|
}
|
|
|
|
if argv.Merge != nil {
|
|
if err := doMerge(); err != nil {
|
|
badExit(err)
|
|
}
|
|
okExit("")
|
|
}
|
|
|
|
if argv.Pull != nil {
|
|
log.Info("pull here")
|
|
okExit("")
|
|
}
|
|
|
|
if argv.Daemon == true {
|
|
mux := http.NewServeMux()
|
|
okHandlerFunc := http.HandlerFunc(okHandler)
|
|
|
|
// Set a limit of 50 kilobytes for requests to this handler.
|
|
// Adjust this value to your needs.
|
|
const maxUploadSize = 1025 * 1024 // 1 MB
|
|
mux.Handle("/", http.MaxBytesHandler(okHandlerFunc, maxUploadSize))
|
|
|
|
p := fmt.Sprintf(":%d", argv.Port)
|
|
log.Printf("Server starting on port %s...\n", p)
|
|
log.Printf("Test with: curl -d 'hello world' http://localhost:%s/\n", p)
|
|
|
|
server := &http.Server{
|
|
Addr: p,
|
|
Handler: mux,
|
|
ReadTimeout: 5 * time.Minute,
|
|
WriteTimeout: 10 * time.Second,
|
|
IdleTimeout: 120 * time.Second,
|
|
}
|
|
|
|
log.Printf("Server starting on port %s with a 1 MB request body limit...\n", p)
|
|
if err := server.ListenAndServe(); err != nil {
|
|
log.Fatal("Could not start server: %s\n", err)
|
|
}
|
|
okExit("")
|
|
}
|
|
|
|
/*
|
|
// --- Best Practice: Create a custom http.Server ---
|
|
server := &http.Server{
|
|
Addr: p,
|
|
Handler: mux,
|
|
|
|
// ReadTimeout is the total time to read the entire request, including the body.
|
|
// Increase this to a value that can accommodate your largest expected uploads.
|
|
// For example, 5 minutes.
|
|
ReadTimeout: 5 * time.Minute,
|
|
|
|
// WriteTimeout is the maximum duration before timing out writes of the response.
|
|
WriteTimeout: 10 * time.Second,
|
|
|
|
// IdleTimeout is the maximum amount of time to wait for the
|
|
// next request when keep-alives are enabled.
|
|
IdleTimeout: 120 * time.Second,
|
|
}
|
|
*/
|
|
|
|
/*
|
|
log.Println(argv.Version(), "HOSTNAME set to:", HOSTNAME)
|
|
log.Println("Running on port", "http://localhost"+p)
|
|
log.Println("Running on port", "http://localhost"+p+"/ipv6.png")
|
|
// if err := http.ListenAndServe(p, nil); err != nil {
|
|
if err := server.ListenAndServe(); err != nil {
|
|
log.Fatalf("Could not start server: %s\n", err)
|
|
}
|
|
/*
|
|
log.Info("Running in --daemon mode")
|
|
http.HandleFunc("/", okHandler)
|
|
// go https() // use caddy instead
|
|
p := fmt.Sprintf(":%d", argv.Port)
|
|
log.Println(argv.Version(), "HOSTNAME set to:", HOSTNAME)
|
|
log.Println("Running on port", "http://localhost"+p)
|
|
log.Println("Running on port", "http://localhost"+p+"/ipv6.png")
|
|
err := http.ListenAndServe(p, nil)
|
|
if err != nil {
|
|
log.Println("Error starting server:", err)
|
|
}
|
|
return
|
|
}
|
|
*/
|
|
log.Info("--daemon was not set. Just list the patches.")
|
|
// doList()
|
|
}
|
|
|
|
func formatDuration(d time.Duration) string {
|
|
seconds := int(d.Seconds()) % 60
|
|
minutes := int(d.Minutes()) % 60
|
|
hours := int(d.Hours()) % 24
|
|
days := int(d.Hours()) / 24
|
|
|
|
result := ""
|
|
if days > 0 {
|
|
result += fmt.Sprintf("%dd ", days)
|
|
return result
|
|
}
|
|
if hours > 0 {
|
|
result += fmt.Sprintf("%dh ", hours)
|
|
return result
|
|
}
|
|
if minutes > 0 {
|
|
result += fmt.Sprintf("%dm ", minutes)
|
|
return result
|
|
}
|
|
if seconds > 0 {
|
|
result += fmt.Sprintf("%ds", seconds)
|
|
}
|
|
return result
|
|
}
|