2015-01-14 03:43:45 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-05-05 06:23:16 +08:00
|
|
|
"bytes"
|
2015-04-25 10:08:14 +08:00
|
|
|
"errors"
|
2015-01-14 07:14:00 +08:00
|
|
|
"flag"
|
2015-04-16 04:11:32 +08:00
|
|
|
"fmt"
|
2015-05-05 06:23:16 +08:00
|
|
|
"io/ioutil"
|
2015-01-14 03:43:45 +08:00
|
|
|
"log"
|
2015-04-16 04:11:32 +08:00
|
|
|
"net"
|
2015-05-04 20:53:54 +08:00
|
|
|
"os"
|
2015-05-21 10:46:27 +08:00
|
|
|
"os/exec"
|
2015-05-04 20:53:54 +08:00
|
|
|
"path"
|
2015-04-25 10:08:14 +08:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-01-14 03:43:45 +08:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/mholt/caddy/config"
|
|
|
|
"github.com/mholt/caddy/server"
|
|
|
|
)
|
|
|
|
|
2015-04-10 00:08:22 +08:00
|
|
|
var (
|
2015-05-08 03:09:40 +08:00
|
|
|
conf string
|
|
|
|
http2 bool // TODO: temporary flag until http2 is standard
|
|
|
|
quiet bool
|
|
|
|
cpu string
|
|
|
|
version bool
|
2015-04-10 00:08:22 +08:00
|
|
|
)
|
2015-01-19 14:11:21 +08:00
|
|
|
|
|
|
|
func init() {
|
2015-05-06 13:19:14 +08:00
|
|
|
flag.StringVar(&conf, "conf", "", "Configuration file to use (default="+config.DefaultConfigFile+")")
|
2015-05-04 20:53:54 +08:00
|
|
|
flag.BoolVar(&http2, "http2", true, "Enable HTTP/2 support") // TODO: temporary flag until http2 merged into std lib
|
|
|
|
flag.BoolVar(&quiet, "quiet", false, "Quiet mode (no initialization output)")
|
2015-04-25 10:08:14 +08:00
|
|
|
flag.StringVar(&cpu, "cpu", "100%", "CPU cap")
|
2015-05-04 20:53:54 +08:00
|
|
|
flag.StringVar(&config.Root, "root", config.DefaultRoot, "Root path to default site")
|
2015-04-30 12:30:12 +08:00
|
|
|
flag.StringVar(&config.Host, "host", config.DefaultHost, "Default host")
|
2015-04-29 12:13:00 +08:00
|
|
|
flag.StringVar(&config.Port, "port", config.DefaultPort, "Default port")
|
2015-05-08 03:09:40 +08:00
|
|
|
flag.BoolVar(&version, "version", false, "Show version")
|
2015-05-05 06:23:16 +08:00
|
|
|
|
|
|
|
config.AppName = "Caddy"
|
|
|
|
config.AppVersion = "0.6.0"
|
2015-01-19 14:11:21 +08:00
|
|
|
}
|
|
|
|
|
2015-01-14 03:43:45 +08:00
|
|
|
func main() {
|
2015-05-07 04:57:32 +08:00
|
|
|
flag.Parse()
|
|
|
|
|
2015-05-08 03:09:40 +08:00
|
|
|
if version {
|
|
|
|
fmt.Printf("%s %s\n", config.AppName, config.AppVersion)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2015-01-14 03:43:45 +08:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2015-04-25 10:08:14 +08:00
|
|
|
// Set CPU cap
|
|
|
|
err := setCPU(cpu)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-04-16 04:11:32 +08:00
|
|
|
// Load config from file
|
2015-05-05 06:23:16 +08:00
|
|
|
allConfigs, err := loadConfigs()
|
2015-01-14 03:43:45 +08:00
|
|
|
if err != nil {
|
2015-05-04 20:53:54 +08:00
|
|
|
log.Fatal(err)
|
2015-04-18 23:53:43 +08:00
|
|
|
}
|
2015-01-14 03:43:45 +08:00
|
|
|
|
2015-04-18 23:53:43 +08:00
|
|
|
// Group by address (virtual hosts)
|
2015-04-16 04:11:32 +08:00
|
|
|
addresses, err := arrangeBindings(allConfigs)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start each server with its one or more configurations
|
|
|
|
for addr, configs := range addresses {
|
2015-05-21 10:06:30 +08:00
|
|
|
s, err := server.New(addr.String(), configs, configs[0].TLS.Enabled)
|
2015-01-14 03:43:45 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2015-04-12 06:58:34 +08:00
|
|
|
s.HTTP2 = http2 // TODO: This setting is temporary
|
2015-01-14 03:43:45 +08:00
|
|
|
wg.Add(1)
|
|
|
|
go func(s *server.Server) {
|
|
|
|
defer wg.Done()
|
|
|
|
err := s.Serve()
|
|
|
|
if err != nil {
|
2015-05-02 03:35:17 +08:00
|
|
|
log.Fatal(err) // kill whole process to avoid a half-alive zombie server
|
2015-01-14 03:43:45 +08:00
|
|
|
}
|
|
|
|
}(s)
|
2015-04-24 03:28:05 +08:00
|
|
|
|
|
|
|
if !quiet {
|
2015-05-21 10:46:27 +08:00
|
|
|
var checkedFdLimit bool
|
2015-05-21 10:06:30 +08:00
|
|
|
for addr, configs := range addresses {
|
|
|
|
for _, conf := range configs {
|
2015-05-21 10:46:27 +08:00
|
|
|
// Print address of site
|
|
|
|
fmt.Println(conf.Address())
|
|
|
|
|
|
|
|
// Note if non-localhost site resolves to loopback interface
|
2015-05-21 10:06:30 +08:00
|
|
|
if addr.IP.IsLoopback() && !isLocalhost(conf.Host) {
|
|
|
|
fmt.Printf("Notice: %s is only accessible on this machine (%s)\n",
|
|
|
|
conf.Host, addr.IP.String())
|
|
|
|
}
|
|
|
|
}
|
2015-05-21 10:46:27 +08:00
|
|
|
|
|
|
|
// Warn if ulimit is too low for production sites
|
|
|
|
if (runtime.GOOS == "linux" || runtime.GOOS == "darwin") &&
|
2015-05-21 10:50:19 +08:00
|
|
|
!addr.IP.IsLoopback() && !checkedFdLimit {
|
2015-05-21 10:46:27 +08:00
|
|
|
out, err := exec.Command("ulimit", "-n").Output()
|
|
|
|
if err == nil {
|
|
|
|
// Note that an error here need not be reported
|
|
|
|
lim, err := strconv.Atoi(string(bytes.TrimSpace(out)))
|
|
|
|
if err == nil && lim < 4096 {
|
|
|
|
fmt.Printf("Warning: File descriptor limit is too low (%d) for production sites\n", lim)
|
|
|
|
}
|
|
|
|
checkedFdLimit = true
|
|
|
|
}
|
|
|
|
}
|
2015-05-21 10:06:30 +08:00
|
|
|
}
|
2015-04-24 03:28:05 +08:00
|
|
|
}
|
2015-01-14 03:43:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
2015-04-16 04:11:32 +08:00
|
|
|
|
2015-05-21 10:06:30 +08:00
|
|
|
func isLocalhost(s string) bool {
|
|
|
|
return s == "localhost" || s == "::1" || strings.HasPrefix(s, "127.")
|
|
|
|
}
|
|
|
|
|
2015-05-05 06:23:16 +08:00
|
|
|
// loadConfigs loads configuration from a file or stdin (piped).
|
|
|
|
// Configuration is obtained from one of three sources, tried
|
|
|
|
// in this order: 1. -conf flag, 2. stdin, 3. Caddyfile.
|
|
|
|
// If none of those are available, a default configuration is
|
|
|
|
// loaded.
|
|
|
|
func loadConfigs() ([]server.Config, error) {
|
|
|
|
// -conf flag
|
|
|
|
if conf != "" {
|
|
|
|
file, err := os.Open(conf)
|
2015-05-04 20:53:54 +08:00
|
|
|
if err != nil {
|
2015-05-05 06:23:16 +08:00
|
|
|
return []server.Config{}, err
|
2015-05-04 20:53:54 +08:00
|
|
|
}
|
2015-05-05 06:23:16 +08:00
|
|
|
defer file.Close()
|
|
|
|
return config.Load(path.Base(conf), file)
|
2015-05-04 20:53:54 +08:00
|
|
|
}
|
|
|
|
|
2015-05-05 06:23:16 +08:00
|
|
|
// stdin
|
2015-05-06 10:31:25 +08:00
|
|
|
fi, err := os.Stdin.Stat()
|
|
|
|
if err == nil && fi.Mode()&os.ModeCharDevice == 0 {
|
2015-05-06 23:16:10 +08:00
|
|
|
// Note that a non-nil error is not a problem. Windows
|
|
|
|
// will not create a stdin if there is no pipe, which
|
|
|
|
// produces an error when calling Stat(). But Unix will
|
|
|
|
// make one either way, which is why we also check that
|
|
|
|
// bitmask.
|
2015-05-06 10:31:25 +08:00
|
|
|
confBody, err := ioutil.ReadAll(os.Stdin)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(confBody) > 0 {
|
|
|
|
return config.Load("stdin", bytes.NewReader(confBody))
|
|
|
|
}
|
2015-05-04 20:53:54 +08:00
|
|
|
}
|
|
|
|
|
2015-05-05 06:23:16 +08:00
|
|
|
// Caddyfile
|
|
|
|
file, err := os.Open(config.DefaultConfigFile)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return []server.Config{config.Default()}, nil
|
|
|
|
}
|
|
|
|
return []server.Config{}, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
2015-05-06 23:16:10 +08:00
|
|
|
|
2015-05-05 06:23:16 +08:00
|
|
|
return config.Load(config.DefaultConfigFile, file)
|
2015-05-04 20:53:54 +08:00
|
|
|
}
|
|
|
|
|
2015-04-16 04:11:32 +08:00
|
|
|
// arrangeBindings groups configurations by their bind address. For example,
|
|
|
|
// a server that should listen on localhost and another on 127.0.0.1 will
|
|
|
|
// be grouped into the same address: 127.0.0.1. It will return an error
|
|
|
|
// if the address lookup fails or if a TLS listener is configured on the
|
|
|
|
// same address as a plaintext HTTP listener.
|
2015-05-21 10:06:30 +08:00
|
|
|
func arrangeBindings(allConfigs []server.Config) (map[*net.TCPAddr][]server.Config, error) {
|
|
|
|
addresses := make(map[*net.TCPAddr][]server.Config)
|
2015-04-16 04:11:32 +08:00
|
|
|
|
|
|
|
// Group configs by bind address
|
|
|
|
for _, conf := range allConfigs {
|
|
|
|
addr, err := net.ResolveTCPAddr("tcp", conf.Address())
|
|
|
|
if err != nil {
|
2015-05-06 23:16:10 +08:00
|
|
|
return addresses, errors.New("Could not serve " + conf.Address() + " - " + err.Error())
|
2015-04-16 04:11:32 +08:00
|
|
|
}
|
2015-05-21 10:06:30 +08:00
|
|
|
addresses[addr] = append(addresses[addr], conf)
|
2015-04-16 04:11:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Don't allow HTTP and HTTPS to be served on the same address
|
|
|
|
for _, configs := range addresses {
|
|
|
|
isTLS := configs[0].TLS.Enabled
|
|
|
|
for _, config := range configs {
|
|
|
|
if config.TLS.Enabled != isTLS {
|
|
|
|
thisConfigProto, otherConfigProto := "HTTP", "HTTP"
|
|
|
|
if config.TLS.Enabled {
|
|
|
|
thisConfigProto = "HTTPS"
|
|
|
|
}
|
|
|
|
if configs[0].TLS.Enabled {
|
|
|
|
otherConfigProto = "HTTPS"
|
|
|
|
}
|
|
|
|
return addresses, fmt.Errorf("Configuration error: Cannot multiplex %s (%s) and %s (%s) on same address",
|
|
|
|
configs[0].Address(), otherConfigProto, config.Address(), thisConfigProto)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return addresses, nil
|
|
|
|
}
|
2015-04-25 10:08:14 +08:00
|
|
|
|
|
|
|
// setCPU parses string cpu and sets GOMAXPROCS
|
|
|
|
// according to its value. It accepts either
|
|
|
|
// a number (e.g. 3) or a percent (e.g. 50%).
|
|
|
|
func setCPU(cpu string) error {
|
|
|
|
var numCPU int
|
|
|
|
|
|
|
|
availCPU := runtime.NumCPU()
|
|
|
|
|
|
|
|
if strings.HasSuffix(cpu, "%") {
|
|
|
|
// Percent
|
|
|
|
var percent float32
|
|
|
|
pctStr := cpu[:len(cpu)-1]
|
|
|
|
pctInt, err := strconv.Atoi(pctStr)
|
|
|
|
if err != nil || pctInt < 1 || pctInt > 100 {
|
|
|
|
return errors.New("Invalid CPU value: percentage must be between 1-100")
|
|
|
|
}
|
|
|
|
percent = float32(pctInt) / 100
|
|
|
|
numCPU = int(float32(availCPU) * percent)
|
|
|
|
} else {
|
|
|
|
// Number
|
|
|
|
num, err := strconv.Atoi(cpu)
|
|
|
|
if err != nil || num < 1 {
|
|
|
|
return errors.New("Invalid CPU value: provide a number or percent greater than 0")
|
|
|
|
}
|
|
|
|
numCPU = num
|
|
|
|
}
|
|
|
|
|
|
|
|
if numCPU > availCPU {
|
|
|
|
numCPU = availCPU
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime.GOMAXPROCS(numCPU)
|
|
|
|
return nil
|
|
|
|
}
|