func main() { // Parse CLI arguments port := flag.String("port", "8080", "Server port") host = *flag.String("host", "http://localhost", "Server host") env = *flag.String("env", "development", "Run in development or production mode") dbPath := flag.String("db", "./ota.sql", "Path to sqlite db file") staticDir := flag.String("static", "./admin_interface/static/", "Path to templates") buildsDir := flag.String("builds", "./builds", "Path to directory containing build images") userFlag := flag.Bool("add_user", false, "Run CLI for adding user to database") testFlag := flag.Bool("test", false, "Run test script to simulate client") flag.Parse() // Connect to database db := database.InitDb(*dbPath, *buildsDir) go database.RefreshBuilds() defer db.Db.Close() if *testFlag { // TestServer("http://localhost:8080") } else { if *userFlag { // Start CLI to create new user account addUser() } else { // Start server server(*port, *staticDir) } } }
// POST /releases/delete func ReleaseDelete(w rest.ResponseWriter, r *rest.Request) { r.ParseForm() id, _ := strconv.ParseInt(r.FormValue("Id"), 10, 64) release := database.FindRelease(id) file := database.FindFile(release.FileId) // Delete from DB database.DeleteRelease(release) database.UnpublishFile(file) go database.RefreshBuilds() // http.Redirect(w, r, "/admin/releases", http.StatusFound) w.WriteJson("") }
// POST /releases/create func ReleaseCreate(w rest.ResponseWriter, r *rest.Request) { r.ParseForm() fileId, _ := strconv.ParseInt(r.FormValue("FileId"), 10, 64) file := database.FindFile(fileId) // Generate release release := database.Release{ Created: time.Now().UnixNano(), Changelog: r.FormValue("Changelog"), Channel: r.FormValue("Channel"), FileId: file.Id, FileName: file.Name, } database.CreateRelease(release) database.PublishFile(file) go database.RefreshBuilds() w.WriteJson("") }