-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
48 lines (43 loc) · 984 Bytes
/
main.go
File metadata and controls
48 lines (43 loc) · 984 Bytes
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
package main
import (
"context"
"fmt"
"net/http"
"os"
"github.com/jackc/pgx/v4"
)
func main() {
mux := http.NewServeMux()
srv := &http.Server{Addr: ":" + os.Getenv("ABX_PORT"), Handler: mux}
mux.HandleFunc("/", index)
err := srv.ListenAndServe()
if err != http.ErrServerClosed {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
}
func index(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
s, err := query()
if err != nil {
s = err.Error()
w.WriteHeader(http.StatusInternalServerError)
}
fmt.Fprintf(w, "%s\n", s)
}
func query() (string, error) {
var s string
ctx := context.Background()
dsn := os.Getenv("ABX_STORE_DSN")
conn, err := pgx.Connect(ctx, dsn)
if err != nil {
return "", err
}
defer conn.Close(ctx)
name := os.Getenv("ABX_NAME")
err = conn.QueryRow(ctx, "SELECT $1", name).Scan(&s)
if err != nil {
return "", err
}
return s, nil
}