Skip to content
Permalink
Browse files

(setup): Add config struct and parser

  • Loading branch information...
erbesharat committed Sep 6, 2019
1 parent 5aea922 commit b9a55e88d2843198f7f036f4afd28b7dd1c7c2aa
Showing with 199 additions and 0 deletions.
  1. +95 −0 config.go
  2. +28 −0 gomods.go
  3. +76 −0 setup.go
@@ -0,0 +1,95 @@
package gomods

import (
"strconv"

"github.com/caddyserver/caddy"
"github.com/spf13/afero"
)

type Config struct {
GoBinary string
Workers int
Cache Cache
Fs afero.Fs
}

type Cache struct {
Enable bool
Type string
Path string
}

const (
// DefaultGomodsCacheType is the default cache mode
DefaultGomodsCacheType = "tmp"
// DefaultGomodsWorkers is the default number of parallel workers
DefaultGomodsWorkers = 1
)

// SetDefaults sets the default values for gomods config
// if the fields are empty
func (conf *Config) SetDefaults() {
conf.Fs = afero.NewOsFs()
if conf.GoBinary == "" {
conf.GoBinary = DefaultGoBinaryPath
}
if conf.Cache.Enable {
if conf.Cache.Type == "" {
conf.Cache.Type = DefaultGomodsCacheType
}
if conf.Cache.Path == "" {
conf.Cache.Path = afero.GetTempDir(conf.Fs, "")
}
}
if conf.Workers == 0 {
conf.Workers = DefaultGomodsWorkers
}
}

// ParseGomods parses the txtdirect config for gomods
func (conf *Config) ParseGomods(c *caddy.Controller) error {
switch c.Val() {
case "gobinary":
conf.GoBinary = c.RemainingArgs()[0]

case "workers":
value, err := strconv.Atoi(c.RemainingArgs()[0])
if err != nil {
return c.ArgErr()
}
conf.Workers = value

case "cache":
conf.Cache.Enable = true
c.NextArg()
if c.Val() != "{" {
break
}
for c.Next() {
if c.Val() == "}" {
break
}
err := conf.Cache.ParseCache(c)
if err != nil {
return err
}
}
default:
return c.ArgErr() // unhandled option for gomods
}
return nil
}

// ParseCache parses the txtdirect config for gomods cache
func (cache *Cache) ParseCache(c *caddy.Controller) error {
switch c.Val() {
case "type":
cache.Type = c.RemainingArgs()[0]
case "path":
cache.Path = c.RemainingArgs()[0]
default:
return c.ArgErr() // unhandled option for gomods cache
}
return nil
}
@@ -0,0 +1,28 @@
package gomods

import (
"net/http"
"os"
"regexp"

"github.com/gomods/athens/pkg/download"
"github.com/gomods/athens/pkg/module"
"github.com/gomods/athens/pkg/storage"
"github.com/spf13/afero"
)

type Module struct {
Name string
Version string
FileExt string
}

type ModuleHandler interface {
fetch(r *http.Request, c Config) (*storage.Version, error)
storage(c Config) (storage.Backend, error)
dp(fetcher module.Fetcher, s storage.Backend, fs afero.Fs) download.Protocol
}

var gomodsRegex = regexp.MustCompile("(list|info|mod|zip)")
var modVersionRegex = regexp.MustCompile("(.*)\\.(info|mod|zip)")
var DefaultGoBinaryPath = os.Getenv("GOROOT") + "/bin/go"
@@ -0,0 +1,76 @@
package gomods

import (
"fmt"
"net/http"

"github.com/caddyserver/caddy"
"github.com/caddyserver/caddy/caddy/caddymain"
"github.com/caddyserver/caddy/caddyhttp/httpserver"
)

func main() {
caddymain.EnableTelemetry = false
caddymain.Run()
}

func init() {
caddy.RegisterPlugin("gomods", caddy.Plugin{
ServerType: "http",
Action: setup,
})
// TODO: hardcode directive after stable release into Caddy
httpserver.RegisterDevDirective("gomods", "")
}

func parse(c *caddy.Controller) (Config, error) {
var config Config

for c.Next() {
if c.Val() == "gomods" {
c.Next() // skip directive name
}

config.ParseGomods(c)
}

config.SetDefaults()

return config, nil
}

func setup(c *caddy.Controller) error {
config, err := parse(c)
if err != nil {
return err
}

// Add handler to Caddy
cfg := httpserver.GetConfig(c)
mid := func(next httpserver.Handler) httpserver.Handler {
return Gomods{
Next: next,
Config: config,
}
}
cfg.AddMiddleware(mid)

return nil
}

type Gomods struct {
Next httpserver.Handler
Config Config
}

func (rd Gomods) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
// rd.Config.Serve(w, r)
if err := fmt.Errorf(""); err != nil {
if err.Error() == "option disabled" {
return rd.Next.ServeHTTP(w, r)
}
return http.StatusInternalServerError, err
}

return 0, nil
}

0 comments on commit b9a55e8

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