Skip to content

Commit

Permalink
prep: native git http(s) repo server
Browse files Browse the repository at this point in the history
  • Loading branch information
paepckehh committed Jan 12, 2025
1 parent 2db3b98 commit e5f668e
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 7 deletions.
3 changes: 3 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ type OPNCall struct {
BG string // color theme foreground
}
}
GitSrv struct {
Enable bool
}
Unifi struct {
WebUI *url.URL
Tag string
Expand Down
3 changes: 2 additions & 1 deletion example-env-config-unifi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export OPN_UNIFI_VERSION='8.5.6'
export OPN_UNIFI_BACKUP_USER='admin'
export OPN_UNIFI_BACKUP_SECRET='start'
export OPN_UNIFI_BACKUP_IMGURL='https://paepcke.de/res/uni.png'
export OPN_UNIFI_EXPORT='1'
#export OPN_UNIFI_EXPORT='1'
export OPN_UNIFI_FORMAT='csv'
export OPN_UNIFI_MONGODB_URI='mongodb://127.0.0.1:27117'
export OPN_GITSRV='1'
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module paepcke.de/opnborg
go 1.23.3

require (
github.com/AaronO/go-git-http v0.0.0-20161214145340-1d9485b3a98f
github.com/alecthomas/chroma/v2 v2.14.0
github.com/cnaude/go-syslog/syslog/v3 v3.0.0-20220728001533-6297dc1b2ff3
github.com/go-git/go-git/v5 v5.12.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
github.com/AaronO/go-git-http v0.0.0-20161214145340-1d9485b3a98f h1:x/RDwGRneK2/891S2o7KhZt3MhHMSCssoeDOfvolTMk=
github.com/AaronO/go-git-http v0.0.0-20161214145340-1d9485b3a98f/go.mod h1:+6Yuq73F9068Na+mSBNXCvyuxvgw4f/g5ii40e3U8Sc=
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY=
Expand Down
8 changes: 8 additions & 0 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var (
updateUnifiBackup = make(chan bool, 1)
updateUnifiExport = make(chan bool, 1)
unifiStatus string
store string
)

// Setup reads OPNBorgs configuration via env, sanitizes, sets sane defaults
Expand Down Expand Up @@ -56,13 +57,20 @@ func Setup() (*OPNCall, error) {
if config.Path == "" {
config.Path = filepath.Dir("./")
}
store = config.Path

// validate bools
config.Daemon = !isEnv("OPN_NODAEMON")
config.Debug = isEnv("OPN_DEBUG")
config.Git = !isEnv("OPN_NOGIT")
config.GitPush = isEnv("OPN_GITPUSH")

// configure git repo https server
config.GitSrv.Enable = false
if isEnv("OPN_GITSRV") {
config.GitSrv.Enable = true
}

// configure remote syslog server
config.RSysLog.Enable = false
if config.Daemon {
Expand Down
2 changes: 2 additions & 0 deletions srv.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ func srv(config *OPNCall) error {
for {
// reset global (atomic) git worktree state tracker
if config.Git {
// init git repo
_ = gitCheckIn(config)
config.dirty.Store(false)
}

Expand Down
22 changes: 16 additions & 6 deletions httpd-srv.go → srvHttpd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,27 @@ import (
"fmt"
"net/http"
"os"

githttp "github.com/AaronO/go-git-http"
)

// httpd spinup the http internal web server
func startWeb(config *OPNCall) {
func startWeb(c *OPNCall) {

// create store structure
if err := os.MkdirAll(config.Path, 0770); err != nil {
if err := os.MkdirAll(c.Path, 0770); err != nil {
fmt.Println(err)
return
}

// change thread into store-root
if err := os.Chdir(config.Path); err != nil {
if err := os.Chdir(c.Path); err != nil {
fmt.Println(err)
return
}

// get listener, bind ports
listener, err := getHTTPTLS(config)
listener, err := getHTTPTLS(c)
if err != nil {
fmt.Println(err)
return
Expand All @@ -33,17 +35,25 @@ func startWeb(config *OPNCall) {

// handler
mux.Handle("/", addSecurityHeader(getIndexHandler()))
mux.Handle("/files/", addSecurityHeader(http.StripPrefix("/files/", http.FileServer(http.Dir(config.Path)))))
mux.Handle("/files/", addSecurityHeader(http.StripPrefix("/files/", http.FileServer(http.Dir(c.Path)))))
mux.Handle("/force", getForceHandler())
mux.Handle("/favicon.ico", getFavIconHandler())

// spin up internal git repo https server
state := "[DISABLED]"
if c.GitSrv.Enable {
mux.Handle("/git", githttp.New(c.Path))
state = "[ENABLED]"
}
displayChan <- []byte("[GITSRV-HTTP]" + state)

// httpsrv
httpsrv := &http.Server{
Handler: mux,
}

// info
displayChan <- []byte("[HTTPD-SRV][SPIN-UP-SERVER] " + config.Httpd.Server)
displayChan <- []byte("[HTTPD-SRV][SPIN-UP-SERVER] " + c.Httpd.Server)

// serve requestes, print err after httpd crash
fmt.Println(httpsrv.Serve(listener))
Expand Down

0 comments on commit e5f668e

Please sign in to comment.