summaryrefslogtreecommitdiff
path: root/gopher.go
blob: ecef9c0fc78f10a43b037b2333f752d81f637425 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main

import (
	"context"
	"fmt"
	"strings"

	sr "tildegit.org/tjp/sliderule"
	"tildegit.org/tjp/sliderule/contrib/cgi"
	"tildegit.org/tjp/sliderule/contrib/fs"
	"tildegit.org/tjp/sliderule/gopher"
	"tildegit.org/tjp/sliderule/gopher/gophermap"
	"tildegit.org/tjp/sliderule/logging"
	"tildegit.org/tjp/syw"
)

func buildGopherServer(server Server, config *Configuration) (sr.Server, error) {
	addr := fmt.Sprintf("%s:%d", server.IP.String(), server.Port)

	_, info, _, errlog := Loggers(config)
	_ = info.Log("msg", "starting gopher server", "addr", addr)

	if server.TLS != nil {
		return gopher.NewTLSServer(
			context.Background(),
			server.Hostnames[0],
			"tcp",
			addr,
			logging.LogRequests(info)(routes(server)),
			errlog,
			server.TLS,
		)
	}

	return gopher.NewServer(
		context.Background(),
		server.Hostnames[0],
		"tcp",
		addr,
		logging.LogRequests(info)(routes(server)),
		errlog,
	)
}

func addGopherRoute(router *sr.Router, route RouteDirective) {
	switch route.Type {
	case "static":
		addGopherStaticRoute(router, route)
	case "cgi":
		addGopherCGIRoute(router, route)
	case "git":
		addGopherGitRoute(router, route)
	}
}

func addGopherStaticRoute(router *sr.Router, route RouteDirective) {
	dirmaps := []string{}
	if route.Modifiers.DirDefault != "" {
		dirmaps = append(dirmaps, route.Modifiers.DirDefault)
	}
	settings := &gophermap.FileSystemSettings{
		ParseExtended: route.Modifiers.ExtendedGophermap,
		Exec:          route.Modifiers.Exec,
		ListUsers:     false,
		DirMaps:       dirmaps,
	}

	buildAndAddRoute(router, route, func(route RouteDirective) sr.Handler {
		handlers := []sr.Handler{}

		if route.Modifiers.Exec {
			handlers = append(handlers, cgi.GopherCGIDirectory(route.FsPath, route.URLPath, route.Modifiers.ExecCmd, settings))
		}

		handlers = append(handlers, fs.GopherFileHandler(route.FsPath, route.URLPath, settings))

		if route.Modifiers.DirDefault != "" {
			handlers = append(handlers, fs.GopherDirectoryDefault(route.FsPath, route.URLPath, settings))
		}

		if route.Modifiers.DirList {
			handlers = append(handlers, fs.GopherDirectoryListing(route.FsPath, route.URLPath, settings))
		}

		if len(handlers) == 1 {
			return handlers[0]
		}
		return sr.FallthroughHandler(handlers...)
	})
}

func addGopherCGIRoute(router *sr.Router, route RouteDirective) {
	dirmaps := []string{}
	if route.Modifiers.DirDefault != "" {
		dirmaps = append(dirmaps, route.Modifiers.DirDefault)
	}
	settings := &gophermap.FileSystemSettings{
		ParseExtended: route.Modifiers.ExtendedGophermap,
		Exec:          true,
		ListUsers:     false,
		DirMaps:       dirmaps,
	}

	buildAndAddRoute(router, route, func(route RouteDirective) sr.Handler {
		return cgi.GopherCGIDirectory(route.FsPath, route.URLPath, route.Modifiers.ExecCmd, settings)
	})
}

func addGopherGitRoute(router *sr.Router, route RouteDirective) {
	buildAndAddRoute(router, route, func(route RouteDirective) sr.Handler {
		return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response {
			subrouter := syw.GopherRouter(route.FsPath, route.Modifiers.Templates)

			reqclone := cloneRequest(request)
			reqclone.Path = strings.TrimPrefix(reqclone.Path, route.URLPath)

			handler, params := subrouter.Match(reqclone)
			if handler == nil {
				return nil
			}
			return handler.Handle(context.WithValue(ctx, sr.RouteParamsKey, params), request)
		})
	})
}

func cloneRequest(request *sr.Request) *sr.Request {
	r := *request
	u := *request.URL
	r.URL = &u
	return &r
}