1
0
mirror of https://github.com/aclindsa/moneygo.git synced 2024-10-31 16:00:05 -04:00
moneygo/main.go
2015-06-25 22:36:58 -04:00

78 lines
2.1 KiB
Go

package main
import (
"flag"
"github.com/gorilla/context"
"log"
"net"
"net/http"
"net/http/fcgi"
"os"
"path"
"strconv"
)
var serveFcgi bool
var baseDir string
var port int
var smtpServer string
var smtpPort int
var smtpUsername string
var smtpPassword string
var reminderEmail string
func init() {
flag.StringVar(&baseDir, "base", "./", "Base directory for server")
flag.IntVar(&port, "port", 80, "Port to serve API/files on")
flag.StringVar(&smtpServer, "smtp.server", "smtp.example.com", "SMTP server to send reminder emails from.")
flag.IntVar(&smtpPort, "smtp.port", 587, "SMTP server port to connect to")
flag.StringVar(&smtpUsername, "smtp.username", "moneygo", "SMTP username")
flag.StringVar(&smtpPassword, "smtp.password", "password", "SMTP password")
flag.StringVar(&reminderEmail, "email", "moneygo@example.com", "Email address to send reminder emails as.")
flag.BoolVar(&serveFcgi, "fcgi", false, "Serve via fcgi rather than http.")
flag.Parse()
static_path := path.Join(baseDir, "static")
// Ensure base directory is valid
dir_err_str := "The base directory doesn't look like it contains the " +
"'static' directory. Check to make sure you're passing the right " +
"value to the -base argument."
static_dir, err := os.Stat(static_path)
if err != nil {
log.Print(err)
log.Fatal(dir_err_str)
}
if !static_dir.IsDir() {
log.Fatal(dir_err_str)
}
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, path.Join(baseDir, "static/index.html"))
}
func staticHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, path.Join(baseDir, r.URL.Path))
}
func main() {
servemux := http.NewServeMux()
servemux.HandleFunc("/", rootHandler)
servemux.HandleFunc("/static/", staticHandler)
servemux.HandleFunc("/session/", SessionHandler)
servemux.HandleFunc("/user/", UserHandler)
listener, err := net.Listen("tcp", ":"+strconv.Itoa(port))
if err != nil {
log.Fatal(err)
}
log.Printf("Serving on port %d out of directory: %s", port, baseDir)
if serveFcgi {
fcgi.Serve(listener, context.ClearHandler(servemux))
} else {
http.Serve(listener, context.ClearHandler(servemux))
}
}