diff options
Diffstat (limited to 'main.go')
| -rw-r--r-- | main.go | 66 |
1 files changed, 66 insertions, 0 deletions
@@ -0,0 +1,66 @@ +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, + }) + + log.Println(fmt.Sprintf("serving on http://%s\n", "127.0.0.1:8080")) + log.Panicln(http.ListenAndServe("127.0.0.1:8080", nil)) +} |
