Compare commits
63 Commits
Author | SHA1 | Date |
---|---|---|
|
460338c4eb | |
|
cd545c7c9f | |
|
b09b86009e | |
|
1f22b771c3 | |
|
ada923ea05 | |
|
2b6107fa51 | |
|
7a76b5acd1 | |
|
6df342bc10 | |
|
209ee29773 | |
|
43d653a8c0 | |
|
6722b019ec | |
|
82b232f47a | |
|
979fd2f978 | |
|
24e137c7ec | |
|
63be841469 | |
|
4827be1d2a | |
|
f1c1ca950c | |
|
7ea7393d6c | |
|
b9ec316d35 | |
|
c1de4f1f5a | |
|
34d9943e73 | |
|
be62079b78 | |
|
4c5ded025f | |
|
192fd065dd | |
|
3a1ba210ba | |
|
f23109dce8 | |
|
e1411dd1e9 | |
|
607886cc6a | |
|
5e71dd3ecc | |
|
873cb39932 | |
|
a099bbd3e6 | |
|
c2e0e8e80b | |
|
eae2c3be2b | |
|
4e5a34b772 | |
|
f8b2ff5ace | |
|
0d1f53afee | |
|
d795dbeb61 | |
|
b2af891b20 | |
|
1442d4f6c5 | |
|
77eed08e7a | |
|
3db711a6c0 | |
|
a94208a768 | |
|
9025bf436a | |
|
f171c458aa | |
|
b55add4b47 | |
|
22f823bebe | |
|
250fa7de4a | |
|
d42b2e8666 | |
|
906d50a771 | |
|
86ac85dd5b | |
|
684c5b1dad | |
|
7a547a203d | |
|
c193af11e7 | |
|
6b6b31eef6 | |
|
81885f4483 | |
|
9d6bc7fa41 | |
|
2be94a377a | |
|
a655c5a47e | |
|
1301c4476e | |
|
0d9fe58e57 | |
|
3d3a07c194 | |
|
8a788bef6d | |
|
3588440aed |
33
Makefile
33
Makefile
|
@ -1,27 +1,22 @@
|
|||
.PHONY: build
|
||||
|
||||
VERSION = $(shell git describe --tags)
|
||||
BUILDTIME = $(shell date +%Y.%m.%d)
|
||||
# BUILDTIME = $(shell date +%Y.%m.%d_%H%M)
|
||||
BUILDTIME = $(shell date +%s)
|
||||
|
||||
all: build
|
||||
@echo "make build # build"
|
||||
@echo "make clean # clean build files"
|
||||
@echo "make run # build and run on port 2233"
|
||||
@echo "make debian # will build a debian package"
|
||||
@echo ""
|
||||
@echo "make restart # restart the daemon"
|
||||
@echo "make enable # enable daemon on boot"
|
||||
@echo "make log # watch the daemon log"
|
||||
FORGE_VERBOSE=true ./forged clean
|
||||
# FORGE_VERBOSE=true ./forged list
|
||||
|
||||
build: goimports
|
||||
GO111MODULE=off go build \
|
||||
-ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}"
|
||||
|
||||
verbose:
|
||||
build-verbose: goimports
|
||||
GO111MODULE=off go build -v -x \
|
||||
-ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}"
|
||||
|
||||
install:
|
||||
install: goimports
|
||||
GO111MODULE=off go install \
|
||||
-ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}"
|
||||
|
||||
|
@ -40,15 +35,22 @@ start:
|
|||
systemctl start forged.service
|
||||
|
||||
stop:
|
||||
systemctl stop forged.service
|
||||
-systemctl stop forged.service
|
||||
|
||||
run: build
|
||||
./forged --port 2233 --hostname forge.wit.com
|
||||
run: build stop
|
||||
./forged --daemon
|
||||
# setcap 'cap_net_bind_service=+ep' forged # allow the binary to open ports below 1024
|
||||
#
|
||||
run-clean: build
|
||||
-rm /var/lib/forged/all-patches.pb
|
||||
./forged --daemon
|
||||
# setcap 'cap_net_bind_service=+ep' forged # allow the binary to open ports below 1024
|
||||
|
||||
prod: build
|
||||
make stop
|
||||
cp forged /usr/bin/forged
|
||||
cp -a forged.service /lib/systemd/system/forged.service
|
||||
systemctl daemon-reload
|
||||
make start
|
||||
journalctl -n 10 -xeu forged.service
|
||||
|
||||
|
@ -71,3 +73,6 @@ squash-the-last-3-commits-together:
|
|||
git rebase -i HEAD~3
|
||||
git fsck
|
||||
git prune
|
||||
|
||||
list:
|
||||
./forged list
|
||||
|
|
73
argv.go
73
argv.go
|
@ -6,20 +6,81 @@ package main
|
|||
*/
|
||||
|
||||
import (
|
||||
"go.wit.com/dev/alexflint/arg"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.wit.com/lib/gui/prep"
|
||||
"go.wit.com/lib/gui/shell"
|
||||
)
|
||||
|
||||
var argv args
|
||||
|
||||
type args struct {
|
||||
Port int `arg:"--port" default:"2520" help:"port to run on"`
|
||||
Hostname string `arg:"--hostname" default:"forge.wit.com" help:"hostname to use"`
|
||||
Pull *EmptyCmd `arg:"subcommand:pull" help:"'git pull' on the repos"`
|
||||
List *EmptyCmd `arg:"subcommand:list" help:"list the repos"`
|
||||
Clean *EmptyCmd `arg:"subcommand:clean" help:"clean the repos"`
|
||||
Gui *EmptyCmd `arg:"subcommand:gui" help:"show gui"`
|
||||
Merge *EmptyCmd `arg:"subcommand:merge" help:"merge in new patchsets"`
|
||||
Init *EmptyCmd `arg:"subcommand:init" help:"init the repo list"`
|
||||
Repos *EmptyCmd `arg:"subcommand:repos" help:"show the repos"`
|
||||
Port int `arg:"--port" default:"2520" help:"port to run on"`
|
||||
Hostname string `arg:"--hostname" help:"hostname to use"`
|
||||
Daemon bool `arg:"--daemon" help:"run as a daemon"`
|
||||
Force bool `arg:"--force" help:"try to strong arm things"`
|
||||
Verbose bool `arg:"--verbose" help:"show more output"`
|
||||
}
|
||||
|
||||
type EmptyCmd struct {
|
||||
}
|
||||
|
||||
func (args) Appname() string {
|
||||
return ARGNAME
|
||||
}
|
||||
|
||||
func (args) Version() string {
|
||||
return "forged " + VERSION + " Built on " + BUILDTIME
|
||||
parts := strings.Split(BUILDTIME, ".")
|
||||
if len(parts) == 1 {
|
||||
// The input epoch seconds
|
||||
// epochSeconds := int64(1758646486)
|
||||
num, err := strconv.Atoi(BUILDTIME)
|
||||
epochSeconds := int64(num)
|
||||
if err == nil {
|
||||
|
||||
// 1. Convert the epoch seconds to a time.Time object.
|
||||
// time.Unix() creates the time in the UTC timezone by default.
|
||||
t := time.Unix(epochSeconds, 0)
|
||||
|
||||
// 2. Convert the UTC time to the computer's local timezone.
|
||||
localTime := t.Local()
|
||||
|
||||
// 3. Print the result. The default format is clear and includes the timezone.
|
||||
// fmt.Println("Default format:", localTime)
|
||||
// For a more human-friendly format, use the Format() method.
|
||||
// Go uses a special reference time for formatting: Mon Jan 2 15:04:05 2006 MST
|
||||
// You lay out your desired format using these specific numbers.
|
||||
// formattedString := localTime.Format("Monday, January 2, 2006 at 3:04:05 PM (MST)")
|
||||
// fmt.Println(" Custom format:", formattedString)
|
||||
|
||||
// now := time.Now()
|
||||
// dur := time.Since(localTime)
|
||||
BUILDTIME = fmt.Sprintf("%s age(%v)", localTime.String(), shell.FormatDuration(time.Since(localTime)))
|
||||
}
|
||||
}
|
||||
|
||||
return ARGNAME + " " + VERSION + " Built on " + BUILDTIME
|
||||
}
|
||||
|
||||
func init() {
|
||||
arg.MustParse(&argv)
|
||||
func (a args) DoAutoComplete(pb *prep.Auto) {
|
||||
switch pb.Cmd {
|
||||
case "list":
|
||||
pb.Autocomplete2("--missing")
|
||||
case "clean":
|
||||
pb.Autocomplete2("")
|
||||
default:
|
||||
pb.Autocomplete2("list clean")
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
package main
|
||||
|
||||
// functions to import and export the protobuf
|
||||
// data to and from config files
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"go.wit.com/lib/config"
|
||||
"go.wit.com/lib/protobuf/forgepb"
|
||||
"go.wit.com/log"
|
||||
)
|
||||
|
||||
func configSave() error {
|
||||
return me.configs.ConfigSave()
|
||||
}
|
||||
|
||||
func configInit() *forgepb.ForgeConfigs {
|
||||
if argv.Hostname != "" {
|
||||
HOSTNAME = argv.Hostname
|
||||
}
|
||||
|
||||
// the default forged dir is /home/forge
|
||||
if os.Getenv("FORGE_REPODIR") == "" {
|
||||
os.Setenv("FORGE_REPODIR", "/home/forge")
|
||||
}
|
||||
|
||||
if os.Getenv("FORGE_PATCHDIR") == "" {
|
||||
os.Setenv("FORGE_PATCHDIR", "/var/lib/forged")
|
||||
}
|
||||
|
||||
configs := new(forgepb.ForgeConfigs)
|
||||
err := config.ConfigLoad(configs, ARGNAME, "forge")
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
configs.ReposDir = os.Getenv("FORGE_REPODIR")
|
||||
configs.ReposPB = filepath.Join(configs.ReposDir, "repos.pb")
|
||||
configs.PatchDir = os.Getenv("FORGE_PATCHDIR")
|
||||
if err := configSave(); err != nil {
|
||||
badExit(err)
|
||||
}
|
||||
log.Info("made a default config file here", configs.Filename)
|
||||
okExit("")
|
||||
}
|
||||
if err != nil {
|
||||
badExit(err)
|
||||
}
|
||||
return configs
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
|
||||
// Use of this source code is governed by the GPL 3.0
|
||||
|
||||
package main
|
||||
|
||||
// An app to submit patches for the 30 GO GUI repos
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"go.wit.com/gui"
|
||||
"go.wit.com/lib/gadgets"
|
||||
"go.wit.com/lib/gui/shell"
|
||||
"go.wit.com/log"
|
||||
)
|
||||
|
||||
func debug() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
gui.Crash(r, "forge debug()")
|
||||
}
|
||||
}()
|
||||
time.Sleep(2 * time.Second)
|
||||
for {
|
||||
now := time.Now()
|
||||
|
||||
doList()
|
||||
|
||||
log.Printf("finished a scan here in (%s)\n", shell.FormatDuration(time.Since(now)))
|
||||
time.Sleep(90 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func doGui() {
|
||||
mainWindow := gadgets.NewGenericWindow("forged: forge.wit.com", "Current Settings")
|
||||
mainWindow.Custom = func() {
|
||||
log.Warn("MAIN WINDOW CLOSE")
|
||||
now := time.Now()
|
||||
log.Printf("rill repos.Reload() took (%s)\n", shell.FormatDuration(time.Since(now)))
|
||||
okExit("")
|
||||
}
|
||||
drawWindow(mainWindow)
|
||||
}
|
||||
|
||||
func drawWindow(win *gadgets.GenericWindow) {
|
||||
grid := win.Group.RawGrid()
|
||||
|
||||
grid.NewButton("stats", func() {
|
||||
doList()
|
||||
})
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"go.wit.com/lib/protobuf/forgepb"
|
||||
"go.wit.com/log"
|
||||
)
|
||||
|
||||
func doList() error {
|
||||
log.Infof("do list here. Patchsets.Len()=%d\n", me.forge.Patchsets.Len())
|
||||
for pset := range me.forge.Patchsets.IterAll() {
|
||||
pset.PrintTable()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func doClean() error {
|
||||
log.Infof("clean Patchsets.Len()=%d\n", me.forge.Patchsets.Len())
|
||||
// show all the patchsets with Names
|
||||
for pset := range me.forge.Patchsets.IterAll() {
|
||||
for patch := range pset.Patches.IterAll() {
|
||||
if patch.PatchId == "" {
|
||||
log.Info("Delete", patch.CommitHash, patch.PatchId, patch.Namespace)
|
||||
pset.Patches.Delete(patch)
|
||||
} else {
|
||||
log.Info("\t", patch.CommitHash, patch.PatchId, patch.Namespace)
|
||||
}
|
||||
}
|
||||
}
|
||||
me.forge.SavePatchsets()
|
||||
return nil
|
||||
}
|
||||
|
||||
// returns true if the patch already exists in the protobuf
|
||||
func findPatch(newpatch *forgepb.Patch) bool {
|
||||
// log.Info("\tlook for patch:", newpatch.CommitHash, newpatch.Namespace)
|
||||
|
||||
for pset := range me.forge.Patchsets.IterAll() {
|
||||
for _, patch := range pset.Patches.Patches {
|
||||
if patch.CommitHash == newpatch.CommitHash {
|
||||
// log.Info("\tfound pset!!!!!!", pset.Uuid, patch.Namespace)
|
||||
return true
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// returns true if the patch already exists in the protobuf
|
||||
func expirePatch(newpatch *forgepb.Patch) bool {
|
||||
// log.Info("\tlook for patch:", newpatch.CommitHash, newpatch.Namespace)
|
||||
|
||||
for pset := range me.forge.Patchsets.IterAll() {
|
||||
for _, patch := range pset.Patches.Patches {
|
||||
if patch.CommitHash == newpatch.CommitHash {
|
||||
patch.NewHash = newpatch.NewHash
|
||||
// log.Info("\tfound pset!!!!!!", pset.Uuid, patch.Namespace)
|
||||
return true
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
|
@ -0,0 +1,135 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"go.wit.com/lib/protobuf/forgepb"
|
||||
"go.wit.com/log"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
func doMerge() error {
|
||||
mergePatchsets()
|
||||
|
||||
if err := me.forge.SavePatchsets(); err != nil {
|
||||
log.Warn("savePatchsets() failed", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func findAutoPatchset() *forgepb.Set {
|
||||
for pset := range me.forge.Patchsets.IterAll() {
|
||||
if pset.Name == "forge auto commit" {
|
||||
return pset
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
var fauto *forgepb.Set
|
||||
log.Warn("findAutoPatchset() had to create 'forge auto commit'")
|
||||
if fauto == nil {
|
||||
fauto = new(forgepb.Set)
|
||||
fauto.Name = "forge auto commit"
|
||||
fauto.Patches = forgepb.NewPatches()
|
||||
fauto.Uuid = uuid.New().String()
|
||||
me.forge.Patchsets.Append(fauto)
|
||||
}
|
||||
return fauto
|
||||
}
|
||||
|
||||
// adds submitted patches not specifically assigned to a patchset
|
||||
// to the generic patchset called "forge auto commit"
|
||||
func addRandomPatch(patch *forgepb.Patch) error {
|
||||
// ignore patch if it's already here
|
||||
if findPatch(patch) {
|
||||
log.Info("already found patch", patch.CommitHash, patch.Namespace)
|
||||
return nil
|
||||
}
|
||||
fauto := findAutoPatchset()
|
||||
if fauto == nil {
|
||||
return log.Errorf("no default place yet")
|
||||
}
|
||||
newpb := proto.Clone(patch).(*forgepb.Patch)
|
||||
if newpb == nil {
|
||||
return log.Errorf("proto.Clone returned nil")
|
||||
}
|
||||
fauto.Patches.Patches = append(fauto.Patches.Patches, newpb)
|
||||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
// adds a patchset or just the patches
|
||||
func addPatchset(filename string, pb *forgepb.Set) {
|
||||
// if the name of the patchset is "forge auto commit"
|
||||
// then just add all the patches
|
||||
if pb.Name == "forge auto commit" {
|
||||
author := "Author: " + pb.GitAuthorName
|
||||
author += " <" + pb.GitAuthorEmail + ">"
|
||||
|
||||
// author := "Author: " + os.Getenv("GIT_AUTHOR_NAME")
|
||||
// author += " <" + os.Getenv("GIT_AUTHOR_EMAIL") + ">"
|
||||
fmt.Println(filename, pb.Name, pb.Comment, author)
|
||||
for _, patch := range pb.Patches.Patches {
|
||||
// log.Info("\tnew patch:", i, patch.CommitHash, patch.Namespace)
|
||||
if findPatch(patch) {
|
||||
// log.Info("\talready found!!!!!!!", pset.Uuid, patch.Namespace)
|
||||
} else {
|
||||
log.Info("\tnew patch:", filename, pb.Name, pb.Comment, author)
|
||||
addRandomPatch(patch)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// if you got here, this patchset was submitted with a name
|
||||
|
||||
// Has this patchset already been submitted?
|
||||
for pset := range me.forge.Patchsets.IterAll() {
|
||||
if pset.Uuid == pb.Uuid {
|
||||
log.Info("ALREADY ADDED", pset.Uuid, pset.Name)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Clone() this protobuf into me.forge.Patchsets
|
||||
var newpb *forgepb.Set
|
||||
newpb = proto.Clone(pb).(*forgepb.Set)
|
||||
if newpb != nil {
|
||||
me.forge.Patchsets.Patchsets = append(me.forge.Patchsets.Patchsets, newpb)
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
func mergePatchsets() {
|
||||
dirname := filepath.Join(LIBDIR, "patchset/")
|
||||
// Open the directory
|
||||
entries, err := os.ReadDir(dirname)
|
||||
if err != nil {
|
||||
fmt.Printf("Error reading directory: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Iterate through the directory entries
|
||||
for _, entry := range entries {
|
||||
// Check if the entry is a file and matches the *.pb pattern
|
||||
if !entry.IsDir() && filepath.Ext(entry.Name()) == ".pb" {
|
||||
bytes, err := os.ReadFile(filepath.Join(dirname, entry.Name()))
|
||||
if err != nil {
|
||||
fmt.Println(entry.Name(), err)
|
||||
continue
|
||||
}
|
||||
var p *forgepb.Set
|
||||
p = new(forgepb.Set)
|
||||
err = p.Unmarshal(bytes)
|
||||
if err != nil {
|
||||
fmt.Println(entry.Name(), err)
|
||||
continue
|
||||
}
|
||||
me.forge.AddPatchset(p)
|
||||
}
|
||||
}
|
||||
}
|
239
doPatchsets.go
239
doPatchsets.go
|
@ -1,239 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"go.wit.com/lib/gui/shell"
|
||||
"go.wit.com/lib/protobuf/forgepb"
|
||||
"go.wit.com/lib/protobuf/gitpb"
|
||||
"go.wit.com/log"
|
||||
)
|
||||
|
||||
func doSendPatchsets(w http.ResponseWriter) {
|
||||
// log.HttpMode(w)
|
||||
// defer log.HttpMode(nil)
|
||||
|
||||
dirname := filepath.Join(LIBDIR, "patchset/")
|
||||
// Open the directory
|
||||
entries, err := os.ReadDir(dirname)
|
||||
if err != nil {
|
||||
log.Printf("Error reading directory: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
var psets *forgepb.Patchsets
|
||||
psets = forgepb.NewPatchsets() // this sets the proper handshake protobuf UUID
|
||||
|
||||
// Iterate through the directory entries
|
||||
for _, entry := range entries {
|
||||
// Check if the entry is a file and matches the *.pb pattern
|
||||
if !entry.IsDir() && filepath.Ext(entry.Name()) == ".pb" {
|
||||
bytes, err := os.ReadFile(filepath.Join(dirname, entry.Name()))
|
||||
if err != nil {
|
||||
fmt.Fprintln(w, entry.Name(), err)
|
||||
fmt.Println(entry.Name(), err)
|
||||
continue
|
||||
}
|
||||
var p *forgepb.Patchset
|
||||
p = new(forgepb.Patchset)
|
||||
err = p.Unmarshal(bytes)
|
||||
if err != nil {
|
||||
fmt.Fprintln(w, entry.Name(), err)
|
||||
fmt.Println(entry.Name(), err)
|
||||
continue
|
||||
}
|
||||
|
||||
psets.Append(p)
|
||||
}
|
||||
}
|
||||
|
||||
// marshal the protobuf to xfer over the socket
|
||||
data, err := psets.Marshal()
|
||||
if err != nil {
|
||||
log.Info("patchsets.Marshal() to wire failed", err)
|
||||
return
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
log.Info("going to w.Write(data) with len", len(data))
|
||||
w.Write(data)
|
||||
age := shell.FormatDuration(time.Since(start))
|
||||
log.Printf("Done with xfer in (%s). happy hacking!\n", age)
|
||||
}
|
||||
|
||||
func getPatchset(w http.ResponseWriter, pbname string) {
|
||||
if pbname == "" {
|
||||
fmt.Fprintf(w, "filename was empty")
|
||||
return
|
||||
}
|
||||
msg := fmt.Sprintf("filename = %s\n", pbname)
|
||||
log.Info(msg)
|
||||
|
||||
filename := filepath.Join(LIBDIR, "patchset/", pbname)
|
||||
data, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
msg := fmt.Sprintf("Error reading file %s: %v\n", filename, err)
|
||||
fmt.Printf(msg)
|
||||
fmt.Fprintf(w, msg)
|
||||
return
|
||||
}
|
||||
var m *forgepb.Patchset
|
||||
m = new(forgepb.Patchset)
|
||||
if err := m.Unmarshal(data); err != nil {
|
||||
msg := fmt.Sprintf("proto.Unmarshal() failed on %s len=%d\n", filename, len(data))
|
||||
fmt.Printf(msg)
|
||||
fmt.Fprintf(w, msg)
|
||||
msg = fmt.Sprintf("proto.Unmarshal() error %v\n", err)
|
||||
fmt.Printf(msg)
|
||||
fmt.Fprintf(w, msg)
|
||||
return
|
||||
}
|
||||
log.Info("going to w.Write(data) with len", len(data))
|
||||
w.Write(data)
|
||||
}
|
||||
|
||||
func listPatchsets(w http.ResponseWriter) {
|
||||
dirname := filepath.Join(LIBDIR, "patchset/")
|
||||
// Open the directory
|
||||
entries, err := os.ReadDir(dirname)
|
||||
if err != nil {
|
||||
fmt.Printf("Error reading directory: %v\n", err)
|
||||
fmt.Fprintf(w, "Error reading directory: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Iterate through the directory entries
|
||||
for _, entry := range entries {
|
||||
// Check if the entry is a file and matches the *.pb pattern
|
||||
if !entry.IsDir() && filepath.Ext(entry.Name()) == ".pb" {
|
||||
bytes, err := os.ReadFile(filepath.Join(dirname, entry.Name()))
|
||||
if err != nil {
|
||||
fmt.Fprintln(w, entry.Name(), err)
|
||||
fmt.Println(entry.Name(), err)
|
||||
continue
|
||||
}
|
||||
var p *forgepb.Patchset
|
||||
p = new(forgepb.Patchset)
|
||||
err = p.Unmarshal(bytes)
|
||||
if err != nil {
|
||||
fmt.Fprintln(w, entry.Name(), err)
|
||||
fmt.Println(entry.Name(), err)
|
||||
continue
|
||||
}
|
||||
author := "Author: " + p.GitAuthorName
|
||||
author += " <" + p.GitAuthorEmail + ">"
|
||||
|
||||
// author := "Author: " + os.Getenv("GIT_AUTHOR_NAME")
|
||||
// author += " <" + os.Getenv("GIT_AUTHOR_EMAIL") + ">"
|
||||
fmt.Fprintln(w, entry.Name(), p.Name, p.Comment, author)
|
||||
fmt.Println(entry.Name(), p.Name, p.Comment, author)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func savePatchset(w http.ResponseWriter, msg []byte) {
|
||||
log.Info("proto.Unmarshal() try message len", len(msg))
|
||||
var m *forgepb.Patchset
|
||||
m = new(forgepb.Patchset)
|
||||
if err := m.Unmarshal(msg); err != nil {
|
||||
log.Info("proto.Unmarshal() failed on wire message len", len(msg))
|
||||
log.Info("error =", err)
|
||||
return
|
||||
}
|
||||
log.Info("GOT patchset:", len(msg))
|
||||
fmt.Fprintln(w, "GOT patchset:", len(msg))
|
||||
all := m.Patches.SortByFilename()
|
||||
for all.Scan() {
|
||||
repo := all.Next()
|
||||
log.Info("filename:", repo.Filename)
|
||||
fmt.Fprintln(w, "filename:", repo.Filename)
|
||||
}
|
||||
now := time.Now()
|
||||
// timestamp := now.Format("2022.07.18.190545") // 50yr shout out to K&R
|
||||
timestamp := now.Format("2006.01.02.150405") // bummer. other date doesn't work?
|
||||
filename := filepath.Join(LIBDIR, "patchset/", timestamp+".submitted.pb")
|
||||
regfile, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
|
||||
if err != nil {
|
||||
log.Info("filename open error:", filename, err)
|
||||
fmt.Fprintln(w, "filename open error:", filename, err)
|
||||
return
|
||||
}
|
||||
regfile.Write(msg)
|
||||
regfile.Close()
|
||||
}
|
||||
|
||||
func lookupRepos(msg []byte) (*gitpb.Repos, error) {
|
||||
log.Info("proto.Unmarshal() try message len", len(msg))
|
||||
m := gitpb.NewRepos()
|
||||
if err := m.Unmarshal(msg); err != nil {
|
||||
log.Info("gitpb.Repos.Unmarshal() failed. len(msg) =", len(msg), err)
|
||||
return m, err
|
||||
}
|
||||
newpb := gitpb.NewRepos()
|
||||
log.Info("GOT repos:", len(msg))
|
||||
for repo := range m.IterAll() {
|
||||
log.Infof("repo:%s,%s\n", repo.Namespace, repo.FullPath)
|
||||
if repo.Namespace == "" {
|
||||
log.Info("todo: namespace empty for", repo.FullPath)
|
||||
continue
|
||||
// return nil, fmt.Errorf("todo: namespace empty for " + repo.FullPath)
|
||||
}
|
||||
// fmt.Fprintln(w, "repo:", repo.FullPath, repo.Namespace)
|
||||
bytes, err := os.ReadFile(filepath.Join("/home/forge", repo.Namespace, "git.pb"))
|
||||
if err != nil {
|
||||
log.Info("todo: git.pb non-existant:", repo.FullPath)
|
||||
// return nil, err
|
||||
continue
|
||||
}
|
||||
newr := new(gitpb.Repo)
|
||||
if err := newr.Unmarshal(bytes); err != nil {
|
||||
log.Info("todo: unmarshal failed for git.pb:", repo.FullPath)
|
||||
// return nil, fmt.Errorf("todo: generate git.pb for " + repo.Namespace)
|
||||
continue
|
||||
}
|
||||
// log.Infof("repo unmarshal worked:%s,%s\n", repo.Namespace, repo.FullPath)
|
||||
newpb.Append(newr)
|
||||
}
|
||||
|
||||
// print out found git.pb files
|
||||
for repo := range newpb.IterAll() {
|
||||
log.Infof("found repo:%-14s,%s\n", repo.GetMasterVersion(), repo.Namespace)
|
||||
}
|
||||
return newpb, nil
|
||||
}
|
||||
/* save the git.pb files
|
||||
+ if repo.Namespace == "" {
|
||||
+ log.Infof("repo: ERROR. no namespace. %s\n", repo.FullPath)
|
||||
+ continue
|
||||
+ }
|
||||
+
|
||||
+ repopath := filepath.Join("/home/forge", repo.Namespace)
|
||||
+ if !shell.IsDir(repopath) {
|
||||
+ if err := os.MkdirAll(repopath, os.ModePerm); err != nil {
|
||||
+ log.Warn("mkdir fail", repo.Namespace, err)
|
||||
+ continue
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ filename := filepath.Join(repopath, "git.pb")
|
||||
+ if !shell.Exists(filename) {
|
||||
+ data, err := repo.Marshal()
|
||||
+ if err != nil {
|
||||
+ log.Info("patchsets.Marshal() to wire failed", err)
|
||||
+ continue
|
||||
+ }
|
||||
+ gitpbf, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
|
||||
+ defer gitpbf.Close()
|
||||
+ if err != nil {
|
||||
+ log.Info("filename open error:", filename, err)
|
||||
+ continue
|
||||
+ }
|
||||
+ gitpbf.Write(data)
|
||||
+ }
|
||||
+
|
||||
+ log.Infof("repo path: %s\n", repopath)
|
||||
*/
|
|
@ -0,0 +1,26 @@
|
|||
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
|
||||
// Use of this source code is governed by the GPL 3.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"go.wit.com/log"
|
||||
)
|
||||
|
||||
func exit(thing any) {
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
func okExit(thing string) {
|
||||
if thing != "" {
|
||||
log.Info("forge exit:", thing, "ok")
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
func badExit(err error) {
|
||||
log.Info("forged failed: ", err)
|
||||
os.Exit(-1)
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"go.wit.com/lib/protobuf/gitpb"
|
||||
"go.wit.com/log"
|
||||
)
|
||||
|
||||
func readGitPB(fullpath string) (*gitpb.Repo, error) {
|
||||
// fmt.Fprintln(w, "repo:", repo.FullPath, repo.Namespace)
|
||||
bytes, err := os.ReadFile(filepath.Join(fullpath, "git.pb"))
|
||||
if err != nil {
|
||||
log.Info("todo: git.pb non-existant:", fullpath)
|
||||
return nil, err
|
||||
}
|
||||
newr := new(gitpb.Repo)
|
||||
if err := newr.Unmarshal(bytes); err != nil {
|
||||
log.Info("todo: unmarshal failed for git.pb:", fullpath)
|
||||
return nil, fmt.Errorf("todo: generate git.pb for " + fullpath)
|
||||
}
|
||||
return newr, nil
|
||||
}
|
||||
|
||||
// doesn't enter the directory any further when it finds a .git/
|
||||
// not stupid like my old version
|
||||
func scanForgedDir(srcDir string) ([]string, error) {
|
||||
var all []string
|
||||
var trip bool
|
||||
err := filepath.WalkDir(srcDir, func(path string, d os.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
// Handle possible errors, like permission issues
|
||||
fmt.Fprintf(os.Stderr, "error accessing path %q: %v\n", path, err)
|
||||
return err
|
||||
}
|
||||
|
||||
if d.IsDir() {
|
||||
// log.Info("path is dir", path)
|
||||
} else {
|
||||
_, fname := filepath.Split(path)
|
||||
switch fname {
|
||||
case "repos.pb":
|
||||
case "git.pb":
|
||||
case "go.work.last":
|
||||
case "go.work.sum":
|
||||
default:
|
||||
// todo: figure out a way to do padding for init()
|
||||
if trip == false {
|
||||
log.Info("WARNING:")
|
||||
}
|
||||
log.Info("WARNING: you have an untracked file outside of any .git repository:", path)
|
||||
trip = true
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
gitdir := filepath.Join(path, "git.pb")
|
||||
_, err2 := os.Stat(gitdir)
|
||||
if !os.IsNotExist(err2) {
|
||||
all = append(all, path)
|
||||
return filepath.SkipDir
|
||||
}
|
||||
// todo: check if dir is empty here and delete dir?
|
||||
return nil
|
||||
})
|
||||
|
||||
// probably always leave this here forever
|
||||
// this check, along with CheckDirty() makes sure you can safely delete ~/go/src or the go.work directory
|
||||
// because everything is either checked in or deleted. An important thing to know!
|
||||
if trip {
|
||||
log.Info("WARNING: junk in", srcDir)
|
||||
}
|
||||
return all, err
|
||||
}
|
|
@ -4,7 +4,7 @@ Description=forged
|
|||
[Service]
|
||||
User=root
|
||||
Type=simple
|
||||
ExecStart=/usr/bin/forged
|
||||
ExecStart=/usr/bin/forged --daemon
|
||||
ExecStop=killall forged
|
||||
Restart=on-failure
|
||||
RestartSec=5
|
||||
|
|
|
@ -0,0 +1,66 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"go.wit.com/lib/protobuf/forgepb"
|
||||
"go.wit.com/lib/protobuf/httppb"
|
||||
"go.wit.com/log"
|
||||
)
|
||||
|
||||
func addNewPatches(pb *forgepb.Patches, reqPB *httppb.HttpRequest) *forgepb.Patches {
|
||||
newPatchesPB := new(forgepb.Patches)
|
||||
for newpatch := range pb.IterAll() {
|
||||
me.forge.AddNewPatch(newpatch)
|
||||
newPatchesPB.Append(newpatch)
|
||||
}
|
||||
reqPB.Logf("Recived %d patches from client", newPatchesPB.Len())
|
||||
return newPatchesPB
|
||||
}
|
||||
|
||||
func handleMergedPatches(pb *forgepb.Patches, reqPB *httppb.HttpRequest) *forgepb.Patches {
|
||||
newPatchesPB := new(forgepb.Patches)
|
||||
for newpatch := range pb.IterAll() {
|
||||
if expirePatch(newpatch) {
|
||||
newPatchesPB.Append(newpatch)
|
||||
}
|
||||
}
|
||||
return newPatchesPB
|
||||
}
|
||||
|
||||
func sendPendingPatches(pb *forgepb.Patches, reqPB *httppb.HttpRequest) *forgepb.Patches {
|
||||
allPatchesPB := new(forgepb.Patches)
|
||||
for pset := range me.forge.Patchsets.IterAll() {
|
||||
for newpatch := range pset.Patches.IterAll() {
|
||||
allPatchesPB.Append(newpatch)
|
||||
}
|
||||
}
|
||||
return allPatchesPB
|
||||
}
|
||||
|
||||
func sendPendingPatchsets(pb *forgepb.Sets, reqPB *httppb.HttpRequest) *forgepb.Sets {
|
||||
allPatchsetsPB := new(forgepb.Sets)
|
||||
for pset := range me.forge.Patchsets.IterAll() {
|
||||
allPatchsetsPB.Append(pset)
|
||||
}
|
||||
return allPatchsetsPB
|
||||
}
|
||||
|
||||
/*
|
||||
func makePatchesPB(reqPB *httppb.HttpRequest) (*forgepb.Patches, error) {
|
||||
pb := forgepb.NewPatches()
|
||||
err := pb.Unmarshal(reqPB.ServerData)
|
||||
return pb, err
|
||||
}
|
||||
*/
|
||||
|
||||
func makePatchsetsPB(reqPB *httppb.HttpRequest) (*forgepb.Sets, error) {
|
||||
pb := forgepb.NewSets()
|
||||
err := pb.Unmarshal(reqPB.ServerData)
|
||||
return pb, err
|
||||
}
|
||||
|
||||
func sendPatchesError(w http.ResponseWriter, r *forgepb.Patches, err error) error {
|
||||
log.Info("send error back to user", err)
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"go.wit.com/lib/protobuf/gitpb"
|
||||
"go.wit.com/lib/protobuf/httppb"
|
||||
"go.wit.com/log"
|
||||
)
|
||||
|
||||
func handleRepos(pb *gitpb.Repos, route string) error {
|
||||
log.Info("GOT PATCHES ROUTE", route, "with # patches =", pb.Len())
|
||||
if strings.HasPrefix(route, "/patches/old") {
|
||||
} else if strings.HasPrefix(route, "/patches/new") {
|
||||
log.Info("add new patches")
|
||||
} else {
|
||||
log.Info("unknown route", route)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// returns a repo PB with just the master and devel branch versions
|
||||
// is just this information so it keeps things small when it's sent over the wire
|
||||
func getCurrentRepoVersions(namespace string) *gitpb.Repo {
|
||||
newrepo := new(gitpb.Repo)
|
||||
newrepo.Namespace = namespace
|
||||
found := me.forge.Repos.FindByNamespace(namespace)
|
||||
if found == nil {
|
||||
// todo: clone repo here
|
||||
return newrepo
|
||||
}
|
||||
newrepo.MasterHash = found.MasterHash
|
||||
newrepo.DevelHash = found.DevelHash
|
||||
newrepo.URL = found.URL
|
||||
return newrepo
|
||||
}
|
||||
|
||||
func pullRequest(pb *gitpb.Repos, reqPB *httppb.HttpRequest) *gitpb.Repos {
|
||||
versionsPB := gitpb.NewRepos()
|
||||
for repo := range pb.IterAll() {
|
||||
found := getCurrentRepoVersions(repo.Namespace)
|
||||
versionsPB.Append(found)
|
||||
}
|
||||
return versionsPB
|
||||
}
|
||||
|
||||
func addRequest(pb *gitpb.Repos, reqPB *httppb.HttpRequest) *gitpb.Repos {
|
||||
newReposPB := gitpb.NewRepos()
|
||||
for repo := range pb.IterAll() {
|
||||
if found := me.forge.Repos.FindByNamespace(repo.Namespace); found != nil {
|
||||
// already know about this namespace
|
||||
continue
|
||||
}
|
||||
newrepo := new(gitpb.Repo)
|
||||
newrepo.Namespace = repo.Namespace
|
||||
newrepo.URL = repo.URL
|
||||
newReposPB.Append(newrepo)
|
||||
}
|
||||
return newReposPB
|
||||
}
|
186
http.go
186
http.go
|
@ -2,101 +2,193 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"go.wit.com/lib/protobuf/forgepb"
|
||||
"go.wit.com/lib/protobuf/gitpb"
|
||||
"go.wit.com/lib/protobuf/httppb"
|
||||
"go.wit.com/log"
|
||||
)
|
||||
|
||||
// remove '?' part and trailing '/'
|
||||
func cleanURL(url string) string {
|
||||
url = "/" + strings.Trim(url, "/")
|
||||
return url
|
||||
func logReqPB(pb *httppb.HttpRequest) {
|
||||
log.Info("LOG: httppb.HttpRequest START:")
|
||||
for i, line := range pb.Logs {
|
||||
log.Infof("%d, URL:%s LINE: %s\n", int(i), pb.URL, line)
|
||||
}
|
||||
log.Info("LOG: httppb.HttpRequest END")
|
||||
}
|
||||
|
||||
func okHandler(w http.ResponseWriter, r *http.Request) {
|
||||
msg, err := ioutil.ReadAll(r.Body) // Read the body as []byte
|
||||
reqPB, err := httppb.ReqToPB(r)
|
||||
reqPB.Logf("START: Got %d bytes from the client", len(reqPB.ClientData))
|
||||
if err != nil {
|
||||
log.Info("ioutil.ReadAll() error =", err)
|
||||
return
|
||||
reqPB.Logf("httppb err %v", err)
|
||||
}
|
||||
// fmt.Fprintln(w, "ioutil.ReadAll() msg =", len(msg))
|
||||
// dumpClient(accessf, clientf, r)
|
||||
var route string
|
||||
// tmp = r.URL.String()
|
||||
route = cleanURL(r.URL.Path)
|
||||
parts := strings.Split(route, "?")
|
||||
log.Info("client sent url =", route, parts)
|
||||
requrl := parts[0]
|
||||
|
||||
log.Warn("forged REQUEST URL =", requrl, "msg =", len(msg))
|
||||
route := reqPB.Route
|
||||
|
||||
if route == "/" {
|
||||
w.Header().Set("Content-Type", "text")
|
||||
fmt.Fprintf(w, "go.wit.com/apps/utils/forged Version: %s\n", argv.Version())
|
||||
fmt.Fprintf(w, "\n")
|
||||
listPatchsets(w)
|
||||
return
|
||||
}
|
||||
|
||||
if route == "/patchset" {
|
||||
savePatchset(w, msg)
|
||||
if strings.HasPrefix(route, "/repos/") {
|
||||
pb := gitpb.NewRepos()
|
||||
if err := pb.Unmarshal(reqPB.ClientData); err == nil {
|
||||
reqPB.Logf("Repos Unmarshal() len=%d", pb.Len())
|
||||
} else {
|
||||
reqPB.Logf("Repos Unmarshal() err=%v", err)
|
||||
}
|
||||
result := gitpb.NewRepos()
|
||||
switch route {
|
||||
case "/repos/check":
|
||||
result = addRequest(pb, reqPB)
|
||||
reqPB.Logf("repos check result.Len()=%d pb.Len()=%d", result.Len(), pb.Len())
|
||||
case "/repos/pull":
|
||||
result = pullRequest(pb, reqPB)
|
||||
case "/repos/add":
|
||||
result = addRequest(pb, reqPB)
|
||||
default:
|
||||
reqPB.Logf("repos check result.Len()=%d pb.Len()=%d", result.Len(), pb.Len())
|
||||
log.Info("repos", route, "unknown")
|
||||
}
|
||||
if err := result.SendReply(w, reqPB); err != nil {
|
||||
reqPB.Logf("Oh well, Send to client failed. err=%v", err)
|
||||
}
|
||||
// todo: logReq(reqPB)
|
||||
logReqPB(reqPB)
|
||||
return
|
||||
}
|
||||
|
||||
if route == "/lookup" {
|
||||
found, err := lookupRepos(msg)
|
||||
if err != nil {
|
||||
if strings.HasPrefix(route, "/set/") {
|
||||
pb := new(forgepb.Set)
|
||||
if err := pb.Unmarshal(reqPB.ClientData); err != nil {
|
||||
reqPB.Logf("Patches Unmarshal() len(data)=%d err=%v", len(reqPB.ClientData), err)
|
||||
logReqPB(reqPB)
|
||||
return
|
||||
}
|
||||
reqPB.Logf("Patches Unmarshal() len=%d", pb.Patches.Len())
|
||||
result := new(forgepb.Set)
|
||||
switch route {
|
||||
case "/set/new":
|
||||
me.forge.Patchsets.Append(pb)
|
||||
reqPB.Logf("addNewPatches() pb.Patches.Len()=%d Patchsets.Len()=%d", pb.Patches.Len(), me.forge.Patchsets.Len())
|
||||
me.forge.SavePatchsets()
|
||||
result.Uuid = pb.Uuid
|
||||
default:
|
||||
}
|
||||
if err := result.SendReply(w, reqPB); err != nil {
|
||||
reqPB.Logf("Oh well, Send to client failed. err=%v", err)
|
||||
}
|
||||
// todo: logReq(reqPB)
|
||||
logReqPB(reqPB)
|
||||
return
|
||||
}
|
||||
|
||||
/*
|
||||
for repo := range repos.IterAll() {
|
||||
repo.Namespace += "good"
|
||||
log.Infof("repo:%s,%s\n", repo.Namespace, repo.FullPath)
|
||||
if strings.HasPrefix(route, "/sets/") {
|
||||
pb := forgepb.NewSets()
|
||||
if err := pb.Unmarshal(reqPB.ClientData); err != nil {
|
||||
reqPB.Logf("Patches Unmarshal() len(data)=%d err=%v", len(reqPB.ClientData), err)
|
||||
logReqPB(reqPB)
|
||||
return
|
||||
}
|
||||
reqPB.Logf("Patches Unmarshal() len=%d", pb.Len())
|
||||
switch route {
|
||||
case "/sets/get":
|
||||
if err := me.forge.Patchsets.SendReply(w, reqPB); err != nil {
|
||||
reqPB.Logf("Oh well, Send to client failed. err=%v", err)
|
||||
} else {
|
||||
reqPB.Logf("SendReply() worked")
|
||||
}
|
||||
*/
|
||||
found.SendPB(w)
|
||||
default:
|
||||
}
|
||||
// todo: logReq(reqPB)
|
||||
logReqPB(reqPB)
|
||||
return
|
||||
}
|
||||
|
||||
if route == "/GetPatchsets" {
|
||||
doSendPatchsets(w)
|
||||
if strings.HasPrefix(route, "/patches/") {
|
||||
pb := forgepb.NewPatches()
|
||||
if err := pb.Unmarshal(reqPB.ClientData); err == nil {
|
||||
reqPB.Logf("Patches Unmarshal() len=%d", pb.Len())
|
||||
} else {
|
||||
reqPB.Logf("Patches Unmarshal() err=%v", err)
|
||||
}
|
||||
result := forgepb.NewPatches()
|
||||
switch route {
|
||||
case "/patches/new":
|
||||
result = addNewPatches(pb, reqPB)
|
||||
reqPB.Logf("addNewPatches() pb.Len()=%d result.Len()=%d", pb.Len(), result.Len())
|
||||
case "/patches/applied":
|
||||
reqPB.Log("not really anything needs to be done on applied patches?")
|
||||
case "/patches/merged":
|
||||
reqPB.Log("a maintainer has merged these patches")
|
||||
result = handleMergedPatches(pb, reqPB)
|
||||
// result = handleAppliedPatches(pb, reqPB)
|
||||
case "/patches/get":
|
||||
result = sendPendingPatches(pb, reqPB)
|
||||
default:
|
||||
result = addNewPatches(pb, reqPB)
|
||||
}
|
||||
if err := result.SendReply(w, reqPB); err != nil {
|
||||
reqPB.Logf("Oh well, Send to client failed. err=%v", err)
|
||||
}
|
||||
me.forge.SavePatchsets()
|
||||
// todo: logReq(reqPB)
|
||||
logReqPB(reqPB)
|
||||
return
|
||||
}
|
||||
|
||||
if route == "/patchsetlist" {
|
||||
listPatchsets(w)
|
||||
if strings.HasPrefix(route, "/patchsets/") {
|
||||
pb, err := makePatchsetsPB(reqPB)
|
||||
if err != nil {
|
||||
reqPB.Logf("%v", err)
|
||||
}
|
||||
result := forgepb.NewSets()
|
||||
switch route {
|
||||
case "/patches/get":
|
||||
result = sendPendingPatchsets(pb, reqPB)
|
||||
default:
|
||||
result = sendPendingPatchsets(pb, reqPB)
|
||||
}
|
||||
if err := result.SendReply(w, reqPB); err != nil {
|
||||
log.Info("Oh well, Send to client failed. err =", err)
|
||||
}
|
||||
log.Info("Send to client seems to have worked. log:", reqPB.Log)
|
||||
me.forge.SavePatchsets()
|
||||
// todo: logReq(reqPB)
|
||||
logReqPB(reqPB)
|
||||
return
|
||||
}
|
||||
if route == "/patchsetget" {
|
||||
filename := r.URL.Query().Get("filename")
|
||||
getPatchset(w, filename)
|
||||
logReqPB(reqPB)
|
||||
|
||||
// used for uptime monitor checking
|
||||
if route == "/uptime" {
|
||||
httppb.WriteFile(w, resources, "uptime.html")
|
||||
return
|
||||
}
|
||||
|
||||
if route == "/goReference.svg" {
|
||||
w.Header().Set("Content-Type", "image/svg+xml")
|
||||
writeFile(w, "goReference.svg")
|
||||
httppb.WriteFile(w, resources, "goReference.svg")
|
||||
return
|
||||
}
|
||||
if route == "/skeleton.v2.css" {
|
||||
writeFile(w, "skeleton.v2.css")
|
||||
httppb.WriteFile(w, resources, "skeleton.v2.css")
|
||||
return
|
||||
}
|
||||
if route == "/favicon.ico" {
|
||||
writeFile(w, "ipv6.png")
|
||||
httppb.WriteFile(w, resources, "ipv6.png")
|
||||
return
|
||||
}
|
||||
// used for uptime monitor checking
|
||||
if route == "/uptime" {
|
||||
writeFile(w, "uptime.html")
|
||||
return
|
||||
}
|
||||
log.Warn("BAD URL =", requrl)
|
||||
log.Warn("BAD URL =", reqPB.URL, "from", reqPB.IP, "author", reqPB.Hostname)
|
||||
badurl(w, r.URL.String())
|
||||
}
|
||||
|
||||
/*
|
||||
func writeFile(w http.ResponseWriter, filename string) {
|
||||
// fmt.Fprintln(w, "GOT TEST?")
|
||||
fullname := "resources/" + filename
|
||||
|
@ -110,8 +202,8 @@ func writeFile(w http.ResponseWriter, filename string) {
|
|||
var repohtml string
|
||||
repohtml = string(pfile)
|
||||
fmt.Fprintln(w, repohtml)
|
||||
log.Println("writeFile() found internal file:", filename)
|
||||
}
|
||||
*/
|
||||
|
||||
func badurl(w http.ResponseWriter, badurl string) {
|
||||
fmt.Fprintln(w, "<!DOCTYPE html>")
|
||||
|
@ -120,7 +212,7 @@ func badurl(w http.ResponseWriter, badurl string) {
|
|||
fmt.Fprintln(w, " <meta http-equiv=\"refresh\" content=\"3; url=https://"+HOSTNAME+"/\">")
|
||||
fmt.Fprintln(w, " </head>")
|
||||
fmt.Fprintln(w, " <body>")
|
||||
fmt.Fprintln(w, " IPv4 IS NOT SUPPORTED<br>")
|
||||
fmt.Fprintln(w, " IPv4 MAY NOT WORK<br>")
|
||||
fmt.Fprintln(w, " FORGE REQUIRES IPv6.<br>")
|
||||
fmt.Fprintln(w, " <br>")
|
||||
fmt.Fprintln(w, " bad url", badurl, "<a href=\"https://forge.wit.com/\">redirecting</a>")
|
||||
|
|
117
main.go
117
main.go
|
@ -6,6 +6,8 @@ import (
|
|||
"net/http"
|
||||
"time"
|
||||
|
||||
"go.wit.com/lib/fhelp"
|
||||
"go.wit.com/lib/gui/prep"
|
||||
"go.wit.com/lib/protobuf/forgepb"
|
||||
"go.wit.com/log"
|
||||
)
|
||||
|
@ -13,53 +15,88 @@ import (
|
|||
// are sent via -ldflags at buildtime
|
||||
var VERSION string
|
||||
var BUILDTIME string
|
||||
var ARGNAME string = "forged"
|
||||
|
||||
//go:embed resources/*
|
||||
var resources embed.FS
|
||||
var forge *forgepb.Forge
|
||||
|
||||
var HOSTNAME string = "forge.wit.com"
|
||||
var LIBDIR string = "/var/lib/forged/"
|
||||
var LIBDIR string = "/var/lib/forged/" // need to deprecate this
|
||||
// var FORGEDIR string = "/home/forge" // deprecated?
|
||||
|
||||
func main() {
|
||||
if argv.Hostname != "" {
|
||||
HOSTNAME = argv.Hostname
|
||||
}
|
||||
forge = forgepb.Init()
|
||||
me = new(mainType)
|
||||
me.myGui = prep.Gui() // prepares the GUI package for go-args
|
||||
me.auto = prep.Bash3(&argv) // this line should be: prep.Bash(&argv)
|
||||
|
||||
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)
|
||||
me.forge = forgepb.InitByAppname(ARGNAME)
|
||||
|
||||
if err := me.forge.InitPatchsets(); err != nil {
|
||||
log.Info("patches failed to open", err)
|
||||
badExit(err)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
if argv.List != nil {
|
||||
log.Printf("forge.Init() %s len()=%d\n", me.forge.Config.Filename, me.forge.Repos.Len())
|
||||
fhelp.DumpENV("finit:")
|
||||
me.forge.Config.DumpENV()
|
||||
doList()
|
||||
okExit("")
|
||||
}
|
||||
|
||||
if argv.Clean != nil {
|
||||
log.Printf("forge.Init() %s len()=%d\n", me.forge.Config.Filename, me.forge.Repos.Len())
|
||||
fhelp.DumpENV("finit:")
|
||||
me.forge.Config.DumpENV()
|
||||
doClean()
|
||||
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 {
|
||||
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()
|
||||
}
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
|
||||
// Use of this source code is governed by the GPL 3.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"go.wit.com/dev/alexflint/arg"
|
||||
"go.wit.com/lib/gui/prep"
|
||||
"go.wit.com/lib/protobuf/forgepb"
|
||||
)
|
||||
|
||||
var me *mainType
|
||||
|
||||
// this app's variables
|
||||
type mainType struct {
|
||||
pp *arg.Parser // for parsing the command line args. Yay to alexf lint!
|
||||
forge *forgepb.Forge // for holding the forge protobuf files
|
||||
myGui *prep.GuiPrep // the gui toolkit handle
|
||||
auto *prep.Auto // more experiments for bash handling
|
||||
configs *forgepb.ForgeConfigs // for holding the forge protobuf files
|
||||
}
|
Loading…
Reference in New Issue