-
Notifications
You must be signed in to change notification settings - Fork 9
/
env.go
106 lines (82 loc) · 2.6 KB
/
env.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package core
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"sync/atomic"
"time"
"google.golang.org/protobuf/encoding/prototext"
"namespacelabs.dev/foundation/framework/runtime"
"namespacelabs.dev/foundation/schema"
runtimepb "namespacelabs.dev/foundation/schema/runtime"
"namespacelabs.dev/foundation/std/core/types"
)
var (
debug = flag.Bool("debug_init", false, "If set to true, emits additional initialization information.")
rt *runtimepb.RuntimeConfig
rtVcs *runtimepb.BuildVCS
serverName string
initialized uint32
)
var Log = log.New(os.Stderr, "[ns] ", log.Ldate|log.Ltime|log.Lmicroseconds)
func PrepareEnv(specifiedServerName string) *ServerResources {
if !atomic.CompareAndSwapUint32(&initialized, 0, 1) {
Log.Fatal("already initialized")
}
Log.Println("Initializing server...")
var err error
rt, err = runtime.LoadRuntimeConfig()
if err != nil {
Log.Fatal(err)
}
rtVcs, err = runtime.LoadBuildVCS()
if err != nil {
Log.Fatal(err)
}
serverName = specifiedServerName
return &ServerResources{startupTime: time.Now()}
}
func ProvideServerInfo(ctx context.Context, _ *types.ServerInfoArgs) (*types.ServerInfo, error) {
return &types.ServerInfo{
ServerName: serverName,
EnvName: rt.Environment.Name,
EnvPurpose: rt.Environment.Purpose,
Vcs: rtVcs,
}, nil
}
func EnvIs(purpose schema.Environment_Purpose) bool {
return rt.Environment.Purpose == purpose.String()
}
type frameworkKey string
const ctxResourcesKey = frameworkKey("ns.serverresources")
func WithResources(ctx context.Context, res *ServerResources) context.Context {
return context.WithValue(ctx, ctxResourcesKey, res)
}
func ServerResourcesFrom(ctx context.Context) *ServerResources {
v := ctx.Value(ctxResourcesKey)
if v == nil {
return nil
}
return v.(*ServerResources)
}
func StatusHandler(registered []string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusOK)
vcsStr, _ := json.Marshal(rtVcs)
fmt.Fprintf(w, "<!doctype html><html><body><pre>%s\nimage_version=%s\n%s\n%s</pre>",
serverName, rt.Current.ImageRef, prototext.Format(rt.Environment), vcsStr)
fmt.Fprintf(w, "<b>Registered endpoints</b></br><ul>")
for _, endpoint := range registered {
fmt.Fprintf(w, "<li><a href=%s>%s</a></li>", endpoint, endpoint)
}
fmt.Fprintf(w, "</ul>")
fmt.Fprintf(w, "</body></html>")
})
}