secretshop/main.go

65 lines
1.8 KiB
Go
Raw Normal View History

2020-02-12 20:13:19 -05:00
package main
import (
"log"
2020-02-19 15:26:46 -05:00
"sync"
2020-02-12 20:13:19 -05:00
2020-02-20 16:20:21 -05:00
"github.com/prologic/go-gopher"
2020-02-12 20:13:19 -05:00
"github.com/spf13/viper"
)
func main() {
viper.SetConfigName("config")
2020-02-12 20:36:05 -05:00
viper.AddConfigPath("/etc/secretshop/")
2020-02-12 20:13:19 -05:00
viper.AddConfigPath(".")
err := viper.ReadInConfig()
2020-02-20 16:20:21 -05:00
if err != nil {
2020-02-12 20:13:19 -05:00
log.Fatalf("Fatal error config file: %v \n", err)
}
viper.SetConfigType("yaml")
2020-02-20 16:20:21 -05:00
//Load configs
2020-02-12 20:13:19 -05:00
active_capsules := viper.GetStringSlice("active_capsules")
2020-02-20 16:20:21 -05:00
active_holes := viper.GetStringSlice("active_holes")
2020-02-22 16:17:38 -05:00
port := viper.GetString("port")
2020-02-20 16:20:21 -05:00
capsule_list := make([]GeminiConfig, len(active_capsules))
hole_list := make([]GopherConfig, len(active_holes))
2020-02-12 20:13:19 -05:00
for i, c := range active_capsules {
viper.UnmarshalKey(c, &(capsule_list[i]))
2020-02-19 15:26:46 -05:00
log.Printf("Loading capsule %v %v", i, capsule_list[i].Hostname)
2020-02-12 20:13:19 -05:00
}
2020-02-20 16:20:21 -05:00
for i, h := range active_holes {
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.")
2020-02-12 20:13:19 -05:00
return
}
2020-02-20 17:03:34 -05:00
log.Printf("%v capsules loaded, %v gopherholes loaded", len(capsule_list), len(hole_list))
2020-02-19 15:26:46 -05:00
// Intialize servers
wg := new(sync.WaitGroup)
2020-02-22 16:17:38 -05:00
wg.Add(1 + len(hole_list))
log.Printf("Starting gemini capsule")
go func(c interface{}) {
log.Fatal(ListenAndServeTLS(port, c.([]GeminiConfig)))
wg.Done()
}(capsule_list)
2020-02-20 16:20:21 -05:00
for i, h := range hole_list {
log.Printf("Starting gopherhole %v %v", i, h.Hostname)
go func(h interface{}) {
hole := h.(GopherConfig)
gopher.Handle("/", index(gopher.Dir(hole.RootDir)))
2020-02-20 17:00:20 -05:00
server := &gopher.Server{Addr: "0.0.0.0:" + hole.Port, Hostname: hole.Hostname, Handler: nil}
log.Fatal(server.ListenAndServe())
2020-02-20 16:20:21 -05:00
wg.Done()
}(h)
}
2020-02-22 16:17:38 -05:00
2020-02-20 17:03:34 -05:00
log.Println("Done bringing up capsules and gopherholes")
log.Println("Ho ho! You found me!")
2020-02-19 15:26:46 -05:00
wg.Wait()
2020-02-12 20:13:19 -05:00
}