package main

import (
	"flag"
	"fmt"
	"net/http"
	"netoik.io/netoik-website/pkg/captcha"
	"netoik.io/netoik-website/pkg/conf"
	"netoik.io/netoik-website/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)
	}
}
