golang, echo, gorm
go-webapp-sample
Preface
This repository is the sample of web application using golang. This sample uses Echo as web application framework, Gorm as OR mapper and Zap logger as logger. This sample application provides only several functions as Web APIs. Please refer to the 'Service' section about the detail of those functions.Also, this application contains the static contents such as html file, css file and javascript file which built vuejs-webapp-sample project to easily check the behavior of those functions. So, you can check this application without starting a web server for front end. Please refer to the 'Starting Server' section about checking the behavior of this application.
If you would like to develop a web application using golang, please feel free to use this sample.
Install
Perform the following steps:- Download and install Visual Studio Code(VS Code).
- Download and install Golang.
- Get the source code of this repository by the following command.
go install github.com/ybkuroki/go-webapp-sample@latest
Starting Server
There are 2 methods for starting server.Without Web Server
- Starting this web application by the following command.
go run main.go
- When startup is complete, the console shows the following message:
http server started on [::]:8080
- Access http://localhost:8080 in your browser.
- Login with the following username and password.
test
- password : test
With Web Server
Starting Application Server
- Starting this web application by the following command.
<pre><code class="lang-bash">go run main.go</code></pre>
- When startup is complete, the console shows the following message:
<pre><code class="lang-">http server started on [::]:8080</code></pre>
- Access http://localhost:8080/api/health in your browser and confirm that this application has started.
<pre><code class="lang-">healthy</code></pre>
Starting Web Server
- Clone vuejs-webapp-sample project and install some tools.
- Start by the following command.
<pre><code class="lang-bash">npm run dev</code></pre>
- When startup is complete, the console shows the following message:
<pre><code class="lang-">> vuejs-webapp-sample@..* dev
> vite --mode development
VITE v..* ready in 1362 ms
➜ Local: http://localhost:3000/
➜ press h to show help</code></pre>
- Access http://localhost:3000 in your browser.
- Login with the following username and password.
- username : test
- password : test
With Docker
building Docker image
- Run the below following commands:
<pre><code class="lang-bash">docker build -t go-webapp-sample .</code></pre>
- command to start the container at port 8080
<pre><code class="lang-bash">docker run --name app -p 8080:8080 go-webapp-sample</code></pre>
- Access http://localhost:8000 in your browser.
- Login with the following username and password.
- username : test
- password : test
Using Swagger
In this sample, Swagger is enabled only when executed this application on the development environment.
Swagger isn't enabled on the another environments in default.
Accessing to Swagger
- Start this application according to the 'Starting Application Server' section.
- Access http://localhost:8080/swagger/index.html in your browser.
Updating the existing Swagger document
- Update some comments of some controllers.
- Download Swag library. (Only first time)
<pre><code class="lang-bash">go install github.com/swaggo/swag/cmd/swag@latest</code></pre>
- Update
docs/docs.go.
<pre><code class="lang-bash">swag init</code></pre>
Build executable file
Build this source code by the following command.
<pre><code class="lang-bash">go build main.go</code></pre>
Project Map
The following figure is the map of this sample project.
<pre><code class="lang-">- go-webapp-sample + config … Define configurations of this system. + logger … Provide loggers. + middleware … Define custom middleware. + migration … Provide database migration service for development. + router … Define routing. + controller … Define controllers. + model … Define models. + repository … Provide a service of database access. + service … Provide a service of book management. + session … Provide session management. + test … for unit test - main.go … Entry Point.</code></pre>
Services
This sample provides 3 services: book management, account management, and master management.
Regarding the detail of the API specification, please refer to the 'Using Swagger' section.
Book Management
There are the following services in the book management.
|Service Name|HTTP Method|URL|Parameter|Summary| |:---|:---:|:---|:---|:---| |Get Service|GET|
/api/books/[BOOK_ID]|Book ID|Get a book data.| |List/Search Service|GET|/api/books?query=[KEYWORD]&page=[PAGENUMBER]&size=[PAGESIZE]|Page, Keyword(Optional)|Get a list of books.| |Regist Service|POST|/api/books|Book|Regist a book data.| |Edit Service|PUT|/api/books|Book|Edit a book data.| |Delete Service|DELETE|/api/books|Book|Delete a book data.|
Account Management
There are the following services in the Account management.
|Service Name|HTTP Method|URL|Parameter|Summary| |:---|:---:|:---|:---|:---| |Login Service|POST|
/api/auth/login|Session ID, User Name, Password|Session authentication with username and password.| |Logout Service|POST|/api/auth/logout|Session ID|Logout a user.| |Login Status Check Service|GET|/api/auth/loginStatus|Session ID|Check if the user is logged in.| |Login Username Service|GET|/api/auth/loginAccount|Session ID|Get the login user's username.|
Master Management
There are the following services in the Master management.
|Service Name|HTTP Method|URL|Parameter|Summary| |:---|:---:|:---|:---|:---| |Category List Service|GET|
/api/categories|Nothing|Get a list of categories.| |Format List Service|GET|/api/formats`|Nothing|Get a list of formats.|
Tests
Create the unit tests only for the packages such as controller, service, model/dto and util. The test cases is included the regular cases and irregular cases. Please refer to the source code in each packages for more detail.The command for testing is the following:
go test ./... -v
Libraries
This sample uses the following libraries.|Library Name|Version| |:---|:---:| |echo|4.11.4| |gorm|1.25.9| |go-playground/validator.v9|9.31.0| |zap|1.26.0|