run.go (2180B)
1 package main 2 3 import ( 4 "flag" 5 "fmt" 6 "net/url" 7 "os" 8 "strings" 9 10 "cerca/server" 11 "cerca/util" 12 ) 13 14 func readAllowlist(location string) []string { 15 ed := util.Describe("read allowlist") 16 data, err := os.ReadFile(location) 17 ed.Check(err, "read file") 18 list := strings.Split(strings.TrimSpace(string(data)), "\n") 19 var processed []string 20 for _, fullpath := range list { 21 u, err := url.Parse(fullpath) 22 if err != nil { 23 continue 24 } 25 processed = append(processed, u.Host) 26 } 27 return processed 28 } 29 30 func complain(msg string) { 31 fmt.Printf("cerca: %s\n", msg) 32 os.Exit(0) 33 } 34 35 func main() { 36 // TODO (2022-01-10): somehow continually update veri sites by scraping merveilles webring sites || webring domain 37 var allowlistLocation string 38 var sessionKey string 39 var configPath string 40 var dataDir string 41 var dev bool 42 flag.BoolVar(&dev, "dev", false, "trigger development mode") 43 flag.StringVar(&allowlistLocation, "allowlist", "", "domains which can be used to read verification codes from during registration") 44 flag.StringVar(&sessionKey, "authkey", "", "session cookies authentication key") 45 flag.StringVar(&configPath, "config", "cerca.toml", "config and settings file containing cerca's customizations") 46 flag.StringVar(&dataDir, "data", "./data", "directory where cerca will dump its database") 47 flag.Parse() 48 if len(sessionKey) == 0 { 49 if !dev { 50 complain("please pass a random session auth key with --authkey") 51 } 52 sessionKey = "0" 53 } 54 if len(allowlistLocation) == 0 { 55 if !dev { 56 complain("please pass a file containing the verification code domain allowlist") 57 } 58 allowlistLocation = "temp-allowlist.txt" 59 created, err := util.CreateIfNotExist(allowlistLocation, "") 60 if err != nil { 61 complain(fmt.Sprintf("couldn't create %s: %s", allowlistLocation, err)) 62 } 63 if created { 64 fmt.Println(fmt.Sprintf("Created %s", allowlistLocation)) 65 } 66 } 67 68 err := os.MkdirAll(dataDir, 0750) 69 if err != nil { 70 complain(fmt.Sprintf("couldn't create dir '%s'", dataDir)) 71 } 72 allowlist := readAllowlist(allowlistLocation) 73 allowlist = append(allowlist, "merveilles.town") 74 config := util.ReadConfig(configPath) 75 server.Serve(allowlist, sessionKey, dev, dataDir, config) 76 }