Welcome to my Go based MVC framework. Feel free to fork this repo and get started with the following.
The easiest way to get started is by installing the tools found in this repo's cmd directory like so:
go install github.com/ccutch/congo/...
create-congo-app # To get help writing code
congo-hosting # For help deploying your appTo get started running the project locally use the following command:
go run ./apps/blogfrontYou can find a full example of a model in apps/blogfront/models/post.go.
//...
type Post struct {
congo.Model
Title string
Content string
}
func SearchPosts(db *congo.Database, query string) (posts []*Post, err error) {
return posts, db.Query(`
SELECT id, title, content, created_at, updated_at
FROM posts
WHERE title LIKE ?
`, "%"+query+"%").All(func(scan congo.Scanner) (err error) {
p := Post{Model: congo.Model{DB: db}}
posts = append(posts, &p)
return scan(&p.ID, &p.Title, &p.Content, &p.CreatedAt, &p.UpdatedAt)
})
}
//...You can find an example of a controller in apps/blogfront/controllers/posts.go.
//...
type PostController struct{ congo.BaseController }
func (posts *PostController) Setup(app *congo.Application) {
posts.Application = app
http.HandleFunc("POST /blog", posts.handleCreate)
http.HandleFunc("PUT /blog/{post}", posts.handleUpdate)
}
func (posts PostController) Handle(r *http.Request) congo.Controller {
posts.Request = r
return &posts
}
func (posts *PostController) SearchPosts() ([]*models.Post, error) {
return models.SearchPosts(posts.DB, posts.PathValue("query"))
}
//...You can find an example of a view in apps/blogfront/templates/blog-posts.html.
<!-- ... -->
{{range posts.SearchPosts}}
<a href="{{host}}/blog/{{.ID}}" class="card bg-base-300 shadow">
<div class="card-body">
<h2 class="card-title">{{.Title}}</h2>
</div>
</a>
{{end}}
<!-- ... -->To run the app setup the app like shown in ./apps/blogfront/main.go:
You can use the command line tool found in cmd/congo-hosting to quickly get setup hosting you own congo instance. Run the following commands:
export DIGITAL_OCEAN_API_KEY=<your-digital-ocean-api-key>
congo-hosting launch
curl http://<your-ip-address>:8080First take the IP address given to you when the server was launch and create an A record with your DNS provider. Then run the following:
congo-hosting gen-certs --domain www.example.com
curl https://www.example.comIf you want to connect to the server directly via a secure connection use:
congo-hosting connect
tmux ls # <-- check for the running server process in tmuxIf you need to restart the server, and maybe want to update the binary use:
go build -o congo . # Building a new binary executable
congo-hosting restart --binary ./congo