github.com/rogpeppe/juju@v0.0.0-20140613142852-6337964b789e/cmd/charmd/main.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package main
     5  
     6  import (
     7  	"fmt"
     8  	"net/http"
     9  	"os"
    10  	"path/filepath"
    11  
    12  	"github.com/juju/charmstore"
    13  )
    14  
    15  func main() {
    16  	err := serve()
    17  	if err != nil {
    18  		fmt.Fprintf(os.Stderr, "%v\n", err)
    19  		os.Exit(1)
    20  	}
    21  }
    22  
    23  func serve() error {
    24  	var confPath string
    25  	if len(os.Args) == 2 {
    26  		if _, err := os.Stat(os.Args[1]); err == nil {
    27  			confPath = os.Args[1]
    28  		}
    29  	}
    30  	if confPath == "" {
    31  		return fmt.Errorf("usage: %s <config path>", filepath.Base(os.Args[0]))
    32  	}
    33  	conf, err := charmstore.ReadConfig(confPath)
    34  	if err != nil {
    35  		return err
    36  	}
    37  	if conf.MongoURL == "" || conf.APIAddr == "" {
    38  		return fmt.Errorf("missing mongo-url or api-addr in config file")
    39  	}
    40  	s, err := charmstore.Open(conf.MongoURL)
    41  	if err != nil {
    42  		return err
    43  	}
    44  	defer s.Close()
    45  	server, err := charmstore.NewServer(s)
    46  	if err != nil {
    47  		return err
    48  	}
    49  	return http.ListenAndServe(conf.APIAddr, server)
    50  }