1
0
Fork 0
mirror of https://github.com/gocsaf/csaf.git synced 2025-12-22 18:15:42 +01:00
gocsaf/cmd/csaf_provider/main.go
Sascha L. Teichmann 7a8cdb6d19
Lift distribution from v2 to v3. (#467)
* v2 -> v3

* Increase version within Makefile

---------

Co-authored-by: JanHoefelmeyer <hoefelmeyer.jan@gmail.com>
2023-09-27 15:46:57 +02:00

68 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 implements the csaf_provider.
package main
import (
"fmt"
"log"
"net/http"
"net/http/cgi"
"os"
"github.com/jessevdk/go-flags"
"github.com/csaf-poc/csaf_distribution/v3/util"
)
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, "Something went wrong. Check server logs for more details",
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)
}
}