github.com/stackdocker/rkt@v0.10.1-0.20151109095037-1aa827478248/tests/test-auth-server/main.go (about)

     1  // Copyright 2015 The rkt Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  
    21  	taas "github.com/coreos/rkt/tests/test-auth-server/aci"
    22  )
    23  
    24  func main() {
    25  	cmdsStr := "start, stop"
    26  	if len(os.Args) < 2 {
    27  		fmt.Printf("Error: expected a command - %s\n", cmdsStr)
    28  		os.Exit(1)
    29  	}
    30  	var err error
    31  	switch os.Args[1] {
    32  	case "start":
    33  		err = start(os.Args[2:])
    34  	case "stop":
    35  		err = stop(os.Args[2:])
    36  	default:
    37  		err = fmt.Errorf("wrong command %q, should be %s", os.Args[1], cmdsStr)
    38  	}
    39  	if err != nil {
    40  		fmt.Printf("Error: %v\n", err)
    41  		os.Exit(1)
    42  	}
    43  }
    44  
    45  func start(args []string) error {
    46  	typesStr := "none, basic, oauth"
    47  	if len(args) < 1 {
    48  		return fmt.Errorf("expected a type - %s", typesStr)
    49  	}
    50  	types := map[string]taas.Type{
    51  		"none":  taas.None,
    52  		"basic": taas.Basic,
    53  		"oauth": taas.Oauth,
    54  	}
    55  	auth, ok := types[args[0]]
    56  	if !ok {
    57  		return fmt.Errorf("wrong type %q, should, be %s", args[0], typesStr)
    58  	}
    59  	server, err := taas.StartServer(auth)
    60  	if err != nil {
    61  		return fmt.Errorf("failed to start server: %v", err)
    62  	}
    63  	if server.Conf != "" {
    64  		fmt.Printf(server.Conf)
    65  	}
    66  	fmt.Printf("Ready, waiting for connections at %s\n", server.URL)
    67  	loop(server)
    68  	fmt.Println("Byebye")
    69  	return nil
    70  }
    71  
    72  func loop(server *taas.Server) {
    73  	for {
    74  		select {
    75  		case <-server.Stop:
    76  			server.Close()
    77  			return
    78  		case msg, ok := <-server.Msg:
    79  			if ok {
    80  				fmt.Println(msg)
    81  			}
    82  		}
    83  	}
    84  }
    85  
    86  func stop(args []string) error {
    87  	if len(args) < 1 {
    88  		return fmt.Errorf("expected a host")
    89  	}
    90  	host := args[0]
    91  	res, err := taas.StopServer(host)
    92  	if err != nil {
    93  		return fmt.Errorf("failed to stop server: %v", err)
    94  	}
    95  	defer res.Body.Close()
    96  	fmt.Printf("Response status: %s\n", res.Status)
    97  	if res.StatusCode/100 != 2 {
    98  		return fmt.Errorf("got a nonsuccess status")
    99  	}
   100  	return nil
   101  }