summaryrefslogtreecommitdiff
path: root/main.go
blob: 1cd490a231a612feb0ef0ffaa5b39459e23f60c1 (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
package main

import (
	"embed"
	"errors"
	"fmt"
	"net/http"
	"os"
	"path/filepath"
	"time"

	"go.wit.com/lib/env"
	"go.wit.com/lib/protobuf/argvpb"
	"go.wit.com/lib/protobuf/forgepb"
	"go.wit.com/lib/protobuf/gitpb"
	"go.wit.com/log"
)

//go:embed resources/*
var resources embed.FS

var HOSTNAME string = "forge.wit.com"

func saveMissing() error {
	err := me.missing.SaveValidate(filepath.Join(env.Get("ReposDir"), "missing.pb"))
	if err != nil {
		log.Info("failed to save missing.pb", err)
	}
	return err
}

func main() {
	me = new(mainType)
	me.argv = argvpb.Autocomplete(&argv) // adds shell auto complete to go-args

	var err error
	me.forge, err = forgepb.InitByFullpath("/etc/forged/forge.text")
	if err != nil {
		panic("forge.InitByFullPath() failed")
	}
	env.PrintTable()

	if err := me.forge.InitPatchsets(); err != nil {
		log.Info("patches failed to open", err)
		badExit(err)
	}

	if argv.Patch != nil {
		if err := doPatches(); err != nil {
			badExit(err)
		}
		okExit("")
	}

	me.missing = gitpb.NewRepos()
	err = me.missing.ConfigLoad(filepath.Join(env.Get("ReposDir"), "missing.pb"))
	if errors.Is(err, os.ErrNotExist) {
		saveMissing()
	} else if err != nil {
		log.Info("loading missing.pb failed", err)
		badExit(err)
	}

	if argv.Missing != nil {
		log.Info("loading missing.pb worked len =", me.missing.Len())
		me.missing.PrintMissingTable()
		okExit("")
	}

	if argv.Repos != nil {
		if err := doRepos(); err != nil {
			badExit(err)
		}
		okExit("")
	}

	if argv.Daemon == true {
		if argv.Gui != nil {
			me.myGui.Start() // loads the GUI toolkit
			doGui()          // start making our forge GUI
			debug()          // sits here forever
		}
		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("")
	}

	log.Info("--daemon was not set. Just list the patches.")
	// doList()
}