new feature - include an embedded http server for uploaded code
This commit is contained in:
1 parent
a28822f121
commit
e449aa01a9
4 files changed
+57
-16
No files matched your search
@@ -12,6 +12,24 @@ import (
|
|||||||
"github.com/spf13/viper"
|
"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) {
|
func fiche(conn net.Conn) {
|
||||||
remoteHost, err := net.LookupCNAME(conn.RemoteAddr().String())
|
remoteHost, err := net.LookupCNAME(conn.RemoteAddr().String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -30,7 +48,7 @@ func fiche(conn net.Conn) {
|
|||||||
|
|
||||||
file, err := os.Create(slugFullpath)
|
file, err := os.Create(slugFullpath)
|
||||||
if err != nil {
|
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)
|
log.Fatalf("Unable to create slug file: %s", slugFullpath)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -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.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.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.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()
|
pflag.Parse()
|
||||||
viper.BindPFlags(pflag.CommandLine)
|
viper.BindPFlags(pflag.CommandLine)
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
"net"
|
|
||||||
"os"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
@@ -15,19 +12,13 @@ const AppName = "Go-Fiche"
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log.Printf("Starting %s on %s...", AppName, time.Now().Format(time.RFC822Z))
|
log.Printf("Starting %s on %s...", AppName, time.Now().Format(time.RFC822Z))
|
||||||
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", viper.Get("port")))
|
go ficheInit()
|
||||||
if err != nil {
|
|
||||||
// handle error
|
if viper.GetBool("http") {
|
||||||
log.Fatalf("Could not bind to port: %d!", viper.Get("port"))
|
go serveHttp()
|
||||||
os.Exit(-1)
|
|
||||||
}
|
}
|
||||||
log.Printf("Server started listening on port: %d.", viper.Get("port"))
|
|
||||||
for {
|
for {
|
||||||
conn, err := listener.Accept()
|
time.Sleep(time.Minute)
|
||||||
if err != nil {
|
|
||||||
log.Printf("Error on accepting connection!")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
go fiche(conn)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user