Skip to content

Commit

Permalink
feat: add API support for creating HTTPS services in StartServer
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayideyia committed Oct 23, 2024
1 parent dccca9c commit a0f8064
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 7 deletions.
9 changes: 7 additions & 2 deletions bridge/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type ResponseData struct {
Body string
}

func (a *App) StartServer(address string, serverID string) FlagResult {
func (a *App) StartServer(address string, serverID string, options ServerOptions) FlagResult {
log.Printf("StartServer: %s", address)

server := &http.Server{
Expand Down Expand Up @@ -85,7 +85,12 @@ func (a *App) StartServer(address string, serverID string) FlagResult {
var result error

go func() {
err := server.ListenAndServe()
var err error
if options.Cert != "" && options.Key != "" {
err = server.ListenAndServeTLS(GetPath(options.Cert), GetPath(options.Key))
} else {
err = server.ListenAndServe()
}
if err != nil {
result = err
}
Expand Down
5 changes: 5 additions & 0 deletions bridge/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ type FlagResult struct {
Data string `json:"data"`
}

type ServerOptions struct {
Cert string `json:"cert"`
Key string `json:"key"`
}

type HTTPResult struct {
Flag bool `json:"flag"`
Status int `json:"status"`
Expand Down
14 changes: 12 additions & 2 deletions frontend/src/bridge/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ type ResponseType = {
options: { mode: 'Binary' | 'Text' }
}

type ServerOptions = {
cert: string
key: string
}

type HttpServerHandler = (
req: RequestType,
res: {
Expand All @@ -28,8 +33,13 @@ type HttpServerHandler = (
}
) => Promise<void>

export const StartServer = async (address: string, id: string, handler: HttpServerHandler) => {
const { flag, data } = await App.StartServer(address, id)
export const StartServer = async (
address: string,
id: string,
handler: HttpServerHandler,
options: ServerOptions = { cert: '', key: '' }
) => {
const { flag, data } = await App.StartServer(address, id, options)
if (!flag) {
throw data
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/wailsjs/go/bridge/App.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function RestartApp():Promise<bridge.FlagResult>;

export function ShowMainWindow():Promise<void>;

export function StartServer(arg1:string,arg2:string):Promise<bridge.FlagResult>;
export function StartServer(arg1:string,arg2:string,arg3:bridge.ServerOptions):Promise<bridge.FlagResult>;

export function StopServer(arg1:string):Promise<bridge.FlagResult>;

Expand Down
4 changes: 2 additions & 2 deletions frontend/wailsjs/go/bridge/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ export function ShowMainWindow() {
return window['go']['bridge']['App']['ShowMainWindow']();
}

export function StartServer(arg1, arg2) {
return window['go']['bridge']['App']['StartServer'](arg1, arg2);
export function StartServer(arg1, arg2, arg3) {
return window['go']['bridge']['App']['StartServer'](arg1, arg2, arg3);
}

export function StopServer(arg1) {
Expand Down
14 changes: 14 additions & 0 deletions frontend/wailsjs/go/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,20 @@ export namespace bridge {
this.FileField = source["FileField"];
}
}
export class ServerOptions {
cert: string;
key: string;

static createFrom(source: any = {}) {
return new ServerOptions(source);
}

constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.cert = source["cert"];
this.key = source["key"];
}
}
export class TrayContent {
icon: string;
title: string;
Expand Down

0 comments on commit a0f8064

Please sign in to comment.