echoip/main.go
Martin Polden f4420781a2 Refactor
2016-04-15 20:14:16 +02:00

44 lines
1.2 KiB
Go

package main
import (
flags "github.com/jessevdk/go-flags"
"log"
"os"
"github.com/martinp/ipd/api"
)
func main() {
var opts struct {
DBPath string `short:"f" long:"file" description:"Path to GeoIP database" value-name:"FILE" default:""`
Listen string `short:"l" long:"listen" description:"Listening address" value-name:"ADDR" default:":8080"`
CORS bool `short:"x" long:"cors" description:"Allow requests from other domains"`
ReverseLookup bool `short:"r" long:"reverse-lookup" description:"Perform reverse hostname lookups"`
Template string `short:"t" long:"template" description:"Path to template" default:"index.html"`
}
_, err := flags.ParseArgs(&opts, os.Args)
if err != nil {
os.Exit(1)
}
api := api.New()
api.CORS = opts.CORS
if opts.ReverseLookup {
log.Println("Enabling reverse lookup")
api.EnableReverseLookup()
}
if opts.DBPath != "" {
log.Printf("Enabling country lookup (using database: %s)\n", opts.DBPath)
if err := api.EnableCountryLookup(opts.DBPath); err != nil {
log.Fatal(err)
}
}
api.Template = opts.Template
log.Printf("Listening on %s", opts.Listen)
if err := api.ListenAndServe(opts.Listen); err != nil {
log.Fatal(err)
}
}