mirror of
https://codeberg.org/git-pages/git-pages.git
synced 2026-05-14 11:11:35 +00:00
Add support for Caddy on-demand TLS.
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
data-dir = "./data"
|
||||
|
||||
[listen]
|
||||
[pages]
|
||||
protocol = "tcp"
|
||||
address = ":3333"
|
||||
|
||||
[caddy]
|
||||
protocol = "tcp"
|
||||
address = ":3334"
|
||||
|
||||
[wildcard]
|
||||
domain = "codeberg.page"
|
||||
clone-url = "https://codeberg.org/%s/%s.git"
|
||||
|
||||
25
src/caddy.go
Normal file
25
src/caddy.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func ServeCaddy(w http.ResponseWriter, r *http.Request) {
|
||||
domain := r.URL.Query().Get("domain")
|
||||
if domain == "" {
|
||||
http.Error(w, "domain parameter required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
wwwRoot := filepath.Join(config.DataDir, "www", domain)
|
||||
if stat, err := os.Stat(wwwRoot); err == nil && stat.IsDir() {
|
||||
log.Println("caddy:", domain, 200)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
} else {
|
||||
log.Println("caddy:", domain, 404)
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
}
|
||||
@@ -6,12 +6,15 @@ import (
|
||||
"github.com/pelletier/go-toml/v2"
|
||||
)
|
||||
|
||||
type Listen struct {
|
||||
Protocol string `toml:"protocol"`
|
||||
Address string `toml:"address"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
DataDir string `toml:"data-dir"`
|
||||
Listen struct {
|
||||
Protocol string `toml:"protocol"`
|
||||
Address string `toml:"address"`
|
||||
} `toml:"listen"`
|
||||
DataDir string `toml:"data-dir"`
|
||||
Pages Listen `toml:"pages"`
|
||||
Caddy Listen `toml:"caddy"`
|
||||
Wildcard struct {
|
||||
Domain string `toml:"domain"`
|
||||
CloneURL string `toml:"clone-url"`
|
||||
|
||||
25
src/main.go
25
src/main.go
@@ -9,21 +9,30 @@ import (
|
||||
|
||||
var config Config
|
||||
|
||||
func serveHandler(name string, listen Listen, serve func(http.ResponseWriter, *http.Request)) {
|
||||
listener, err := net.Listen(listen.Protocol, listen.Address)
|
||||
if err != nil {
|
||||
log.Fatalf("%s: %s\n", name, err)
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/", serve)
|
||||
if err := http.Serve(listener, mux); err != nil {
|
||||
log.Fatalf("%s: %s\n", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
configPath := flag.String("config", "config.toml", "path to configuration file")
|
||||
flag.Parse()
|
||||
|
||||
if err := readConfig(*configPath, &config); err != nil {
|
||||
log.Fatalln("failed to read configuration:", err)
|
||||
log.Fatalln("configuration:", err)
|
||||
}
|
||||
|
||||
listener, err := net.Listen(config.Listen.Protocol, config.Listen.Address)
|
||||
if err != nil {
|
||||
log.Fatalln("failed to listen:", err)
|
||||
if config.Caddy != (Listen{}) {
|
||||
go serveHandler("caddy", config.Caddy, ServeCaddy)
|
||||
}
|
||||
|
||||
http.HandleFunc("/", Serve)
|
||||
if err := http.Serve(listener, nil); err != nil {
|
||||
log.Fatalln("failed to serve:", err)
|
||||
}
|
||||
serveHandler("pages", config.Pages, ServePages)
|
||||
}
|
||||
|
||||
@@ -233,8 +233,8 @@ func postPage(w http.ResponseWriter, r *http.Request) error {
|
||||
return result.err
|
||||
}
|
||||
|
||||
func Serve(w http.ResponseWriter, r *http.Request) {
|
||||
log.Println("serve:", r.Method, r.Host, r.URL)
|
||||
func ServePages(w http.ResponseWriter, r *http.Request) {
|
||||
log.Println("pages:", r.Method, r.Host, r.URL)
|
||||
err := error(nil)
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
@@ -248,6 +248,6 @@ func Serve(w http.ResponseWriter, r *http.Request) {
|
||||
err = fmt.Errorf("method %s not allowed", r.Method)
|
||||
}
|
||||
if err != nil {
|
||||
log.Println("serve err:", err)
|
||||
log.Println("pages err:", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user