Code

Use the current username by default.
[sysdb/webui.git] / main.go
1 //
2 // Copyright (C) 2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions
7 // are met:
8 // 1. Redistributions of source code must retain the above copyright
9 //    notice, this list of conditions and the following disclaimer.
10 // 2. Redistributions in binary form must reproduce the above copyright
11 //    notice, this list of conditions and the following disclaimer in the
12 //    documentation and/or other materials provided with the distribution.
13 //
14 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 // ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
18 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // webui is a web-based user-interface for SysDB.
27 package main
29 import (
30         "flag"
31         "fmt"
32         "log"
33         "net/http"
34         "os"
35         "os/user"
37         "github.com/sysdb/go/client"
38         "github.com/sysdb/webui/server"
39 )
41 var (
42         addr     = flag.String("address", "/var/run/sysdbd.sock", "SysDB server address")
43         username *string
45         listen = flag.String("listen", ":8080", "address to listen for incoming connections")
46         tmpl   = flag.String("template-path", "templates", "location of template files")
47         static = flag.String("static-path", "static", "location of static files")
48 )
50 func init() {
51         u, err := user.Current()
52         var def string
53         if err != nil {
54                 log.Printf("WARNING: Unable to determine current user: %v", err)
55         } else {
56                 def = u.Username
57         }
59         username = flag.String("user", def, "SysDB user name")
60 }
62 func main() {
63         flag.Parse()
65         log.Printf("Connecting to SysDB at %s.", *addr)
66         var conns []*client.Conn
67         for i := 0; i < 10; i++ {
68                 conn, err := client.Connect(*addr, *username)
69                 if err != nil {
70                         fatalf("Failed to connect to SysDB at %q: %v", *addr, err)
71                 }
72                 conns = append(conns, conn)
73         }
75         srv, err := server.New(server.Config{
76                 Conns:        conns,
77                 TemplatePath: *tmpl,
78                 StaticPath:   *static,
79         })
80         if err != nil {
81                 fatalf("Failed to construct web-server: %v", err)
82         }
84         log.Printf("Listening on %s.", *listen)
85         http.Handle("/", srv)
86         err = http.ListenAndServe(*listen, nil)
87         if err != nil {
88                 fatalf("Failed to set up HTTP server on address %q: %v", *listen, err)
89         }
90 }
92 func fatalf(format string, a ...interface{}) {
93         fmt.Fprintf(os.Stderr, format, a...)
94         fmt.Fprintln(os.Stderr)
95         os.Exit(1)
96 }
98 // vim: set tw=78 sw=4 sw=4 noexpandtab :