Skip to content

Commit

Permalink
feat: add docs, Dockerfile, GitHub Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
JingYiJun committed Mar 30, 2024
1 parent e16f40b commit 0b4f11a
Show file tree
Hide file tree
Showing 34 changed files with 3,882 additions and 905 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
11 changes: 11 additions & 0 deletions .github/workflows/danke.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
on:
push:
branches:
- main

jobs:
docker-build-danke:
uses: ./.github/workflows/main.yml
with:
service_name: danke
secrets: inherit
35 changes: 35 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Production Build
on:
workflow_call:
inputs:
service_name:
type: string
required: true

jobs:
docker:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@master

- name: Set up QEMU
uses: docker/setup-qemu-action@master

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@master

- name: Login to DockerHub
uses: docker/login-action@master
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build and push
id: docker_build
uses: docker/build-push-action@master
with:
push: true
tags: |
${{ secrets.DOCKERHUB_USERNAME }}/${{ inputs.service_name }}_backend:latest
${{ secrets.DOCKERHUB_USERNAME }}/${{ inputs.service_name }}_backend:master
31 changes: 31 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
FROM golang:1.22-alpine as builder

ARG SERVICE_NAME

WORKDIR /app

COPY go.mod go.sum ./

RUN apk add --no-cache ca-certificates tzdata && \
go mod download

COPY . .

RUN go build -ldflags "-s -w" -tags netgo -o backend ./$SERVICE_NAME/main.go

FROM alpine

WORKDIR /app

COPY --from=builder /app/backend /app/
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo

VOLUME ["/app/data"]

ENV TZ=Asia/Shanghai
ENV MODE=prod
ENV LOG_LEVEL=info

EXPOSE 8000

ENTRYPOINT ["./backend"]
3 changes: 0 additions & 3 deletions auth/go.mod

This file was deleted.

8 changes: 7 additions & 1 deletion auth/main.go
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
package auth
package main

import "fmt"

