func main() { // Run HTTP server fmt.Println("Domain: " + config.String("server.domain")) fmt.Println("Start rotator server: " + *flagHost) if *flagFastCGI { fmt.Println("FastCGI Mode: ON") } if *flagDebug { fmt.Println("Debug Mode: ON") } // Run server if *flagFastCGI { if tcp, err := net.Listen("tcp", *flagHost); nil == err { fcgi.Serve(tcp, router.NewRouter(*flagDebug)) } else if err != nil { log.Fatal(err) } } else { var err error http.Handle("/", router.NewRouter(*flagDebug)) http.Handle("/public/", http.FileServer(http.Dir(*flagBaseDir))) if err = http.ListenAndServe(*flagHost, nil); nil != err { log.Fatal(err) } } }
func NewRouter(debug bool) http.Handler { static := http.StripPrefix("/public/", http.FileServer(http.Dir(config.String("path")+"/public/"))) r := martini.Classic() r.Get("/", templates.HttpHandler(nil, views.Index)) r.Post("/", templates.HttpHandler(nil, views.Subscribe)) r.Any("/public/**", func(w http.ResponseWriter, r *http.Request) { static.ServeHTTP(w, r) }) r.NotFound(NotFoundHandler) return r }
func Subscribe(w http.ResponseWriter, r *http.Request) *templates.HttpResponse { writeLock.Lock() defer writeLock.Unlock() params := map[string]interface{}{} good := false if f, err := os.OpenFile(config.String("storage.file"), os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666); nil == err { r.ParseForm() if email := strings.TrimSpace(r.Form.Get("email")); len(email) > 0 { _, err := f.WriteString(email + "\n") good = nil == err } f.Close() } if good { params["good"] = true } else { params["error"] = true } return templates.Response(200, "index.html", params) }