Compare commits

..

8 Commits

Author SHA1 Message Date
Muyao CHEN
9094d12c3a fix: abandon google addlicense use marmotedu addlicense 2024-10-05 14:35:36 +02:00
Muyao CHEN
dc75af4dc7 feat: add admin table 2024-10-05 14:12:55 +02:00
Muyao CHEN
1295d15eb3 fix: addlicense bug!!! 2024-10-05 00:09:14 +02:00
Muyao CHEN
4794137d42 feat:add context interface, and change core.writeresponse function to use it 2024-10-04 23:40:45 +02:00
Muyao CHEN
321b4704a2 docker: take the db_data out of the repo 2024-10-04 23:39:35 +02:00
Muyao CHEN
a9a6f6ad49 makefile: ignore copyright for docker files and migration files 2024-10-04 23:38:50 +02:00
Muyao CHEN
ab26e9d585 fix: set request id into headers instead of context directly 2024-10-04 23:37:48 +02:00
Muyao CHEN
b9d4a58d71 db: create user table 2024-10-04 21:21:03 +02:00
14 changed files with 72 additions and 35 deletions

1
.gitignore vendored
View File

@ -24,3 +24,4 @@ go.work.sum
# Custom
/_output
/deployment/db_data

View File

@ -42,7 +42,7 @@ format: # format code.
.PHONY: add-copyright
add-copyright: # add license to file headers.
@addlicense -v -f $(ROOT_DIR)/LICENSE -ignore third_party/** -ignore vendor/** -ignore $(OUTPUT_DIR) $(ROOT_DIR)
@addlicense -v -f $(ROOT_DIR)/LICENSE $(ROOT_DIR) --skip-files=database.yml --skip-dirs=$(OUTPUT_DIR),deployment,migrations,configs
.PHONY: swagger
swagger: # Run swagger.

View File

@ -195,3 +195,5 @@ type User struct {
ID int
}
```
Use Buffalo pop `Soda CLI` to create database migrations.

View File

@ -1,25 +1,3 @@
# MIT License
#
# Copyright (c) 2024 vinchent <vinchent@vinchent.xyz>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
dev-mode: true
web:

7
database.yml Normal file
View File

@ -0,0 +1,7 @@
development:
dialect: postgres
database: howmuch
user: postgres
password: example
host: 127.0.0.1
pool: 5

View File

@ -0,0 +1,22 @@
services:
postgres:
image: postgres
restart: always
ports:
- "5432:5432"
deploy:
mode: replicated
replicas: 1
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: example
POSTGRES_DB: howmuch
volumes:
- ../../db_data_howmuch/postgres/:/var/lib/postgresql/data
adminer:
image: adminer
restart: always
ports:
- 8080:8080

View File

@ -0,0 +1,5 @@
package core
type Context interface {
JSON(code int, obj any)
}

View File

@ -26,7 +26,6 @@ import (
"net/http"
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/errno"
"github.com/gin-gonic/gin"
)
type ErrResponse struct {
@ -36,7 +35,7 @@ type ErrResponse struct {
// WriteResponse writes the response to the HTTP response writer with HTTP code
// and potential errors.
func WriteResponse(c *gin.Context, err error, data any) {
func WriteResponse(c Context, err error, data any) {
// No error, write json response directly
if err == nil {
c.JSON(http.StatusOK, data)

View File

@ -33,19 +33,14 @@ func RequestID() gin.HandlerFunc {
return func(ctx *gin.Context) {
var rid string
if rid = ctx.GetString(requestID); rid != "" {
// request id exists already
ctx.Next()
}
if rid = ctx.GetHeader(requestID); rid != "" {
ctx.Set(requestID, rid)
ctx.Request.Header.Add(requestID, rid)
ctx.Next()
}
rid = uuid.NewString()
ctx.Set(requestID, rid)
ctx.Writer.Header().Add(requestID, rid)
ctx.Request.Header.Add(requestID, rid)
ctx.Header(requestID, rid)
ctx.Next()
}
}

View File

@ -58,12 +58,12 @@ func TestRequestID(t *testing.T) {
wanted := "123"
r.GET("/example", func(c *gin.Context) {
got = c.GetString(requestID)
got = c.GetHeader(requestID)
c.Status(http.StatusOK)
})
r.POST("/example", func(c *gin.Context) {
got = c.GetString(requestID)
got = c.GetHeader(requestID)
c.String(http.StatusAccepted, "ok")
})

View File

@ -0,0 +1,3 @@
DROP TABLE IF EXISTS "user";
DROP SEQUENCE IF EXISTS user_id_seq;

View File

@ -0,0 +1,14 @@
CREATE SEQUENCE user_id_seq INCREMENT 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1;
CREATE TABLE "public"."user" (
"id" integer DEFAULT nextval('user_id_seq') NOT NULL,
"email" character varying(255) NOT NULL UNIQUE,
"first_name" character varying(255) NOT NULL,
"last_name" character varying(255) NOT NULL,
"password" character varying(60) NOT NULL,
"created_at" timestamp NOT NULL,
"updated_at" timestamp NOT NULL,
CONSTRAINT "user_pkey" PRIMARY KEY ("id")
) WITH (oids = false);

View File

@ -0,0 +1,2 @@
DROP TABLE IF EXISTS "admin";
DROP SEQUENCE IF EXISTS admin_id_seq;

View File

@ -0,0 +1,9 @@
CREATE SEQUENCE admin_id_seq INCREMENT 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1;
CREATE TABLE "public"."admin" (
"id" integer DEFAULT nextval('admin_id_seq') NOT NULL,
"email" character varying(255) NOT NULL,
"password" character varying(255) NOT NULL,
"access_level" integer NOT NULL,
CONSTRAINT "admin_pkey" PRIMARY KEY ("id")
) WITH (oids = false);