-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
125 lines (108 loc) · 3 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
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"embed"
"fmt"
"html/template"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
// "github.com/aws/aws-sdk-go-v2/aws"
// "github.com/aws/aws-sdk-go-v2/service/s3"
)
var (
//go:embed all:templates/*
templateFS embed.FS
//go:embed css/output.css
css embed.FS
//parsed templates
html *template.Template
)
func main() {
handleSigTerms()
var err error
html, err = TemplateParseFSRecursive(templateFS, ".html", true, nil)
if err != nil {
log.Fatal(err, "failed to parse templates")
}
router := http.NewServeMux()
router.Handle("/css/output.css", http.FileServer(http.FS(css)))
router.Handle("/company/add", Action(companyAdd))
router.Handle("/company/add/", Action(companyAdd))
router.Handle("/company/edit", Action(companyEdit))
router.Handle("/company/edit/", Action(companyEdit))
router.Handle("/company", Action(companies))
router.Handle("/company/", Action(companies))
router.Handle("/", Action(index))
router.Handle("/index.html", Action(index))
//logging/tracing
var f *os.File
log_dir := os.Getenv("LOG_DIR")
if log_dir != "" {
if _, err := os.Stat(log_dir); os.IsNotExist(err) {
err = os.Mkdir(log_dir, 0755)
if err != nil {
log.Fatalf("error creating log dir: %v", err)
}
}
logpath := getLogFilePath(log_dir)
f, err = os.OpenFile(logpath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening log file: %v", err)
}
defer f.Close()
} else {
f = os.Stdout
}
nextRequestID := func() string {
return fmt.Sprintf("%d", time.Now().UnixNano())
}
logger := log.New(f, "http: ", log.LstdFlags)
middleware := tracing(nextRequestID)(logging(logger)(router))
port := os.Getenv("PORT")
if port == "" {
port = "80"
}
logger.Println("listening on port " + port)
if err := http.ListenAndServe(":"+port, middleware); err != nil {
logger.Println("http.ListenAndServe():", err)
os.Exit(1)
}
}
// exit process immediately upon sigterm
func handleSigTerms() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
fmt.Println("received SIGTERM, exiting")
os.Exit(1)
}()
}
// cfg, err := config.LoadDefaultConfig(context.TODO())
// if err != nil {
// log.Fatal(err)
// }
// s3client := s3.NewFromConfig(cfg)
// logfilename := filepath.Base(logpath)
// uploadFile(s3client, logpath, "silas-s3-bucket", fmt.Sprintf("application-logs/%s", logfilename))
// func uploadFile(s3Client *s3.Client, fileName string, bucketName string, objectKey string) error {
// file, err := os.Open(fileName)
// if err != nil {
// log.Printf("Couldn't open file %v to upload. Here's why: %v\n", fileName, err)
// } else {
// defer file.Close()
// _, err = s3Client.PutObject(context.TODO(), &s3.PutObjectInput{
// Bucket: aws.String(bucketName),
// Key: aws.String(objectKey),
// Body: file,
// })
// if err != nil {
// log.Printf("Couldn't upload file %v to %v:%v. Here's why: %v\n",
// fileName, bucketName, objectKey, err)
// }
// }
// return err
// }