Skip to content

Commit

Permalink
Merge pull request #1 from humpback/develop-0.0.1
Browse files Browse the repository at this point in the history
try build dev
  • Loading branch information
JamesYYang authored Jan 1, 2025
2 parents b6a94ca + dc63736 commit f2fc283
Show file tree
Hide file tree
Showing 150 changed files with 6,756 additions and 24 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/build-develop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Build and Deploy

on:
push:
branches:
- develop

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '20.x'

- name: Install dependencies and build front
working-directory: ./front
run: |
npm install -g pnpm
npm run build
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.23'

- name: Install dependencies and build backend
working-directory: ./backend
run: |
go mod download
go build -o humpback
- name: 'Build and push image'
uses: azure/docker-login@v1
with:
login-server: jamesyyangapp.azurecr.io
username: jamesyyangapp
password: ${{ secrets.AZURE_REGISTRY_PASSWORD }}
- run: |
docker build . -t jamesyyangapp.azurecr.io/humpback-server:${{ github.sha }}
docker push jamesyyangapp.azurecr.io/humpback-server:${{ github.sha }}
50 changes: 27 additions & 23 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

# Dependency directories (remove the comment below to include it)
# vendor/
node_modules
dist
dist-ssr
*.local

# Go workspace file
go.work
go.work.sum

# env file
.env
# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
package-lock.json
pnpm-lock.yaml
*.exe
humpback_linux
humpback
15 changes: 15 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM alpine:latest

LABEL maintainer="skyler.w.yang"

RUN mkdir -p /workspace/config

COPY ./backend/config/*.yaml /workspace/config

COPY ./front/projects/web/dist /workspace/html/web

COPY ./backend/humpback /workspace/

WORKDIR /workspace

CMD ["./humpback"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# humpback-server
# humpback-server
18 changes: 18 additions & 0 deletions backend/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}\\main.go",
"env": {
},
"args": []
}
]
}
88 changes: 88 additions & 0 deletions backend/api/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package api

import (
"context"
"fmt"
"log/slog"
"net/http"

"humpback/api/handle"
"humpback/api/middleware"
"humpback/api/static"
"humpback/config"

"github.com/gin-gonic/gin"
)

// var Router RouterInterface

// type RouterInterface interface {
// Start() error
// }

type Router struct {
engine *gin.Engine
httpSrv *http.Server
}

func InitRouter() *Router {
gin.SetMode(gin.ReleaseMode)
r := &Router{
engine: gin.New(),
}
r.setMiddleware()
r.setRoute()
return r
}

func (api *Router) Start() {
go func() {
if err := static.InitStaticsResource(); err != nil {
slog.Error(fmt.Sprintf("init front static resource to cache failed: %s", err))
}
listeningAddress := fmt.Sprintf("%s:%s", config.NodeArgs().HostIp, config.NodeArgs().SitePort)
slog.Info("[Api] listening...", "Address", listeningAddress)
api.httpSrv = &http.Server{
Addr: listeningAddress,
Handler: api.engine,
}
if err := api.httpSrv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
slog.Error(fmt.Sprintf("listening %s failed: %s", listeningAddress, err))
}
}()
}

func (api *Router) Close(c context.Context) error {
return api.httpSrv.Shutdown(c)
}

func (api *Router) setMiddleware() {
api.engine.Use(gin.Recovery(), middleware.Log(), middleware.CorsCheck(), middleware.HandleError())
}

func (api *Router) setRoute() {
var routes = map[string]map[string][]any{
"/webapi": {
"/user": {handle.RouteUser},
},
}

for group, list := range routes {
for path, fList := range list {
routerGroup := api.engine.Group(fmt.Sprintf("%s%s", group, path), parseSliceAnyToSliceFunc(fList[:len(fList)-1])...)
groupFunc := fList[len(fList)-1].(func(*gin.RouterGroup))
groupFunc(routerGroup)
}
}
api.engine.NoRoute(static.Web)
}

func parseSliceAnyToSliceFunc(functions []any) []gin.HandlerFunc {
result := make([]gin.HandlerFunc, 0)
for _, f := range functions {
if fun, ok := f.(gin.HandlerFunc); ok {
result = append(result, fun)
}
}
return result
}
22 changes: 22 additions & 0 deletions backend/api/handle/handle_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package handle

import (
"net/http"

"github.com/gin-gonic/gin"
"humpback/api/handle/models"
"humpback/api/middleware"
"humpback/common/response"
)

func RouteUser(router *gin.RouterGroup) {
router.POST("/login", login)
}

func login(c *gin.Context) {
body := new(models.UserLoginReqInfo)
if !middleware.BindAndCheckBody(c, body) {
return
}
c.JSON(http.StatusOK, response.NewRespSucceed())
}
10 changes: 10 additions & 0 deletions backend/api/handle/models/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package models

type UserLoginReqInfo struct {
Name string `json:"name"`
Password string `json:"password"`
}

func (u *UserLoginReqInfo) Check() error {
return nil
}
44 changes: 44 additions & 0 deletions backend/api/middleware/func.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package middleware

import (
"github.com/gin-gonic/gin"
"humpback/common/locales"
"humpback/common/response"
)

type CheckInterface interface {
Check() error
}

func BindAndCheckBody(c *gin.Context, obj CheckInterface) bool {
if err := c.ShouldBindJSON(obj); err != nil {
AbortErr(c, response.NewBadRequestErr(locales.CodeRequestParamsInvalid, err.Error()))
return false
}
if err := obj.Check(); err != nil {
AbortErr(c, err)
return false
}
return true
}

func BindBody(c *gin.Context, obj interface{}) bool {
if err := c.ShouldBindJSON(obj); err != nil {
AbortErr(c, response.NewBadRequestErr(locales.CodeRequestParamsInvalid, err.Error()))
return false
}
return true
}

func CheckBody(c *gin.Context, body CheckInterface) bool {
if err := body.Check(); err != nil {
AbortErr(c, err)
return false
}
return true
}

func AbortErr(c *gin.Context, err error) {
c.Error(err)
c.Abort()
}
Loading

0 comments on commit f2fc283

Please sign in to comment.