echoip/main.go

49 lines
1.4 KiB
Go
Raw Normal View History

2015-09-17 20:57:27 +02:00
package main
import (
flags "github.com/jessevdk/go-flags"
"log"
"os"
2016-04-15 17:59:56 +02:00
"github.com/martinp/ipd/api"
2015-09-17 20:57:27 +02:00
)
func main() {
var opts struct {
2015-09-21 18:03:52 +02:00
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"`
2016-03-06 07:10:58 +01:00
CORS bool `short:"x" long:"cors" description:"Allow requests from other domains"`
2016-04-15 20:14:16 +02:00
ReverseLookup bool `short:"r" long:"reverse-lookup" description:"Perform reverse hostname lookups"`
2016-04-15 20:52:15 +02:00
PortTesting bool `short:"p" long:"port-testing" description:"Enable port testing"`
2015-09-21 18:03:52 +02:00
Template string `short:"t" long:"template" description:"Path to template" default:"index.html"`
2015-09-17 20:57:27 +02:00
}
_, err := flags.ParseArgs(&opts, os.Args)
if err != nil {
2016-04-15 20:14:16 +02:00
os.Exit(1)
2015-09-17 20:57:27 +02:00
}
2016-04-15 20:14:16 +02:00
api := api.New()
api.CORS = opts.CORS
if opts.ReverseLookup {
log.Println("Enabling reverse lookup")
api.EnableReverseLookup()
}
2016-04-15 20:52:15 +02:00
if opts.PortTesting {
log.Println("Enabling port testing")
api.EnablePortTesting()
}
2016-04-15 20:14:16 +02:00
if opts.DBPath != "" {
log.Printf("Enabling country lookup (using database: %s)\n", opts.DBPath)
if err := api.EnableCountryLookup(opts.DBPath); err != nil {
2015-09-17 20:57:27 +02:00
log.Fatal(err)
}
}
2016-04-15 20:14:16 +02:00
api.Template = opts.Template
2015-09-17 20:57:27 +02:00
2015-09-18 17:13:14 +02:00
log.Printf("Listening on %s", opts.Listen)
2016-04-15 20:14:16 +02:00
if err := api.ListenAndServe(opts.Listen); err != nil {
2015-09-17 20:57:27 +02:00
log.Fatal(err)
}
}