2021-03-04 17:41:44 +03:00
|
|
|
// Copyright 2020 Security Scorecard 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.
|
|
|
|
|
2021-03-02 05:00:01 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-03-04 17:41:44 +03:00
|
|
|
"encoding/json"
|
2021-03-02 05:00:01 +03:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2021-03-04 17:41:44 +03:00
|
|
|
"net/http"
|
2021-03-02 05:00:01 +03:00
|
|
|
"os"
|
|
|
|
|
2021-03-04 17:41:44 +03:00
|
|
|
"github.com/ossf/scorecard/gitcache/pkg"
|
2021-03-04 06:22:17 +03:00
|
|
|
"go.uber.org/zap"
|
2021-03-02 05:00:01 +03:00
|
|
|
)
|
|
|
|
|
2021-03-04 17:41:44 +03:00
|
|
|
type cache struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
blob string
|
|
|
|
logf func(s string, f ...interface{})
|
|
|
|
)
|
|
|
|
|
|
|
|
func handler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch r.Method {
|
|
|
|
case "POST":
|
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
c := &cache{}
|
|
|
|
if err := d.Decode(c); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
2021-03-05 03:56:16 +03:00
|
|
|
return
|
2021-03-04 17:41:44 +03:00
|
|
|
}
|
|
|
|
cache, err := pkg.NewCacheService(blob, logf)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2021-03-05 03:56:16 +03:00
|
|
|
return
|
2021-03-04 17:41:44 +03:00
|
|
|
}
|
|
|
|
if err := cache.UpdateCache(c.URL); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2021-03-05 03:56:16 +03:00
|
|
|
return
|
2021-03-04 17:41:44 +03:00
|
|
|
}
|
2021-03-02 05:00:01 +03:00
|
|
|
|
2021-03-04 17:41:44 +03:00
|
|
|
default:
|
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
fmt.Fprintf(w, "I can't do that.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2021-03-04 06:22:17 +03:00
|
|
|
logLevel := zap.LevelFlag("verbosity", zap.InfoLevel, "override the default log level")
|
2021-03-02 05:00:01 +03:00
|
|
|
|
2021-03-04 06:22:17 +03:00
|
|
|
cfg := zap.NewProductionConfig()
|
|
|
|
cfg.Level.SetLevel(*logLevel)
|
|
|
|
logger, err := cfg.Build()
|
2021-03-02 05:00:01 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2021-03-04 17:41:44 +03:00
|
|
|
|
2021-03-04 06:22:17 +03:00
|
|
|
//nolint
|
|
|
|
defer logger.Sync() // flushes buffer, if any
|
|
|
|
sugar := logger.Sugar()
|
2021-03-04 17:41:44 +03:00
|
|
|
// no need to lock this as it being written only within this method.
|
|
|
|
blob = os.Getenv("BLOB_URL")
|
2021-03-04 06:22:17 +03:00
|
|
|
if blob == "" {
|
2021-03-04 17:41:44 +03:00
|
|
|
log.Panic("BLOB_URL env is not set.")
|
2021-03-02 05:00:01 +03:00
|
|
|
}
|
2021-03-04 17:41:44 +03:00
|
|
|
sugar.Info("BLOB_URL ", blob)
|
|
|
|
// no need to lock this as it being written only within this method.
|
|
|
|
logf = sugar.Infof
|
2021-03-02 05:00:01 +03:00
|
|
|
|
2021-03-04 17:41:44 +03:00
|
|
|
http.HandleFunc("/", handler)
|
|
|
|
sugar.Info("Starting server for testing HTTP POST...\n")
|
|
|
|
if err := http.ListenAndServe(":8080", nil); err != nil {
|
|
|
|
sugar.Fatal(err)
|
2021-03-04 06:22:17 +03:00
|
|
|
}
|
2021-03-02 05:00:01 +03:00
|
|
|
}
|