Compare commits

..

No commits in common. "master" and "v0.0.2" have entirely different histories.

18 changed files with 296 additions and 917 deletions

View File

@ -1,29 +0,0 @@
{
email forge@wit.com
debug
servers {
metrics
}
log {
output file /var/log/caddy/caddy.log {
roll_size 500mb
}
format json {
time_format iso8601
}
}
}
forge.wit.com {
encode zstd gzip
reverse_proxy localhost:2520
log {
output file /var/log/caddy/forge.wit.com.log {
roll_size 100mb
}
format json {
time_format iso8601
}
}
}

View File

@ -1,28 +1,33 @@
.PHONY: build
VERSION = $(shell git describe --tags)
# BUILDTIME = $(shell date +%Y.%m.%d_%H%M)
BUILDTIME = $(shell date +%s)
BUILDTIME = $(shell date +%Y.%m.%d)
all: build
FORGE_VERBOSE=true ./forged clean
# FORGE_VERBOSE=true ./forged list
@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"
build: goimports
GO111MODULE=off go build \
-ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}"
build-verbose: goimports
verbose:
GO111MODULE=off go build -v -x \
-ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}"
install: goimports
install:
GO111MODULE=off go install \
-ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}"
log:
@#systemctl status forged.service
journalctl -n 100 -f -xeu forged.service
journalctl -f -xeu forged.service
enable:
systemctl enable forged.service
@ -35,24 +40,11 @@ start:
systemctl start forged.service
stop:
-systemctl stop forged.service
systemctl stop forged.service
run: build stop
./forged --daemon
run: build
./forged --port 2233 --hostname forge.wit.com
# 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
goimports:
reset
@ -62,7 +54,7 @@ clean:
rm -f go.*
rm -f go.wit.com
rm -f forged
go-mod-clean purge
go-mod-clean --purge
# makes a .deb package
debian:
@ -73,6 +65,3 @@ squash-the-last-3-commits-together:
git rebase -i HEAD~3
git fsck
git prune
list:
./forged list

71
argv.go
View File

@ -6,81 +6,20 @@ package main
*/
import (
"fmt"
"os"
"strconv"
"strings"
"time"
"go.wit.com/lib/gui/prep"
"go.wit.com/lib/gui/shell"
"go.wit.com/dev/alexflint/arg"
)
var argv args
type args struct {
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
Hostname string `arg:"--hostname" default:"forge.wit.com" help:"hostname to use"`
}
func (args) Version() string {
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
return "forged " + VERSION + " Built on " + BUILDTIME
}
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)
func init() {
arg.MustParse(&argv)
}

View File

@ -1,50 +0,0 @@
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
}

View File

@ -1,51 +0,0 @@
// 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()
})
}

View File

@ -1,65 +0,0 @@
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
}

View File

@ -1,135 +0,0 @@
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)
}
}
}

166
doPatchsets.go Normal file
View File

@ -0,0 +1,166 @@
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/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()
}

26
exit.go
View File

@ -1,26 +0,0 @@
// 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)
}

View File

@ -1,76 +0,0 @@
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
}

View File

@ -4,7 +4,7 @@ Description=forged
[Service]
User=root
Type=simple
ExecStart=/usr/bin/forged --daemon
ExecStart=/usr/bin/forged
ExecStop=killall forged
Restart=on-failure
RestartSec=5

View File

@ -1,66 +0,0 @@
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
}

View File

@ -1,61 +0,0 @@
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
}

208
http.go
View File

