-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
44 lines (36 loc) · 845 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
/*
#include <stdlib.h>
#include "formatsql.h"
*/
import "C"
import (
"fmt"
"log"
"net/http"
"os"
"unsafe"
"io/ioutil"
)
func handler(w http.ResponseWriter, r *http.Request) {
log.Print("zetasql-server received a request.")
b, err := ioutil.ReadAll(r.Body)
if err != nil || len(b) == 0 {
w.WriteHeader(http.StatusBadRequest)
return
}
cs := C.CString(string(b))
defer C.free(unsafe.Pointer(cs))
formatResult := C.formatSqlC(cs)
defer C.free(unsafe.Pointer(formatResult))
w.Write([]byte(C.GoString(formatResult)))
}
func main() {
log.Print("zetasql-server started.")
http.HandleFunc("/", handler)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}