<commit_before>// Copyright (C) 2016 <NAME>
// See LICENSE for copying information

package main

import (
	"encoding/hex"
	"flag"
	"fmt"
	"net/http"
	"os"
	"strconv"

	"github.com/jinzhu/gorm"
	"github.com/jtolds/webhelp"
	"github.com/jtolds/webhelp/sessions"
	_ "github.com/mattn/go-sqlite3"
	"golang.org/x/net/context"
)

var (
	listenAddr   = flag.String("addr", ":8080", "address to listen on")
	cookieSecret = flag.String("cookie_secret", "<KEY>",
		"the secret for securing cookie information")
	sqlitePath = flag.String("db", "./db.db", "")

	projectId = webhelp.NewArgMux()
)

// data model:
// * user
// * project
// * genes
// * controls
// * perturbagens
// * differential expressions (w/ranks)

type Project struct {
	gorm.Model

	UserId string `sql:"index"`
	Name   string
}

type App struct {
	db gorm.DB
}

func NewApp() (*App, error) {
	db, err := gorm.Open("sqlite3", *sqlitePath)
	if err != nil {
		return nil, err
	}
	return &App{db: db}, nil
}

func (a *App) Close() error { return a.db.Close() }

func (a *App) Migrate() error {
	return a.db.AutoMigrate(&Project{}).Error
}

func (a *App) ProjectList(ctx context.Context, req *http.Request,
	user *UserInfo) (tmpl string, page map[string]interface{},
	err error) {
	var projects []*Project
	err = a.db.Where("user_id = ?", user.Id).Find(&projects).Error
	if err != nil {
		return "", nil, err
	}
	return "projects", map[string]interface{}{"Projects": projects}, nil
}

func (a *App) Project(ctx context.Context, req *http.Request,
	user *UserInfo) (tmpl string, page map[string]interface{},
	err error) {
	id, err := strconv.Atoi(projectId.Get(ctx))
	if err != nil {
		return "", nil, webhelp.ErrNotFound.New("")
	}
	var proj Project
	err = a.db.First(&proj, id).Error
	if err != nil {
		return "", nil, err
	}
	return "project", map[string]interface{}{"Project": proj}, nil
}

func (a *App) NewProject(ctx context.Context, w webhelp.ResponseWriter,
	req *http.Request, user *UserInfo) error {
	tx := a.db.Begin()
	defer tx.Rollback()
	proj := Project{UserId: user.Id, Name: req.FormValue("name")}
	err := tx.Create(&proj).Error
	if err != nil {
		return err
	}
	tx.Commit()
	return webhelp.Redirect(w, req, fmt.Sprintf("/project/%d", proj.ID))
}

func (a *App) UpdateProject(ctx context.Context, w webhelp.ResponseWriter,
	req *http.Request, user *UserInfo) error {
	return webhelp.Redirect(w, req, req.RequestURI)
}

func main() {
	flag.Parse()
	loadOAuth2()
	secret, err := hex.DecodeString(*cookieSecret)
	if err != nil {
		panic(err)
	}

	renderer, err := NewRenderer()
	if err != nil {
		panic(err)
	}

	app, err := NewApp()
	if err != nil {
		panic(err)
	}
	defer app.Close()

	switch flag.Arg(0) {
	case "migrate":
		err := app.Migrate()
		if err != nil {
			panic(err)
		}
	case "serve":
		panic(webhelp.ListenAndServe(*listenAddr, webhelp.LoggingHandler(
			sessions.HandlerWithStore(sessions.NewCookieStore(secret),
				webhelp.DirMux{
					"": oauth2.LoginRequired(renderer.Render(app.ProjectList)),
					"project": projectId.ShiftIf(webhelp.MethodMux{
						"GET":  renderer.Render(app.Project),
						"POST": renderer.Process(app.UpdateProject),
					}, webhelp.MethodMux{
						"GET":  webhelp.RedirectHandler("/"),
						"POST": renderer.Process(app.NewProject),
					}),
					"auth": oauth2}))))
	default:
		fmt.Printf("Usage: %s <serve|migrate>\n", os.Args[0])
	}
}<commit_msg>use intargmux<commit_after>// Copyright (C) 2016 <NAME>
// See LICENSE for copying information

