netoik-api/cmd/server/main.go

36 lines
784 B
Go

package main
import (
"flag"
"fmt"
"net/http"
"netoik.io/netoik-api/pkg/captcha"
"netoik.io/netoik-api/pkg/conf"
"netoik.io/netoik-api/pkg/contact"
"os"
)
func main() {
// Parse command line arguments
path := flag.String("c", "server.conf", "Config file")
flag.Parse()
// Parse config file
if !conf.ParseFile(*path) {
os.Exit(1)
}
// Setup captcha
captcha.Setup()
// Declare api routes
http.HandleFunc("/api/contact/send", contact.HandleSend)
http.HandleFunc("/api/captcha/new", captcha.HandleNew)
// Start listening
if err := http.ListenAndServe(fmt.Sprintf("%s:%d", conf.Conf.BindHost, conf.Conf.BindPort), nil); err != nil {
fmt.Fprintf(os.Stderr, "cannot listen at %s:%d: %s\n", conf.Conf.BindHost, conf.Conf.BindPort, err.Error())
os.Exit(1)
}
}