-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.go
81 lines (68 loc) · 1.64 KB
/
app.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux"
)
func badRequest(writer http.ResponseWriter, msg string) {
http.Error(writer, msg, http.StatusBadRequest)
log.Error(msg)
}
func errored(writer http.ResponseWriter, msg string) {
http.Error(writer, msg, http.StatusInternalServerError)
log.Error(msg)
}
func notFound(writer http.ResponseWriter, msg string) {
http.Error(writer, msg, http.StatusNotFound)
log.Error(msg)
}
func handleNonUser(writer http.ResponseWriter, username string) {
var (
retval []byte
err error
)
retval, err = json.Marshal(map[string]string{
"user": username,
})
if err != nil {
errored(writer, fmt.Sprintf("Error generating json for non-user %s", err))
return
}
notFound(writer, string(retval))
}
func fixAddr(addr string) string {
if !strings.HasPrefix(addr, ":") {
return fmt.Sprintf(":%s", addr)
}
return addr
}
var (
gitref string
appver string
builtby string
)
// AppVersion prints the version information to stdout
func AppVersion() {
if appver != "" {
fmt.Printf("App-Version: %s\n", appver)
}
if gitref != "" {
fmt.Printf("Git-Ref: %s\n", gitref)
}
if builtby != "" {
fmt.Printf("Built-By: %s\n", builtby)
}
}
func makeRouter() *mux.Router {
router := mux.NewRouter()
router.Use(otelmux.Middleware(serviceName))
router.Handle("/debug/vars", http.DefaultServeMux)
router.HandleFunc("/", func(writer http.ResponseWriter, r *http.Request) {
fmt.Fprintf(writer, "Hello from user-info.\n")
}).Methods("GET")
return router
}