-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain.go
112 lines (90 loc) · 2.41 KB
/
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
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
107
108
109
110
111
112
//go:build windows
// +build windows
package main
import (
"crypto/rand"
"errors"
"flag"
"fmt"
"io"
"os"
"os/signal"
"path/filepath"
_ "runtime/cgo"
"strconv"
"syscall"
cfenv "github.com/cloudfoundry-community/go-cfenv"
"code.cloudfoundry.org/hwc/contextpath"
"code.cloudfoundry.org/hwc/hwcconfig"
"code.cloudfoundry.org/hwc/validator"
"code.cloudfoundry.org/hwc/webcore"
)
var appRootPath string
func init() {
flag.StringVar(&appRootPath, "appRootPath", ".", "app web root path")
}
func main() {
flag.Parse()
if os.Getenv("PORT") == "" {
checkErr(errors.New("Missing PORT environment variable"))
}
port, err := strconv.Atoi(os.Getenv("PORT"))
checkErr(err)
rootPath, err := filepath.Abs(appRootPath)
checkErr(err)
if os.Getenv("USERPROFILE") == "" {
checkErr(errors.New("Missing USERPROFILE environment variable"))
}
tmpPath, err := filepath.Abs(filepath.Join(os.Getenv("USERPROFILE"), "tmp"))
checkErr(err)
err = os.MkdirAll(tmpPath, 0700)
checkErr(err)
contextPath := contextpath.Default()
if cfenv.IsRunningOnCF() {
appEnv, err := cfenv.Current()
if err != nil {
checkErr(fmt.Errorf("Getting current CF environment: %v", err))
}
contextPath, err = contextpath.New(appEnv)
if err != nil {
checkErr(fmt.Errorf("Getting CF application context path: %v", err))
}
fmt.Printf("Context Path %s\n", contextPath)
}
uuid, err := generateUUID()
if err != nil {
checkErr(fmt.Errorf("Generating UUID: %v", err))
}
err, config := hwcconfig.New(port, rootPath, tmpPath, contextPath, uuid)
checkErr(err)
err = validator.ValidateWebConfig(filepath.Join(rootPath, "Web.config"), os.Stderr)
checkErr(err)
err, wc := webcore.New()
checkErr(err)
defer syscall.FreeLibrary(wc.Handle)
checkErr(wc.Activate(
config.ApplicationHostConfigPath,
config.WebConfigPath,
config.Instance))
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
checkErr(wc.Shutdown(1, config.Instance))
}
func checkErr(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "\n%s\n", err)
os.Exit(1)
}
}
func generateUUID() (string, error) {
const size = 128 / 8
const format = "%08x-%04x-%04x-%04x-%012x"
var u [size]byte
if _, err := io.ReadFull(rand.Reader, u[0:]); err != nil {
return "", fmt.Errorf("error reading random number generator: %v", err)
}
u[6] = (u[6] & 0x0f) | 0x40
u[8] = (u[8] & 0x3f) | 0x80
return fmt.Sprintf(format, u[:4], u[4:6], u[6:8], u[8:10], u[10:]), nil
}