mirror of
				https://github.com/coredns/coredns.git
				synced 2025-11-03 18:53:13 -05:00 
			
		
		
		
	Use the package go/ast/astutil for adding CoreDNS to caddy and removing the http server stuff from it as well - we're only a DNS server, no need to caddy all the HTTP stuff as well. Results in smaller binary and plugin_generate.go being much smaller as well.
		
			
				
	
	
		
			49 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			49 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
//+build ignore
 | 
						|
 | 
						|
package main
 | 
						|
 | 
						|
import (
 | 
						|
	"bytes"
 | 
						|
	"go/ast"
 | 
						|
	"go/parser"
 | 
						|
	"go/printer"
 | 
						|
	"go/token"
 | 
						|
	"io/ioutil"
 | 
						|
	"log"
 | 
						|
 | 
						|
	"golang.org/x/tools/go/ast/astutil"
 | 
						|
)
 | 
						|
 | 
						|
func GenerateFile(fset *token.FileSet, file *ast.File) ([]byte, error) {
 | 
						|
	var output []byte
 | 
						|
	buffer := bytes.NewBuffer(output)
 | 
						|
	if err := printer.Fprint(buffer, fset, file); err != nil {
 | 
						|
		return nil, err
 | 
						|
	}
 | 
						|
 | 
						|
	return buffer.Bytes(), nil
 | 
						|
}
 | 
						|
 | 
						|
func main() {
 | 
						|
	fset := token.NewFileSet()
 | 
						|
	f, err := parser.ParseFile(fset, caddyrun, nil, parser.ParseComments)
 | 
						|
	if err != nil {
 | 
						|
		log.Fatalf("failed to parse %s: %s", caddyrun, err)
 | 
						|
	}
 | 
						|
	astutil.AddNamedImport(fset, f, "_", coredns)
 | 
						|
	astutil.DeleteNamedImport(fset, f, "_", caddy)
 | 
						|
 | 
						|
	out, err := GenerateFile(fset, f)
 | 
						|
	if err := ioutil.WriteFile(caddyrun, out, 0644); err != nil {
 | 
						|
		log.Fatalf("failed to write go file: %s", err)
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
const (
 | 
						|
	coredns = "github.com/miekg/coredns/core"
 | 
						|
	caddy   = "github.com/mholt/caddy/caddyhttp"
 | 
						|
 | 
						|
	// If everything is OK and we are sitting in CoreDNS' dir, this is where run.go should be.
 | 
						|
	caddyrun = "../../mholt/caddy/caddy/caddymain/run.go"
 | 
						|
)
 |