package main import ( "flag" "fmt" "log" "net/http" "runtime/debug" "drop.janw.name/controller" ) var CONFIG_FILENAME string func registerHandler(pattern string, controller controller.Controller) { http.HandleFunc(pattern, func(res http.ResponseWriter, req *http.Request) { defer func() { err := recover() if err != nil { res.WriteHeader(http.StatusInternalServerError) fmt.Fprintln(res, "internal server error") fmt.Fprintln(res, err) fmt.Fprintln(res, string(debug.Stack())) } }() var methodFunc http.HandlerFunc switch req.Method { case http.MethodGet: methodFunc = controller.GET case http.MethodPost: methodFunc = controller.POST } log.Println(req.Method, req.RemoteAddr, req.RequestURI) if methodFunc != nil { methodFunc(res, req) } else { res.WriteHeader(http.StatusMethodNotAllowed) fmt.Fprintln(res, "method not allowed") } }) } func init() { flag.StringVar(&CONFIG_FILENAME, "c", "config_example.json", "path to the configuration file") flag.Parse() } func main() { app := controller.NewApp(CONFIG_FILENAME) registerHandler("/", controller.Controller{ GET: app.Index, POST: app.FilePost, }) registerHandler("/{file}", controller.Controller{ GET: app.FileGet, }) app.StartScheduler() host := app.Config().Http.ListenAddress log.Println(fmt.Sprintf("serving on http://%s\n", host)) log.Panicln(http.ListenAndServe(host, nil)) }