summaryrefslogtreecommitdiff
path: root/main.go
blob: dc59f3e03a061836c5b82b629d65a4a995e97682 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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/" // need to deprecate this
// var FORGEDIR string = "/home/forge" // deprecated?

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
}