Skip to content

Commit

Permalink
feat: init app flag to get started
Browse files Browse the repository at this point in the history
  • Loading branch information
rhnvrm committed Apr 20, 2021
1 parent ab495fb commit 2e1f885
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ tmp/
*.db
*.bin
dist/
config.toml
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ LinkPage is a FOSS self-hosted alternative to link listing websites such as Link

1. Download the latest release
2. Decompress the archive
3. Create a sqlite db with the schema using `sqlite3 linkpage.db < schema.sql`
4. Copy the [config file](config.toml) and update it with your settings
5. Run the app using `./linkpage`
3. Run the app using `./linkpage --init`, this will generate an empty sqlite database and config file in your local directory.
4. Now you can run the app using `./linkpage`, goto the `/admin` page to add new entries.

## Developer Setup

Expand Down
File renamed without changes.
91 changes: 89 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"embed"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"sync"
"text/template"
Expand All @@ -19,6 +21,7 @@ import (
"github.com/knadh/koanf/providers/file"
_ "github.com/mattn/go-sqlite3"
"github.com/otiai10/opengraph/v2"
flag "github.com/spf13/pflag"
"github.com/urfave/negroni"
)

Expand All @@ -28,6 +31,9 @@ var templateFS embed.FS
//go:embed static
var staticFS embed.FS

//go:embed schema.sql config.sample.toml
var setupFS embed.FS

type Config struct {
HTTPAddr string `koanf:"http_address"`
ReadTimeout time.Duration `koanf:"read_timeout"`
Expand Down Expand Up @@ -253,8 +259,24 @@ func basicAuth(cfg Config) negroni.HandlerFunc {
})
}

func main() {
cfg := initConfig("config.toml")
var (
appMode = "run_app"
configFilePath = "config.toml"
)

func init() {
flag.StringVar(&configFilePath, "config", "config.toml", "path to config file")
initApp := flag.Bool("init", false, "app initialization, creates a db and config file in current dir")

flag.Parse()

if *initApp == true {
appMode = "init_app"
}
}

func runApp(configFilePath string) {
cfg := initConfig(configFilePath)

db, err := sqlx.Connect("sqlite3", cfg.DBFile)
if err != nil {
Expand Down Expand Up @@ -536,3 +558,68 @@ func main() {
log.Println("starting server at", cfg.HTTPAddr)
log.Fatal(srv.ListenAndServe())
}

func initApp() {
file, err := os.Create("app.db")
if err != nil {
log.Fatal(err)
}
file.Close()

db, err := sqlx.Connect("sqlite3", "app.db")
if err != nil {
log.Fatal(err)
}

schemaFile, err := setupFS.Open("schema.sql")
if err != nil {
log.Fatal(err)
}

schema, err := ioutil.ReadAll(schemaFile)
if err != nil {
log.Fatal(err)
}

if err := schemaFile.Close(); err != nil {
log.Fatal(err)
}

if _, err := db.Exec(string(schema)); err != nil {
log.Fatal(err)
}

if err := db.Close(); err != nil {
log.Fatal(err)
}

outCfgFile, err := os.Create("config.toml")
if err != nil {
log.Fatal(err)
}
defer outCfgFile.Close()

setupCfgFile, err := setupFS.Open("config.sample.toml")
if err != nil {
log.Fatal(err)
}
defer setupCfgFile.Close()

if _, err := io.Copy(outCfgFile, setupCfgFile); err != nil {
log.Fatal(err)
}

log.Println("config.toml and app.db generated.")
}

func main() {
log.Println(appMode)
switch appMode {
case "init_app":
initApp()
case "run_app":
runApp(configFilePath)
default:
runApp(configFilePath)
}
}

0 comments on commit 2e1f885

Please sign in to comment.