73 lines
1.6 KiB
Go
73 lines
1.6 KiB
Go
|
/*
|
||
|
* Copyright IDMesh Authors
|
||
|
*
|
||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
* you may not use this file except in compliance with the License.
|
||
|
* You may obtain a copy of the License at
|
||
|
*
|
||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
|
*
|
||
|
* Unless required by applicable law or agreed to in writing, software
|
||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
* See the License for the specific language governing permissions and
|
||
|
* limitations under the License.
|
||
|
*/
|
||
|
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"net/http"
|
||
|
"os"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
ServeAddr = ":80"
|
||
|
|
||
|
EnvKeyConfigFilePath = "CONFIG_FILE_PATH"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
config, err := loadConfig()
|
||
|
if err != nil {
|
||
|
log.Fatalf("fail to load config: %v", err)
|
||
|
}
|
||
|
|
||
|
server, err := NewRelayServer(config)
|
||
|
if err != nil {
|
||
|
log.Fatalf("fail to make server: %v", err)
|
||
|
}
|
||
|
|
||
|
log.Println("app serve at:", ServeAddr)
|
||
|
|
||
|
err = http.ListenAndServe(ServeAddr, server)
|
||
|
if err != nil {
|
||
|
if errors.Is(err, http.ErrServerClosed) {
|
||
|
return
|
||
|
}
|
||
|
log.Fatalf("fail to serve: %v", err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func loadConfig() (cfg Config, err error) {
|
||
|
configFilePath := os.Getenv(EnvKeyConfigFilePath)
|
||
|
if configFilePath == "" {
|
||
|
configFilePath = "/data/idmesh/settings.json"
|
||
|
}
|
||
|
|
||
|
raw, err := os.ReadFile(configFilePath)
|
||
|
if err != nil {
|
||
|
return Config{}, fmt.Errorf("fail to read config file: %v", err)
|
||
|
}
|
||
|
|
||
|
err = json.Unmarshal(raw, &cfg)
|
||
|
if err != nil {
|
||
|
return Config{}, fmt.Errorf("fail to decode config file: %v", err)
|
||
|
}
|
||
|
return
|
||
|
}
|