Skip to content
Permalink
Browse files

[caddy]: Use a custom instance of caddymain

  • Loading branch information
erbesharat committed Dec 30, 2019
1 parent f4318a5 commit fdbcb50d0eb7bd96edfda4d7c9c158c8b11fe1c9
Showing with 183 additions and 3 deletions.
  1. +2 −3 cmd/txtdirect/main.go
  2. +173 −0 txtdirectmain/run.go
  3. +8 −0 txtdirectmain/version.go
@@ -14,7 +14,7 @@ limitations under the License.
package main

import (
"github.com/caddyserver/caddy/caddy/caddymain"
"go.txtdirect.org/txtdirect/txtdirectmain"

_ "github.com/SchumacherFM/mailout"
_ "github.com/captncraig/caddy-realip"
@@ -23,6 +23,5 @@ import (
)

func main() {
caddymain.EnableTelemetry = false
caddymain.Run()
txtdirectmain.Run()
}
@@ -0,0 +1,173 @@
// Package txtdirectmain contains the functions for starting TXTDirect.
package txtdirectmain

import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"runtime"
"strings"

"github.com/caddyserver/caddy"
)

func init() {
caddy.Quiet = true // don't show init stuff from caddy
setVersion()

caddy.RegisterCaddyfileLoader("flag", caddy.LoaderFunc(confLoader))
caddy.SetDefaultCaddyfileLoader("default", caddy.LoaderFunc(defaultLoader))

caddy.AppName = TXTDirectName
caddy.AppVersion = TXTDirectVersion
}

// Run is TXTDirect's main() function.
func Run() {
caddy.TrapSignals()

getFlags()

if version {
showVersion()
os.Exit(0)
}

// Get TXTDirect config input
caddyfile, err := caddy.LoadCaddyfile("http")
if err != nil {
mustLogFatal(err)
}

// Start your engines
instance, err := caddy.Start(caddyfile)
if err != nil {
mustLogFatal(err)
}

// Twiddle your thumbs
instance.Wait()
}

// mustLogFatal wraps log.Fatal() in a way that ensures the
// output is always printed to stderr so the user can see it
// if the user is still there, even if the process log was not
// enabled. If this process is an upgrade, however, and the user
// might not be there anymore, this just logs to the process
// log and exits.
func mustLogFatal(args ...interface{}) {
if !caddy.IsUpgrade() {
log.SetOutput(os.Stderr)
}
log.Fatal(args...)
}

// confLoader loads the Caddyfile using the -conf flag.
func confLoader(serverType string) (caddy.Input, error) {
if conf == "" {
return nil, nil
}

if conf == "stdin" {
return caddy.CaddyfileFromPipe(os.Stdin, serverType)
}

contents, err := ioutil.ReadFile(conf)
if err != nil {
return nil, err
}
return caddy.CaddyfileInput{
Contents: contents,
Filepath: conf,
ServerTypeName: serverType,
}, nil
}

// defaultLoader loads the TXTDirect config from the current working directory.
func defaultLoader(serverType string) (caddy.Input, error) {
contents, err := ioutil.ReadFile(caddy.DefaultConfigFile)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
return caddy.CaddyfileInput{
Contents: contents,
Filepath: caddy.DefaultConfigFile,
ServerTypeName: serverType,
}, nil
}

// showVersion prints the version that is starting.
func showVersion() {
fmt.Print(versionString())
fmt.Print(releaseString())
if devBuild && gitShortStat != "" {
fmt.Printf("%s\n%s\n", gitShortStat, gitFilesModified)
}
}

// versionString returns the TXTDirect version as a string.
func versionString() string {
return fmt.Sprintf("%s-%s\n", caddy.AppName, caddy.AppVersion)
}

// releaseString returns the release information related to TXTDirect version:
// <OS>/<ARCH>, <go version>, <commit>
// e.g.,
// linux/amd64, go1.8.3, a6d2d7b5
func releaseString() string {
return fmt.Sprintf("%s/%s, %s, %s\n", runtime.GOOS, runtime.GOARCH, runtime.Version(), GitCommit)
}

// setVersion figures out the version information
// based on variables set by -ldflags.
func setVersion() {
// A development build is one that's not at a tag or has uncommitted changes
devBuild = gitTag == "" || gitShortStat != ""

// Only set the appVersion if -ldflags was used
if gitNearestTag != "" || gitTag != "" {
if devBuild && gitNearestTag != "" {
appVersion = fmt.Sprintf("%s (+%s %s)", strings.TrimPrefix(gitNearestTag, "v"), GitCommit, buildDate)
} else if gitTag != "" {
appVersion = strings.TrimPrefix(gitTag, "v")
}
}
}

// Flags that control program flow or startup
var (
conf string
version bool
)

// Build information obtained with the help of -ldflags
var (
appVersion = "(untracked dev build)" // inferred at startup
devBuild = true // inferred at startup

buildDate string // date -u
gitTag string // git describe --exact-match HEAD 2> /dev/null
gitNearestTag string // git describe --abbrev=0 --tags HEAD
gitShortStat string // git diff-index --shortstat
gitFilesModified string // git diff-index --name-only HEAD

// Gitcommit contains the commit where we built TXTDirect from.
GitCommit string
)

func getFlags() {
confFlag := flag.Lookup("conf")
if confFlag != nil {
conf = confFlag.Value.String()
}

versionFlag := flag.Lookup("version")
if versionFlag != nil {
version = true
}
}
@@ -0,0 +1,8 @@
package txtdirectmain

// Various CoreDNS constants.
const (
TXTDirectVersion = "0.4.0"
TXTDirectName = "TXTDirect"
serverType = "http"
)

0 comments on commit fdbcb50

Please sign in to comment.
You can’t perform that action at this time.