Skip to content

Commit

Permalink
config file (vlang#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
walking dev authored Dec 1, 2022
1 parent cbf88ba commit c67fbd9
Show file tree
Hide file tree
Showing 15 changed files with 58 additions and 51 deletions.
6 changes: 6 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"repo_storage_path": "./repos",
"archive_path": "./archives",
"avatars_path": "./avatars",
"hostname": "gitly.org"
}
18 changes: 18 additions & 0 deletions config/loader.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module config

import os
import json

pub struct Config {
pub:
repo_storage_path string
archive_path string
avatars_path string
hostname string
}

pub fn read_config(path string) !Config {
config_raw := os.read_file(path)!

return json.decode(Config, config_raw)!
}
4 changes: 2 additions & 2 deletions src/admin_routes.v
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ pub fn (mut app App) admin_settings() vweb.Result {
}

['/admin/settings'; post]
pub fn (mut app App) handle_admin_update_settings(oauth_client_id string, oauth_client_secret string, hostname string, repo_storage_path string) vweb.Result {
pub fn (mut app App) handle_admin_update_settings(oauth_client_id string, oauth_client_secret string) vweb.Result {
if !app.is_admin() {
return app.redirect_to_index()
}

app.update_gitly_settings(oauth_client_id, oauth_client_secret, hostname, repo_storage_path)
app.update_gitly_settings(oauth_client_id, oauth_client_secret)

return app.redirect('/admin')
}
Expand Down
4 changes: 2 additions & 2 deletions src/admin_service.v
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ pub fn (mut app App) remove_admin(user_id int) {
app.set_user_admin_status(user_id, false)
}

pub fn (mut app App) update_gitly_settings(oauth_client_id string, oauth_client_secret string, hostname string, repo_storage_path string) {
app.update_settings(oauth_client_id, oauth_client_secret, hostname, repo_storage_path)
pub fn (mut app App) update_gitly_settings(oauth_client_id string, oauth_client_secret string) {
app.update_settings(oauth_client_id, oauth_client_secret)

app.load_settings()
}
Expand Down
6 changes: 3 additions & 3 deletions src/avatar_service.v
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ fn validate_avatar_file_size(content string) bool {
}

fn (app App) build_avatar_file_path(avatar_filename string) string {
relative_path := os.join_path(app.settings.avatars_path, avatar_filename)
relative_path := os.join_path(app.config.avatars_path, avatar_filename)

return os.abs_path(relative_path)
}

fn (app App) build_avatar_file_url(avatar_filename string) string {
clean_path := app.settings.avatars_path.trim_string_left('./')
clean_path := app.config.avatars_path.trim_string_left('./')

return os.join_path('/', clean_path, avatar_filename)
}

fn (app App) write_user_avatar(avatar_filename string, file_content string) bool {
path := os.join_path(app.settings.avatars_path, avatar_filename)
path := os.join_path(app.config.avatars_path, avatar_filename)

os.write_file(path, file_content) or { return false }

Expand Down
16 changes: 11 additions & 5 deletions src/gitly.v
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import os
import log
import sqlite
import api
import config

const (
commits_per_page = 35
Expand All @@ -30,7 +31,8 @@ pub mut:
mut:
version string [vweb_global]
logger log.Log [vweb_global]
settings GitlySettings
config config.Config [vweb_global]
settings Settings
current_path string
page_gen_time string
is_tree bool
Expand Down Expand Up @@ -84,9 +86,13 @@ fn new_app() &App {

app.load_settings()

create_directory_if_not_exists(app.settings.repo_storage_path)
create_directory_if_not_exists(app.settings.archive_path)
create_directory_if_not_exists(app.settings.avatars_path)
app.config = config.read_config('./config.json') or {
panic('Config not found or has syntax errors')
}

create_directory_if_not_exists(app.config.repo_storage_path)
create_directory_if_not_exists(app.config.archive_path)
create_directory_if_not_exists(app.config.avatars_path)

// Create the first admin user if the db is empty
app.get_user_by_id(1) or {}
Expand Down Expand Up @@ -209,7 +215,7 @@ fn (mut app App) create_tables() {
create table Branch
}
sql app.db {
create table GitlySettings
create table Settings
}
sql app.db {
create table Token
Expand Down
2 changes: 1 addition & 1 deletion src/repo_routes.v
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ pub fn (mut app App) handle_new_repo(name string, clone_url string, description
}
}

repo_path := os.join_path(app.settings.repo_storage_path, app.user.username, name)
repo_path := os.join_path(app.config.repo_storage_path, app.user.username, name)

mut new_repo := Repo{
name: name
Expand Down
6 changes: 3 additions & 3 deletions src/repo_service.v
Original file line number Diff line number Diff line change
Expand Up @@ -684,11 +684,11 @@ fn (r Repo) git_smart(service string, input string) string {
}

fn (mut app App) generate_clone_url(repo Repo) string {
hostname := app.settings.hostname
hostname := app.config.hostname
username := repo.user_name
repository_name := repo.name
repo_name := repo.name

return 'https://${hostname}/${username}/${repository_name}.git'
return 'https://${hostname}/${username}/${repo_name}.git'
}

fn first_line(s string) string {
Expand Down
6 changes: 1 addition & 5 deletions src/settings_entities.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@
// Use of this source code is governed by a GPL license that can be found in the LICENSE file.
module main

struct GitlySettings {
struct Settings {
id int [primary; sql: serial]
mut:
oauth_client_id string
oauth_client_secret string
repo_storage_path string = './repos'
archive_path string = './archives'
avatars_path string = './avatars'
hostname string = 'gitly.org'
}
24 changes: 7 additions & 17 deletions src/settings_service.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ module main

fn (mut app App) load_settings() {
app.settings = sql app.db {
select from GitlySettings limit 1
select from Settings limit 1
}
}

fn (mut app App) update_settings(oauth_client_id string, oauth_client_secret string, hostname string, repo_storage_path string) {
fn (mut app App) update_settings(oauth_client_id string, oauth_client_secret string) {
old_settings := sql app.db {
select from GitlySettings limit 1
select from Settings limit 1
}

github_oauth_client_id := if oauth_client_id != '' {
Expand All @@ -23,29 +23,19 @@ fn (mut app App) update_settings(oauth_client_id string, oauth_client_secret str
old_settings.oauth_client_secret
}

gitly_hostname := if hostname != '' { hostname } else { old_settings.hostname }

gitly_repo_storage_path := if repo_storage_path != '' {
repo_storage_path
} else {
old_settings.repo_storage_path
}

if old_settings.id == 0 {
settings := GitlySettings{
settings := Settings{
oauth_client_id: github_oauth_client_id
oauth_client_secret: github_oauth_client_secret
repo_storage_path: gitly_repo_storage_path
hostname: gitly_hostname
}

sql app.db {
insert settings into GitlySettings
insert settings into Settings
}
} else {
sql app.db {
update GitlySettings set oauth_client_id = github_oauth_client_id, oauth_client_secret = github_oauth_client_secret,
repo_storage_path = gitly_repo_storage_path, hostname = gitly_hostname where id == old_settings.id
update Settings set oauth_client_id = github_oauth_client_id, oauth_client_secret = github_oauth_client_secret
where id == old_settings.id
}
}
}
2 changes: 1 addition & 1 deletion src/tag_routes.v
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn (mut app App) handle_download_tag_archive(username string, repo_name stri
return app.not_found()
}

archive_abs_path := os.abs_path(app.settings.archive_path)
archive_abs_path := os.abs_path(app.config.archive_path)
snapshot_format := if format == 'zip' { 'zip' } else { 'tar.gz' }
snapshot_name := '${username}_${repo_name}_${tag}.${snapshot_format}'
archive_path := '${archive_abs_path}/${snapshot_name}'
Expand Down
8 changes: 0 additions & 8 deletions src/templates/admin/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ <h1>Admin panel</h1>
<label>OAuth client secret:</label>
<input name='oauth_client_secret' type='text' placeholder='...'>
</div>
<div class="field">
<label>Hostname:</label>
<input name='hostname' type='text' value='@app.settings.hostname'>
</div>
<div class="field">
<label>Repo storage path:</label>
<input name='repo_storage_path' type='text' value='@app.settings.repo_storage_path'>
</div>
<div class="field">
<input type='submit' value='Update settings'>
</div>
Expand Down
3 changes: 1 addition & 2 deletions src/templates/new.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ <h1>New repository</h1>

<div class="form">
<form method='post'>

@app.settings.hostname/@app.user.username/
@app.config.hostname/@app.user.username/
<input type='text' name='name'required autofocus maxlength=@max_repo_name_len>

<div class='field'>
Expand Down
2 changes: 1 addition & 1 deletion src/user_routes.v
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ pub fn (mut app App) handle_update_user_settings(username string) vweb.Result {
}

fn (mut app App) rename_user_directory(old_name string, new_name string) {
os.mv('${app.settings.repo_storage_path}/${old_name}', '${app.settings.repo_storage_path}/${new_name}') or {
os.mv('${app.config.repo_storage_path}/${old_name}', '${app.config.repo_storage_path}/${new_name}') or {
panic(err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/user_service.v
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub fn (mut app App) register_user(username string, password string, salt string
}

fn (mut app App) create_user_dir(username string) {
user_path := '${app.settings.repo_storage_path}/${username}'
user_path := '${app.config.repo_storage_path}/${username}'

os.mkdir(user_path) or {
app.info('Failed to create ${user_path}')
Expand Down

0 comments on commit c67fbd9

Please sign in to comment.