package main

import (
	"encoding/hex"
	"flag"
	"fmt"
	"net/http"
	"os"

	"github.com/jinzhu/gorm"
	"github.com/jtolds/webhelp"
	"github.com/jtolds/webhelp/sessions"
	_ "github.com/mattn/go-sqlite3"
	"golang.org/x/net/context"
)

var (
	listenAddr   = flag.String("addr", ":8080", "address to listen on")
	cookieSecret = flag.String("cookie_secret", "<KEY>",
		"the secret for securing cookie information")
	sqlitePath = flag.String("db", "./db.db", "")

	projectId = webhelp.NewIntArgMux()
)

// data model:
// * user
// * project
// * genes
// * controls
// * perturbagens
// * differential expressions (w/ranks)

type Project struct {
	gorm.Model

	UserId string `sql:"index"`
	Name   string
}

type App struct {
	db gorm.DB
}

func NewApp() (*App, error) {
	db, err := gorm.Open("sqlite3", *sqlitePath)
	if err != nil {
		return nil, err
	}
	return &App{db: db}, nil
}

func (a *App) Close() error { return a.db.Close() }

func (a *App) Migrate() error {
	return a.db.AutoMigrate(&Project{}).Error
}

func (a *App) ProjectList(ctx context.Context, req *http.Request,
	user *UserInfo) (tmpl string, page map[string]interface{},
	err error) {
	var projects []*Project
	err = a.db.Where("user_id = ?", user.Id).Find(&projects).Error
	if err != nil {
		return "", nil, err
	}
	return "projects", map[string]interface{}{"Projects": projects}, nil
}

func (a *App) Project(ctx context.Context, req *http.Request,
	user *UserInfo) (tmpl string, page map[string]interface{}, err error) {
	var proj Project
	err = a.db.First(&proj, projectId.Get(ctx)).Error
	if err != nil {
		return "", nil, err
	}
	return "project", map[string]interface{}{"Project": proj}, nil
}

func (a *App) NewProject(ctx context.Context, w webhelp.ResponseWriter,
	req *http.Request, user *UserInfo) error {
	tx := a.db.Begin()
	defer tx.Rollback()
	proj := Project{UserId: user.Id, Name: req.FormValue("name")}
	err := tx.Create(&proj).Error
	if err != nil {
		return err
	}
	tx.Commit()
	return webhelp.Redirect(w, req, fmt.Sprintf("/project/%d", proj.ID))
}

func (a *App) UpdateProject(ctx context.Context, w webhelp.ResponseWriter,
	req *http.Request, user *UserInfo) error {
	return webhelp.Redirect(w, req, req.RequestURI)
}

func main() {
	flag.Parse()
	loadOAuth2()
	secret, err := hex.DecodeString(*cookieSecret)
	if err != nil {
		panic(err)
	}

	renderer, err := NewRenderer()
	if err != nil {
		panic(err)
	}

	app, err := NewApp()
	if err != nil {
		panic(err)
	}
	defer app.Close()

	switch flag.Arg(0) {
	case "migrate":
		err := app.Migrate()
		if err != nil {
			panic(err)
		}
	case "serve":
		panic(webhelp.ListenAndServe(*listenAddr, webhelp.LoggingHandler(
			sessions.HandlerWithStore(sessions.NewCookieStore(secret),
				webhelp.DirMux{
					"": oauth2.LoginRequired(renderer.Render(app.ProjectList)),
					"project": projectId.ShiftIf(webhelp.MethodMux{
						"GET":  renderer.Render(app.Project),
						"POST": renderer.Process(app.UpdateProject),
					}, webhelp.ExactPath(webhelp.MethodMux{
						"GET":  webhelp.RedirectHandler("/"),
						"POST": renderer.Process(app.NewProject),
					})),
					"auth": oauth2}))))
	default:
		fmt.Printf("Usage: %s <serve|migrate>\n", os.Args[0])
	}
}
{'fraction_non_alphanumeric': 0.1716047525943751, 'fraction_numerical': 0.004511956685215822, 'mean_word_length': 3.9850074962518742, 'pattern_counts': {'":': 24, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5717382', 'n_tokens_mistral': 2606, 'n_tokens_neox': 2453, 'n_words': 565}