Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
66 lines (57 loc) · 1.56 KB
/
Copy pathmain.go
File metadata and controls
66 lines (57 loc) · 1.56 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Command sqltable serves a web editor that turns spreadsheet-like data
// into a text table formatted like the mysql command-line client output.
package main
import (
"context"
"embed"
"errors"
"flag"
"fmt"
"io/fs"
"log"
"net/http"
"os/signal"
"syscall"
"time"
)
//go:embed web
var webFS embed.FS
func main() {
addr := flag.String("addr", "127.0.0.1:8080", "address for the HTTP server to listen on")
showVersion := flag.Bool("version", false, "print the version and exit")
flag.Parse()
if *showVersion {
v, c, d := buildInfo()
fmt.Printf("sql-table-generator %s\n commit : %s\n built : %s\n", v, c, d)
return
}
static, err := fs.Sub(webFS, "web")
if err != nil {
log.Fatal(err)
}
mux := http.NewServeMux()
mux.Handle("GET /", http.FileServerFS(static))
mux.HandleFunc("POST /api/render", handleRender)
mux.HandleFunc("POST /api/parse", handleParse)
mux.HandleFunc("POST /api/upload", handleUpload)
srv := &http.Server{
Addr: *addr,
Handler: mux,
ReadHeaderTimeout: 5 * time.Second,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
}
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
go func() {
<-ctx.Done()
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_ = srv.Shutdown(shutdownCtx)
}()
v, _, _ := buildInfo()
log.Printf("SQL Table Generator %s: http://%s", v, *addr)
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal(err)
}
}