preliminary gopher support

This commit is contained in:
Steve 2020-02-20 16:20:21 -05:00
parent 7e15756a68
commit 9d04050913
2 changed files with 37 additions and 14 deletions

49
main.go
View File

@ -5,6 +5,7 @@ import (
"log" "log"
"sync" "sync"
"github.com/prologic/go-gopher"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -13,39 +14,52 @@ func main() {
viper.AddConfigPath("/etc/secretshop/") viper.AddConfigPath("/etc/secretshop/")
viper.AddConfigPath(".") viper.AddConfigPath(".")
err := viper.ReadInConfig() err := viper.ReadInConfig()
if err != nil { // Handle errors reading the config file if err != nil {
log.Fatalf("Fatal error config file: %v \n", err) log.Fatalf("Fatal error config file: %v \n", err)
} }
viper.SetConfigType("yaml") viper.SetConfigType("yaml")
//Load config //Load configs
active_capsules := viper.GetStringSlice("active_capsules") active_capsules := viper.GetStringSlice("active_capsules")
active_holes := viper.GetStringSlice("active_holes")
capsule_list := make([]Config, len(active_capsules)) capsule_list := make([]GeminiConfig, len(active_capsules))
hole_list := make([]GopherConfig, len(active_holes))
for i, c := range active_capsules { for i, c := range active_capsules {
viper.UnmarshalKey(c, &(capsule_list[i])) viper.UnmarshalKey(c, &(capsule_list[i]))
log.Printf("Loading capsule %v %v", i, capsule_list[i].Hostname) log.Printf("Loading capsule %v %v", i, capsule_list[i].Hostname)
} }
if len(capsule_list) < 1 { for i, h := range active_holes {
log.Println("No capsules defined. Shutting down.") viper.UnmarshalKey(h, &(hole_list[i]))
log.Printf("Loading hole %v %v", i, hole_list[i].Hostname)
}
if len(capsule_list) < 1 && len(hole_list) < 1 {
log.Println("No capsules or gopherholes loaded. Shutting down.")
return return
} }
log.Printf("%v capsules loaded", len(capsule_list)) log.Printf("%v capsules loaded, %v holes loaded", len(capsule_list), len(hole_list))
// Intialize servers // Intialize servers
wg := new(sync.WaitGroup) wg := new(sync.WaitGroup)
wg.Add(len(capsule_list)) wg.Add(len(capsule_list) + len(hole_list))
for i, c := range capsule_list { for i, c := range capsule_list {
log.Printf("Starting capsule %v %v", i, c.Hostname) log.Printf("Starting capsule %v %v", i, c.Hostname)
go func(c interface{}) { go func(c interface{}) {
log.Fatal(ListenAndServeTLS(c.(Config))) log.Fatal(ListenAndServeTLS(c.(GeminiConfig)))
wg.Done() wg.Done()
}(c) }(c)
} }
for i, h := range hole_list {
log.Printf("Starting gopherhole %v %v", i, h.Hostname)
go func(h interface{}) {
hole := h.(GopherConfig)
gopher.Handle("/", gopher.FileServer(gopher.Dir(hole.RootDir)))
log.Fatal(gopher.ListenAndServe(hole.Hostname+":"+hole.Port, nil))
wg.Done()
}(h)
}
wg.Wait() wg.Wait()
//log.Fatal(ListenAndServeTLS(capsule_list[0]))
} }
type Config struct { type GeminiConfig struct {
Hostname string Hostname string
Port string Port string
KeyFile string KeyFile string
@ -54,6 +68,15 @@ type Config struct {
CGIDir string CGIDir string
} }
func (c *Config) String() string { type GopherConfig struct {
return fmt.Sprintf("Config: %v:%v Files:%v CGI:%v", c.Hostname, c.Port, c.RootDir, c.CGIDir) Hostname string
Port string
RootDir string
}
func (c *GeminiConfig) String() string {
return fmt.Sprintf("Gemini Config: %v:%v Files:%v CGI:%v", c.Hostname, c.Port, c.RootDir, c.CGIDir)
}
func (c *GopherConfig) String() string {
return fmt.Sprintf("Gopher Config: %v:%v Files:%v", c.Hostname, c.Port, c.RootDir)
} }

View File

@ -81,7 +81,7 @@ func (s *Server) ListenAndServeTLS(certFile, keyFile string) error {
return s.Serve(ln) return s.Serve(ln)
} }
func ListenAndServeTLS(cp Config) error { func ListenAndServeTLS(cp GeminiConfig) error {
server := &Server{Addr: ":" + cp.Port, ServerRoot: cp.RootDir, Hostname: cp.Hostname} server := &Server{Addr: ":" + cp.Port, ServerRoot: cp.RootDir, Hostname: cp.Hostname}
return server.ListenAndServeTLS(cp.CertFile, cp.KeyFile) return server.ListenAndServeTLS(cp.CertFile, cp.KeyFile)
} }