@ -2,193 +2,82 @@ 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"
)
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")
// remove '?' part and trailing '/'
func cleanURL(url string) string {
url = "/" + strings.Trim(url, "/")
return url
}
func okHandler(w http.ResponseWriter, r *http.Request) {
reqPB, err := httppb.ReqToPB(r)
reqPB.Logf("START: Got %d bytes from the client", len(reqPB.ClientData))
msg, err := ioutil.ReadAll(r.Body) // Read the body as []byte
if err != nil {
reqPB.Logf("httppb err %v", err)
log.Info("ioutil.ReadAll() error =", err)
return
}
// 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]
route := reqPB.Route
log.Warn("forged REQUEST URL =", requrl, "msg =", len(msg))
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")
// indexHeader(w)
// indexBodyStart(w)
// indexBodyScanConfig(w)
// indexDivEnd(w)
return
}
if route == "/patchset" {
savePatchset(w, msg)
return
}
if route == "/GetPatchsets" {
doSendPatchsets(w)
return
}
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)
if route == "/patchsetlist" {
listPatchsets(w)
return
}
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
}
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")
}
default:
}
// todo: logReq(reqPB)
logReqPB(reqPB)
return
}
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 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
}
logReqPB(reqPB)
// used for uptime monitor checking
if route == "/uptime" {
httppb.WriteFile(w, resources, "uptime.html")
if route == "/patchsetget" {
filename := r.URL.Query().Get("filename")
getPatchset(w, filename)
return
}
if route == "/goReference.svg" {
w.Header().Set("Content-Type", "image/svg+xml")
httppb.WriteFile(w, resources, "goReference.svg")
writeFile(w, "goReference.svg")
return
}
if route == "/skeleton.v2.css" {
httppb.WriteFile(w, resources, "skeleton.v2.css")
writeFile(w, "skeleton.v2.css")
return
}
if route == "/favicon.ico" {
httppb.WriteFile(w, resources, "ipv6.png")
writeFile(w, "ipv6.png")
return
}
log.Warn("BAD URL =", reqPB.URL, "from", reqPB.IP, "author", reqPB.Hostname)
// used for uptime monitor checking
if route == "/uptime" {
writeFile(w, "uptime.html")
return
}
log.Warn("BAD URL =", requrl)
badurl(w, r.URL.String())
}
/*
func writeFile(w http.ResponseWriter, filename string) {
// fmt.Fprintln(w, "GOT TEST?")
fullname := "resources/" + filename
@ -201,9 +90,12 @@ func writeFile(w http.ResponseWriter, filename string) {
var repohtml string
repohtml = string(pfile)
if filename == "goReference.svg" {
w.Header().Set("Content-Type", "image/svg+xml")
}
fmt.Fprintln(w, repohtml)
log.Println("writeFile() found internal file:", filename)
}
*/
func badurl(w http.ResponseWriter, badurl string) {
fmt.Fprintln(w, "<!DOCTYPE html>")
@ -212,10 +104,10 @@ 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 MAY NOT WORK<br>")
fmt.Fprintln(w, " FORGE REQUIRES IPv6.<br>")
fmt.Fprintln(w, " IPv4 IS NOT SUPPORTED<br>")
fmt.Fprintln(w, " MANY OF THESE REPOS REQUIRE IPv6.<br>")
fmt.Fprintln(w, " <br>")
fmt.Fprintln(w, " bad url", badurl, "<a href=\"https://forge.wit.com/\">redirecting</a>")
fmt.Fprintln(w, " bad url", badurl, "<a href=\"https://go.wit.com/\">redirecting</a>")
fmt.Fprintln(w, " </body>")
fmt.Fprintln(w, "</html>")
}

18
https.go Normal file
View File

@ -0,0 +1,18 @@
package main
import (
"fmt"
"net/http"
)
func https() {
// http.HandleFunc("/", okHandler)
certPath := "/etc/letsencrypt/live/go.wit.com/fullchain.pem"
keyPath := "/etc/letsencrypt/live/go.wit.com/privkey.pem"
err := http.ListenAndServeTLS(":443", certPath, keyPath, nil)
if err != nil {
fmt.Println("Error starting HTTPS server:", err)
}
}

113
main.go
View File

@ -6,8 +6,6 @@ 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"
)
@ -15,88 +13,53 @@ 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/" // need to deprecate this
// var FORGEDIR string = "/home/forge" // deprecated?
var LIBDIR string = "/var/lib/forged/"
func main() {
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)
me.forge = forgepb.InitByAppname(ARGNAME)
if err := me.forge.InitPatchsets(); err != nil {
log.Info("patches failed to open", err)
badExit(err)
if argv.Hostname != "" {
HOSTNAME = argv.Hostname
}
forge = forgepb.Init()
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))
http.HandleFunc("/", okHandler)
// go https() // use caddy instead
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.Println("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)
}
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()
}
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
}

View File

@ -1,8 +0,0 @@
<!DOCTYPE html>
<html>
<head>
</head>
<body>
OKFORGE
</body>
</html>

View File

@ -1,21 +0,0 @@
// 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
}