1
0
Fork 0
mirror of https://github.com/gocsaf/csaf.git synced 2025-12-23 00:32:55 +01:00
gocsaf/cmd/csaf_provider/main.go
JanHoefelmeyer 38d3679704
Add hint if provider is called outside of CGI
Co-authored-by: Sascha L. Teichmann <sascha.teichmann@intevation.de>
2022-06-23 17:27:25 +02:00

65 lines
1.5 KiB
Go

// This file is Free Software under the MIT License
// without warranty, see README.md and LICENSES/MIT.txt for details.
//
// SPDX-License-Identifier: MIT
//
// SPDX-FileCopyrightText: 2021 German Federal Office for Information Security (BSI) <https://www.bsi.bund.de>
// Software-Engineering: 2021 Intevation GmbH <https://intevation.de>
package main
import (
"fmt"
"log"
"net/http"
"net/http/cgi"
"os"
"github.com/csaf-poc/csaf_distribution/util"
"github.com/jessevdk/go-flags"
)
type options struct {
Version bool `long:"version" description:"Display version of the binary"`
}
const cgiRequired = "The csaf_provider is a cgi binary and is designed to be served via a web server."
func ensureCGI() {
if _, ok := os.LookupEnv("REQUEST_METHOD"); !ok {
fmt.Println(cgiRequired)
fmt.Println("Version: " + util.SemVersion)
os.Exit(1)
}
}
func main() {
var opts options
parser := flags.NewParser(&opts, flags.Default)
parser.Parse()
if opts.Version {
fmt.Println(util.SemVersion)
return
}
ensureCGI()
cfg, err := loadConfig()
if err != nil {
cgi.Serve(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
http.Error(rw, fmt.Sprintf("Config error: %v\n", err), http.StatusInternalServerError)
}))
log.Fatalf("error: %v\n", err)
}
c, err := newController(cfg)
if err != nil {
log.Fatalf("error: %v\n", err)
}
pim := newPathInfoMux()
c.bind(pim)
if err := cgi.Serve(pim); err != nil {
log.Fatalf("error: %v\n", err)
}
}