-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfoNode.go
More file actions
121 lines (110 loc) · 3.13 KB
/
Copy pathinfoNode.go
File metadata and controls
121 lines (110 loc) · 3.13 KB
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
// Copyright (c) 2026 Golodnikov Sergey. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package main
// todo: self update
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"GS90/infoNode/core/conf"
"GS90/infoNode/core/handler"
"GS90/infoNode/core/inspector"
"GS90/infoNode/core/logger"
"GS90/infoNode/core/reader"
)
const info string = `
infoNode, version 1.0
https://digiroad.tech
infoNode - hybrid system for creating a website
free software; Copyright (c) 2026 Golodnikov Sergey
`
func init() {
if path, err := os.Executable(); err != nil {
log.Fatalln("panic: working dir:", err)
} else if err = os.Chdir(filepath.Dir(path)); err != nil {
log.Fatalln("panic: working dir:", err)
}
}
func main() {
var (
c = flag.String("conf", "infoNode.conf", "system configuration file")
i = flag.Bool("info", false, "print information about infoNode")
)
flag.Parse()
if *i {
fmt.Print(info, "\n")
os.Exit(0)
}
setup(*c)
startup(*c)
}
// -----------------------------------------------------------------------------
func setup(c string) {
if filepath.Ext(c) != conf.FileExtCONF {
log.Printf("panic: system .conf file: %s: invalid extension\n", c)
log.Fatalf("-----> valid file extension is '%s'\n", conf.FileExtCONF)
}
info, err := os.Stat(c)
if err != nil {
log.Println("warning: system .conf file:", err)
return
} else if info.IsDir() {
log.Fatalf("panic: system .conf file: %s: not a file\n", c)
}
set, err := reader.ReadConf(c)
if err != nil {
log.Fatalln("panic: system .conf file:", err)
}
// configuration:
conf.Path = filepath.Dir(c)
session := strings.TrimSpace(
strings.TrimSuffix(filepath.Base(c), conf.FileExtCONF))
errors := conf.C.Fill(session, set)
if len(errors) != 0 {
for i := range errors {
log.Println(errors[i])
}
// for a safe start:
log.Fatalf("panic: system .conf file: %s: invalid file\n", c)
}
conf.C.Timestamp = info.ModTime().Unix()
}
func startup(c string) {
logger.Init()
logger.L.System.Println("start: session name:", conf.C.Session)
go inspector.Launch(c)
// handlers:
http.Handle("/res/", http.StripPrefix("/res/",
handler.FileServer(http.Dir(conf.PathResources), false)))
http.Handle("/add/", http.StripPrefix("/add/",
handler.FileServer(http.Dir(conf.C.Resources), true)))
http.HandleFunc("/favicon.ico", handler.Favicon)
http.HandleFunc("/", handler.Main)
// server:
const format = "panic: %s server: %v\n"
switch conf.C.Protocol {
case "https":
go func() {
if err := http.ListenAndServe(conf.C.Address,
http.HandlerFunc(handler.RedirectToHTTPS)); err != nil {
logger.L.System.Printf(format, "'http' slave", err)
log.Fatalf(format, "'http' slave", err)
}
}()
if err := http.ListenAndServeTLS(conf.C.AddressTLS,
conf.C.CertFile, conf.C.KeyFile, nil); err != nil {
logger.L.System.Printf(format, "'https' master", err)
log.Fatalf(format, "'https' master", err)
}
default:
if err := http.ListenAndServe(conf.C.Address, nil); err != nil {
logger.L.System.Printf(format, "'http' master", err)
log.Fatalf(format, "'http' master", err)
}
}
}