From e449aa01a9ceb45cefb17ec2ee4af2606512fd9a Mon Sep 17 00:00:00 2001 From: Amrit Panesar Date: Fri, 29 Dec 2017 00:47:54 -0800 Subject: [PATCH] new feature - include an embedded http server for uploaded code --- fiche.go | 20 +++++++++++++++++++- http.go | 30 ++++++++++++++++++++++++++++++ init.go | 2 ++ main.go | 21 ++++++--------------- 4 files changed, 57 insertions(+), 16 deletions(-) create mode 100644 http.go diff --git a/fiche.go b/fiche.go index 43e9f79..d118ce5 100644 --- a/fiche.go +++ b/fiche.go @@ -12,6 +12,24 @@ import ( "github.com/spf13/viper" ) +func ficheInit() { + listener, err := net.Listen("tcp", fmt.Sprintf(":%d", viper.Get("port"))) + if err != nil { + // handle error + log.Fatalf("Could not bind to port: %d!", viper.Get("port")) + os.Exit(-1) + } + log.Printf("Server started listening on port: %d.", viper.Get("port")) + for { + conn, err := listener.Accept() + if err != nil { + log.Printf("Error on accepting connection!") + continue + } + go fiche(conn) + } +} + func fiche(conn net.Conn) { remoteHost, err := net.LookupCNAME(conn.RemoteAddr().String()) if err != nil { @@ -30,7 +48,7 @@ func fiche(conn net.Conn) { file, err := os.Create(slugFullpath) if err != nil { - conn.Write([]byte(fmt.Sprintf("%s", "Internal error encountered - Please try again later...\n\n"))) + conn.Write([]byte(fmt.Sprintf("%s", "Internal server error - Please try again later...\n\n"))) log.Fatalf("Unable to create slug file: %s", slugFullpath) return } diff --git a/http.go b/http.go new file mode 100644 index 0000000..f9764b5 --- /dev/null +++ b/http.go @@ -0,0 +1,30 @@ +package main + +import ( + "fmt" + "log" + "net/http" + "os" + "strings" + + "github.com/gorilla/handlers" + "github.com/spf13/viper" +) + +func serveHttp() { + files := http.FileServer(http.Dir(viper.GetString("output"))) + http.Handle("/", disableDirectoryListing(files)) + + log.Printf("Starting embedded http server on port %d\n", viper.GetInt("httpport")) + http.ListenAndServe(fmt.Sprintf(":%d", viper.GetInt("httpport")), handlers.LoggingHandler(os.Stdout, http.DefaultServeMux)) +} + +func disableDirectoryListing(h http.Handler) http.HandlerFunc { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if strings.HasSuffix(r.URL.Path, "/") { + http.NotFound(w, r) + return + } + h.ServeHTTP(w, r) + }) +} diff --git a/init.go b/init.go index 7d227ae..f3c3a02 100644 --- a/init.go +++ b/init.go @@ -33,6 +33,8 @@ func init() { pflag.BoolP("https", "S", false, fmt.Sprintf("If set, %s returns url with https prefix instead of http.", AppName)) pflag.IntP("buffer", "B", 32768, "This parameter defines size of the buffer used for getting data from the user. Maximum size (in bytes) of all input files is defined by this value.") pflag.StringP("log", "l", "", "Log file. This file has to be user-writable.") + pflag.BoolP("http", "H", false, "Enable the embedded http daemon.") + pflag.IntP("httpport", "P", 9989, "Sets the port for the embedded http daemon.") pflag.Parse() viper.BindPFlags(pflag.CommandLine) diff --git a/main.go b/main.go index 6824860..97ce7c5 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,7 @@ package main import ( - "fmt" "log" - "net" - "os" "time" "github.com/spf13/viper" @@ -15,19 +12,13 @@ const AppName = "Go-Fiche" func main() { log.Printf("Starting %s on %s...", AppName, time.Now().Format(time.RFC822Z)) - listener, err := net.Listen("tcp", fmt.Sprintf(":%d", viper.Get("port"))) - if err != nil { - // handle error - log.Fatalf("Could not bind to port: %d!", viper.Get("port")) - os.Exit(-1) + go ficheInit() + + if viper.GetBool("http") { + go serveHttp() } - log.Printf("Server started listening on port: %d.", viper.Get("port")) + for { - conn, err := listener.Accept() - if err != nil { - log.Printf("Error on accepting connection!") - continue - } - go fiche(conn) + time.Sleep(time.Minute) } }