Skip to content

Commit

Permalink
automatic cgi ws file downloads - temporary hard code
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessandro-Salerno committed Oct 16, 2024
1 parent 1d1f9a0 commit e1835be
Showing 1 changed file with 69 additions and 11 deletions.
80 changes: 69 additions & 11 deletions cgi-ws/cgi-ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,46 @@ import (
"net/http"
"net/http/cgi"
"os"
"os/exec"
"bufio"
"strings"
"io"
)

func download(filepath string, url string) {
fmt.Print("Downloading <")
fmt.Print(url)
fmt.Print("> to ")
fmt.Print(filepath)
fmt.Print(" ... ")

out, err := os.Create(filepath)
if err != nil {
fmt.Println("IO Error")
return
}
defer out.Close()

resp, err := http.Get(url)
if err != nil {
fmt.Println("Request Error")
return
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
fmt.Println("Request Status Error")
return
}

_, err = io.Copy(out, resp.Body)
if err != nil {
fmt.Println("IO Error")
return
}
fmt.Println("Done!")
}

func htmcCGI(w http.ResponseWriter, r *http.Request) {
handler := cgi.Handler{Path: "./bin/htmc", Dir: "./", Args: []string{"-ll", "off", "-ns"}}
handler.ServeHTTP(w, r)
Expand Down Expand Up @@ -56,25 +94,45 @@ func main() {
}
}

if !exists("./htdocs") {
err := os.MkdirAll("./htdocs", 0755)
if !exists("./include/libhtmc") {
err := os.MkdirAll("./include/libhtmc", 0755)
if err != nil {
fmt.Println("Unable to create htdocs/ directory")
fmt.Println("Unable to create include/libhtmc/ directory")
return
}
}

if (!exists("./bin/htmc") && !exists("./bin/htmc.exe")) || !exists("./bin/libhtmc.a") || !exists("./include/libhtmc/libhtmc.h") {
fmt.Println("You're missing some important htmc files:")
fmt.Println("\t1. Go to <https://github.com/Alessandro-Salerno/htmc> and download the latest release files")
fmt.Println("\t2. Place the htmc executable (htmc or htmc.exe) in the bin/ directory")
fmt.Println("\t3. Place the libhtmc.a file in the bin/ directory")
fmt.Println("\t4. Copy the include/ directory in the working directory")
fmt.Println("\t5. Make sure that include/libhtmc/libhtmc.h exists and is the libhtmc header file")
fmt.Println("\t6. Make sure that GCC and GNU ld are installed")
return
fmt.Print("You're missing some important htmc files, proceed with the downlaod? [Y/n]: ")
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
text = strings.Replace(text, "\n", "", -1)

if (len(text) != 0 && text != "Y") {
fmt.Println("\t1. Go to <https://github.com/Alessandro-Salerno/htmc> and download the latest release or latest-linux-bundle branch files")
fmt.Println("\t2. Place the htmc executable (htmc or htmc.exe) in the bin/ directory")
fmt.Println("\t3. Place the libhtmc.a file in the bin/ directory")
fmt.Println("\t4. Copy the include/ directory in the working directory")
fmt.Println("\t5. Make sure that include/libhtmc/libhtmc.h exists and is the libhtmc header file")
fmt.Println("\t6. Make sure that GCC and GNU ld are installed")
return
}

download("./bin/htmc", "https://alessandro-salerno.github.io/htmc/bin/htmc")
download("./bin/libhtmc.a", "https://alessandro-salerno.github.io/htmc/bin/libhtmc.a")
download("./include/libhtmc/libhtmc.h", "https://alessandro-salerno.github.io/htmc/include/libhtmc/libhtmc.h")
download("./index.htmc", "https://alessandro-salerno.github.io/htmc/examples/index.htmc")

// Temporary hard code
if exec.Command("chmod", "+x", "./bin/htmc").Run() != nil {
fmt.Println("Error while setting permissions for ./bin/htmc")
return
}
}

fmt.Println("Listening on localhost:80")
fmt.Println()

http.HandleFunc("/", htmcCGI)
http.ListenAndServe("localhost:80", nil)
}

0 comments on commit e1835be

Please sign in to comment.