Created
June 26, 2017 17:40
-
-
Save tamanobi/d10c932eebbd8c4c5ee0ca6f5143f3e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net/http" | |
"os" | |
"os/exec" | |
"io" | |
"io/ioutil" | |
"github.com/zenazn/goji" | |
"github.com/zenazn/goji/web" | |
) | |
func ngtAppend(c web.C, w http.ResponseWriter, r *http.Request) { | |
r.ParseMultipartForm(32 << 20) | |
file, _, err := r.FormFile("uploadfile") | |
if err != nil { | |
panic(err) | |
} | |
defer file.Close() | |
fp, err := ioutil.TempFile("", "temp") | |
if err != nil { | |
panic(err) | |
} | |
defer fp.Close() | |
io.Copy(fp, file) | |
out, err := exec.Command("ngt", "append", "index", fp.Name()).Output() | |
if err != nil { | |
fmt.Fprintf(w, "") | |
} else { | |
fmt.Fprintf(w, "%s", out) | |
} | |
os.Remove(fp.Name()) | |
} | |
func ngtCreate(c web.C, w http.ResponseWriter, r *http.Request) { | |
r.ParseMultipartForm(32 << 20) | |
file, _, err := r.FormFile("uploadfile") | |
if err != nil { | |
panic(err) | |
} | |
defer file.Close() | |
fp, err := ioutil.TempFile("", "temp") | |
if err != nil { | |
panic(err) | |
} | |
defer fp.Close() | |
io.Copy(fp, file) | |
out, err := exec.Command("ngt", "create", "-d", "9427", "-o", "f", "index", fp.Name()).Output() | |
if err != nil { | |
fmt.Fprintf(w, "yabai") | |
} else { | |
fmt.Fprintf(w, "%s", out) | |
} | |
os.Remove(fp.Name()) | |
} | |
func ngtSearch(c web.C, w http.ResponseWriter, r *http.Request) { | |
r.ParseMultipartForm(32 << 20) | |
file, _, err := r.FormFile("uploadfile") | |
if err != nil { | |
panic(err) | |
} | |
defer file.Close() | |
fp, err := ioutil.TempFile("", "temp") | |
if err != nil { | |
panic(err) | |
} | |
defer fp.Close() | |
io.Copy(fp, file) | |
out, err := exec.Command("ngt", "search", "-o", "j", "-n", "20", "index", fp.Name()).Output() | |
if err != nil { | |
fmt.Fprintf(w, "") | |
} else { | |
fmt.Fprintf(w, "%s", out) | |
} | |
os.Remove(fp.Name()) | |
} | |
func main() { | |
goji.Post("/create", ngtCreate) | |
goji.Post("/append", ngtAppend) | |
goji.Post("/search", ngtSearch) | |
goji.Serve() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment