first round of files

This commit is contained in:
2026-05-06 23:13:06 +02:00
parent a30962474d
commit 2aa5d01307
24 changed files with 991 additions and 0 deletions
+22
View File
@@ -0,0 +1,22 @@
package handlers
import (
"encoding/json"
"net/http"
)
type PipelineHandler struct{}
func NewPipelineHandler() *PipelineHandler {
return &PipelineHandler{}
}
func (h *PipelineHandler) List(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode([]any{})
}
func (h *PipelineHandler) Get(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
}
+33
View File
@@ -0,0 +1,33 @@
package handlers
import (
"encoding/json"
"net/http"
)
type PRHandler struct{}
func NewPRHandler() *PRHandler {
return &PRHandler{}
}
func (h *PRHandler) List(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode([]any{})
}
func (h *PRHandler) Create(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(map[string]string{"status": "created"})
}
func (h *PRHandler) Get(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
}
func (h *PRHandler) Merge(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"status": "merged"})
}
+38
View File
@@ -0,0 +1,38 @@
package handlers
import (
"encoding/json"
"net/http"
)
type RepoHandler struct{}
func NewRepoHandler() *RepoHandler {
return &RepoHandler{}
}
func (h *RepoHandler) List(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode([]any{})
}
func (h *RepoHandler) Create(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(map[string]string{"status": "created"})
}
func (h *RepoHandler) Get(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
}
func (h *RepoHandler) Tree(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode([]any{})
}
func (h *RepoHandler) Blob(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"content": ""})
}
+33
View File
@@ -0,0 +1,33 @@
package handlers
import (
"encoding/json"
"net/http"
"github.com/gorilla/sessions"
)
type UserHandler struct {
store sessions.Store
}
func NewUserHandler(store sessions.Store) *UserHandler {
return &UserHandler{store: store}
}
func (h *UserHandler) Me(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
}
func (h *UserHandler) Login(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
}
func (h *UserHandler) Logout(w http.ResponseWriter, r *http.Request) {
session, _ := h.store.Get(r, "fb_session")
session.Options.MaxAge = -1
session.Save(r, w)
w.WriteHeader(http.StatusNoContent)
}
+35
View File
@@ -0,0 +1,35 @@
package handlers
import (
"net/http"
"nhooyr.io/websocket"
"nhooyr.io/websocket/wsjson"
)
type WSHandler struct{}
func NewWSHandler() *WSHandler {
return &WSHandler{}
}
func (h *WSHandler) Hub(w http.ResponseWriter, r *http.Request) {
conn, err := websocket.Accept(w, r, &websocket.AcceptOptions{
OriginPatterns: []string{"localhost:*"},
})
if err != nil {
return
}
defer conn.CloseNow()
ctx := r.Context()
for {
var msg map[string]any
if err := wsjson.Read(ctx, conn, &msg); err != nil {
break
}
if err := wsjson.Write(ctx, conn, msg); err != nil {
break
}
}
}
+68
View File
@@ -0,0 +1,68 @@
package middleware
import (
"context"
"net/http"
"github.com/gorilla/sessions"
)
type contextKey string
const (
ContextKeyUserID contextKey = "userID"
ContextKeyUsername contextKey = "username"
ContextKeyIsAdmin contextKey = "isAdmin"
)
type AuthMiddleware struct {
store sessions.Store
}
func NewAuth(store sessions.Store) *AuthMiddleware {
return &AuthMiddleware{store: store}
}
func (a *AuthMiddleware) Require(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session, err := a.store.Get(r, "fb_session")
if err != nil || session.IsNew {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
userID, ok := session.Values["userID"].(int64)
if !ok || userID == 0 {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
ctx := context.WithValue(r.Context(), ContextKeyUserID, userID)
if username, ok := session.Values["username"].(string); ok {
ctx = context.WithValue(ctx, ContextKeyUsername, username)
}
if isAdmin, ok := session.Values["isAdmin"].(bool); ok {
ctx = context.WithValue(ctx, ContextKeyIsAdmin, isAdmin)
}
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func (a *AuthMiddleware) Optional(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session, err := a.store.Get(r, "fb_session")
if err == nil && !session.IsNew {
if userID, ok := session.Values["userID"].(int64); ok && userID != 0 {
ctx := context.WithValue(r.Context(), ContextKeyUserID, userID)
r = r.WithContext(ctx)
}
}
next.ServeHTTP(w, r)
})
}
func UserIDFromContext(ctx context.Context) (int64, bool) {
id, ok := ctx.Value(ContextKeyUserID).(int64)
return id, ok
}
+16
View File
@@ -0,0 +1,16 @@
package middleware
import (
"net/http"
)
func RequireAdmin(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
isAdmin, _ := r.Context().Value(ContextKeyIsAdmin).(bool)
if !isAdmin {
http.Error(w, "forbidden", http.StatusForbidden)
return
}
next.ServeHTTP(w, r)
})
}
+118
View File
@@ -0,0 +1,118 @@
package api
import (
"io/fs"
"net/http"
"github.com/go-chi/chi/v5"
chimiddleware "github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
gcsrf "github.com/gorilla/csrf"
"github.com/gorilla/sessions"
"github.com/forgao/forgebucket/internal/api/handlers"
"github.com/forgao/forgebucket/internal/api/middleware"
"github.com/forgao/forgebucket/internal/config"
)
func New(cfg *config.Config, store sessions.Store, staticFiles fs.FS) http.Handler {
r := chi.NewRouter()
r.Use(chimiddleware.Logger)
r.Use(chimiddleware.RealIP)
r.Use(chimiddleware.Recoverer)
r.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{"http://localhost:5173", cfg.InstanceURL},
AllowedMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
AllowCredentials: true,
MaxAge: 300,
}))
csrfMiddleware := gcsrf.Protect(
[]byte(cfg.CSRFSecret),
gcsrf.Secure(!cfg.Debug),
gcsrf.SameSite(gcsrf.SameSiteLaxMode),
gcsrf.ErrorHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "CSRF validation failed", http.StatusForbidden)
})),
)
auth := middleware.NewAuth(store)
repoH := handlers.NewRepoHandler()
userH := handlers.NewUserHandler(store)
prH := handlers.NewPRHandler()
pipeH := handlers.NewPipelineHandler()
wsH := handlers.NewWSHandler()
// Health check (no auth, no CSRF)
r.Get("/api/v1/health", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"status":"ok"}`))
})
// CSRF token endpoint for SPA bootstrap
r.With(csrfMiddleware).Get("/api/v1/csrf", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-CSRF-Token", gcsrf.Token(r))
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"ok":true}`))
})
// Auth routes (CSRF protected, no session required)
r.With(csrfMiddleware).Route("/api/v1/auth", func(r chi.Router) {
r.Post("/login", userH.Login)
r.Post("/logout", userH.Logout)
})
// Authenticated API routes
r.With(csrfMiddleware).With(auth.Require).Route("/api/v1", func(r chi.Router) {
r.Get("/me", userH.Me)
r.Route("/repos", func(r chi.Router) {
r.Get("/", repoH.List)
r.Post("/", repoH.Create)
r.Route("/{owner}/{repo}", func(r chi.Router) {
r.Get("/", repoH.Get)
r.Get("/tree", repoH.Tree)
r.Get("/blob", repoH.Blob)
r.Route("/pulls", func(r chi.Router) {
r.Get("/", prH.List)
r.Post("/", prH.Create)
r.Get("/{prID}", prH.Get)
r.Post("/{prID}/merge", prH.Merge)
})
r.Route("/pipelines", func(r chi.Router) {
r.Get("/", pipeH.List)
r.Get("/{runID}", pipeH.Get)
})
})
})
})
// WebSocket hub (auth via session cookie, no CSRF needed for WS)
r.With(auth.Optional).Get("/ws", wsH.Hub)
// SPA fallback — serve embedded React app for all other routes
r.Handle("/*", spaHandler(staticFiles))
return r
}
func spaHandler(staticFiles fs.FS) http.Handler {
fileServer := http.FileServer(http.FS(staticFiles))
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := staticFiles.Open(r.URL.Path)
if err != nil {
// Unknown path → serve index.html for client-side routing
index, err := staticFiles.Open("index.html")
if err != nil {
http.Error(w, "not found", http.StatusNotFound)
return
}
index.Close()
r.URL.Path = "/"
}
fileServer.ServeHTTP(w, r)
})
}
+96
View File
@@ -0,0 +1,96 @@
package config
import (
"fmt"
"os"
"strconv"
)
type Config struct {
// Server
Port string
// Database
DatabaseURL string
// Storage
RepoRoot string
// Security
SessionSecret string // must be 32 or 64 bytes for AES-GCM
CSRFSecret string // must be 32 bytes
// OIDC (optional)
OIDCIssuer string
OIDCClientID string
OIDCClientSecret string
// Federation
InstanceURL string
InstanceName string
// Dev
Debug bool
}
func Load() (*Config, error) {
cfg := &Config{
Port: getEnv("PORT", "8080"),
RepoRoot: getEnv("REPO_ROOT", "/var/lib/forgebucket/repos"),
Debug: getEnvBool("DEBUG", false),
InstanceURL: getEnv("INSTANCE_URL", ""),
InstanceName: getEnv("INSTANCE_NAME", "ForgeBucket"),
}
var missing []string
cfg.DatabaseURL = requireEnv("DATABASE_URL", &missing)
cfg.SessionSecret = requireEnv("SESSION_SECRET", &missing)
cfg.CSRFSecret = requireEnv("CSRF_SECRET", &missing)
// Optional OIDC
cfg.OIDCIssuer = os.Getenv("OIDC_ISSUER")
cfg.OIDCClientID = os.Getenv("OIDC_CLIENT_ID")
cfg.OIDCClientSecret = os.Getenv("OIDC_CLIENT_SECRET")
if len(missing) > 0 {
return nil, fmt.Errorf("missing required env vars: %v", missing)
}
if len(cfg.SessionSecret) < 32 {
return nil, fmt.Errorf("SESSION_SECRET must be at least 32 characters")
}
if len(cfg.CSRFSecret) != 32 {
return nil, fmt.Errorf("CSRF_SECRET must be exactly 32 characters")
}
return cfg, nil
}
func requireEnv(key string, missing *[]string) string {
v := os.Getenv(key)
if v == "" {
*missing = append(*missing, key)
}
return v
}
func getEnv(key, fallback string) string {
if v := os.Getenv(key); v != "" {
return v
}
return fallback
}
func getEnvBool(key string, fallback bool) bool {
v := os.Getenv(key)
if v == "" {
return fallback
}
b, err := strconv.ParseBool(v)
if err != nil {
return fallback
}
return b
}
+22
View File
@@ -0,0 +1,22 @@
package git
import "strings"
// ParseAGitRef extracts target and source branch from an AGit push ref.
// AGit refs follow the pattern: refs/for/<target>[/<source>]
// Returns ("", "", false) if the ref is not an AGit ref.
func ParseAGitRef(ref string) (target, source string, ok bool) {
const prefix = "refs/for/"
if !strings.HasPrefix(ref, prefix) {
return "", "", false
}
rest := strings.TrimPrefix(ref, prefix)
parts := strings.SplitN(rest, "/", 2)
target = parts[0]
if len(parts) == 2 {
source = parts[1]
} else {
source = target
}
return target, source, true
}
+113
View File
@@ -0,0 +1,113 @@
package git
import (
"errors"
"os/exec"
"path/filepath"
"strings"
)
var ErrPathTraversal = errors.New("repo path outside of configured root")
var repoRoot string
func SetRepoRoot(root string) {
repoRoot = filepath.Clean(root)
}
// run executes a git command inside repoPath with strict safety guarantees:
// - repoPath is validated to be under the configured repoRoot
// - args are passed as discrete values — never via shell interpolation
// - the process inherits only a minimal environment
func run(repoPath string, args ...string) ([]byte, error) {
clean := filepath.Clean(repoPath)
if repoRoot != "" && !strings.HasPrefix(clean, repoRoot+string(filepath.Separator)) && clean != repoRoot {
return nil, ErrPathTraversal
}
cmd := exec.Command("git", args...)
cmd.Dir = clean
cmd.Env = []string{
"GIT_TERMINAL_PROMPT=0",
"HOME=" + filepath.Dir(repoRoot), // needed for .gitconfig lookups
}
return cmd.Output()
}
func Init(path string) error {
_, err := run(path, "init", "--bare")
return err
}
type Commit struct {
Hash string
Author string
Message string
Date string
}
func Log(repoPath, branch string, limit int) ([]Commit, error) {
out, err := run(repoPath, "log", branch,
"--format=%H\x1f%an\x1f%s\x1f%ci",
"--max-count", strings.TrimSpace(string(rune(limit+'0'))),
)
if err != nil {
return nil, err
}
var commits []Commit
for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") {
parts := strings.Split(line, "\x1f")
if len(parts) != 4 {
continue
}
commits = append(commits, Commit{
Hash: parts[0],
Author: parts[1],
Message: parts[2],
Date: parts[3],
})
}
return commits, nil
}
type TreeEntry struct {
Mode string
Type string
Hash string
Name string
}
func TreeLS(repoPath, ref, subPath string) ([]TreeEntry, error) {
treeRef := ref + ":" + subPath
out, err := run(repoPath, "ls-tree", treeRef)
if err != nil {
return nil, err
}
var entries []TreeEntry
for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") {
if line == "" {
continue
}
// format: <mode> SP <type> SP <hash> TAB <name>
tabIdx := strings.Index(line, "\t")
if tabIdx < 0 {
continue
}
name := line[tabIdx+1:]
fields := strings.Fields(line[:tabIdx])
if len(fields) != 3 {
continue
}
entries = append(entries, TreeEntry{
Mode: fields[0],
Type: fields[1],
Hash: fields[2],
Name: name,
})
}
return entries, nil
}
func BlobCat(repoPath, ref, filePath string) ([]byte, error) {
return run(repoPath, "show", ref+":"+filePath)
}
+15
View File
@@ -0,0 +1,15 @@
package models
import "time"
type FederationActor struct {
ID int64 `xorm:"pk autoincr"`
UserID int64 `xorm:"notnull unique index"`
APID string `xorm:"notnull unique varchar(500)"` // https://instance/users/alice
InboxURL string `xorm:"notnull varchar(500)"`
OutboxURL string `xorm:"notnull varchar(500)"`
PublicKey string `xorm:"text notnull"`
PrivateKey string `xorm:"text notnull"` // AES-GCM encrypted at rest
CreatedAt time.Time `xorm:"created"`
UpdatedAt time.Time `xorm:"updated"`
}
+15
View File
@@ -0,0 +1,15 @@
package migrations
import (
"github.com/forgao/forgebucket/internal/models"
"xorm.io/xorm"
)
func Run(engine *xorm.Engine) error {
return engine.Sync2(
&models.User{},
&models.Repository{},
&models.PullRequest{},
&models.FederationActor{},
)
}
+24
View File
@@ -0,0 +1,24 @@
package models
import "time"
type PRStatus string
const (
PRStatusOpen PRStatus = "open"
PRStatusMerged PRStatus = "merged"
PRStatusClosed PRStatus = "closed"
)
type PullRequest struct {
ID int64 `xorm:"pk autoincr"`
RepoID int64 `xorm:"notnull index"`
AuthorID int64 `xorm:"notnull index"`
Title string `xorm:"notnull varchar(255)"`
Body string `xorm:"text"`
SourceBranch string `xorm:"notnull varchar(255)"`
TargetBranch string `xorm:"default 'main' varchar(255)"`
Status PRStatus `xorm:"default 'open' varchar(16)"`
CreatedAt time.Time `xorm:"created"`
UpdatedAt time.Time `xorm:"updated"`
}
+15
View File
@@ -0,0 +1,15 @@
package models
import "time"
type Repository struct {
ID int64 `xorm:"pk autoincr"`
OwnerID int64 `xorm:"notnull index"`
Name string `xorm:"notnull varchar(100)"`
Description string `xorm:"varchar(500)"`
IsPrivate bool `xorm:"default false"`
DefaultBranch string `xorm:"default 'main' varchar(255)"`
DiskPath string `xorm:"notnull"` // absolute path under REPO_ROOT
CreatedAt time.Time `xorm:"created"`
UpdatedAt time.Time `xorm:"updated"`
}
+14
View File
@@ -0,0 +1,14 @@
package models
import "time"
type User struct {
ID int64 `xorm:"pk autoincr"`
Username string `xorm:"unique notnull varchar(64)"`
Email string `xorm:"unique notnull varchar(255)"`
PasswordHash string `xorm:"notnull"`
AvatarURL string `xorm:"varchar(500)"`
IsAdmin bool `xorm:"default false"`
CreatedAt time.Time `xorm:"created"`
UpdatedAt time.Time `xorm:"updated"`
}