我想将Caddy导入go项目中,但是即使是最基本的示例,我也似乎无法提出。我尝试用depgo mod拉依赖项,但都失败了。
dep init

No versions of golang.org/x/text met constraints:
    v0.3.0: unable to update checked out version: : command failed: [git checkout f21a4dfb5e38f5895301dc265a8def02365cc3d0]: exit status 128
    v0.2.0: unable to update checked out version: : command failed: [git checkout c4d099d611ac3ded35360abf03581e13d91c828f]: exit status 128
    v0.1.0: unable to update checked out version: : command failed: [git checkout ab5ac5f9a8deb4855a60fab02bc61a4ec770bd49]: exit status 1
    master: unable to update checked out version: : command failed: [git checkout fe223c5a2583471b2791ca99e716c65b4a76117e]: exit status 1
    release-branch.go1.11: unable to update checked out version: : command failed: [git checkout cb6730876b985e110843c1842a7a63a63677cf08]: exit status 1
    release-branch.go1.12: unable to update checked out version: : command failed: [git checkout e6919f6577db79269a6443b9dc46d18f2238fb5d]: exit status 1
go build(带有go mod)
go build
go: finding github.com/mholt/caddy/caddyhttp latest
go: finding github.com/jimstudt/http-authentication/basic latest
go: finding github.com/jimstudt/http-authentication latest
go: finding github.com/mholt/certmagic latest
go: finding github.com/flynn/go-shlex latest
go: finding github.com/lucas-clemente/quic-go/h2quic latest
go: finding golang.org/x/net/http2 latest
go: finding golang.org/x/net latest
go: finding github.com/xenolf/lego/certcrypto latest
go: finding github.com/xenolf/lego/challenge latest
go: finding github.com/xenolf/lego/challenge/tlsalpn01 latest
go: finding github.com/lucas-clemente/quic-go-certificates latest
go: finding github.com/cheekybits/genny/generic latest
go: finding github.com/lucas-clemente/aes12 latest
go: finding github.com/bifurcation/mint latest
# github.com/mholt/caddy/caddyhttp/markdown/summary
/home/ciokan/go/pkg/mod/github.com/mholt/[email protected]/caddyhttp/markdown/summary/render.go:24:5: cannot use (*renderer)(nil) (type *renderer) as type blackfriday.Renderer in assignment:
    *renderer does not implement blackfriday.Renderer (missing RenderFooter method)
/home/ciokan/go/pkg/mod/github.com/mholt/[email protected]/caddyhttp/markdown/summary/summary.go:26:44: too many arguments to conversion to blackfriday.Markdown: blackfriday.Markdown(input, renderer literal, 0)
# github.com/lucas-clemente/quic-go/internal/crypto
/home/ciokan/go/pkg/mod/github.com/lucas-clemente/[email protected]/internal/crypto/key_derivation.go:46:37: cs.KeyLen undefined (type mint.CipherSuiteParams has no field or method KeyLen)
/home/ciokan/go/pkg/mod/github.com/lucas-clemente/[email protected]/internal/crypto/key_derivation.go:47:35: cs.IvLen undefined (type mint.CipherSuiteParams has no field or method IvLen)
# github.com/mholt/caddy/caddytls
/home/ciokan/go/pkg/mod/github.com/mholt/[email protected]/caddytls/setup.go:174:28: cannot use value (type "github.com/xenolf/lego/certcrypto".KeyType) as type "github.com/go-acme/lego/certcrypto".KeyType in assignment
/home/ciokan/go/pkg/mod/github.com/mholt/[email protected]/caddytls/setup.go:354:4: cannot use config.Manager.KeyType (type "github.com/go-acme/lego/certcrypto".KeyType) as type "github.com/xenolf/lego/certcrypto".KeyType in field value

这是一个非常基本的脚本:
package main

import (
        "fmt"
        "io/ioutil"
        "os"

        "github.com/mholt/caddy"
        _ "github.com/mholt/caddy/caddyhttp"
)

func init() {
        caddy.SetDefaultCaddyfileLoader("default", caddy.LoaderFunc(loadConfig))
}

func loadConfig(serverType string) (caddy.Input, error) {
        contents, err := ioutil.ReadFile(caddy.DefaultConfigFile)
        if err != nil {
                if os.IsNotExist(err) {
                        return nil, nil
                }
                return nil, err
        }
        fmt.Printf("Loading Caddyfile: %s\n", string(contents))
        return caddy.CaddyfileInput{
                Contents:       contents,
                Filepath:       caddy.DefaultConfigFile,
                ServerTypeName: serverType,
        }, nil
}

func main() {
        caddy.AppName = "MyApp"
        caddy.AppVersion = "0.1"

        caddyfile, err := caddy.LoadCaddyfile("http")
        if err != nil {
                panic(err)
        }

        inst, err := caddy.Start(caddyfile)
        if err != nil {
                panic(err)
        }
        inst.Wait()
}

那么我应该如何在自己的包装中使用Caddy?

最佳答案

您可以尝试以下一种方法吗?

module stack/cady

go 1.12

require (
    github.com/bifurcation/mint v0.0.0-20180715133206-93c51c6ce115 // indirect
    github.com/cenkalti/backoff v2.1.1+incompatible // indirect
    github.com/cheekybits/genny v1.0.0 // indirect
    github.com/dustin/go-humanize v1.0.0 // indirect
    github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 // indirect
    github.com/google/uuid v1.1.1 // indirect
    github.com/gorilla/websocket v1.4.0 // indirect
    github.com/hashicorp/go-syslog v1.0.0 // indirect
    github.com/hashicorp/golang-lru v0.5.1 // indirect
    github.com/jimstudt/http-authentication v0.0.0-20140401203705-3eca13d6893a // indirect
    github.com/lucas-clemente/aes12 v0.0.0-20171027163421-cd47fb39b79f // indirect
    github.com/lucas-clemente/quic-go v0.10.1-no-integrationtests // indirect
    github.com/lucas-clemente/quic-go-certificates v0.0.0-20160823095156-d2f86524cced // indirect
    github.com/mholt/caddy v0.11.5
    github.com/mholt/certmagic v0.0.0-20190225061201-e3e89d1096d7 // indirect
    github.com/naoina/go-stringutil v0.1.0 // indirect
    github.com/naoina/toml v0.1.1 // indirect
    github.com/russross/blackfriday v0.0.0-20170610170232-067529f716f4 // indirect
    github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
    github.com/xenolf/lego v2.2.1-0.20190304181753-67b329e3e370+incompatible // indirect
    golang.org/x/net v0.0.0-20190320064053-1272bf9dcd53 // indirect
    gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
    gopkg.in/yaml.v2 v2.2.2 // indirect
)

可能不是最好的解决方案,但我做了什么:
  • 读取https://github.com/mholt/caddy/blob/v0.11.5/vendor/manifest的供应商文件夹中的清单
  • 获取出现问题的导入的版本
  • 修改需求部分
  • 中的版本

    10-04 10:13