<commit_before>// Package main implements the fake gateway.
package main

import (
	"flag"
	"fmt"
	"google.golang.org/grpc"
	"log"
	"net"
	// "regexp"

	"golang.org/x/net/context"
	"google.golang.org/grpc/credentials"
	g "google/fakes/gateway.pb"
	google_protobuf "google/protobuf/empty.pb"
)

var (
	tls      = flag.Bool("tls", false, "Connection uses TLS if true, else plain TCP")
	certFile = flag.String("cert_file", "server1.pem", "The TLS cert file")
	keyFile  = flag.String("key_file", "server1.key", "The TLS key file")
	port     = flag.Int("port", 10000, "The server port")
	EMPTY    = &google_protobuf.Empty{}
)

type server struct{}

// we implement the gateway here
func (s *server) Register(ctx context.Context, req *g.RegisterRequest) (*g.RegisterResponse, error) {
	log.Printf("Register %q", req)
	return nil, nil
}

func (s *server) Resolve(ctx context.Context, req *g.ResolveRequest) (*g.ResolveResponse, error) {
	log.Printf("Resolve %q", req)
	return nil, nil
}

func (s *server) Ping(ctx context.Context, e *google_protobuf.Empty) (*google_protobuf.Empty, error) {
	log.Println("Ping")
	return EMPTY, nil
}

func main() {
	log.Printf("Fakes Gateway starting up...")
	flag.Parse()
	lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
	if err != nil {
		log.Fatalf("failed to listen: %v.", err)
	}
	grpcServer := grpc.NewServer()
	server := server{}
	g.RegisterGatewayServer(grpcServer, &server)
	if *tls {
		creds, err := credentials.NewServerTLSFromFile(*certFile, *keyFile)
		if err != nil {
			log.Fatalf("Failed to generate credentials %v.", err)
		}<commit_msg>pass on a first implementation<commit_after>// Package main implements the fake gateway.
package main

import (
	"flag"
	"fmt"
	"google.golang.org/grpc"
	"log"
	"net"
	"regexp"

	"golang.org/x/net/context"
	"google.golang.org/grpc/credentials"
	g "google/fakes/gateway.pb"
	google_protobuf "google/protobuf/empty.pb"
)

var (
	tls      = flag.Bool("tls", false, "Connection uses TLS if true, else plain TCP")
	certFile = flag.String("cert_file", "server1.pem", "The TLS cert file")
	keyFile  = flag.String("key_file", "server1.key", "The TLS key file")
	port     = flag.Int("port", 10000, "The server port")
	EMPTY    = &google_protobuf.Empty{}
)

type server struct{}

type cmdSpec struct {
	regexp string
	path   string
	args   []string
}

type matcher struct {
	regexp string
	target string
}

// This maps the url patterns to targets urls.
// this is a list as the evaluation order matters.
var activeFakes []matcher

// This maps the url patterns to cmd to start to have the fake
var ondemandFakes []cmdSpec

func init() {
	activeFakes = make([]matcher, 0, 10)
	ondemandFakes = make([]cmdSpec, 0, 10)
}

// we implement the gateway here
func (s *server) Register(ctx context.Context, req *g.RegisterRequest) (*g.RegisterResponse, error) {
	log.Printf("Register req %q", req)
	if req.Registration.ResolvedTarget != "" {
		activeFakes = append(activeFakes, matcher{
			regexp: req.Registration.TargetPattern,
			target: req.Registration.ResolvedTarget,
		})
	} else {
		log.Printf("TODO: implement")
	}
	return &g.RegisterResponse{}, nil
}

func (s *server) Resolve(ctx context.Context, req *g.ResolveRequest) (*g.ResolveResponse, error) {
	log.Printf("Resolve %q", req)
	target := []byte(req.Target)
	for _, matcher := range activeFakes {
		matched, err := regexp.Match(matcher.regexp, target)
		if err != nil {
			return nil, err
		}
		if matched {
			res := &g.ResolveResponse{
				ResolvedTarget: matcher.target,
			}
			return res, nil
		}
	}
	return nil, fmt.Errorf("%s not found", req.Target)
}

func (s *server) Ping(ctx context.Context, e *google_protobuf.Empty) (*google_protobuf.Empty, error) {
	log.Println("Ping")
	return EMPTY, nil
}

func main() {
	log.Printf("Fakes Gateway starting up...")
	flag.Parse()
	lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
	if err != nil {
		log.Fatalf("failed to listen: %v.", err)
	}
	grpcServer := grpc.NewServer()
	server := server{}
	g.RegisterGatewayServer(grpcServer, &server)
	if *tls {
		creds, err := credentials.NewServerTLSFromFile(*certFile, *keyFile)
		if err != nil {
			log.Fatalf("Failed to generate credentials %v.", err)
		}
{'fraction_non_alphanumeric': 0.14960629921259844, 'fraction_numerical': 0.004772130756382725, 'mean_word_length': 4.175308641975309, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3340480', 'n_tokens_mistral': 1526, 'n_tokens_neox': 1476, 'n_words': 403}