func main() {
fmt.Println("Hello, auth!")
}
14 changes: 13 additions & 1 deletion common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,22 @@ import (
// common config
const (
EnvMode = "MODE"
EnvPort = "PORT"
EnvLogLevel = "LOG_LEVEL"
EnvDBType = "DB_TYPE"
EnvDBUrl = "DB_URL"
EnvCacheType = "CACHE_TYPE"
EnvCacheUrl = "CACHE_URL"
)

var defaultConfig = map[string]string{
EnvMode: "dev",
EnvLogLevel: "debug",
EnvDBType: "sqlite",
EnvDBUrl: "file::memory:?cache=shared",
EnvCacheType: "memory",
EnvCacheUrl: "",
}

var GormConfig = &gorm.Config{
NamingStrategy: schema.NamingStrategy{
SingularTable: true, // 表名使用单数, `User` -> `user`
Expand All @@ -36,4 +45,7 @@ var GormConfig = &gorm.Config{

func init() {
viper.AutomaticEnv()
for k, v := range defaultConfig {
viper.SetDefault(k, v)
}
}
70 changes: 0 additions & 70 deletions common/go.mod

This file was deleted.

6 changes: 3 additions & 3 deletions danke/api/course.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ func ListCoursesV1(c *fiber.Ctx) (err error) {
func GetCourseV1(c *fiber.Ctx) (err error) {
user, err := GetCurrentUser(c)
if err != nil {
return err
return
}

id, err := c.ParamsInt("id")
if err != nil {
return err
return
}

// 获取课程,课程的评论,评论的历史记录和用户成就
Expand All @@ -69,7 +69,7 @@ func GetCourseV1(c *fiber.Ctx) (err error) {
Preload("Reviews.UserAchievements.Achievement").
First(&course, id).Error
if err != nil {
return err
return
}

// 获取课程的评论的自己的投票
Expand Down
6 changes: 3 additions & 3 deletions danke/api/course_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func GetCourseGroupV1(c *fiber.Ctx) (err error) {
}

// 获取课程组的所有课程的所有评论,同时加载评论的历史记录和用户成就
err = courseGroup.Courses.LoadReviewList(DB, true, true)
err = courseGroup.Courses.LoadReviewList(DB, FindReviewOption{PreloadHistory: true, PreloadAchievement: true})
if err != nil {
return err
}
Expand Down Expand Up @@ -122,7 +122,7 @@ func RefreshCourseGroupHashV1(c *fiber.Ctx) (err error) {
// @Produce json
// @Router /v3/course_groups/search [get]
// @Param request query schema.CourseGroupSearchV3Request true "search query"
// @Success 200 {object} common.PagedResponse[schema.CourseGroupV3Response, any]
// @Success 200 {object} PagedResponse[schema.CourseGroupV3Response, any]
// @Failure 400 {object} common.HttpError
// @Failure 404 {object} common.HttpBaseError
// @Failure 500 {object} common.HttpBaseError
Expand Down Expand Up @@ -214,7 +214,7 @@ func GetCourseGroupV3(c *fiber.Ctx) (err error) {
}

// 获取课程组的所有课程的所有评论,同时加载评论的历史记录和用户成就
err = courseGroup.Courses.LoadReviewList(DB, true, true)
err = courseGroup.Courses.LoadReviewList(DB, FindReviewOption{PreloadHistory: true, PreloadAchievement: true})
if err != nil {
return err
}
Expand Down
46 changes: 28 additions & 18 deletions danke/api/review.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,9 @@ func ModifyReviewV1(c *fiber.Ctx) (err error) {
}

// 查找评论
var review Review
err = DB.First(&review, id).Error
review, err := FindReviewByID(DB, id)
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
return
}
return NotFound("评论不存在")
return
}

// 检查权限
Expand All @@ -98,7 +94,16 @@ func ModifyReviewV1(c *fiber.Ctx) (err error) {
return
}

return c.JSON(new(ReviewV1Response).FromModel(user, &review))
// 查找评论
review, err = FindReviewByID(DB, id, FindReviewOption{
PreloadHistory: true,
PreloadAchievement: true,
})
if err != nil {
return
}

return c.JSON(new(ReviewV1Response).FromModel(user, review))
}

// VoteForReviewV1 godoc
Expand Down Expand Up @@ -131,13 +136,9 @@ func VoteForReviewV1(c *fiber.Ctx) (err error) {
}

// 查找评论
var review Review
err = DB.First(&review, reviewID).Error
review, err := FindReviewByID(DB, reviewID)
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
return
}
return NotFound("评论不存在")
return err
}

err = DB.Transaction(func(tx *gorm.DB) (err error) {
Expand Down Expand Up @@ -176,8 +177,14 @@ func VoteForReviewV1(c *fiber.Ctx) (err error) {
// 更新评论投票数
err = tx.Model(&review).
UpdateColumns(map[string]any{
"upvote_count": tx.Model(&ReviewVote{}).Where("review_id = ? AND data = 1", reviewID).Select("count(*)"),
"downvote_count": tx.Model(&ReviewVote{}).Where("review_id = ? AND data = -1", reviewID).Select("count(*)"),
"upvote_count": tx.
Model(&ReviewVote{}).
Where("review_id = ? AND data = 1", reviewID).
Select("count(*)"),
"downvote_count": tx.
Model(&ReviewVote{}).
Where("review_id = ? AND data = -1", reviewID).
Select("count(*)"),
}).Error
return
})
Expand All @@ -186,12 +193,15 @@ func VoteForReviewV1(c *fiber.Ctx) (err error) {
}

// 查找评论
err = DB.First(&review, reviewID).Error
review, err = FindReviewByID(DB, reviewID, FindReviewOption{
PreloadHistory: true,
PreloadAchievement: true,
})
if err != nil {
return
return err
}

return c.JSON(new(ReviewV1Response).FromModel(user, &review))
return c.JSON(new(ReviewV1Response).FromModel(user, review))
}

// ListMyReviewsV1 godoc
Expand Down
24 changes: 22 additions & 2 deletions danke/api/routes.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
package api

import "github.com/gofiber/fiber/v2"
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/swagger"
)

func RegisterRoutes(app *fiber.App) {
app.Get("/", func(c *fiber.Ctx) error {
return c.Redirect("/api")
})
app.Get("/docs", func(c *fiber.Ctx) error {
return c.Redirect("/docs/index.html")
})
app.Get("/docs/*", swagger.HandlerDefault)

api := app.Group("/api")
registerRoutes(api)
}

func registerRoutes(r fiber.Router) {
r.Get("", func(c *fiber.Ctx) error {
return c.JSON(fiber.Map{"message": "Welcome to danke API"})
})

func RegisterRoutes(r fiber.Router) {
// v1
// Course
r.Get("/courses", ListCoursesV1)
Expand Down
16 changes: 2 additions & 14 deletions danke/config/config.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
package config

import (
. "github.com/opentreehole/backend/common"
"github.com/spf13/viper"
_ "github.com/opentreehole/backend/common"
)

var defaultConfig = map[string]string{
EnvMode: "dev",
EnvPort: "8000",
EnvDBType: "sqlite",
EnvCacheType: "memory",
}

func init() {
for k, v := range defaultConfig {
viper.SetDefault(k, v)
}
}
// add custom config here
Loading

0 comments on commit 0b4f11a

Please sign in to comment.