-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
112 lines (89 loc) · 2.33 KB
/
server.go
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
package houston
import (
"crypto/tls"
"fmt"
"log"
"os"
"golang.org/x/time/rate"
)
type Server struct {
TLSConfig *tls.Config
Router *Router
Config ServerConfig
}
type ServerConfig struct {
// TLS certificate and key file paths.
CertificatePath string
KeyPath string
// Self-explanatory.
Hostname string
Port uint16
// Whether connection logging should occur,
// and where the file should be located.
EnableLog bool
LogFilePath string
// This can be accessed during the server's lifetime to
// manually write more info to the config, other than what's
// built-in to Houston.
LogFile *os.File
// Whether the server should apply rate-limiting.
EnableLimiting bool
MaxRate rate.Limit
BucketSize int
// Example: file exists at `/page.gmi`, and user requests `/page`. Should
// the server append the `.gmi` and return `/page.gmi`? This assumption will
// happen before handler functions are checked.
ImplyExtension bool
}
func NewServer(router *Router, config *ServerConfig) Server {
if config.CertificatePath == "" || config.KeyPath == "" {
log.Fatal("Must provide TLS certificate and key path to server config!")
}
cer, err := tls.LoadX509KeyPair(config.CertificatePath, config.KeyPath)
if err != nil {
log.Fatalf("Error when loading key and certificate: %v", err)
}
// Enforce hostname and port number defaults if not set.
if len(config.Hostname) == 0 {
config.Hostname = "localhost"
}
if config.Port == 0 {
config.Port = 1965
}
// Rate-limiting defaults.
if config.MaxRate == 0 {
config.MaxRate = 2
}
if config.BucketSize == 0 {
config.BucketSize = 2
}
if len(config.LogFilePath) == 0 {
config.LogFilePath = "houston.log"
}
return Server{
Router: router,
TLSConfig: &tls.Config{Certificates: []tls.Certificate{cer}},
Config: *config,
}
}
func (s *Server) Start() {
var f *os.File
var fileErr error
if s.Config.EnableLog {
f, fileErr = os.OpenFile(s.Config.LogFilePath, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
defer f.Close()
s.Config.LogFile = f
log.SetOutput(f)
}
if fileErr != nil {
log.Println("Failed to open log file!")
}
ln, _ := tls.Listen("tcp", fmt.Sprintf("%s:%d", s.Config.Hostname, s.Config.Port), s.TLSConfig)
for {
conn, err := ln.Accept()
if err != nil {
log.Fatal(err)
}
go handleConnection(s, conn)
}
}